aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstat/backends/cdb_backend.cxx
blob: bd05e8ef8c236a20c44d23172b72f4876bd96d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * Copyright 2024 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * CDB read only statistics backend
 */

#include "config.h"
#include "stat_internal.h"
#include "contrib/cdb/cdb.h"

#include <utility>
#include <memory>
#include <string>
#include <optional>
#include "contrib/expected/expected.hpp"
#include "contrib/ankerl/unordered_dense.h"
#include "fmt/base.h"

namespace rspamd::stat::cdb {

/*
 * Utility class to share cdb instances over statfiles instances, as each
 * cdb has tokens for both ham and spam classes
 */
class cdb_shared_storage {
public:
	using cdb_element_t = std::shared_ptr<struct cdb>;
	cdb_shared_storage() = default;

	auto get_cdb(const char *path) const -> std::optional<cdb_element_t>
	{
		auto found = elts.find(path);

		if (found != elts.end()) {
			if (!found->second.expired()) {
				return found->second.lock();
			}
		}

		return std::nullopt;
	}
	/* Create a new smart pointer over POD cdb structure */
	static auto new_cdb() -> cdb_element_t
	{
		auto ret = cdb_element_t(new struct cdb, cdb_deleter());
		memset(ret.get(), 0, sizeof(struct cdb));
		return ret;
	}
	/* Enclose cdb into storage */
	auto push_cdb(const char *path, cdb_element_t cdbp) -> cdb_element_t
	{
		auto found = elts.find(path);

		if (found != elts.end()) {
			if (found->second.expired()) {
				/* OK, move in lieu of the expired weak pointer */

				found->second = cdbp;
				return cdbp;
			}
			else {
				/*
				 * Existing and not expired, return the existing one
				 */
				return found->second.lock();
			}
		}
		else {
			/* Not existing, make a weak ptr and return the original */
			elts.emplace(path, std::weak_ptr<struct cdb>(cdbp));
			return cdbp;
		}
	}

private:
	/*
	 * We store weak pointers here to allow owning cdb statfiles to free
	 * expensive cdb before this cache is terminated (e.g. on dynamic cdb reload)
	 */
	ankerl::unordered_dense::map<std::string, std::weak_ptr<struct cdb>> elts;

	struct cdb_deleter {
		void operator()(struct cdb *c) const
		{
			cdb_free(c);
			delete c;
		}
	};
};

static cdb_shared_storage cdb_shared_storage;

class ro_backend final {
public:
	explicit ro_backend(struct rspamd_statfile *_st, cdb_shared_storage::cdb_element_t _db)
		: st(_st), db(std::move(_db))
	{
	}
	ro_backend() = delete;
	ro_backend(const ro_backend &) = delete;
	ro_backend(ro_backend &&other) noexcept
	{
		*this = std::move(other);
	}
	ro_backend &operator=(ro_backend &&other) noexcept
	{
		std::swap(st, other.st);
		std::swap(db, other.db);
		std::swap(loaded, other.loaded);
		std::swap(learns_spam, other.learns_spam);
		std::swap(learns_ham, other.learns_ham);

		return *this;
	}
	~ro_backend()
	{
	}

