diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libserver | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libserver')
115 files changed, 25381 insertions, 25311 deletions
diff --git a/src/libserver/async_session.c b/src/libserver/async_session.c index 2f084687b..66552418a 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 b5323b1df..4573545f1 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 d7c3789e7..9b09608f2 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 8a88f6e3e..d221e8427 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 073a01126..8267a4bc8 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 = <base32_string>;" - " privkey = <base32_string>; " - "}"), - "example", - 0, - false); + ucl_object_insert_key(doc_obj, + ucl_object_fromstring("keypair { " + "pubkey = <base32_string>;" + " privkey = <base32_string>; " + "}"), + "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 d7777ccc7..6bbeda0e6 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 67bc97070..8f41d8638 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 9779b4678..aa231a315 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<std::string_view, - std::vector<symbol_remove_data>> symbols_to_remove; + std::vector<symbol_remove_data>> + symbols_to_remove; std::vector<bool> 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<symbol_remove_data> 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<symbol_remove_data> { 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<symbol_remove_data> 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 335a5d424..5d5802909 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 af05b95df..038e217a0 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<rspamd_composite>()); composite->expr = expr; @@ -94,12 +100,13 @@ private: } ankerl::unordered_dense::map<std::string, - std::shared_ptr<rspamd_composite>, rspamd::smart_str_hash, rspamd::smart_str_equal> composites; + std::shared_ptr<rspamd_composite>, rspamd::smart_str_hash, rspamd::smart_str_equal> + composites; /* Store all composites here, even if we have duplicates */ std::vector<std::shared_ptr<rspamd_composite>> 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 fc19a4162..1ee5c4092 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<std::string_view, - 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}, + 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<map_cbdata *>(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<map_cbdata *>(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<void *>(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<void *>(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<void *>(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<void *>(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 7de4ab307..1b369ed17 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<css_selector>; using selector_ptr = std::unique_ptr<css_selector>; using selectors_hash = ankerl::unordered_dense::map<selector_ptr, css_declarations_block_ptr, - sel_shared_hash, sel_shared_eq>; + sel_shared_hash, sel_shared_eq>; using universal_selector_t = std::pair<selector_ptr, css_declarations_block_ptr>; 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<css_selector> &&selector, - css_declarations_block_ptr decls) -> void +auto css_style_sheet::add_selector_rule(std::unique_ptr<css_selector> &&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<css_selector> &&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<css_selector> &&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<std::string_view> 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_t>(tag->id)}); + css_selector{static_cast<tag_id_t>(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<css_style_sheet> &&existing) - -> css_return_pair + -> css_return_pair { auto parse_res = rspamd::css::parse_css(pool, input, - std::forward<std::shared_ptr<css_style_sheet>>(existing)); + std::forward<std::shared_ptr<css_style_sheet>>(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 7768bde55..f0f8120fb 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<css_selector> &&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<std::shared_ptr<css_style_sheet>, css_parse_error>; auto css_parse_style(rspamd_mempool_t *pool, std::string_view input, - std::shared_ptr<css_style_sheet> &&existing) -> - css_return_pair; + std::shared_ptr<css_style_sheet> &&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 e880aa24b..6dfe54f67 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<std::string_view, css_color> 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 d7d56f3e5..aed035aa4 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<std::monostate>(content)) { /* Switch from monostate */ content = std::vector<consumed_block_ptr>(); @@ -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<css_function_block>(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<decltype(arg)>; + std::visit([&](auto &arg) { + using T = std::decay_t<decltype(arg)>; - if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) { - /* Array of blocks */ - ret += "["; - for (const auto &block : arg) { - ret += "{"; - ret += block->debug_str(); - ret += "}, "; - } + if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) { + /* 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<T, std::monostate>) { - /* Empty block */ - ret += R"("empty")"; - } - else if constexpr (std::is_same_v<T, css_function_block>) { - 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<T, std::monostate>) { + /* Empty block */ + ret += R"("empty")"; + } + else if constexpr (std::is_same_v<T, css_function_block>) { + 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<css_style_sheet> &&existing, rspamd_mempool_t *pool) : - style_object(existing), pool(pool) { + explicit css_parser(std::shared_ptr<css_style_sheet> &&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<css_consumed_block> consume_css_rule(const std::string_view &sv); std::optional<css_parse_error> consume_input(const std::string_view &sv); - auto get_object_maybe(void) -> tl::expected<std::shared_ptr<css_style_sheet>, css_parse_error> { + auto get_object_maybe(void) -> tl::expected<std::shared_ptr<css_style_sheet>, 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<css_consumed_block> &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<css_consumed_block> &top) -> default: /* Attach everything to the function block */ top->add_function_argument(std::make_unique<css_consumed_block>( - 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<css_consumed_block> &top, std::unique_ptr<css_consumed_block> 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>( - 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<css_consumed_block> &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<css_consumed_block> &top, auto css_parser::qualified_rule_consumer(std::unique_ptr<css_consumed_block> &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>( - 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<css_consumed_block> &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<css_consumed_block> &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<css_consumed_block> &to auto css_parser::at_rule_consumer(std::unique_ptr<css_consumed_block> &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>( - 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<css_consumed_block> &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<css_consumed_block> &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<css_consumed_block> &t std::unique_ptr<css_consumed_block> 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<css_consumed_block> &t break; case css_parser_token::token_type::ocurlbrace_token: block = std::make_unique<css_consumed_block>( - 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>( - 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>( - 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<css_consumed_block> &t case css_parser_token::token_type::function_token: { need_more = false; block = std::make_unique<css_consumed_block>( - 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<css_consumed_block> &t } default: block = std::make_unique<css_consumed_block>( - 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<css_consumed_block> &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<css_consumed_block> &t return ret; } -auto -css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr<css_consumed_block> +auto css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr<css_consumed_block> { tokeniser = std::make_unique<css_tokeniser>(pool, sv); auto ret = true; auto consumed_blocks = - std::make_unique<css_consumed_block>(css_consumed_block::parser_tag_type::css_top_block); + std::make_unique<css_consumed_block>(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<cs ret = qualified_rule_consumer(consumed_blocks); break; } - } tokeniser.reset(nullptr); /* No longer needed */ @@ -575,14 +577,13 @@ css_parser::consume_css_blocks(const std::string_view &sv) -> std::unique_ptr<cs return consumed_blocks; } -auto -css_parser::consume_css_rule(const std::string_view &sv) -> std::unique_ptr<css_consumed_block> +auto css_parser::consume_css_rule(const std::string_view &sv) -> std::unique_ptr<css_consumed_block> { tokeniser = std::make_unique<css_tokeniser>(pool, sv); auto ret = true; auto rule_block = - std::make_unique<css_consumed_block>(css_consumed_block::parser_tag_type::css_simple_block); + std::make_unique<css_consumed_block>(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<css_ ret = component_value_consumer(rule_block); break; } - } tokeniser.reset(nullptr); /* No longer needed */ @@ -616,7 +616,7 @@ css_parser::consume_input(const std::string_view &sv) if (rules.empty()) { if (error.type == css_parse_error_type::PARSE_ERROR_NO_ERROR) { return css_parse_error(css_parse_error_type::PARSE_ERROR_EMPTY, - "no css rules consumed"); + "no css rules consumed"); } else { return error; @@ -627,7 +627,7 @@ css_parser::consume_input(const std::string_view &sv) style_object = std::make_shared<css_style_sheet>(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<const char *> 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<css_style_sheet> 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 35b51ed23..d5a9671b7 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<consumed_block_ptr> args; - css_function_block(css_parser_token &&tok) : - function(std::forward<css_parser_token>(tok)) {} + css_function_block(css_parser_token &&tok) + : function(std::forward<css_parser_token>(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<consumed_block_ptr> empty_block_vec{}; - auto is_blocks_vec() const -> bool { + auto is_blocks_vec() const -> bool + { return (std::holds_alternative<std::vector<consumed_block_ptr>>(content)); } - auto get_blocks_or_empty() const -> const std::vector<consumed_block_ptr>& { + auto get_blocks_or_empty() const -> const std::vector<consumed_block_ptr> & + { if (is_blocks_vec()) { return std::get<std::vector<consumed_block_ptr>>(content); } @@ -119,11 +132,13 @@ public: return empty_block_vec; } - auto is_token() const -> bool { + auto is_token() const -> bool + { return (std::holds_alternative<css_parser_token>(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<css_parser_token>(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<css_function_block>(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<css_function_block>(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<decltype(arg)>; - - if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) { - /* Array of blocks */ - ret = arg.size(); - } - else if constexpr (std::is_same_v<T, std::monostate>) { - /* Empty block */ - ret = 0; - } - else { - /* Single element block */ - ret = 1; - } - }, - content); + std::visit([&](auto &arg) { + using T = std::decay_t<decltype(arg)>; + + if constexpr (std::is_same_v<T, std::vector<consumed_block_ptr>>) { + /* Array of blocks */ + ret = arg.size(); + } + else if constexpr (std::is_same_v<T, std::monostate>) { + /* 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<std::monostate, - std::vector<consumed_block_ptr>, - css_parser_token, - css_function_block> content; + std::vector<consumed_block_ptr>, + 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 992d5a0b0..155710942 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<frozen::string, css_property_type>({ - {"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<css_property,css_parse_error> + -> tl::expected<css_property, css_parse_error> { 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 7af6c7bd6..9661222de 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<css_property,css_parse_error> 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<css_property, css_parse_error> 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<rspamd::css::css_property> { 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<std::size_t>(prop.type); + constexpr size_t operator()(const rspamd::css::css_property &prop) const + { + std::size_t key = 0xdeadbeef ^ static_cast<std::size_t>(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 3a93d97d4..4e33ac7a0 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<decltype(css_value::value)> < std::numeric_limits<int>::max()); - for (const auto &v : values) { + for (const auto &v: values) { bits |= static_cast<int>(1 << v.value.index()); } - for (const auto &ov : other.values) { + for (const auto &ov: other.values) { if (isset(&bits, static_cast<int>(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<int>(elt.value.index()))) == 0; - }); + [&bits](const auto &elt) -> bool { + return (bits & (1 << static_cast<int>(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<css_value> { + -> std::optional<css_value> +{ 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<std::pair<const char *, std::vector<css_property>>> 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 df385bdad..114b83e11 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<css_values_vec>(values)) {} + explicit css_rule(css_property &&prop, css_values_vec &&values) noexcept + : prop(prop), values(std::forward<css_values_vec>(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<rspamd::css::css_rule> { 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<rspamd::css::css_property>()(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 86ff482fe..a62ffff9c 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>( - 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>( - 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>( - 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>( - 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<std::pair<const char *, std::vector<css_selector::selector_type>>> 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 f49251338..65b185a6e 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<css_attribute_condition, css_selector_ptr>; std::vector<css_selector_dep> dependencies; - auto to_tag(void) const -> std::optional<tag_id_t> { + auto to_tag(void) const -> std::optional<tag_id_t> + { if (type == selector_type::SELECTOR_TAG) { return std::get<tag_id_t>(value); } return std::nullopt; } - auto to_string(void) const -> std::optional<const std::string_view> { + auto to_string(void) const -> std::optional<const std::string_view> + { if (type != selector_type::SELECTOR_TAG) { return std::string_view(std::get<std::string_view>(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<rspamd::css::css_selector> { 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::size_t>(std::get<tag_id_t>(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 2a97f8f0e..429e58fdd 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<css_style> &_parent) : parent(_parent) { - propagate_from_parent (); + css_style(const std::shared_ptr<css_style> &_parent) + : parent(_parent) + { + propagate_from_parent(); } - css_style (const std::shared_ptr<css_style> &_parent, - const std::vector<std::shared_ptr<css_selector> > &_selectors) : parent(_parent) { - selectors.reserve (_selectors.size ()); + css_style(const std::shared_ptr<css_style> &_parent, + const std::vector<std::shared_ptr<css_selector>> &_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<std::weak_ptr<css_selector> > selectors; + std::vector<std::weak_ptr<css_selector>> selectors; std::weak_ptr<css_style> parent; std::vector<css_rule> 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 f3c010f1e..6d3f41e8d 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<css_parser_token::token_type::string_token, std::string_view>(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<css_parser_token::token_type::ident_token, std::string_view>(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<css_parser_token::token_type::function_token, std::string_view>(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<css_parser_token::token_type::url_token, std::string_view>(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<css_parser_token::token_type::whitespace_token, std::string_view>(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<css_parser_token::token_type::delim_token, char>(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<int>(css_parser_token::dim_type::dim_max); constexpr frozen::unordered_map<frozen::string, css_dimension_data, max_dims> 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<float>(value) || - !std::holds_alternative<std::string_view>(dim_token.value)) { + !std::holds_alternative<std::string_view>(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<css_parser_token::token_type::delim_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<std::string_view>(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<css_parser_token::token_type::whitespace_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<css_parser_token::token_type::colon_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<css_parser_token::token_type::cdo_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<css_parser_token::token_type::eof_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 897489974..aa6a1a711 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<std::string_view, /* For strings and string like tokens */ - char, /* For delimiters (might need to move to unicode point) */ - float, /* For numeric stuff */ - css_parser_token_placeholder /* For general no token stuff */ - >; + using value_type = std::variant<std::string_view, /* For strings and string like tokens */ + char, /* For delimiters (might need to move to unicode point) */ + float, /* For numeric stuff */ + css_parser_token_placeholder /* For general no token stuff */ + >; /* 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<std::string_view>(value)) { return std::get<std::string_view>(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<char>(value)) { return std::get<char>(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<float>(value)) { auto dbl = std::get<float>(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<float>(value)) { auto dbl = std::get<float>(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<css_parser_token>); 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 7add8043c..07f87229c 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<char *>(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 5daf9ee82..4837a4611 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 7a451e1e5..2546e0159 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<rspamd::css::css_color> { - static String convert(const rspamd::css::css_color& value) { +template<> +struct StringMaker<rspamd::css::css_color> { + 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<css_value> { + -> std::optional<css_value> +{ 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<css_value> { + -> std::optional<css_value> +{ 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<css_value> { + -> std::optional<css_value> +{ 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<css_value> { + -> std::optional<css_value> +{ if (std::holds_alternative<float>(tok.value)) { auto dbl = std::get<float>(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<frozen::string, css_display_value>({ - {"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<css_value> { + -> std::optional<css_value> +{ 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<T, css_color>) { 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<T, double>) { 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<const char*, css_color> 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<const char *, css_color> 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 8dcfa63da..1d5742126 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<css_color, - 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<css_color> { + 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<css_color> + { return extract_value_maybe<css_color>(); } - auto to_number(void) const -> std::optional<float> { + auto to_number(void) const -> std::optional<float> + { return extract_value_maybe<float>(); } - auto to_dimension(void) const -> std::optional<css_dimension> { + auto to_dimension(void) const -> std::optional<css_dimension> + { return extract_value_maybe<css_dimension>(); } - auto to_display(void) const -> std::optional<css_display_value> { + auto to_display(void) const -> std::optional<css_display_value> + { return extract_value_maybe<css_display_value>(); } - auto is_valid(void) const -> bool { + auto is_valid(void) const -> bool + { return !(std::holds_alternative<std::monostate>(value)); } @@ -132,9 +155,11 @@ struct css_value { -> std::optional<css_value>; static auto maybe_display_from_string(const std::string_view &input) -> std::optional<css_value>; + private: template<typename T> - auto extract_value_maybe(void) const -> std::optional<T> { + auto extract_value_maybe(void) const -> std::optional<T> + { if (std::holds_alternative<T>(value)) { return std::get<T>(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 57ff0c0ce..22b76f028 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<std::string> 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 27d28e6fc..4318e87ad 100644 --- a/src/libserver/dkim.c +++ b/src/libserver/dkim.c @@ -27,18 +27,18 @@ #include <openssl/engine.h> /* 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 953be2763..50703da1a 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 c25f5f29a..84582202a 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 50db8c891..d6977d0c0 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 ab59b38dc..8254ebc91 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 938fbb074..bb386ca7e 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 2d4f75f60..9099f38dd 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 23b9b68ef..a1b74bcd0 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 390119fbc..2f9d1ed10 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 <key> is <prefix> || <digest> */ - 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 <prefix>_<number>_<value> <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 544b20f60..3cfa16259 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 0f9b3c1ee..9ec448ebc 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 33dc94f30..766f7c9d2 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 fd65ffb38..51bd12244 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 ed034b928..0e1b602f4 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<frozen::string, html_component_type>( - { - {"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<html_component_type> +auto html_component_from_string(const std::string_view &st) -> std::optional<html_component_type> { 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<guchar *>(decoded), &decoded_len); + reinterpret_cast<guchar *>(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<css::css_value> 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<rspamd_url *>(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<int>(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<int>(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<const html_image *> + -> std::optional<const html_image *> { - 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<tag_id_t> + -> std::optional<tag_id_t> { 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 17067b3b1..2d34f2a2e 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 c119adc3f..3320fd634 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::css_style_sheet> 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<html_content* >(ptr); + static auto from_ptr(void *ptr) -> html_content * + { + return static_cast<html_content *>(ptr); } enum class traverse_type { @@ -70,7 +73,8 @@ struct html_content { POST_ORDER }; auto traverse_block_tags(fu2::function<bool(const html_tag *)> &&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<bool(const html_tag *)> &&func) const -> bool { - for (const auto &tag : all_tags) { - if (!(tag->flags & (FL_XML|FL_VIRTUAL))) { + auto traverse_all_tags(fu2::function<bool(const html_tag *)> &&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<tag_id_t>; 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 9cdbfd728..f9b518472 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<typename T, typename MT> - 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<std::int16_t>(800)); + height, other.height, static_cast<std::int16_t>(800)); width_mask = html_block::size_prop(width_mask, other.width_mask, - width, other.width, static_cast<std::int16_t>(1024)); + width, other.width, static_cast<std::int16_t>(1024)); font_mask = html_block::size_prop(font_mask, other.font_mask, - font_size, other.font_size, static_cast<std::int8_t>(10)); + font_size, other.font_size, static_cast<std::int8_t>(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 f1bc91963..c64253617 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<std::string_view, html_entity_def> entity_by_name; ankerl::unordered_dense::map<std::string_view, html_entity_def> entity_by_name_heur; ankerl::unordered_dense::map<unsigned, html_entity_def> 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<std::pair<std::string, std::string>> 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 68084bf92..fc1f7cc22 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 cb83502f5..309d76177 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. <a attrs />) */ -#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<std::string_view> { - 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 e166e0b7c..647f7c3c5 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<std::string_view, html_tag_def> tag_by_name; ankerl::unordered_dense::map<tag_id_t, html_tag_def> 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<tag_id_t>(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<tag_id_t>(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 0048a28fc..c18631494 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 2492337bf..2fe6702df 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<std::pair<std::string, std::string>> cases{ - {"<html><!DOCTYPE html><body>", "+html;++xml;++body;"}, - {"<html><div><div></div></div></html>", "+html;++div;+++div;"}, - {"<html><div><div></div></html>", "+html;++div;+++div;"}, - {"<html><div><div></div></html></div>", "+html;++div;+++div;"}, - {"<p><p><a></p></a></a>", "+p;++p;+++a;"}, + const std::vector<std::pair<std::string, std::string>> cases{ + {"<html><!DOCTYPE html><body>", "+html;++xml;++body;"}, + {"<html><div><div></div></div></html>", "+html;++div;+++div;"}, + {"<html><div><div></div></html>", "+html;++div;+++div;"}, + {"<html><div><div></div></html></div>", "+html;++div;+++div;"}, + {"<p><p><a></p></a></a>", "+p;++p;+++a;"}, {"<div><a href=\"http://example.com\"></div></a>", "+div;++a;"}, /* Broken, as I don't know how the hell this should be really parsed */ //{"<html><!DOCTYPE html><body><head><body></body></html></body></html>", // "+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<std::pair<std::string, std::string>> cases{ + TEST_CASE("html text extraction") + { + using namespace std::string_literals; + const std::vector<std::pair<std::string, std::string>> cases{ {"test", "test"}, {"test\0"s, "test\uFFFD"s}, {"test\0test"s, "test\uFFFDtest"s}, @@ -90,13 +92,15 @@ TEST_CASE("html text extraction") {" <body>\n" " <!-- escape content -->\n" " a b a > b a < b a & b 'a "a"\n" - " </body>", R"|(a b a > b a < b a & b 'a "a")|"}, + " </body>", + R"|(a b a > b a < b a & b 'a "a")|"}, /* XML tags */ {"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" " <!DOCTYPE html\n" " PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" - "<body>test</body>", "test"}, + "<body>test</body>", + "test"}, {"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head>" " <body>\n" " <p><br>\n" @@ -104,9 +108,11 @@ TEST_CASE("html text extraction") " <div class=\"moz-forward-container\"><br>\n" " <br>\n" " test</div>" - "</body>", "\n\n\ntest\n"}, + "</body>", + "\n\n\ntest\n"}, {"<div>fi<span style=\"FONT-SIZE: 0px\">le </span>" - "sh<span style=\"FONT-SIZE: 0px\">aring </span></div>", "fish\n"}, + "sh<span style=\"FONT-SIZE: 0px\">aring </span></div>", + "fish\n"}, /* FIXME: broken until rework of css parser */ //{"<div>fi<span style=\"FONT-SIZE: 0px\">le </span>" // "sh<span style=\"FONT-SIZE: 0px\">aring </div>foo</span>", "fish\nfoo"}, @@ -126,7 +132,8 @@ TEST_CASE("html text extraction") " </P>\n" " <b>stuff</p>?\n" " </body>\n" - "</html>", "Hello, world! test \ndata<>\nstuff\n?"}, + "</html>", + "Hello, world! test \ndata<>\nstuff\n?"}, {"<p><!--comment-->test</br></hr><br>", "test\n"}, /* Tables */ {"<table>\n" @@ -138,7 +145,8 @@ TEST_CASE("html text extraction") " <td>data1</td>\n" " <td>data2</td>\n" " </tr>\n" - " </table>", "heada headb\ndata1 data2\n"}, + " </table>", + "heada headb\ndata1 data2\n"}, /* Invalid closing br and hr + comment */ {" <body>\n" " <!-- page content -->\n" @@ -146,17 +154,20 @@ TEST_CASE("html text extraction") " <div>\n" " content inside div\n" " </div>\n" - " </body>", "Hello, world!\ntest\ncontentmore content\ncontent inside div\n"}, + " </body>", + "Hello, world!\ntest\ncontentmore content\ncontent inside div\n"}, /* First closing tag */ {"</head>\n" "<body>\n" "<p> Hello. I have some bad news.\n" "<br /> <br /> <br /> <strong> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> </strong><span> <br /> </span>test</p>\n" "</body>\n" - "</html>", "Hello. I have some bad news. \n\n\n\n\n\n\n\n\n\n\n\ntest\n"}, + "</html>", + "Hello. I have some bad news. \n\n\n\n\n\n\n\n\n\n\n\ntest\n"}, /* Invalid tags */ {"lol <sht> omg </sht> oh my!\n" - "<name>words words</name> goodbye","lol omg oh my! words words goodbye"}, + "<name>words words</name> goodbye", + "lol omg oh my! words words goodbye"}, /* Invisible stuff */ {"<div style=\"color:#555555;font-family:Arial, 'Helvetica Neue', Helvetica, sans-serif;line-height:1.2;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px;font-style: italic;\">\n" "<p style=\"font-size: 11px; line-height: 1.2; color: #555555; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; mso-line-height-alt: 14px; margin: 0;\">\n" @@ -166,75 +177,81 @@ TEST_CASE("html text extraction") "<span style=\"color:#FFFFFF; \">kreyes</span>\n" "<p style=\"font-size: 11px; line-height: 1.2; color: #555555; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; mso-line-height-alt: 14px; margin: 0;\">\n" " </p>", - " Sincerely,\n Skype Web\n"}, + " Sincerely,\n Skype Web\n"}, {"lala<p hidden>fafa</p>", "lala"}, {"<table style=\"FONT-SIZE: 0px;\"><tbody><tr><td>\n" "DONKEY\n" - "</td></tr></tbody></table>", ""}, + "</td></tr></tbody></table>", + ""}, /* bgcolor propagation */ {"<a style=\"display: inline-block; color: #ffffff; background-color: #00aff0;\">\n" "<span style=\"color: #00aff0;\">F</span>Rev<span style=\"opacity: 1;\"></span></span>ie<span style=\"opacity: 1;\"></span>" "</span>w<span style=\"color: #00aff0;\">F<span style=\"opacity: 1;\">̹</span></span>", - " Review"}, + " Review"}, {"<td style=\"color:#ffffff\" bgcolor=\"#005595\">\n" "hello world\n" - "</td>", "hello world"}, + "</td>", + "hello world"}, /* Colors */ {"goodbye <span style=\"COLOR: rgb(64,64,64)\">cruel</span>" - "<span>world</span>", "goodbye cruelworld"}, + "<span>world</span>", + "goodbye cruelworld"}, /* Font-size propagation */ {"<p style=\"font-size: 11pt;line-height:22px\">goodbye <span style=\"font-size:0px\">cruel</span>world</p>", - "goodbye world\n"}, + "goodbye world\n"}, /* Newline before tag -> must be space */ {"goodbye <span style=\"COLOR: rgb(64,64,64)\">cruel</span>\n" - "<span>world</span>", "goodbye cruel world"}, + "<span>world</span>", + "goodbye cruel world"}, /* Head tag with some stuff */ {"<html><head><p>oh my god</head><body></body></html>", "oh my god\n"}, {"<html><head><title>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 ae2514ba1..8f29f2c46 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 6c2f5a71d..46dde6d38 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 e1c6ccf31..5557fbf25 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 029dbc745..e98d16455 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 ff8e65893..721bc2782 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 635da7ba5..f3622ae7f 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 435cdcf13..670122d52 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 f0c0cc2dc..fa8ed04c2 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 0e48d9f3c..096545e7e 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 a70ea223f..2fdfe48aa 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 115ee9b8a..1bf70ed97 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 c9035375b..d5c4a57f9 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 3d8356c6d..ec575081e 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 6fdc72e87..4f068e604 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 d1707f490..c8dd490ab 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 f6abec658..8d4e313a8 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 d44e81aa8..b62048c86 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 2c1c535d5..b5250f657 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 6531f9916..91ae1956c 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 cb06abe7c..a5ce1cdc3 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 ad10b7c13..3c4f7f7fd 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 04557f0fe..d28f1c478 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 ac2edc82a..04df16ef0 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 a1052326c..339fee7c8 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 d56149c55..82c62b637 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 bbbac0cd6..60751c0ac 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 8c78a6c7a..cfb7d3ce3 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 df2a5efc6..096cda89f 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 693869c6f..bc292d380 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 99fe64c49..3aebaf660 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 2453a8f48..01f050a76 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 1d1214c27..c8e3fe441 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 460e70fd6..0e3c18744 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 a20ecbd6e..c604e9630 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 cbf76d1a9..2e3a7f54d 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 63c19f941..d6449a9f9 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 0bbfa55de..86ff2adb2 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 eb3de7194..339bf5f53 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 45b847923..f567b0b82 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 aeb731e82..62bce7f7d 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 cbafec270..9ed78a316 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 dd661c145..c3c861f8e 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 597245ca7..f2f95fcc5 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 907e2fc4b..72d8b99b2 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 cbfaec174..871ed2978 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 8ffd9fde5..8ee53b0fb 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 7c804421f..cde7d47a8 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 3554bdcf2..52881f77e 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 31da62abd..bef4fa906 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 4208d4882..94e90751b 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 be2a2b2f5..240939780 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 1ad113c54..a468aaf03 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 de25199f2..5ceffa121 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 31c94c413..535956b46 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 f60b81fbd..b4da5a888 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 358cba4fb..91fe1c783 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 17ab23a35..864bbedf4 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 3a4d24187..5404a1128 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 d5dafeaea..48e8949d1 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 f3d561736..d1fb8c908 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 3c90b8fb1..d2a900e01 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 bf379421a..ef48188ac 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 |