Example #1
0
static gboolean
check_metric_settings (struct rspamd_task *task, struct metric *metric,
	double *score)
{
	const ucl_object_t *mobj, *reject, *act;
	double val;

	if (task->settings == NULL) {
		return FALSE;
	}

	mobj = ucl_object_find_key (task->settings, metric->name);
	if (mobj != NULL) {
		act = ucl_object_find_key (mobj, "actions");
		if (act != NULL) {
			reject = ucl_object_find_key (act,
					rspamd_action_to_str (METRIC_ACTION_REJECT));
			if (reject != NULL && ucl_object_todouble_safe (reject, &val)) {
				*score = val;
				return TRUE;
			}
		}
	}

	return FALSE;
}
Example #2
0
static double
get_specific_action_score (const ucl_object_t *metric,
		struct metric_action *action)
{
	const ucl_object_t *act, *sact;
	double score;

	if (metric) {
		act = ucl_object_find_key (metric, "actions");
		if (act) {
			sact = ucl_object_find_key (act, rspamd_action_to_str (action->action));
			if (sact != NULL && ucl_object_todouble_safe (sact, &score)) {
				return score;
			}
		}
	}

	return action->score;
}
Example #3
0
/**
 * Add action for specified metric
 * @param cfg config file object
 * @param metric metric's name
 * @param action action's name
 * @param value value of symbol
 * @return
 */
gboolean
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);

	if (cfg->dynamic_conf == NULL) {
		msg_info ("dynamic conf is disabled");
		return FALSE;
	}

	metric = dynamic_metric_find_metric (cfg->current_dynamic_conf,
			metric_name);
	if (metric == NULL) {
		metric = new_dynamic_metric (metric_name, cfg->current_dynamic_conf);
	}

	acts = (ucl_object_t *)ucl_object_find_key (metric, "actions");
	if (acts != NULL) {
		ucl_object_t *act;

		act = dynamic_metric_find_elt (acts, action_name);
		if (act) {
			act->value.dv = value;
		}
		else {
			new_dynamic_elt (acts, action_name, value);
		}
	}

	apply_dynamic_conf (cfg->current_dynamic_conf, cfg);

	return TRUE;
}
Example #4
0
static double
get_specific_action_score (struct rspamd_task *task,
		const ucl_object_t *metric,
		struct metric_action *action)
{
	const ucl_object_t *act, *sact;
	const gchar *act_name;
	double score;

	if (metric) {
		act = ucl_object_find_key (metric, "actions");
		if (act) {
			act_name = rspamd_action_to_str (action->action);
			sact = ucl_object_find_key (act, act_name);
			if (sact != NULL && ucl_object_todouble_safe (sact, &score)) {
				msg_debug_task ("found override score %.2f for action %s in settings",
						score, act_name);
				return score;
			}
		}
	}

	return action->score;
}
Example #5
0
static void
smtp_metric_callback (gpointer key, gpointer value, gpointer ud)
{
	struct smtp_metric_callback_data *cd = ud;
	struct metric_result *metric_res = value;
	enum rspamd_metric_action action = METRIC_ACTION_NOACTION;
	double ms = 0, rs = 0;
	gboolean is_spam = FALSE;
	struct rspamd_task *task;

	task = cd->session->task;

	/* XXX rewrite */
	ms = metric_res->metric->actions[METRIC_ACTION_REJECT].score;
	rs = metric_res->metric->actions[METRIC_ACTION_REJECT].score;
#if 0
	if (!check_metric_settings (metric_res, &ms, &rs)) {
		ms = metric_res->metric->actions[METRIC_ACTION_REJECT].score;
		rs = metric_res->metric->actions[METRIC_ACTION_REJECT].score;
	}
	if (!check_metric_action_settings (task, metric_res, metric_res->score,
		&action)) {
		action =
			check_metric_action (metric_res->score, ms, metric_res->metric);
	}
#endif
	if (metric_res->score >= ms) {
		is_spam = 1;
	}
	if (action < cd->action) {
		cd->action = action;
		cd->res = metric_res;
	}

	if (!task->is_skipped) {
		cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset,
				cd->log_size - cd->log_offset,
				"(%s: %c (%s): [%.2f/%.2f/%.2f] [",
				(gchar *)key,
				is_spam ? 'T' : 'F',
				rspamd_action_to_str (action),
				metric_res->score,
				ms,
				rs);
	}
	else {
		cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset,
				cd->log_size - cd->log_offset,
				"(%s: %c (default): [%.2f/%.2f/%.2f] [",
				(gchar *)key,
				'S',
				metric_res->score,
				ms,
				rs);

	}
	g_hash_table_foreach (metric_res->symbols, smtp_metric_symbols_callback,
		cd);
	/* Remove last , from log buf */
	if (cd->log_buf[cd->log_offset - 1] == ',') {
		cd->log_buf[--cd->log_offset] = '\0';
	}