	auto load_cdb() -> tl::expected<bool, std::string>;
	auto process_token(const rspamd_token_t *tok) const -> std::optional<float>;
	constexpr auto is_spam() const -> bool
	{
		return st->stcf->is_spam;
	}
	auto get_learns() const -> std::uint64_t
	{
		if (is_spam()) {
			return learns_spam;
		}
		else {
			return learns_ham;
		}
	}
	auto get_total_learns() const -> std::uint64_t
	{
		return learns_spam + learns_ham;
	}

private:
	struct rspamd_statfile *st;
	cdb_shared_storage::cdb_element_t db;
	bool loaded = false;
	std::uint64_t learns_spam = 0;
	std::uint64_t learns_ham = 0;
};

template<typename T>
static inline auto
cdb_get_key_as_int64(struct cdb *cdb, T key) -> std::optional<std::int64_t>
{
	auto pos = cdb_find(cdb, (void *) &key, sizeof(key));

	if (pos > 0) {
		auto vpos = cdb_datapos(cdb);
		auto vlen = cdb_datalen(cdb);

		if (vlen == sizeof(std::int64_t)) {
			std::int64_t ret;
			cdb_read(cdb, (void *) &ret, vlen, vpos);

			return ret;
		}
	}

	return std::nullopt;
}

template<typename T>
static inline auto
cdb_get_key_as_float_pair(struct cdb *cdb, T key) -> std::optional<std::pair<float, float>>
{
	auto pos = cdb_find(cdb, (void *) &key, sizeof(key));

	if (pos > 0) {
		auto vpos = cdb_datapos(cdb);
		auto vlen = cdb_datalen(cdb);

		if (vlen == sizeof(float) * 2) {
			union {
				struct {
					float v1;
					float v2;
				} d;
				char c[sizeof(float) * 2];
			} u;
			cdb_read(cdb, (void *) u.c, vlen, vpos);

			return std::make_pair(u.d.v1, u.d.v2);
		}
	}

	return std::nullopt;
}


auto ro_backend::load_cdb() -> tl::expected<bool, std::string>
{
	if (!db) {
		return tl::make_unexpected("no database loaded");
	}

	/* Now get number of learns */
	std::int64_t cdb_key;
	static const char learn_spam_key[9] = "_lrnspam", learn_ham_key[9] = "_lrnham_";

	auto check_key = [&](const char *key, std::uint64_t &target) -> tl::expected<bool, std::string> {
		memcpy((void *) &cdb_key, key, sizeof(cdb_key));

		auto maybe_value = cdb_get_key_as_int64(db.get(), cdb_key);

		if (!maybe_value) {
			return tl::make_unexpected(fmt::format("missing {} key", key));
		}

		target = (std::uint64_t) maybe_value.value();

		return true;
	};

	auto res = check_key(learn_spam_key, learns_spam);

	if (!res) {
		return res;
	}

	res = check_key(learn_ham_key, learns_ham);

	if (!res) {
		return res;
	}

	loaded = true;

	return true;// expected
}

auto ro_backend::process_token(const rspamd_token_t *tok) const -> std::optional<float>
{
	if (!loaded) {
		return std::nullopt;
	}

	auto maybe_value = cdb_get_key_as_float_pair(db.get(), tok->data);

	if (maybe_value) {
		auto [spam_count, ham_count] = maybe_value.value();

		if (is_spam()) {
			return spam_count;
		}
		else {
			return ham_count;
		}
	}

	return std::nullopt;
}

auto open_cdb(struct rspamd_statfile *st) -> tl::expected<ro_backend, std::string>
{
	const char *path = nullptr;
	const auto *stf = st->stcf;

	auto get_filename = [](const ucl_object_t *obj) -> const char * {
		const auto *filename = ucl_object_lookup_any(obj,
													 "filename", "path", "cdb", nullptr);

		if (filename && ucl_object_type(filename) == UCL_STRING) {
			return ucl_object_tostring(filename);
		}

		return nullptr;
	};

	/* First search in backend configuration */
	const auto *obj = ucl_object_lookup(st->classifier->cfg->opts, "backend");
	if (obj != NULL && ucl_object_type(obj) == UCL_OBJECT) {
		path = get_filename(obj);
	}

	/* Now try statfiles config */
	if (!path && stf->opts) {
		path = get_filename(stf->opts);
	}

	/* Now try classifier config */
	if (!path && st->classifier->cfg->opts) {
		path = get_filename(st->classifier->cfg->opts);
	}

	if (!path) {
		return tl::make_unexpected("missing/malformed filename attribute");
	}

	auto cached_cdb_maybe = cdb_shared_storage.get_cdb(path);
	cdb_shared_storage::cdb_element_t cdbp;

	if (!cached_cdb_maybe) {

		auto fd = rspamd_file_xopen(path, O_RDONLY, 0, true);

		if (fd == -1) {
			return tl::make_unexpected(fmt::format("cannot open {}: {}",
												   path, strerror(errno)));
		}

		cdbp = cdb_shared_storage::new_cdb();

		if (cdb_init(cdbp.get(), fd) == -1) {
			close(fd);

			return tl::make_unexpected(fmt::format("cannot init cdb in {}: {}",
												   path, strerror(errno)));
		}

		cdbp = cdb_shared_storage.push_cdb(path, cdbp);

		close(fd);
	}
	else {
		cdbp = cached_cdb_maybe.value();
	}

	if (!cdbp) {
		return tl::make_unexpected(fmt::format("cannot init cdb in {}: internal error",
											   path));
	}

	ro_backend bk{st, std::move(cdbp)};

	auto res = bk.load_cdb();

	if (!res) {
		return tl::make_unexpected(res.error());
	}

	return bk;
}

}// namespace rspamd::stat::cdb

