31 #include <hiredis/hiredis.h> 38 #define G_LOG_DOMAIN "lib kb" 50 #define GLOBAL_DBINDEX_NAME "GVM.__GlobalDBIndex" 67 #define redis_kb(__kb) ((struct kb_redis *) (__kb)) 84 redisContext *ctx = kbr->
rctx;
92 if (rep->type != REDIS_REPLY_INTEGER)
94 else if (rep->integer == 0)
99 freeReplyObject (rep);
113 #define MAX_DB_INDEX__24 1000 124 redisContext *ctx = kbr->
rctx;
136 current = min + ((max - min) / 2);
138 rep = redisCommand (ctx,
"SELECT %d", current);
142 "%s: redis command failed with '%s'", __func__, ctx->errstr);
148 case REDIS_REPLY_ERROR:
152 case REDIS_REPLY_STATUS:
158 "%s: unexpected reply of type %d", __func__, rep->type);
159 freeReplyObject (rep);
162 freeReplyObject (rep);
168 rep = redisCommand (ctx,
"SELECT 0");
172 "%s: DB selection failed with '%s'", __func__, ctx->errstr);
177 freeReplyObject (rep);
192 redisContext *ctx = kbr->
rctx;
193 redisReply *rep = NULL;
195 rep = redisCommand (ctx,
"CONFIG GET databases");
199 "%s: redis command failed with '%s'", __func__, ctx->errstr);
204 if (rep->type != REDIS_REPLY_ARRAY)
207 "%s: cannot retrieve max DB number: %s", __func__, rep->str);
212 if (rep->elements == 0)
217 else if (rep->elements == 2)
219 kbr->
max_db = (unsigned) atoi (rep->element[1]->str);
224 "%s: unexpected reply length (%zd)", __func__, rep->elements);
229 g_debug (
"%s: maximum DB number: %u", __func__, kbr->
max_db);
233 freeReplyObject (rep);
250 redisContext *ctx = kbr->
rctx;
251 redisReply *rep = NULL;
260 for (i = 1; i < kbr->
max_db; i++)
275 rep = redisCommand (ctx,
"SELECT %u", kbr->
db);
276 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
286 freeReplyObject (rep);
301 redisContext *ctx = kbr->
rctx;
307 rep = redisCommand (ctx,
"SELECT 0");
308 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
313 freeReplyObject (rep);
316 if (rep == NULL || rep->type != REDIS_REPLY_INTEGER)
326 freeReplyObject (rep);
343 if (kbr->
rctx != NULL)
346 kbr->
rctx = redisConnectUnix (kbr->
path);
347 if (kbr->
rctx == NULL || kbr->
rctx->err)
350 "%s: redis connection error: %s", __func__,
351 kbr->
rctx ? kbr->
rctx->errstr : strerror (ENOMEM));
352 redisFree (kbr->
rctx);
360 g_log (
G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"No redis DB available");
361 redisFree (kbr->
rctx);
366 g_debug (
"%s: connected to redis://%s/%d", __func__, kbr->
path, kbr->
db);
390 if (rep->type != REDIS_REPLY_STATUS)
396 if (g_ascii_strcasecmp (rep->str,
"PONG"))
404 freeReplyObject (rep);
424 if (kbr->
rctx != NULL)
426 redisFree (kbr->
rctx);
461 kbr = g_malloc0 (
sizeof (
struct kb_redis) + strlen (kb_path) + 1);
463 strncpy (kbr->
path, kb_path, strlen (kb_path));
470 "%s: cannot access redis at '%s'", __func__, kb_path);
492 kbr = g_malloc0 (
sizeof (
struct kb_redis) + strlen (kb_path) + 1);
494 strncpy (kbr->
path, kb_path, strlen (kb_path));
496 kbr->
rctx = redisConnectUnix (kbr->
path);
497 if (kbr->
rctx == NULL || kbr->
rctx->err)
500 "%s: redis connection error: %s", __func__,
501 kbr->
rctx ? kbr->
rctx->errstr : strerror (ENOMEM));
502 redisFree (kbr->
rctx);
507 rep = redisCommand (kbr->
rctx,
"SELECT %d", kb_index);
508 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
511 freeReplyObject (rep);
512 redisFree (kbr->
rctx);
516 freeReplyObject (rep);
532 kbr = g_malloc0 (
sizeof (
struct kb_redis) + strlen (kb_path) + 1);
534 strncpy (kbr->
path, kb_path, strlen (kb_path));
540 kbr->
rctx = redisConnectUnix (kbr->
path);
541 if (kbr->
rctx == NULL || kbr->
rctx->err)
544 "%s: redis connection error: %s", __func__,
545 kbr->
rctx ? kbr->
rctx->errstr : strerror (ENOMEM));
546 redisFree (kbr->
rctx);
556 if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
559 freeReplyObject (rep);
561 redisFree (kbr->
rctx);
565 freeReplyObject (rep);
566 rep = redisCommand (kbr->
rctx,
"SELECT %u", i);
567 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
569 redisFree (kbr->
rctx);
574 freeReplyObject (rep);
584 redisFree (kbr->
rctx);
607 g_free (item->
v_str);
626 if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER)
632 if (elt->type == REDIS_REPLY_INTEGER)
635 item->
v_int = elt->integer;
640 item->
v_int = atoi (elt->str);
645 item->
v_str = g_memdup (elt->str, elt->len + 1);
646 item->
len = elt->len;
673 case REDIS_REPLY_STRING:
674 case REDIS_REPLY_INTEGER:
678 case REDIS_REPLY_ARRAY:
679 for (i = 0; i < rep->elements; i++)
697 case REDIS_REPLY_NIL:
698 case REDIS_REPLY_STATUS:
699 case REDIS_REPLY_ERROR:
730 rep = redisvCommand (kbr->
rctx, fmt, aq);
736 freeReplyObject (rep);
769 rep =
redis_cmd (kbr,
"LINDEX %s -1", name);
770 if (rep == NULL || rep->type != REDIS_REPLY_STRING)
780 freeReplyObject (rep);
821 redisReply *rep = NULL;
825 rep =
redis_cmd (kbr,
"LPUSH %s %s", name, value);
826 if (!rep || rep->type == REDIS_REPLY_ERROR)
830 freeReplyObject (rep);
853 if (rep->type == REDIS_REPLY_STRING)
854 value = g_strdup (rep->str);
855 freeReplyObject (rep);
900 rep =
redis_cmd (kbr,
"LINDEX filename:%s %d", oid,
903 rep =
redis_cmd (kbr,
"LINDEX nvt:%s %d", oid, position);
906 if (rep->type == REDIS_REPLY_INTEGER)
907 res = g_strdup_printf (
"%lld", rep->integer);
908 else if (rep->type == REDIS_REPLY_STRING)
909 res = g_strdup (rep->str);
910 freeReplyObject (rep);
932 if (rep->type != REDIS_REPLY_ARRAY || rep->elements !=
NVT_NAME_POS + 1)
934 freeReplyObject (rep);
958 freeReplyObject (rep);
985 freeReplyObject (rep);
1006 rep =
redis_cmd (kbr,
"KEYS %s", pattern);
1009 if (rep->type != REDIS_REPLY_ARRAY)
1011 freeReplyObject (rep);
1017 for (i = 0; i < rep->elements; i++)
1018 redisAppendCommand (kbr->
rctx,
"LRANGE %s 0 -1", rep->element[i]->str);
1020 for (i = 0; i < rep->elements; i++)
1023 redisReply *rep_range;
1025 redisGetReply (kbr->
rctx, (
void **) &rep_range);
1031 freeReplyObject (rep_range);
1047 freeReplyObject (rep_range);
1050 freeReplyObject (rep);
1064 GSList *list = NULL;
1072 if (rep->type != REDIS_REPLY_ARRAY)
1074 freeReplyObject (rep);
1079 for (i = 0; i < rep->elements; i++)
1080 list = g_slist_prepend (list, g_strdup (rep->element[i]->str + 4));
1081 freeReplyObject (rep);
1103 rep =
redis_cmd (kbr,
"KEYS %s", pattern);
1107 if (rep->type != REDIS_REPLY_ARRAY)
1109 freeReplyObject (rep);
1113 count = rep->elements;
1114 freeReplyObject (rep);
1134 if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1138 freeReplyObject (rep);
1155 redisReply *rep = NULL;
1170 redisAppendCommand (ctx,
"LREM %s 1 %s", name, str);
1171 redisAppendCommand (ctx,
"RPUSH %s %s", name, str);
1172 redisGetReply (ctx, (
void **) &rep);
1173 if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1174 g_debug (
"Key '%s' already contained value '%s'", name, str);
1175 freeReplyObject (rep);
1176 redisGetReply (ctx, (
void **) &rep);
1180 redisAppendCommand (ctx,
"LREM %s 1 %b", name, str, len);
1181 redisAppendCommand (ctx,
"RPUSH %s %b", name, str, len);
1182 redisGetReply (ctx, (
void **) &rep);
1183 if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1184 g_debug (
"Key '%s' already contained string '%s'", name, str);
1185 freeReplyObject (rep);
1186 redisGetReply (ctx, (
void **) &rep);
1188 if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1192 freeReplyObject (rep);
1214 rep =
redis_cmd (kbr,
"RPUSH %s %s", name, str);
1216 rep =
redis_cmd (kbr,
"RPUSH %s %b", name, str, len);
1217 if (!rep || rep->type == REDIS_REPLY_ERROR)
1221 freeReplyObject (rep);
1237 redisReply *rep = NULL;
1245 redisAppendCommand (ctx,
"MULTI");
1246 redisAppendCommand (ctx,
"DEL %s", name);
1248 redisAppendCommand (ctx,
"RPUSH %s %s", name, val);
1250 redisAppendCommand (ctx,
"RPUSH %s %b", name, val, len);
1251 redisAppendCommand (ctx,
"EXEC");
1254 redisGetReply (ctx, (
void **) &rep);
1255 if (!rep || rep->type == REDIS_REPLY_ERROR)
1258 freeReplyObject (rep);
1283 redisAppendCommand (ctx,
"LREM %s 1 %d", name, val);
1284 redisAppendCommand (ctx,
"RPUSH %s %d", name, val);
1285 redisGetReply (ctx, (
void **) &rep);
1286 if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1287 g_debug (
"Key '%s' already contained integer '%d'", name, val);
1288 freeReplyObject (rep);
1289 redisGetReply (ctx, (
void **) &rep);
1290 if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1298 freeReplyObject (rep);
1317 if (!rep || rep->type == REDIS_REPLY_ERROR)
1320 freeReplyObject (rep);
1336 redisReply *rep = NULL;
1344 redisAppendCommand (ctx,
"MULTI");
1345 redisAppendCommand (ctx,
"DEL %s", name);
1346 redisAppendCommand (ctx,
"RPUSH %s %d", name, val);
1347 redisAppendCommand (ctx,
"EXEC");
1350 redisGetReply (ctx, (
void **) &rep);
1351 if (!rep || rep->type == REDIS_REPLY_ERROR)
1354 freeReplyObject (rep);
1371 redisReply *rep = NULL;
1375 if (!nvt || !filename)
1380 kbr,
"RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %d %s %s",
1387 if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1390 freeReplyObject (rep);
1392 element = nvt->
prefs;
1393 if (g_slist_length (element))
1401 if (!rep || rep->type == REDIS_REPLY_ERROR)
1404 freeReplyObject (rep);
1405 element = element->next;
1407 rep =
redis_cmd (kbr,
"RPUSH filename:%s %lu %s", filename, time (NULL),
1409 if (!rep || rep->type == REDIS_REPLY_ERROR)
1412 freeReplyObject (rep);
1429 if (kbr->
rctx != NULL)
1431 redisFree (kbr->
rctx);
1452 redisFree (kbr->
rctx);
1454 g_debug (
"%s: deleting all DBs at %s except %s", __func__, kbr->
path, except);
1459 kbr->
rctx = redisConnectUnix (kbr->
path);
1460 if (kbr->
rctx == NULL || kbr->
rctx->err)
1463 "%s: redis connection error: %s", __func__,
1464 kbr->
rctx ? kbr->
rctx->errstr : strerror (ENOMEM));
1465 redisFree (kbr->
rctx);
1472 if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
1474 freeReplyObject (rep);
1475 redisFree (kbr->
rctx);
1479 freeReplyObject (rep);
1480 rep = redisCommand (kbr->
rctx,
"SELECT %u", i);
1481 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1483 freeReplyObject (rep);
1484 redisFree (kbr->
rctx);
1489 freeReplyObject (rep);
1498 redisFree (kbr->
rctx);
1504 redisFree (kbr->
rctx);
1527 g_debug (
"%s: saving all elements from KB #%u", __func__, kbr->
db);
1529 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1539 freeReplyObject (rep);
1554 struct sigaction new_action, original_action;
1557 new_action.sa_flags = 0;
1558 if (sigemptyset (&new_action.sa_mask))
1560 new_action.sa_handler = SIG_IGN;
1561 if (sigaction (SIGPIPE, &new_action, &original_action))
1564 g_debug (
"%s: deleting all elements from KB #%u", __func__, kbr->
db);
1566 if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1575 if (sigaction (SIGPIPE, &original_action, NULL))
1578 freeReplyObject (rep);
static int redis_push_str(kb_t kb, const char *name, const char *value)
Push a new entry under a given key.
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
static int redis_add_str(kb_t kb, const char *name, const char *str, size_t len)
Insert (append) a new entry under a given name.
kb_nvt_pos
Possible positions of nvt values in cache list.
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
static int fetch_max_db_index_compat(struct kb_redis *kbr)
Set the number of databases have been configured into kbr struct. (For Redis 2.4.* compatibility).
static nvti_t * redis_get_nvt_all(kb_t kb, const char *oid)
Get a full NVT.
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
static int redis_test_connection(struct kb_redis *kbr)
Test redis connection.
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
static int redis_delete_all(struct kb_redis *)
Delete all the KB's content.
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
static char * redis_pop_str(kb_t kb, const char *name)
Pops a single KB string item.
gchar * nvti_cve(const nvti_t *n)
Get the CVE references.
gchar * name
Name of the preference.
The structure of a information record that corresponds to a NVT.
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
static struct kb_item * redis_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
KB interface. Functions provided by an implementation. All functions have to be provided,...
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
int nvti_set_timeout(nvti_t *n, const gint timeout)
Set the timeout of a NVT Info.
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
gint nvti_timeout(const nvti_t *n)
Get the timeout for this NVT.
int nvti_set_cve(nvti_t *n, const gchar *cve)
Set the CVE references of a NVT.
static int redis_add_nvt(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
static int redis_release_db(struct kb_redis *kbr)
Release DB.
gchar * nvti_tag(const nvti_t *n)
Get the tag.
static GSList * redis_get_oids(kb_t kb)
Get all NVT OIDs.
gchar * nvti_family(const nvti_t *n)
Get the family name.
static int redis_flush_all(kb_t, const char *)
Flush all the KB's content. Delete all namespaces.
static struct kb_item * redis2kbitem_single(const char *name, const redisReply *elt, int force_int)
Give a single KB item.
gchar * type
Preference type.
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
static int redis_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
static int select_database(struct kb_redis *kbr)
Select DB.
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Top-level KB. This is to be inherited by KB implementations.
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
const struct kb_operations * kb_ops
static int redis_lnk_reset(kb_t)
Reset connection to the KB. This is called after each fork() to make sure connections aren't shared b...
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
static kb_t redis_direct_conn(const char *kb_path, const int kb_index)
Connect to a Knowledge Base object with the given kb_index.
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
static int redis_add_str_unique(kb_t kb, const char *name, const char *str, size_t len)
Insert (append) a new unique entry under a given name.
Knowledge base management API - Redis backend.
static struct kb_item * redis2kbitem(const char *name, const redisReply *rep)
Fetch a KB item or list from a redis Reply.
static size_t redis_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
int nvti_set_bid(nvti_t *n, const gchar *bid)
Set the bid references of a NVT.
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
int nvti_set_required_udp_ports(nvti_t *n, const gchar *required_udp_ports)
Set the required udp ports of a NVT.
gchar * dflt
Default value of the preference.
static int try_database_index(struct kb_redis *kbr, int index)
Attempt to atomically acquire ownership of a database.
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
kb_item_type
Possible type of a kb_item.
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
static int redis_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
static char * redis_get_str(kb_t kb, const char *name)
Get a single KB string item.
static int redis_set_str(kb_t kb, const char *name, const char *val, size_t len)
Set (replace) a new entry under a given name.
static int redis_add_int_unique(kb_t kb, const char *name, int val)
Insert (append) a new unique entry under a given name.
int redis_save(kb_t kb)
Save all the elements from the KB.
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
static int redis_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
static const struct kb_operations KBRedisOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
The structure for a preference of a NVT.
struct kb * kb_t
type abstraction to hide KB internals.
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
static char * redis_get_nvt(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
#define MAX_DB_INDEX__24
Max number of configured DB.
static kb_t redis_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
static int redis_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
static int redis_get_int(kb_t kb, const char *name)
Get a single KB integer item.
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
static int redis_get_kb_index(kb_t kb)
Return the kb index.
gchar * nvti_bid(const nvti_t *n)
Get the bid references.
const struct kb_operations * KBDefaultOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
static int fetch_max_db_index(struct kb_redis *kbr)
Set the number of databases have been configured into kbr struct.
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
static struct kb_item * redis_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
int(* kb_new)(kb_t *, const char *)
static int redis_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
gchar * nvti_name(const nvti_t *n)
Get the name.
GSList * prefs
Collection of NVT preferences.