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
|
/*-
* Copyright 2016 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.
*/
#include "lua_common.h"
#include "cdb.h"
#define CDB_REFRESH_TIME 60
/***
* @module rspamd_cdb
* Rspamd CDB module is used to read and write key/value pairs to the CDB file
*
* @example
local rspamd_cdb = require "rspamd_cdb"
rspamd_cdb.build('/tmp/test.cdb'):add('test', 'value'):finalize()
local c = rspamd_cdb.open('/tmp/test.cdb')
c:find('test')
-- will return 'value'
*/
/***
* @function rspamd_cdb.open(filename, [ev_base])
* Opens an existing CDB for reading. If `ev_base` is specified, then cdb file is added
* for monitoring, that will get updates on disk file changes.
* @param {string} filename path to file
* @param {ev_base} event loop object
* @return {rspamd_cdb} cdb object
*/
LUA_FUNCTION_DEF(cdb, create);
/***
* @method rspamd_cdb:find(key)
* Finds a specific key in cdb and returns a string or nil if a key has not been found
* @param {string} key key to find
* @return {string/nil} value for the specific key
*/
LUA_FUNCTION_DEF(cdb, lookup);
/***
* @method rspamd_cdb:get_name()
* Returns filename for the specific cdb
* @return {string} filename for cdb
*/
LUA_FUNCTION_DEF(cdb, get_name);
LUA_FUNCTION_DEF(cdb, destroy);
/***
* @function rspamd_cdb.build(filename, [mode])
* Creates a new cdb in a file (existing one will be overwritten!). The object
* returned can be used merely for adding data. Upon finalizing, the data is written to
* disk and cdb can no longer be changed.
* @param {string} filename path to file
* @param {int} mode numeric mode to create a file
* @return {rspamd_cdb_builder} cdb builder object (or nil + error message)
*/
LUA_FUNCTION_DEF(cdb, build);
/***
* @method rspamd_cdb_builder:add(key, value)
* Adds new value to cdb in the builder mode
* @param {string} key key to add
* @param {string} value value to associate with the key
* @return {rspamd_cdb_builder} the same object to allow chaining calls
*/
LUA_FUNCTION_DEF(cdb_builder, add);
/***
* @method rspamd_cdb_builder:finalize()
* Finalizes the CDB and writes it to disk. This method also closes FD associated with
* CDB builder. No further additions are allowed after this point
*/
LUA_FUNCTION_DEF(cdb_builder, finalize);
LUA_FUNCTION_DEF(cdb_builder, dtor);
static const struct luaL_reg cdblib_m[] = {
LUA_INTERFACE_DEF(cdb, lookup),
{"find", lua_cdb_lookup},
LUA_INTERFACE_DEF(cdb, get_name),
{"__tostring", rspamd_lua_class_tostring},
{"__gc", lua_cdb_destroy},
{NULL, NULL}};
static const struct luaL_reg cdbbuilderlib_m[] = {
LUA_INTERFACE_DEF(cdb_builder, add),
LUA_INTERFACE_DEF(cdb_builder, finalize),
{"__tostring", rspamd_lua_class_tostring},
{"__gc", lua_cdb_builder_dtor},
{NULL, NULL}};
static const struct luaL_reg cdblib_f[] = {
LUA_INTERFACE_DEF(cdb, create),
{"open", lua_cdb_create},
{"build", lua_cdb_build},
{NULL, NULL}};
static struct cdb *
lua_check_cdb(lua_State *L, int pos)
{
void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cdb}");
luaL_argcheck(L, ud != NULL, pos, "'cdb' expected");
return ud ? *((struct cdb **) ud) : NULL;
}
static struct cdb_make *
lua_check_cdb_builder(lua_State *L, int pos)
{
void *ud = rspamd_lua_check_udata(L, pos, "rspamd{cdb_builder}");
luaL_argcheck(L, ud != NULL, pos, "'cdb_builder' expected");
return ud ? ((struct cdb_make *) ud) : NULL;
}
static const char *
lua_cdb_get_input(lua_State *L, int pos, gsize *olen)
{
int t = lua_type(L, pos);
switch (t) {
case LUA_TSTRING:
return lua_tolstring(L, pos, olen);
case LUA_TNUMBER: {
static char numbuf[sizeof(lua_Number)];
lua_Number n = lua_tonumber(L, pos);
memcpy(numbuf, &n, sizeof(numbuf));
*olen = sizeof(n);
return numbuf;
}
case LUA_TUSERDATA: {
void *p = rspamd_lua_check_udata_maybe(L, pos, "rspamd{text}");
if (p) {
struct rspamd_lua_text *t = (struct rspamd_lua_text *) p;
*olen = t->len;
return t->start;
}
p = rspamd_lua_check_udata_maybe(L, pos, "rspamd{int64}");
if (p) {
static char numbuf[sizeof(gint64)];
memcpy(numbuf, p, sizeof(numbuf));
*olen = sizeof(numbuf);
return numbuf;
}
}
default:
break;
}
return NULL;
}
static gint
lua_cdb_create(lua_State *L)
{
struct cdb *cdb, **pcdb;
const gchar *filename;
gint fd;
struct ev_loop *ev_base = NULL;
if (lua_type(L, 2) == LUA_TUSERDATA) {
ev_base = lua_check_ev_base(L, 2);
}
filename = luaL_checkstring(L, 1);
/* If file begins with cdb://, just skip it */
if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) {
filename += sizeof("cdb://") - 1;
}
if ((fd = open(filename, O_RDONLY)) == -1) {
msg_warn("cannot open cdb: %s, %s", filename, strerror(errno));
lua_pushnil(L);
}
else {
cdb = g_malloc0(sizeof(struct cdb));
cdb->filename = g_strdup(filename);
if (cdb_init(cdb, fd) == -1) {
g_free(cdb->filename);
g_free(cdb);
msg_warn("cannot open cdb: %s, %s", filename, strerror(errno));
lua_pushnil(L);
}
else {
#ifdef HAVE_READAHEAD
struct stat st;
/*
* Do not readahead more than 100mb,
* which is enough for the vast majority of the use cases
*/
static const size_t max_readahead = 100 * 0x100000;
if (fstat(cdb_fileno(cdb), &st) != 1) {
/* Must always be true because cdb_init calls it as well */
if (readahead(cdb_fileno(cdb), 0, MIN(max_readahead, st.st_size)) == -1) {
msg_warn("cannot readahead cdb: %s, %s", filename, strerror(errno));
}
}
#endif
if (ev_base) {
cdb_add_timer(cdb, ev_base, CDB_REFRESH_TIME);
}
pcdb = lua_newuserdata(L, sizeof(struct cdb *));
rspamd_lua_setclass(L, "rspamd{cdb}", -1);
*pcdb = cdb;
}
}
return 1;
}
static gint
lua_cdb_get_name(lua_State *L)
{
struct cdb *cdb = lua_check_cdb(L, 1);
if (!cdb) {
lua_error(L);
return 1;
}
lua_pushstring(L, cdb->filename);
return 1;
}
static gint
lua_cdb_lookup(lua_State *L)
{
struct cdb *cdb = lua_check_cdb(L, 1);
gsize klen;
const gchar *what = lua_cdb_get_input(L, 2, &klen);
if (!cdb || what == NULL) {
return lua_error(L);
}
if (cdb_find(cdb, what, klen) > 0) {
/* Extract and push value to lua as string */
lua_pushlstring(L, cdb_getdata(cdb), cdb_datalen(cdb));
}
else {
lua_pushnil(L);
}
return 1;
}
static gint
lua_cdb_destroy(lua_State *L)
{
struct cdb *cdb = lua_check_cdb(L, 1);
if (cdb) {
cdb_free(cdb);
if (cdb->cdb_fd != -1) {
(void) close(cdb->cdb_fd);
}
g_free(cdb->filename);
g_free(cdb);
}
return 0;
}
static gint
lua_cdb_build(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
int fd, mode = 00755;
if (filename == NULL) {
return luaL_error(L, "invalid arguments, filename expected");
}
/* If file begins with cdb://, just skip it */
if (g_ascii_strncasecmp(filename, "cdb://", sizeof("cdb://") - 1) == 0) {
filename += sizeof("cdb://") - 1;
}
if (lua_isnumber(L, 2)) {
mode = lua_tointeger(L, 2);
}
fd = rspamd_file_xopen(filename, O_RDWR | O_CREAT | O_TRUNC, mode, 0);
if (fd == -1) {
lua_pushnil(L);
lua_pushfstring(L, "cannot open cdb: %s, %s", filename, strerror(errno));
return 2;
}
struct cdb_make *cdbm = lua_newuserdata(L, sizeof(struct cdb_make));
g_assert(cdb_make_start(cdbm, fd) == 0);
rspamd_lua_setclass(L, "rspamd{cdb_builder}", -1);
return 1;
}
static gint
lua_cdb_builder_add(lua_State *L)
{
struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
gsize data_sz, key_sz;
const char *key = lua_cdb_get_input(L, 2, &key_sz);
const char *data = lua_cdb_get_input(L, 3, &data_sz);
if (cdbm == NULL || key == NULL || data == NULL || cdbm->cdb_fd == -1) {
return luaL_error(L, "invalid arguments");
}
if (cdb_make_add(cdbm, key, key_sz, data, data_sz) == -1) {
lua_pushvalue(L, 1);
lua_pushfstring(L, "cannot push value to cdb: %s", strerror(errno));
return 2;
}
/* Allow chaining */
lua_pushvalue(L, 1);
return 1;
}
static gint
lua_cdb_builder_finalize(lua_State *L)
{
struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
if (cdbm == NULL || cdbm->cdb_fd == -1) {
return luaL_error(L, "invalid arguments");
}
if (cdb_make_finish(cdbm) == -1) {
lua_pushvalue(L, 1);
lua_pushfstring(L, "cannot finish value to cdb: %s", strerror(errno));
return 2;
}
close(cdbm->cdb_fd);
cdbm->cdb_fd = -1; /* To distinguish finalized object */
/* Allow chaining */
lua_pushvalue(L, 1);
return 1;
}
static gint
lua_cdb_builder_dtor(lua_State *L)
{
struct cdb_make *cdbm = lua_check_cdb_builder(L, 1);
if (cdbm == NULL) {
return luaL_error(L, "invalid arguments");
}
if (cdbm->cdb_fd != -1) {
cdb_make_finish(cdbm);
close(cdbm->cdb_fd);
cdbm->cdb_fd = -1; /* Finalized object */
}
return 0;
}
static gint
lua_load_cdb(lua_State *L)
{
lua_newtable(L);
luaL_register(L, NULL, cdblib_f);
return 1;
}
void luaopen_cdb(lua_State *L)
{
rspamd_lua_new_class(L, "rspamd{cdb}", cdblib_m);
lua_pop(L, 1);
rspamd_lua_new_class(L, "rspamd{cdb_builder}", cdbbuilderlib_m);
lua_pop(L, 1);
rspamd_lua_add_preload(L, "rspamd_cdb", lua_load_cdb);
}
|