#define CDB_FROM_RAW(p) (reinterpret_cast<rspamd::stat::cdb::ro_backend *>(p))

/* C exports */
gpointer
rspamd_cdb_init(struct rspamd_stat_ctx *ctx,
				struct rspamd_config *cfg,
				struct rspamd_statfile *st)
{
	auto maybe_backend = rspamd::stat::cdb::open_cdb(st);

	if (maybe_backend) {
		/* Move into a new pointer */
		auto *result = new rspamd::stat::cdb::ro_backend(std::move(maybe_backend.value()));

		return result;
	}
	else {
		msg_err_config("cannot load cdb backend: %s", maybe_backend.error().c_str());
	}

	return nullptr;
}
gpointer
rspamd_cdb_runtime(struct rspamd_task *task,
				   struct rspamd_statfile_config *stcf,
				   gboolean learn,
				   gpointer ctx,
				   int _id)
{
	/* In CDB we don't have any dynamic stuff */
	return ctx;
}

gboolean
rspamd_cdb_process_tokens(struct rspamd_task *task,
						  GPtrArray *tokens,
						  int id,
						  gpointer runtime)
{
	auto *cdbp = CDB_FROM_RAW(runtime);
	bool seen_values = false;

	for (auto i = 0u; i < tokens->len; i++) {
		rspamd_token_t *tok;
		tok = reinterpret_cast<rspamd_token_t *>(g_ptr_array_index(tokens, i));

		auto res = cdbp->process_token(tok);

		if (res) {
			tok->values[id] = res.value();
			seen_values = true;
		}
		else {
			tok->values[id] = 0;
		}
	}

	if (seen_values) {
		if (cdbp->is_spam()) {
			task->flags |= RSPAMD_TASK_FLAG_HAS_SPAM_TOKENS;
		}
		else {
			task->flags |= RSPAMD_TASK_FLAG_HAS_HAM_TOKENS;
		}
	}

	return true;
}
gboolean
rspamd_cdb_finalize_process(struct rspamd_task *task,
							gpointer runtime,
							gpointer ctx)
{
	return true;
}
gboolean
rspamd_cdb_learn_tokens(struct rspamd_task *task,
						GPtrArray *tokens,
						int id,
						gpointer ctx)
{
	return false;
}
gboolean
rspamd_cdb_finalize_learn(struct rspamd_task *task,
						  gpointer runtime,
						  gpointer ctx,
						  GError **err)
{
	return false;
}

gulong rspamd_cdb_total_learns(struct rspamd_task *task,
							   gpointer runtime,
							   gpointer ctx)
{
	auto *cdbp = CDB_FROM_RAW(ctx);
	return cdbp->get_total_learns();
}
gulong
rspamd_cdb_inc_learns(struct rspamd_task *task,
					  gpointer runtime,
					  gpointer ctx)
{
	return (gulong) -1;
}
gulong
rspamd_cdb_dec_learns(struct rspamd_task *task,
					  gpointer runtime,
					  gpointer ctx)
{
	return (gulong) -1;
}
gulong
rspamd_cdb_learns(struct rspamd_task *task,
				  gpointer runtime,
				  gpointer ctx)
{
	auto *cdbp = CDB_FROM_RAW(ctx);
	return cdbp->get_learns();
}
ucl_object_t *
rspamd_cdb_get_stat(gpointer runtime, gpointer ctx)
{
	return nullptr;
}
gpointer
rspamd_cdb_load_tokenizer_config(gpointer runtime, gsize *len)
{
	return nullptr;
}
void rspamd_cdb_close(gpointer ctx)
{
	auto *cdbp = CDB_FROM_RAW(ctx);
	delete cdbp;
}