int main (int argc, char **argv) { struct rspamd_config *cfg; rspamd_main = (struct rspamd_main *)g_malloc (sizeof (struct rspamd_main)); memset (rspamd_main, 0, sizeof (struct rspamd_main)); rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); cfg = rspamd_config_new (); rspamd_main->cfg = cfg; cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL); cfg->log_type = RSPAMD_LOG_CONSOLE; cfg->log_level = G_LOG_LEVEL_INFO; rspamd_set_logger (cfg, g_quark_from_static_string("rspamd-test"), &rspamd_main->logger, rspamd_main->server_pool); (void)rspamd_log_open (rspamd_main->logger); g_test_init (&argc, &argv, NULL); cfg->libs_ctx = rspamd_init_libs (); base = event_init (); rspamd_stat_init (cfg, base); if (g_test_verbose ()) { cfg->log_level = G_LOG_LEVEL_DEBUG; rspamd_set_logger (cfg, g_quark_from_static_string("rspamd-test"), &rspamd_main->logger, rspamd_main->server_pool); (void)rspamd_log_reopen (rspamd_main->logger); } g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); g_test_add_func ("/rspamd/mem_pool", rspamd_mem_pool_test_func); g_test_add_func ("/rspamd/radix", rspamd_radix_test_func); g_test_add_func ("/rspamd/dns", rspamd_dns_test_func); g_test_add_func ("/rspamd/dkim", rspamd_dkim_test_func); g_test_add_func ("/rspamd/rrd", rspamd_rrd_test_func); g_test_add_func ("/rspamd/upstream", rspamd_upstream_test_func); g_test_add_func ("/rspamd/shingles", rspamd_shingles_test_func); g_test_add_func ("/rspamd/http", rspamd_http_test_func); g_test_add_func ("/rspamd/lua", rspamd_lua_test_func); g_test_add_func ("/rspamd/cryptobox", rspamd_cryptobox_test_func); g_test_add_func ("/rspamd/heap", rspamd_heap_test_func); #if 0 g_test_add_func ("/rspamd/url", rspamd_url_test_func); g_test_add_func ("/rspamd/statfile", rspamd_statfile_test_func); g_test_add_func ("/rspamd/aio", rspamd_async_test_func); #endif g_test_run (); g_mime_shutdown (); rspamd_regexp_library_finalize (); return 0; }
int main (int argc, char **argv) { gint i, start = 1, mode = MODE_NORMAL; struct rspamd_config *cfg; rspamd_logger_t *logger = NULL; if (argc > 2 && *argv[1] == '-') { start = 2; if (argv[1][1] == 'g') { mode = MODE_GMIME; } } cfg = rspamd_config_new (); cfg->libs_ctx = rspamd_init_libs (); cfg->log_type = RSPAMD_LOG_CONSOLE; rspamd_set_logger (cfg, g_quark_from_static_string ("mime"), &logger, NULL); (void) rspamd_log_open (logger); g_log_set_default_handler (rspamd_glib_log_function, logger); g_set_printerr_handler (rspamd_glib_printerr_function); rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_URL|RSPAMD_CONFIG_INIT_NO_TLD); for (i = start; i < argc; i ++) { if (argv[i]) { rspamd_process_file (cfg, argv[i], mode); } } rspamd_printf ("Total time parsing: %.4f seconds\n", total_time); rspamd_log_close (logger); REF_RELEASE (cfg); return 0; }
static void rspamadm_confighelp (gint argc, gchar **argv, const struct rspamadm_command *cmd) { struct rspamd_config *cfg; ucl_object_t *doc_obj; const ucl_object_t *elt; GOptionContext *context; GError *error = NULL; module_t *mod, **pmod; worker_t **pworker; struct module_ctx *mod_ctx; gint i, ret = 0, processed_args = 0; context = g_option_context_new ( "confighelp - displays help for the configuration options"); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_ignore_unknown_options (context, TRUE); if (!g_option_context_parse (context, &argc, &argv, &error)) { rspamd_fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } pworker = &workers[0]; while (*pworker) { /* Init string quarks */ (void) g_quark_from_static_string ((*pworker)->name); pworker++; } cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_SKIP_LUA); cfg->lua_state = rspamd_main->cfg->lua_state; cfg->compiled_modules = modules; cfg->compiled_workers = workers; rspamd_rcl_config_init (cfg, NULL); lua_pushboolean (cfg->lua_state, true); lua_setglobal (cfg->lua_state, "confighelp"); rspamd_rcl_add_lua_plugins_path (cfg, plugins_path, FALSE, NULL, NULL); /* Init modules to get documentation strings */ i = 0; for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod++) { mod = *pmod; mod_ctx = g_malloc0 (sizeof (struct module_ctx)); if (mod->module_init_func (cfg, &mod_ctx) == 0) { g_ptr_array_add (cfg->c_modules, mod_ctx); mod_ctx->mod = mod; mod->ctx_offset = i++; mod_ctx->mod = mod; } } /* Also init all workers */ for (pworker = cfg->compiled_workers; *pworker != NULL; pworker ++) { (*pworker)->worker_init_func (cfg); } /* Init lua modules */ rspamd_lua_set_path (cfg->lua_state, cfg->rcl_obj, ucl_vars); rspamd_init_lua_filters (cfg, TRUE); if (argc > 1) { for (i = 1; i < argc; i ++) { if (argv[i][0] != '-') { if (keyword) { doc_obj = rspamadm_confighelp_search_word (cfg->doc_strings, argv[i]); } else { doc_obj = ucl_object_typed_new (UCL_OBJECT); elt = ucl_object_lookup_path (cfg->doc_strings, argv[i]); if (elt) { ucl_object_insert_key (doc_obj, ucl_object_ref (elt), argv[i], 0, false); } } if (doc_obj != NULL) { rspamadm_confighelp_show (cfg, argc, argv, argv[i], doc_obj); ucl_object_unref (doc_obj); } else { rspamd_fprintf (stderr, "Cannot find help for %s\n", argv[i]); ret = EXIT_FAILURE; } processed_args ++; } } } if (processed_args == 0) { /* Show all documentation strings */ rspamadm_confighelp_show (cfg, argc, argv, NULL, cfg->doc_strings); } rspamd_config_free (cfg); exit (ret); }
gint main (gint argc, gchar **argv, gchar **env) { GError *error = NULL; GOptionContext *context; GOptionGroup *og; struct rspamd_config *cfg; GQuark process_quark; gchar **nargv, **targv; const gchar *cmd_name; const struct rspamadm_command *cmd; gint i, nargc, targc; ucl_vars = g_hash_table_new_full (rspamd_strcase_hash, rspamd_strcase_equal, g_free, g_free); process_quark = g_quark_from_static_string ("rspamadm"); cfg = rspamd_config_new (); cfg->libs_ctx = rspamd_init_libs (); rspamd_main = g_malloc0 (sizeof (*rspamd_main)); rspamd_main->cfg = cfg; rspamd_main->pid = getpid (); rspamd_main->type = process_quark; rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "rspamadm"); cfg->log_level = G_LOG_LEVEL_WARNING; cfg->log_type = RSPAMD_LOG_CONSOLE; rspamd_set_logger (cfg, process_quark, rspamd_main); (void) rspamd_log_open (rspamd_main->logger); g_log_set_default_handler (rspamd_glib_log_function, rspamd_main->logger); g_set_printerr_handler (rspamd_glib_printerr_function); rspamd_config_post_load (cfg, FALSE); /* Setup logger */ if (verbose) { cfg->log_level = G_LOG_LEVEL_DEBUG; } else { cfg->log_level = G_LOG_LEVEL_INFO; } gperf_profiler_init (cfg, "rspamadm"); setproctitle ("rspamdadm"); /* Now read options and store everything till the first non-dash argument */ nargv = g_malloc0 (sizeof (gchar *) * (argc + 1)); nargv[0] = g_strdup (argv[0]); for (i = 1, nargc = 1; i < argc; i ++) { if (argv[i] && argv[i][0] == '-') { /* Copy to nargv */ nargv[nargc] = g_strdup (argv[i]); nargc ++; } else { break; } } context = g_option_context_new ("command - rspamd administration utility"); og = g_option_group_new ("global", "global options", "global options", NULL, NULL); g_option_context_set_help_enabled (context, FALSE); g_option_group_add_entries (og, entries); g_option_context_set_summary (context, "Summary:\n Rspamd administration utility version " RVERSION "\n Release id: " RID); g_option_context_set_main_group (context, og); targv = nargv; targc = nargc; if (!g_option_context_parse (context, &targc, &targv, &error)) { fprintf (stderr, "option parsing failed: %s\n", error->message); g_error_free (error); exit (1); } g_strfreev (nargv); if (show_version) { rspamadm_version (); exit (EXIT_SUCCESS); } if (show_help) { rspamadm_usage (context); exit (EXIT_SUCCESS); } if (list_commands) { rspamadm_commands (); exit (EXIT_SUCCESS); } cmd_name = argv[nargc]; if (cmd_name == NULL) { cmd_name = "help"; } cmd = rspamadm_search_command (cmd_name); if (cmd == NULL) { fprintf (stderr, "Invalid command name: %s\n", cmd_name); exit (EXIT_FAILURE); } if (nargc < argc) { nargv = g_malloc0 (sizeof (gchar *) * (argc - nargc + 1)); nargv[0] = g_strdup_printf ("%s %s", argv[0], cmd_name); for (i = 1; i < argc - nargc; i ++) { nargv[i] = g_strdup (argv[i + nargc]); } targc = argc - nargc; targv = nargv; cmd->run (targc, targv); g_strfreev (nargv); } else { cmd->run (0, NULL); } rspamd_log_close (rspamd_main->logger); REF_RELEASE (rspamd_main->cfg); g_free (rspamd_main); return 0; }
void rspamd_upstream_test_func (void) { struct upstream_list *ls, *nls; struct upstream *up, *upn; struct event_base *ev_base = event_init (); struct rspamd_dns_resolver *resolver; struct rspamd_config *cfg; gint i, success = 0; const gint assumptions = 100500; gdouble p; struct event ev; struct timeval tv; rspamd_inet_addr_t *addr, *next_addr, *paddr; cfg = rspamd_config_new (); cfg->dns_retransmits = 2; cfg->dns_timeout = 0.5; cfg->upstream_max_errors = 1; cfg->upstream_revive_time = 0.5; cfg->upstream_error_time = 2; resolver = dns_resolver_init (NULL, ev_base, cfg); rspamd_upstreams_library_config (cfg, cfg->ups_ctx, ev_base, resolver->r); /* * Test v4/v6 priorities */ nls = rspamd_upstreams_create (cfg->ups_ctx); g_assert (rspamd_upstreams_add_upstream (nls, "127.0.0.1", 0, NULL)); up = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_RANDOM, NULL, 0); rspamd_parse_inet_address (&paddr, "127.0.0.2", 0); g_assert (rspamd_upstream_add_addr (up, paddr)); rspamd_parse_inet_address (&paddr, "::1", 0); g_assert (rspamd_upstream_add_addr (up, paddr)); /* Rewind to start */ addr = rspamd_upstream_addr (up); addr = rspamd_upstream_addr (up); /* cur should be zero here */ addr = rspamd_upstream_addr (up); next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (addr) == AF_INET); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET6); /* Test errors with IPv6 */ rspamd_upstream_fail (up); /* Now we should have merely IPv4 addresses in rotation */ addr = rspamd_upstream_addr (up); for (i = 0; i < 256; i++) { next_addr = rspamd_upstream_addr (up); g_assert (rspamd_inet_address_get_af (addr) == AF_INET); g_assert (rspamd_inet_address_get_af (next_addr) == AF_INET); g_assert (rspamd_inet_address_compare (addr, next_addr) != 0); addr = next_addr; } rspamd_upstreams_destroy (nls); ls = rspamd_upstreams_create (cfg->ups_ctx); g_assert (rspamd_upstreams_parse_line (ls, test_upstream_list, 443, NULL)); g_assert (rspamd_upstreams_count (ls) == 3); /* Test master-slave rotation */ rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, "kernel.org"); /* Test round-robin rotation */ rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "kernel.org"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "google.com"); rspamd_upstream_test_method (ls, RSPAMD_UPSTREAM_ROUND_ROBIN, "microsoft.com"); /* Test stable hashing */ nls = rspamd_upstreams_create (cfg->ups_ctx); g_assert (rspamd_upstreams_parse_line (nls, test_upstream_list, 443, NULL)); g_assert (rspamd_upstreams_parse_line (nls, new_upstream_list, 443, NULL)); for (i = 0; i < assumptions; i ++) { ottery_rand_bytes (test_key, sizeof (test_key)); up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_HASHED, test_key, sizeof (test_key)); upn = rspamd_upstream_get (nls, RSPAMD_UPSTREAM_HASHED, test_key, sizeof (test_key)); if (strcmp (rspamd_upstream_name (up), rspamd_upstream_name (upn)) == 0) { success ++; } } p = 1.0 - fabs (3.0 / 4.0 - (gdouble)success / (gdouble)assumptions); /* * P value is calculated as following: * when we add/remove M upstreams from the list, the probability of hash * miss should be close to the relation N / (N + M), where N is the size of * the previous upstreams list. */ msg_debug ("p value for hash consistency: %.6f", p); g_assert (p > 0.9); rspamd_upstreams_destroy (nls); /* Upstream fail test */ evtimer_set (&ev, rspamd_upstream_timeout_handler, resolver); event_base_set (ev_base, &ev); up = rspamd_upstream_get (ls, RSPAMD_UPSTREAM_MASTER_SLAVE, NULL, 0); for (i = 0; i < 100; i ++) { rspamd_upstream_fail (up); } g_assert (rspamd_upstreams_alive (ls) == 2); tv.tv_sec = 2; tv.tv_usec = 0; event_add (&ev, &tv); event_base_loop (ev_base, 0); g_assert (rspamd_upstreams_alive (ls) == 3); rspamd_upstreams_destroy (ls); REF_RELEASE (cfg); }