#ifdef HAVE_CLOCK_GETTIME
	cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset,
			cd->log_size - cd->log_offset,
			"]), len: %z, time: %s,",
			task->msg.len,
			calculate_check_time (&task->tv, &task->ts, task->cfg->clock_res,
			&task->scan_milliseconds));
#else
	cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset,
			cd->log_size - cd->log_offset,
			"]), len: %z, time: %s,",
			task->msg.len,
			calculate_check_time (&task->tv, task->cfg->clock_res,
			&task->scan_milliseconds));
#endif
}
Example #6
0
static gint
lua_worker_get_stat (lua_State *L)
{
	struct rspamd_worker *w = lua_check_worker (L, 1);

	if (w) {
		rspamd_mempool_stat_t mem_st;
		struct rspamd_stat *stat, stat_copy;
		ucl_object_t *top, *sub;
		gint i;
		guint64 spam = 0, ham = 0;

		memset (&mem_st, 0, sizeof (mem_st));
		rspamd_mempool_stat (&mem_st);
		memcpy (&stat_copy, w->srv->stat, sizeof (stat_copy));
		stat = &stat_copy;
		top = ucl_object_typed_new (UCL_OBJECT);
		ucl_object_insert_key (top, ucl_object_fromint (
				stat->messages_scanned), "scanned", 0, false);
		ucl_object_insert_key (top, ucl_object_fromint (
				stat->messages_learned), "learned", 0, false);
		if (stat->messages_scanned > 0) {
			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);
				if (i < METRIC_ACTION_GREYLIST) {
					spam += stat->actions_stat[i];
				}
				else {
					ham += stat->actions_stat[i];
				}
			}
			ucl_object_insert_key (top, sub, "actions", 0, false);
		}
		else {
			sub = ucl_object_typed_new (UCL_OBJECT);
			for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
				ucl_object_insert_key (sub,
						0,
						rspamd_action_to_str (i), 0, false);
			}
			ucl_object_insert_key (top, sub, "actions", 0, false);
		}
		ucl_object_insert_key (top, ucl_object_fromint (
				spam), "spam_count", 0, false);
		ucl_object_insert_key (top, ucl_object_fromint (
				ham),  "ham_count",      0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (stat->connections_count), "connections", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (stat->control_connections_count),
				"control_connections", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0,
				false);
		ucl_object_insert_key (top,
				ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0,
				false);
		ucl_object_insert_key (top,
				ucl_object_fromint (
						mem_st.chunks_allocated), "chunks_allocated", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (mem_st.shared_chunks_allocated),
				"shared_chunks_allocated", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false);
		ucl_object_insert_key (top,
				ucl_object_fromint (
						mem_st.oversized_chunks), "chunks_oversized", 0, false);

		ucl_object_push_lua (L, top, true);
		ucl_object_unref (top);
	}
	else {
		return luaL_error (L, "invalid arguments");
	}

	return 1;
}