Greenbone Vulnerability Management Libraries  10.0.1
kb.c File Reference

Knowledge base management API - Redis backend. More...

#include "kb.h"
#include <errno.h>
#include <glib.h>
#include <hiredis/hiredis.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for kb.c:

Go to the source code of this file.

Data Structures

struct  kb_redis
 Subclass of struct kb, it contains the redis-specific fields, such as the redis context, current DB (namespace) id and the server socket path. More...
 

Macros

#define _GNU_SOURCE
 
#define G_LOG_DOMAIN   "lib kb"
 
#define GLOBAL_DBINDEX_NAME   "GVM.__GlobalDBIndex"
 Name of the namespace usage bitmap in redis. More...
 
#define redis_kb(__kb)   ((struct kb_redis *) (__kb))
 
#define MAX_DB_INDEX__24   1000
 Max number of configured DB. More...
 

Functions

static int redis_delete_all (struct kb_redis *kbr)
 Delete all the KB's content. More...
 
static int redis_lnk_reset (kb_t kb)
 Reset connection to the KB. This is called after each fork() to make sure connections aren't shared between concurrent processes. More...
 
static int redis_flush_all (kb_t kb, const char *except)
 Flush all the KB's content. Delete all namespaces. More...
 
static redisReply * redis_cmd (struct kb_redis *kbr, const char *fmt,...)
 Execute a redis command and get a redis reply. More...
 
static int try_database_index (struct kb_redis *kbr, int index)
 Attempt to atomically acquire ownership of a database. More...
 
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). More...
 
static int fetch_max_db_index (struct kb_redis *kbr)
 Set the number of databases have been configured into kbr struct. More...
 
static int select_database (struct kb_redis *kbr)
 Select DB. More...
 
static int redis_release_db (struct kb_redis *kbr)
 Release DB. More...
 
static int get_redis_ctx (struct kb_redis *kbr)
 Get redis context if it is already connected or do a a connection. More...
 
static int redis_test_connection (struct kb_redis *kbr)
 Test redis connection. More...
 
static int redis_delete (kb_t kb)
 Delete all entries and release ownership on the namespace. More...
 
static int redis_get_kb_index (kb_t kb)
 Return the kb index. More...
 
static int redis_new (kb_t *kb, const char *kb_path)
 Initialize a new Knowledge Base object. More...
 
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. More...
 
static kb_t redis_find (const char *kb_path, const char *key)
 Find an existing Knowledge Base object with key. More...
 
void kb_item_free (struct kb_item *item)
 Release a KB item (or a list). More...
 
static struct kb_item * redis2kbitem_single (const char *name, const redisReply *elt, int force_int)
 Give a single KB item. More...
 
static struct kb_item * redis2kbitem (const char *name, const redisReply *rep)
 Fetch a KB item or list from a redis Reply. More...
 
static struct kb_item * redis_get_single (kb_t kb, const char *name, enum kb_item_type type)
 Get a single KB element. More...
 
static char * redis_get_str (kb_t kb, const char *name)
 Get a single KB string item. More...
 
static int redis_push_str (kb_t kb, const char *name, const char *value)
 Push a new entry under a given key. More...
 
static char * redis_pop_str (kb_t kb, const char *name)
 Pops a single KB string item. More...
 
static int redis_get_int (kb_t kb, const char *name)
 Get a single KB integer item. More...
 
static char * redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position)
 Get field of a NVT. More...
 
static nvti_t * redis_get_nvt_all (kb_t kb, const char *oid)
 Get a full NVT. More...
 
static struct kb_item * redis_get_all (kb_t kb, const char *name)
 Get all items stored under a given name. More...
 
static struct kb_item * redis_get_pattern (kb_t kb, const char *pattern)
 Get all items stored under a given pattern. More...
 
static GSList * redis_get_oids (kb_t kb)
 Get all NVT OIDs. More...
 
static size_t redis_count (kb_t kb, const char *pattern)
 Count all items stored under a given pattern. More...
 
static int redis_del_items (kb_t kb, const char *name)
 Delete all entries under a given name. More...
 
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. More...
 
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. More...
 
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. More...
 
static int redis_add_int_unique (kb_t kb, const char *name, int val)
 Insert (append) a new unique entry under a given name. More...
 
static int redis_add_int (kb_t kb, const char *name, int val)
 Insert (append) a new entry under a given name. More...
 
static int redis_set_int (kb_t kb, const char *name, int val)
 Set (replace) a new entry under a given name. More...
 
static int redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename)
 Insert a new nvt. More...
 
int redis_save (kb_t kb)
 Save all the elements from the KB. More...
 

Variables

static const struct kb_operations KBRedisOperations
 Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based). More...
 
const struct kb_operations * KBDefaultOperations = &KBRedisOperations
 Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based). More...
 

Detailed Description

Knowledge base management API - Redis backend.

Contains specialized structures and functions to use redis as a KB server.

Definition in file kb.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 25 of file kb.c.

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib kb"

Definition at line 38 of file kb.c.

◆ GLOBAL_DBINDEX_NAME

#define GLOBAL_DBINDEX_NAME   "GVM.__GlobalDBIndex"

Name of the namespace usage bitmap in redis.

Definition at line 50 of file kb.c.

◆ MAX_DB_INDEX__24

#define MAX_DB_INDEX__24   1000

Max number of configured DB.

Definition at line 113 of file kb.c.

◆ redis_kb

#define redis_kb (   __kb)    ((struct kb_redis *) (__kb))

Definition at line 67 of file kb.c.

Function Documentation

◆ fetch_max_db_index()

static int fetch_max_db_index ( struct kb_redis *  kbr)
static

Set the number of databases have been configured into kbr struct.

Parameters
[in]kbrSubclass of struct kb where to save the max db index founded.
Returns
0 on success, -1 on error.

Definition at line 189 of file kb.c.

190 {
191  int rc = 0;
192  redisContext *ctx = kbr->rctx;
193  redisReply *rep = NULL;
194 
195  rep = redisCommand (ctx, "CONFIG GET databases");
196  if (rep == NULL)
197  {
198  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
199  "%s: redis command failed with '%s'", __func__, ctx->errstr);
200  rc = -1;
201  goto err_cleanup;
202  }
203 
204  if (rep->type != REDIS_REPLY_ARRAY)
205  {
206  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
207  "%s: cannot retrieve max DB number: %s", __func__, rep->str);
208  rc = -1;
209  goto err_cleanup;
210  }
211 
212  if (rep->elements == 0)
213  {
214  /* Redis 2.4 compatibility mode. Suboptimal... */
215  rc = fetch_max_db_index_compat (kbr);
216  }
217  else if (rep->elements == 2)
218  {
219  kbr->max_db = (unsigned) atoi (rep->element[1]->str);
220  }
221  else
222  {
223  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
224  "%s: unexpected reply length (%zd)", __func__, rep->elements);
225  rc = -1;
226  goto err_cleanup;
227  }
228 
229  g_debug ("%s: maximum DB number: %u", __func__, kbr->max_db);
230 
231 err_cleanup:
232  if (rep != NULL)
233  freeReplyObject (rep);
234 
235  return rc;
236 }
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).
Definition: kb.c:122
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int max_db
Definition: kb.c:62
redisContext * rctx
Definition: kb.c:64

References fetch_max_db_index_compat(), G_LOG_DOMAIN, kb_redis::max_db, and kb_redis::rctx.

Referenced by redis_find(), and select_database().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fetch_max_db_index_compat()

static int fetch_max_db_index_compat ( struct kb_redis *  kbr)
static

Set the number of databases have been configured into kbr struct. (For Redis 2.4.* compatibility).

Parameters
[in]kbrSubclass of struct kb where to save the max db index founded.
Returns
0 on success, -1 on error.

Definition at line 122 of file kb.c.

123 {
124  redisContext *ctx = kbr->rctx;
125  redisReply *rep;
126  int min, max;
127  int rc = 0;
128 
129  min = 1;
130  max = MAX_DB_INDEX__24;
131 
132  while (min < max)
133  {
134  int current;
135 
136  current = min + ((max - min) / 2);
137 
138  rep = redisCommand (ctx, "SELECT %d", current);
139  if (rep == NULL)
140  {
141  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
142  "%s: redis command failed with '%s'", __func__, ctx->errstr);
143  return -1;
144  }
145 
146  switch (rep->type)
147  {
148  case REDIS_REPLY_ERROR:
149  max = current;
150  break;
151 
152  case REDIS_REPLY_STATUS:
153  min = current + 1;
154  break;
155 
156  default:
157  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
158  "%s: unexpected reply of type %d", __func__, rep->type);
159  freeReplyObject (rep);
160  return -1;
161  }
162  freeReplyObject (rep);
163  }
164 
165  kbr->max_db = min;
166 
167  /* Go back to DB #0 */
168  rep = redisCommand (ctx, "SELECT 0");
169  if (rep == NULL)
170  {
171  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
172  "%s: DB selection failed with '%s'", __func__, ctx->errstr);
173  rc = -1;
174  }
175 
176  if (rep)
177  freeReplyObject (rep);
178 
179  return rc;
180 }
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int max_db
Definition: kb.c:62
redisContext * rctx
Definition: kb.c:64
#define MAX_DB_INDEX__24
Max number of configured DB.
Definition: kb.c:113

References G_LOG_DOMAIN, kb_redis::max_db, MAX_DB_INDEX__24, and kb_redis::rctx.

Referenced by fetch_max_db_index().

Here is the caller graph for this function:

◆ get_redis_ctx()

static int get_redis_ctx ( struct kb_redis *  kbr)
static

Get redis context if it is already connected or do a a connection.

Parameters
[in]kbrSubclass of struct kb where to fetch the context. or where it is saved in case of a new connection.
Returns
0 on success, -1 on connection error, -2 on unavailable DB slot.

Definition at line 339 of file kb.c.

340 {
341  int rc;
342 
343  if (kbr->rctx != NULL)
344  return 0;
345 
346  kbr->rctx = redisConnectUnix (kbr->path);
347  if (kbr->rctx == NULL || kbr->rctx->err)
348  {
349  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
350  "%s: redis connection error: %s", __func__,
351  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
352  redisFree (kbr->rctx);
353  kbr->rctx = NULL;
354  return -1;
355  }
356 
357  rc = select_database (kbr);
358  if (rc)
359  {
360  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No redis DB available");
361  redisFree (kbr->rctx);
362  kbr->rctx = NULL;
363  return -2;
364  }
365 
366  g_debug ("%s: connected to redis://%s/%d", __func__, kbr->path, kbr->db);
367  return 0;
368 }
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
char path[0]
Definition: kb.c:65
static int select_database(struct kb_redis *kbr)
Select DB.
Definition: kb.c:247

References kb_redis::db, G_LOG_DOMAIN, kb_redis::path, kb_redis::rctx, and select_database().

Referenced by redis_add_int_unique(), redis_add_str_unique(), redis_cmd(), redis_get_pattern(), redis_new(), redis_set_int(), and redis_set_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_item_free()

void kb_item_free ( struct kb_item *  item)

Release a KB item (or a list).

Parameters
[in]itemItem or list to be release

Definition at line 599 of file kb.c.

600 {
601  while (item != NULL)
602  {
603  struct kb_item *next;
604 
605  next = item->next;
606  if (item->type == KB_TYPE_STR && item->v_str != NULL)
607  g_free (item->v_str);
608  g_free (item);
609  item = next;
610  }
611 }
Definition: kb.h:46
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
struct kb_item * next
Definition: kb.h:90
enum kb_item_type type
Definition: kb.h:81
char * v_str
Definition: kb.h:85

References KB_TYPE_STR, kb_item::next, kb_item::type, and kb_item::v_str.

Referenced by nvticache_get_prefs(), redis_get_int(), and redis_get_str().

Here is the caller graph for this function:

◆ redis2kbitem()

static struct kb_item* redis2kbitem ( const char *  name,
const redisReply *  rep 
)
static

Fetch a KB item or list from a redis Reply.

Parameters
[in]nameName of the item.
[in]repA redisReply element where to fetch the item.
Returns
kb_item or list on success, NULL otherwise.

Definition at line 663 of file kb.c.

664 {
665  struct kb_item *kbi;
666 
667  kbi = NULL;
668 
669  switch (rep->type)
670  {
671  unsigned int i;
672 
673  case REDIS_REPLY_STRING:
674  case REDIS_REPLY_INTEGER:
675  kbi = redis2kbitem_single (name, rep, 0);
676  break;
677 
678  case REDIS_REPLY_ARRAY:
679  for (i = 0; i < rep->elements; i++)
680  {
681  struct kb_item *tmpitem;
682 
683  tmpitem = redis2kbitem_single (name, rep->element[i], 0);
684  if (tmpitem == NULL)
685  break;
686 
687  if (kbi != NULL)
688  {
689  tmpitem->next = kbi;
690  kbi = tmpitem;
691  }
692  else
693  kbi = tmpitem;
694  }
695  break;
696 
697  case REDIS_REPLY_NIL:
698  case REDIS_REPLY_STATUS:
699  case REDIS_REPLY_ERROR:
700  default:
701  break;
702  }
703 
704  return kbi;
705 }
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
struct kb_item * next
Definition: kb.h:90
static struct kb_item * redis2kbitem_single(const char *name, const redisReply *elt, int force_int)
Give a single KB item.
Definition: kb.c:621
char name[0]
Definition: kb.h:93

References kb_item::name, kb_item::next, and redis2kbitem_single().

Referenced by redis_get_all(), and redis_get_pattern().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redis2kbitem_single()

static struct kb_item* redis2kbitem_single ( const char *  name,
const redisReply *  elt,
int  force_int 
)
static

Give a single KB item.

Parameters
[in]nameName of the item.
[in]eltA redisReply element where to fetch the item.
[in]force_intTo force string to integer conversion.
Returns
Single retrieve kb_item on success, NULL otherwise.

Definition at line 621 of file kb.c.

622 {
623  struct kb_item *item;
624  size_t namelen;
625 
626  if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER)
627  return NULL;
628 
629  namelen = strlen (name) + 1;
630 
631  item = g_malloc0 (sizeof (struct kb_item) + namelen);
632  if (elt->type == REDIS_REPLY_INTEGER)
633  {
634  item->type = KB_TYPE_INT;
635  item->v_int = elt->integer;
636  }
637  else if (force_int)
638  {
639  item->type = KB_TYPE_INT;
640  item->v_int = atoi (elt->str);
641  }
642  else
643  {
644  item->type = KB_TYPE_STR;
645  item->v_str = g_memdup (elt->str, elt->len + 1);
646  item->len = elt->len;
647  }
648 
649  item->next = NULL;
650  item->namelen = namelen;
651  strncpy (item->name, name, namelen);
652 
653  return item;
654 }
Definition: kb.h:46
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
struct kb_item * next
Definition: kb.h:90
enum kb_item_type type
Definition: kb.h:81
Definition: kb.h:45
size_t len
Definition: kb.h:89
int v_int
Definition: kb.h:86
size_t namelen
Definition: kb.h:92
char name[0]
Definition: kb.h:93
char * v_str
Definition: kb.h:85

References KB_TYPE_INT, KB_TYPE_STR, kb_item::len, kb_item::name, kb_item::namelen, kb_item::next, kb_item::type, kb_item::v_int, and kb_item::v_str.

Referenced by redis2kbitem(), and redis_get_single().

Here is the caller graph for this function:

◆ redis_add_int()

static int redis_add_int ( kb_t  kb,
const char *  name,
int  val 
)
static

Insert (append) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

Definition at line 1311 of file kb.c.

1312 {
1313  redisReply *rep;
1314  int rc = 0;
1315 
1316  rep = redis_cmd (redis_kb (kb), "RPUSH %s %d", name, val);
1317  if (!rep || rep->type == REDIS_REPLY_ERROR)
1318  rc = -1;
1319  if (rep)
1320  freeReplyObject (rep);
1321 
1322  return rc;
1323 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
char name[0]
Definition: kb.h:93

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_add_int_unique()

static int redis_add_int_unique ( kb_t  kb,
const char *  name,
int  val 
)
static

Insert (append) a new unique entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

Definition at line 1272 of file kb.c.

1273 {
1274  struct kb_redis *kbr;
1275  redisReply *rep;
1276  int rc = 0;
1277  redisContext *ctx;
1278 
1279  kbr = redis_kb (kb);
1280  if (get_redis_ctx (kbr) < 0)
1281  return -1;
1282  ctx = kbr->rctx;
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)
1291  {
1292  rc = -1;
1293  goto out;
1294  }
1295 
1296 out:
1297  if (rep != NULL)
1298  freeReplyObject (rep);
1299 
1300  return rc;
1301 }
#define redis_kb(__kb)
Definition: kb.c:67
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References get_redis_ctx(), kb_redis::rctx, and redis_kb.

Here is the call graph for this function:

◆ redis_add_nvt()

static int redis_add_nvt ( kb_t  kb,
const nvti_t *  nvt,
const char *  filename 
)
static

Insert a new nvt.

Parameters
[in]kbKB handle where to store the nvt.
[in]nvtnvt to store.
[in]filenamePath to nvt to store.
Returns
0 on success, non-null on error.

Definition at line 1368 of file kb.c.

1369 {
1370  struct kb_redis *kbr;
1371  redisReply *rep = NULL;
1372  int rc = 0;
1373  GSList *element;
1374 
1375  if (!nvt || !filename)
1376  return -1;
1377 
1378  kbr = redis_kb (kb);
1379  rep = redis_cmd (
1380  kbr, "RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %d %s %s",
1381  nvti_oid (nvt), filename, nvti_required_keys (nvt) ?: "",
1382  nvti_mandatory_keys (nvt) ?: "", nvti_excluded_keys (nvt) ?: "",
1383  nvti_required_udp_ports (nvt) ?: "", nvti_required_ports (nvt) ?: "",
1384  nvti_dependencies (nvt) ?: "", nvti_tag (nvt) ?: "", nvti_cve (nvt) ?: "",
1385  nvti_bid (nvt) ?: "", nvti_xref (nvt) ?: "", nvti_category (nvt),
1386  nvti_timeout (nvt), nvti_family (nvt), nvti_name (nvt));
1387  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1388  rc = -1;
1389  if (rep != NULL)
1390  freeReplyObject (rep);
1391 
1392  element = nvt->prefs;
1393  if (g_slist_length (element))
1394  rep = redis_cmd (kbr, "DEL oid:%s:prefs", nvti_oid (nvt));
1395  while (element)
1396  {
1397  nvtpref_t *pref = element->data;
1398 
1399  rep = redis_cmd (kbr, "RPUSH oid:%s:prefs %s|||%s|||%s", nvti_oid (nvt),
1400  pref->name, pref->type, pref->dflt);
1401  if (!rep || rep->type == REDIS_REPLY_ERROR)
1402  rc = -1;
1403  if (rep)
1404  freeReplyObject (rep);
1405  element = element->next;
1406  }
1407  rep = redis_cmd (kbr, "RPUSH filename:%s %lu %s", filename, time (NULL),
1408  nvti_oid (nvt));
1409  if (!rep || rep->type == REDIS_REPLY_ERROR)
1410  rc = -1;
1411  if (rep)
1412  freeReplyObject (rep);
1413  return rc;
1414 }
#define redis_kb(__kb)
Definition: kb.c:67
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
Definition: nvti.c:275
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition: nvti.c:373
gchar * nvti_cve(const nvti_t *n)
Get the CVE references.
Definition: nvti.c:247
gchar * name
Name of the preference.
Definition: nvti.h:41
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition: nvti.c:456
gint nvti_timeout(const nvti_t *n)
Get the timeout for this NVT.
Definition: nvti.c:443
gchar * nvti_tag(const nvti_t *n)
Get the tag.
Definition: nvti.c:289
gchar * nvti_family(const nvti_t *n)
Get the family name.
Definition: nvti.c:401
gchar * type
Preference type.
Definition: nvti.h:40
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition: nvti.c:317
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition: nvti.c:359
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition: nvti.c:345
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition: nvti.c:387
gchar * dflt
Default value of the preference.
Definition: nvti.h:42
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
The structure for a preference of a NVT.
Definition: nvti.h:38
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition: nvti.c:331
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:219
gchar * nvti_bid(const nvti_t *n)
Get the bid references.
Definition: nvti.c:261
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition: nvti.c:233
GSList * prefs
Collection of NVT preferences.
Definition: nvti.h:83

References nvtpref::dflt, nvtpref::name, nvti_bid(), nvti_category(), nvti_cve(), nvti_dependencies(), nvti_excluded_keys(), nvti_family(), nvti_mandatory_keys(), nvti_name(), nvti_oid(), nvti_required_keys(), nvti_required_ports(), nvti_required_udp_ports(), nvti_tag(), nvti_timeout(), nvti_xref(), nvti::prefs, redis_cmd(), redis_kb, and nvtpref::type.

Here is the call graph for this function:

◆ redis_add_str()

static int redis_add_str ( kb_t  kb,
const char *  name,
const char *  str,
size_t  len 
)
static

Insert (append) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]strItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

Definition at line 1206 of file kb.c.

1207 {
1208  struct kb_redis *kbr;
1209  redisReply *rep;
1210  int rc = 0;
1211 
1212  kbr = redis_kb (kb);
1213  if (len == 0)
1214  rep = redis_cmd (kbr, "RPUSH %s %s", name, str);
1215  else
1216  rep = redis_cmd (kbr, "RPUSH %s %b", name, str, len);
1217  if (!rep || rep->type == REDIS_REPLY_ERROR)
1218  rc = -1;
1219 
1220  if (rep)
1221  freeReplyObject (rep);
1222  return rc;
1223 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_add_str_unique()

static int redis_add_str_unique ( kb_t  kb,
const char *  name,
const char *  str,
size_t  len 
)
static

Insert (append) a new unique entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]strItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

Definition at line 1152 of file kb.c.

1153 {
1154  struct kb_redis *kbr;
1155  redisReply *rep = NULL;
1156  int rc = 0;
1157  redisContext *ctx;
1158 
1159  kbr = redis_kb (kb);
1160  if (get_redis_ctx (kbr) < 0)
1161  return -1;
1162  ctx = kbr->rctx;
1163 
1164  /* Some VTs still rely on values being unique (ie. a value inserted multiple
1165  * times, will only be present once.)
1166  * Once these are fixed, the LREM becomes redundant and should be removed.
1167  */
1168  if (len == 0)
1169  {
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);
1177  }
1178  else
1179  {
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);
1187  }
1188  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1189  rc = -1;
1190 
1191  if (rep != NULL)
1192  freeReplyObject (rep);
1193 
1194  return rc;
1195 }
#define redis_kb(__kb)
Definition: kb.c:67
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References get_redis_ctx(), kb_redis::rctx, and redis_kb.

Here is the call graph for this function:

◆ redis_cmd()

static redisReply * redis_cmd ( struct kb_redis *  kbr,
const char *  fmt,
  ... 
)
static

Execute a redis command and get a redis reply.

Parameters
[in]kbrSubclass of struct kb to connect to.
[in]fmtFormatted variable argument list with the cmd to be executed.
Returns
Redis reply on success, NULL otherwise.

Definition at line 714 of file kb.c.

715 {
716  redisReply *rep;
717  va_list ap, aq;
718  int retry = 0;
719 
720  va_start (ap, fmt);
721  do
722  {
723  if (get_redis_ctx (kbr) < 0)
724  {
725  va_end (ap);
726  return NULL;
727  }
728 
729  va_copy (aq, ap);
730  rep = redisvCommand (kbr->rctx, fmt, aq);
731  va_end (aq);
732 
733  if (kbr->rctx->err)
734  {
735  if (rep != NULL)
736  freeReplyObject (rep);
737 
738  redis_lnk_reset ((kb_t) kbr);
739  retry = !retry;
740  }
741  else
742  retry = 0;
743  }
744  while (retry);
745 
746  va_end (ap);
747 
748  return rep;
749 }
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
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...
Definition: kb.c:1423
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339

References get_redis_ctx(), kb_redis::rctx, and redis_lnk_reset().

Referenced by redis_add_int(), redis_add_nvt(), redis_add_str(), redis_count(), redis_del_items(), redis_delete_all(), redis_get_all(), redis_get_nvt(), redis_get_nvt_all(), redis_get_oids(), redis_get_pattern(), redis_get_single(), redis_pop_str(), redis_push_str(), redis_save(), and redis_test_connection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redis_count()

static size_t redis_count ( kb_t  kb,
const char *  pattern 
)
static

Count all items stored under a given pattern.

Parameters
[in]kbKB handle where to count the items.
[in]pattern'*' pattern of the elements to count.
Returns
Count of items.

Definition at line 1095 of file kb.c.

1096 {
1097  struct kb_redis *kbr;
1098  redisReply *rep;
1099  size_t count;
1100 
1101  kbr = redis_kb (kb);
1102 
1103  rep = redis_cmd (kbr, "KEYS %s", pattern);
1104  if (rep == NULL)
1105  return 0;
1106 
1107  if (rep->type != REDIS_REPLY_ARRAY)
1108  {
1109  freeReplyObject (rep);
1110  return 0;
1111  }
1112 
1113  count = rep->elements;
1114  freeReplyObject (rep);
1115  return count;
1116 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_del_items()

static int redis_del_items ( kb_t  kb,
const char *  name 
)
static

Delete all entries under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
Returns
0 on success, non-null on error.

Definition at line 1125 of file kb.c.

1126 {
1127  struct kb_redis *kbr;
1128  redisReply *rep;
1129  int rc = 0;
1130 
1131  kbr = redis_kb (kb);
1132 
1133  rep = redis_cmd (kbr, "DEL %s", name);
1134  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1135  rc = -1;
1136 
1137  if (rep != NULL)
1138  freeReplyObject (rep);
1139 
1140  return rc;
1141 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_delete()

static int redis_delete ( kb_t  kb)
static

Delete all entries and release ownership on the namespace.

Parameters
[in]kbKB handle to release.
Returns
0 on success, non-null on error.

Definition at line 415 of file kb.c.

416 {
417  struct kb_redis *kbr;
418 
419  kbr = redis_kb (kb);
420 
421  redis_delete_all (kbr);
422  redis_release_db (kbr);
423 
424  if (kbr->rctx != NULL)
425  {
426  redisFree (kbr->rctx);
427  kbr->rctx = NULL;
428  }
429 
430  g_free (kb);
431  return 0;
432 }
#define redis_kb(__kb)
Definition: kb.c:67
static int redis_delete_all(struct kb_redis *)
Delete all the KB's content.
Definition: kb.c:1550
redisContext * rctx
Definition: kb.c:64
static int redis_release_db(struct kb_redis *kbr)
Release DB.
Definition: kb.c:298
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References kb_redis::rctx, redis_delete_all(), redis_kb, and redis_release_db().

Referenced by redis_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redis_delete_all()

int redis_delete_all ( struct kb_redis *  kbr)
static

Delete all the KB's content.

Parameters
[in]kbrSubclass of struct kb.
Returns
0 on success, non-null on error.

Definition at line 1550 of file kb.c.

1551 {
1552  int rc;
1553  redisReply *rep;
1554  struct sigaction new_action, original_action;
1555 
1556  /* Ignore SIGPIPE, in case of a lost connection. */
1557  new_action.sa_flags = 0;
1558  if (sigemptyset (&new_action.sa_mask))
1559  return -1;
1560  new_action.sa_handler = SIG_IGN;
1561  if (sigaction (SIGPIPE, &new_action, &original_action))
1562  return -1;
1563 
1564  g_debug ("%s: deleting all elements from KB #%u", __func__, kbr->db);
1565  rep = redis_cmd (kbr, "FLUSHDB");
1566  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1567  {
1568  rc = -1;
1569  goto err_cleanup;
1570  }
1571 
1572  rc = 0;
1573 
1574 err_cleanup:
1575  if (sigaction (SIGPIPE, &original_action, NULL))
1576  return -1;
1577  if (rep != NULL)
1578  freeReplyObject (rep);
1579 
1580  return rc;
1581 }
unsigned int db
Definition: kb.c:63
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714

References kb_redis::db, and redis_cmd().

Referenced by redis_delete(), and redis_flush_all().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redis_direct_conn()

static kb_t redis_direct_conn ( const char *  kb_path,
const int  kb_index 
)
static

Connect to a Knowledge Base object with the given kb_index.

Parameters
[in]kb_pathPath to KB.
[in]kb_indexDB index
Returns
Knowledge Base object, NULL otherwise.

Definition at line 487 of file kb.c.

488 {
489  struct kb_redis *kbr;
490  redisReply *rep;
491 
492  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
493  kbr->kb.kb_ops = &KBRedisOperations;
494  strncpy (kbr->path, kb_path, strlen (kb_path));
495 
496  kbr->rctx = redisConnectUnix (kbr->path);
497  if (kbr->rctx == NULL || kbr->rctx->err)
498  {
499  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
500  "%s: redis connection error: %s", __func__,
501  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
502  redisFree (kbr->rctx);
503  g_free (kbr);
504  return NULL;
505  }
506  kbr->db = kb_index;
507  rep = redisCommand (kbr->rctx, "SELECT %d", kb_index);
508  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
509  {
510  if (rep != NULL)
511  freeReplyObject (rep);
512  redisFree (kbr->rctx);
513  kbr->rctx = NULL;
514  return NULL;
515  }
516  freeReplyObject (rep);
517  return (kb_t) kbr;
518 }
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
char path[0]
Definition: kb.c:65
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
const struct kb_operations * kb_ops
Definition: kb.h:103
struct kb kb
Definition: kb.c:61
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
static const struct kb_operations KBRedisOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:52

References kb_redis::db, G_LOG_DOMAIN, kb_redis::kb, kb::kb_ops, KBRedisOperations, kb_redis::path, and kb_redis::rctx.

◆ redis_find()

static kb_t redis_find ( const char *  kb_path,
const char *  key 
)
static

Find an existing Knowledge Base object with key.

Parameters
[in]kb_pathPath to KB.
[in]keyMarker key to search for in KB objects.
Returns
Knowledge Base object, NULL otherwise.

Definition at line 527 of file kb.c.

528 {
529  struct kb_redis *kbr;
530  unsigned int i = 1;
531 
532  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
533  kbr->kb.kb_ops = &KBRedisOperations;
534  strncpy (kbr->path, kb_path, strlen (kb_path));
535 
536  do
537  {
538  redisReply *rep;
539 
540  kbr->rctx = redisConnectUnix (kbr->path);
541  if (kbr->rctx == NULL || kbr->rctx->err)
542  {
543  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
544  "%s: redis connection error: %s", __func__,
545  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
546  redisFree (kbr->rctx);
547  g_free (kbr);
548  return NULL;
549  }
550 
551  if (kbr->max_db == 0)
552  fetch_max_db_index (kbr);
553 
554  kbr->db = i;
555  rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
556  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
557  {
558  if (rep != NULL)
559  freeReplyObject (rep);
560  i++;
561  redisFree (kbr->rctx);
562  kbr->rctx = NULL;
563  continue;
564  }
565  freeReplyObject (rep);
566  rep = redisCommand (kbr->rctx, "SELECT %u", i);
567  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
568  {
569  redisFree (kbr->rctx);
570  kbr->rctx = NULL;
571  }
572  else
573  {
574  freeReplyObject (rep);
575  if (key)
576  {
577  char *tmp = kb_item_get_str (&kbr->kb, key);
578  if (tmp)
579  {
580  g_free (tmp);
581  return (kb_t) kbr;
582  }
583  }
584  redisFree (kbr->rctx);
585  }
586  i++;
587  }
588  while (i < kbr->max_db);
589 
590  g_free (kbr);
591  return NULL;
592 }
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int max_db
Definition: kb.c:62
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
char path[0]
Definition: kb.c:65
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
Definition: kb.c:50
const struct kb_operations * kb_ops
Definition: kb.h:103
struct kb kb
Definition: kb.c:61
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.h:323
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
static const struct kb_operations KBRedisOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:52
static int fetch_max_db_index(struct kb_redis *kbr)
Set the number of databases have been configured into kbr struct.
Definition: kb.c:189

References kb_redis::db, fetch_max_db_index(), G_LOG_DOMAIN, GLOBAL_DBINDEX_NAME, kb_redis::kb, kb_item_get_str(), kb::kb_ops, KBRedisOperations, kb_redis::max_db, kb_redis::path, and kb_redis::rctx.

Here is the call graph for this function:

◆ redis_flush_all()

static int redis_flush_all ( kb_t  kb,
const char *  except 
)
static

Flush all the KB's content. Delete all namespaces.

Parameters
[in]kbKB handle.
[in]exceptDon't flush DB with except key.
Returns
0 on success, non-null on error.

Definition at line 1445 of file kb.c.

1446 {
1447  unsigned int i = 1;
1448  struct kb_redis *kbr;
1449 
1450  kbr = redis_kb (kb);
1451  if (kbr->rctx)
1452  redisFree (kbr->rctx);
1453 
1454  g_debug ("%s: deleting all DBs at %s except %s", __func__, kbr->path, except);
1455  do
1456  {
1457  redisReply *rep;
1458 
1459  kbr->rctx = redisConnectUnix (kbr->path);
1460  if (kbr->rctx == NULL || kbr->rctx->err)
1461  {
1462  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1463  "%s: redis connection error: %s", __func__,
1464  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
1465  redisFree (kbr->rctx);
1466  kbr->rctx = NULL;
1467  return -1;
1468  }
1469 
1470  kbr->db = i;
1471  rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
1472  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
1473  {
1474  freeReplyObject (rep);
1475  redisFree (kbr->rctx);
1476  i++;
1477  continue;
1478  }
1479  freeReplyObject (rep);
1480  rep = redisCommand (kbr->rctx, "SELECT %u", i);
1481  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1482  {
1483  freeReplyObject (rep);
1484  redisFree (kbr->rctx);
1485  kbr->rctx = NULL;
1486  }
1487  else
1488  {
1489  freeReplyObject (rep);
1490  /* Don't remove DB if it has "except" key. */
1491  if (except)
1492  {
1493  char *tmp = kb_item_get_str (kb, except);
1494  if (tmp)
1495  {
1496  g_free (tmp);
1497  i++;
1498  redisFree (kbr->rctx);
1499  continue;
1500  }
1501  }
1502  redis_delete_all (kbr);
1503  redis_release_db (kbr);
1504  redisFree (kbr->rctx);
1505  }
1506  i++;
1507  }
1508  while (i < kbr->max_db);
1509 
1510  g_free (kb);
1511  return 0;
1512 }
#define redis_kb(__kb)
Definition: kb.c:67
static int redis_delete_all(struct kb_redis *)
Delete all the KB's content.
Definition: kb.c:1550
#define G_LOG_DOMAIN
Definition: kb.c:38
unsigned int max_db
Definition: kb.c:62
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
static int redis_release_db(struct kb_redis *kbr)
Release DB.
Definition: kb.c:298
char path[0]
Definition: kb.c:65
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
Definition: kb.c:50
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.h:323
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References kb_redis::db, G_LOG_DOMAIN, GLOBAL_DBINDEX_NAME, kb_item_get_str(), kb_redis::max_db, kb_redis::path, kb_redis::rctx, redis_delete_all(), redis_kb, and redis_release_db().

Here is the call graph for this function:

◆ redis_get_all()

static struct kb_item* redis_get_all ( kb_t  kb,
const char *  name 
)
static

Get all items stored under a given name.

Parameters
[in]kbKB handle where to fetch the items.
[in]nameName of the elements to retrieve.
Returns
Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

Definition at line 971 of file kb.c.

972 {
973  struct kb_redis *kbr;
974  struct kb_item *kbi;
975  redisReply *rep;
976 
977  kbr = redis_kb (kb);
978 
979  rep = redis_cmd (kbr, "LRANGE %s 0 -1", name);
980  if (rep == NULL)
981  return NULL;
982 
983  kbi = redis2kbitem (name, rep);
984 
985  freeReplyObject (rep);
986 
987  return kbi;
988 }
#define redis_kb(__kb)
Definition: kb.c:67
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static struct kb_item * redis2kbitem(const char *name, const redisReply *rep)
Fetch a KB item or list from a redis Reply.
Definition: kb.c:663
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
char name[0]
Definition: kb.h:93

References kb_item::name, redis2kbitem(), redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_get_int()

static int redis_get_int ( kb_t  kb,
const char *  name 
)
static

Get a single KB integer item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

Definition at line 868 of file kb.c.

869 {
870  struct kb_item *kbi;
871 
873  if (kbi != NULL)
874  {
875  int res;
876 
877  res = kbi->v_int;
878  kb_item_free (kbi);
879  return res;
880  }
881  return -1;
882 }
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.c:760
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
Definition: kb.c:599
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
Definition: kb.h:45
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
int v_int
Definition: kb.h:86
char name[0]
Definition: kb.h:93

References kb_item_free(), KB_TYPE_INT, kb_item::name, redis_get_single(), and kb_item::v_int.

Here is the call graph for this function:

◆ redis_get_kb_index()

static int redis_get_kb_index ( kb_t  kb)
static

Return the kb index.

Parameters
[in]kbKB handle.
Returns
kb_index on success, null on error.

Definition at line 440 of file kb.c.

441 {
442  int i;
443  i = ((struct kb_redis *) kb)->db;
444  if (i > 0)
445  return i;
446  return -1;
447 }
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

◆ redis_get_nvt()

static char* redis_get_nvt ( kb_t  kb,
const char *  oid,
enum kb_nvt_pos  position 
)
static

Get field of a NVT.

Parameters
[in]kbKB handle where to store the nvt.
[in]oidOID of NVT to get from.
[in]positionPosition of field to get.
Returns
Value of field, NULL otherwise.

Definition at line 892 of file kb.c.

893 {
894  struct kb_redis *kbr;
895  redisReply *rep;
896  char *res = NULL;
897 
898  kbr = redis_kb (kb);
899  if (position >= NVT_TIMESTAMP_POS)
900  rep = redis_cmd (kbr, "LINDEX filename:%s %d", oid,
901  position - NVT_TIMESTAMP_POS);
902  else
903  rep = redis_cmd (kbr, "LINDEX nvt:%s %d", oid, position);
904  if (!rep)
905  return NULL;
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);
911 
912  return res;
913 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References NVT_TIMESTAMP_POS, redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_get_nvt_all()

static nvti_t* redis_get_nvt_all ( kb_t  kb,
const char *  oid 
)
static

Get a full NVT.

Parameters
[in]kbKB handle where to store the nvt.
[in]oidOID of NVT to get.
Returns
nvti_t of NVT, NULL otherwise.

Definition at line 922 of file kb.c.

923 {
924  struct kb_redis *kbr;
925  redisReply *rep;
926 
927  kbr = redis_kb (kb);
928  rep =
929  redis_cmd (kbr, "LRANGE nvt:%s %d %d", oid, NVT_FILENAME_POS, NVT_NAME_POS);
930  if (!rep)
931  return NULL;
932  if (rep->type != REDIS_REPLY_ARRAY || rep->elements != NVT_NAME_POS + 1)
933  {
934  freeReplyObject (rep);
935  return NULL;
936  }
937  else
938  {
939  nvti_t *nvti = nvti_new ();
940 
941  nvti_set_oid (nvti, oid);
942  nvti_set_required_keys (nvti, rep->element[NVT_REQUIRED_KEYS_POS]->str);
944  nvti_set_excluded_keys (nvti, rep->element[NVT_EXCLUDED_KEYS_POS]->str);
946  nvti, rep->element[NVT_REQUIRED_UDP_PORTS_POS]->str);
948  nvti_set_dependencies (nvti, rep->element[NVT_DEPENDENCIES_POS]->str);
949  nvti_set_tag (nvti, rep->element[NVT_TAGS_POS]->str);
950  nvti_set_cve (nvti, rep->element[NVT_CVES_POS]->str);
951  nvti_set_bid (nvti, rep->element[NVT_BIDS_POS]->str);
952  nvti_set_xref (nvti, rep->element[NVT_XREFS_POS]->str);
953  nvti_set_category (nvti, atoi (rep->element[NVT_CATEGORY_POS]->str));
954  nvti_set_timeout (nvti, atoi (rep->element[NVT_TIMEOUT_POS]->str));
955  nvti_set_family (nvti, rep->element[NVT_FAMILY_POS]->str);
956  nvti_set_name (nvti, rep->element[NVT_NAME_POS]->str);
957 
958  freeReplyObject (rep);
959  return nvti;
960  }
961 }
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition: nvti.c:817
#define redis_kb(__kb)
Definition: kb.c:67
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:156
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition: nvti.c:777
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition: nvti.c:728
The structure of a information record that corresponds to a NVT.
Definition: nvti.h:62
int nvti_set_timeout(nvti_t *n, const gint timeout)
Set the timeout of a NVT Info.
Definition: nvti.c:798
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:579
int nvti_set_cve(nvti_t *n, const gchar *cve)
Set the CVE references of a NVT.
Definition: nvti.c:513
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:471
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition: nvti.c:678
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition: nvti.c:492
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
Definition: nvti.c:555
int nvti_set_bid(nvti_t *n, const gchar *bid)
Set the bid references of a NVT.
Definition: nvti.c:534
int nvti_set_required_udp_ports(nvti_t *n, const gchar *required_udp_ports)
Set the required udp ports of a NVT.
Definition: nvti.c:753
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition: nvti.c:703
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition: nvti.c:628
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition: nvti.c:653

References NVT_BIDS_POS, NVT_CATEGORY_POS, NVT_CVES_POS, NVT_DEPENDENCIES_POS, NVT_EXCLUDED_KEYS_POS, NVT_FAMILY_POS, NVT_FILENAME_POS, NVT_MANDATORY_KEYS_POS, NVT_NAME_POS, NVT_REQUIRED_KEYS_POS, NVT_REQUIRED_PORTS_POS, NVT_REQUIRED_UDP_PORTS_POS, NVT_TAGS_POS, NVT_TIMEOUT_POS, NVT_XREFS_POS, nvti_new(), nvti_set_bid(), nvti_set_category(), nvti_set_cve(), nvti_set_dependencies(), nvti_set_excluded_keys(), nvti_set_family(), nvti_set_mandatory_keys(), nvti_set_name(), nvti_set_oid(), nvti_set_required_keys(), nvti_set_required_ports(), nvti_set_required_udp_ports(), nvti_set_tag(), nvti_set_timeout(), nvti_set_xref(), redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_get_oids()

static GSList* redis_get_oids ( kb_t  kb)
static

Get all NVT OIDs.

Parameters
[in]kbKB handle where to fetch the items.
Returns
Linked list of all OIDs or NULL.

Definition at line 1060 of file kb.c.

1061 {
1062  struct kb_redis *kbr;
1063  redisReply *rep;
1064  GSList *list = NULL;
1065  size_t i;
1066 
1067  kbr = redis_kb (kb);
1068  rep = redis_cmd (kbr, "KEYS nvt:*");
1069  if (!rep)
1070  return NULL;
1071 
1072  if (rep->type != REDIS_REPLY_ARRAY)
1073  {
1074  freeReplyObject (rep);
1075  return NULL;
1076  }
1077 
1078  /* Fetch OID values from key names nvt:OID. */
1079  for (i = 0; i < rep->elements; i++)
1080  list = g_slist_prepend (list, g_strdup (rep->element[i]->str + 4));
1081  freeReplyObject (rep);
1082 
1083  return list;
1084 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_get_pattern()

static struct kb_item* redis_get_pattern ( kb_t  kb,
const char *  pattern 
)
static

Get all items stored under a given pattern.

Parameters
[in]kbKB handle where to fetch the items.
[in]pattern'*' pattern of the elements to retrieve.
Returns
Linked struct kb_item instances to be freed with kb_item_free() or NULL if no element was found or on error.

Definition at line 998 of file kb.c.

999 {
1000  struct kb_redis *kbr;
1001  struct kb_item *kbi = NULL;
1002  redisReply *rep;
1003  unsigned int i;
1004 
1005  kbr = redis_kb (kb);
1006  rep = redis_cmd (kbr, "KEYS %s", pattern);
1007  if (!rep)
1008  return NULL;
1009  if (rep->type != REDIS_REPLY_ARRAY)
1010  {
1011  freeReplyObject (rep);
1012  return NULL;
1013  }
1014 
1015  if (get_redis_ctx (kbr) < 0)
1016  return NULL;
1017  for (i = 0; i < rep->elements; i++)
1018  redisAppendCommand (kbr->rctx, "LRANGE %s 0 -1", rep->element[i]->str);
1019 
1020  for (i = 0; i < rep->elements; i++)
1021  {
1022  struct kb_item *tmp;
1023  redisReply *rep_range;
1024 
1025  redisGetReply (kbr->rctx, (void **) &rep_range);
1026  if (!rep)
1027  continue;
1028  tmp = redis2kbitem (rep->element[i]->str, rep_range);
1029  if (!tmp)
1030  {
1031  freeReplyObject (rep_range);
1032  continue;
1033  }
1034 
1035  if (kbi)
1036  {
1037  struct kb_item *tmp2;
1038 
1039  tmp2 = tmp;
1040  while (tmp->next)
1041  tmp = tmp->next;
1042  tmp->next = kbi;
1043  kbi = tmp2;
1044  }
1045  else
1046  kbi = tmp;
1047  freeReplyObject (rep_range);
1048  }
1049 
1050  freeReplyObject (rep);
1051  return kbi;
1052 }
#define redis_kb(__kb)
Definition: kb.c:67
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
struct kb_item * next
Definition: kb.h:90
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static struct kb_item * redis2kbitem(const char *name, const redisReply *rep)
Fetch a KB item or list from a redis Reply.
Definition: kb.c:663
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References get_redis_ctx(), kb_item::next, kb_redis::rctx, redis2kbitem(), redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_get_single()

static struct kb_item* redis_get_single ( kb_t  kb,
const char *  name,
enum kb_item_type  type 
)
static

Get a single KB element.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
[in]typeDesired element type.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

Definition at line 760 of file kb.c.

761 {
762  struct kb_item *kbi;
763  struct kb_redis *kbr;
764  redisReply *rep;
765 
766  kbr = redis_kb (kb);
767  kbi = NULL;
768 
769  rep = redis_cmd (kbr, "LINDEX %s -1", name);
770  if (rep == NULL || rep->type != REDIS_REPLY_STRING)
771  {
772  kbi = NULL;
773  goto out;
774  }
775 
776  kbi = redis2kbitem_single (name, rep, type == KB_TYPE_INT);
777 
778 out:
779  if (rep != NULL)
780  freeReplyObject (rep);
781 
782  return kbi;
783 }
#define redis_kb(__kb)
Definition: kb.c:67
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
static struct kb_item * redis2kbitem_single(const char *name, const redisReply *elt, int force_int)
Give a single KB item.
Definition: kb.c:621
Definition: kb.h:45
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References KB_TYPE_INT, redis2kbitem_single(), redis_cmd(), and redis_kb.

Referenced by redis_get_int(), and redis_get_str().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ redis_get_str()

static char* redis_get_str ( kb_t  kb,
const char *  name 
)
static

Get a single KB string item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
Returns
A struct kb_item to be freed with kb_item_free() or NULL if no element was found or on error.

Definition at line 793 of file kb.c.

794 {
795  struct kb_item *kbi;
796 
798  if (kbi != NULL)
799  {
800  char *res;
801 
802  res = kbi->v_str;
803  kbi->v_str = NULL;
804  kb_item_free (kbi);
805  return res;
806  }
807  return NULL;
808 }
Definition: kb.h:46
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.c:760
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
Definition: kb.c:599
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
char name[0]
Definition: kb.h:93
char * v_str
Definition: kb.h:85

References kb_item_free(), KB_TYPE_STR, kb_item::name, redis_get_single(), and kb_item::v_str.

Here is the call graph for this function:

◆ redis_lnk_reset()

static int redis_lnk_reset ( kb_t  kb)
static

Reset connection to the KB. This is called after each fork() to make sure connections aren't shared between concurrent processes.

Parameters
[in]kbKB handle.
Returns
0 on success, non-null on error.

Definition at line 1423 of file kb.c.

1424 {
1425  struct kb_redis *kbr;
1426 
1427  kbr = redis_kb (kb);
1428 
1429  if (kbr->rctx != NULL)
1430  {
1431  redisFree (kbr->rctx);
1432  kbr->rctx = NULL;
1433  }
1434 
1435  return 0;
1436 }
#define redis_kb(__kb)
Definition: kb.c:67
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References kb_redis::rctx, and redis_kb.

Referenced by redis_cmd().

Here is the caller graph for this function:

◆ redis_new()

static int redis_new ( kb_t *  kb,
const char *  kb_path 
)
static

Initialize a new Knowledge Base object.

Parameters
[in]kbReference to a kb_t to initialize.
[in]kb_pathPath to KB.
Returns
0 on success, -1 on connection error, -2 when no DB is available.

Definition at line 456 of file kb.c.

457 {
458  struct kb_redis *kbr;
459  int rc = 0;
460 
461  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
462  kbr->kb.kb_ops = &KBRedisOperations;
463  strncpy (kbr->path, kb_path, strlen (kb_path));
464 
465  if ((rc = get_redis_ctx (kbr)) < 0)
466  return rc;
467  if (redis_test_connection (kbr))
468  {
469  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
470  "%s: cannot access redis at '%s'", __func__, kb_path);
471  redis_delete ((kb_t) kbr);
472  kbr = NULL;
473  rc = -1;
474  }
475 
476  *kb = (kb_t) kbr;
477  return rc;
478 }
static int redis_test_connection(struct kb_redis *kbr)
Test redis connection.
Definition: kb.c:376
#define G_LOG_DOMAIN
Definition: kb.c:38
char path[0]
Definition: kb.c:65
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
const struct kb_operations * kb_ops
Definition: kb.h:103
struct kb kb
Definition: kb.c:61
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
static int redis_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition: kb.c:415
static const struct kb_operations KBRedisOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:52
struct kb * kb_t
type abstraction to hide KB internals.
Definition: kb.h:109

References G_LOG_DOMAIN, get_redis_ctx(), kb_redis::kb, kb::kb_ops, KBRedisOperations, kb_redis::path, redis_delete(), and redis_test_connection().

Here is the call graph for this function:

◆ redis_pop_str()

static char* redis_pop_str ( kb_t  kb,
const char *  name 
)
static

Pops a single KB string item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the key from where to retrieve.
Returns
A string to be freed or NULL if list is empty or on error.

Definition at line 842 of file kb.c.

843 {
844  struct kb_redis *kbr;
845  redisReply *rep;
846  char *value = NULL;
847 
848  kbr = redis_kb (kb);
849  rep = redis_cmd (kbr, "RPOP %s", name);
850  if (!rep)
851  return NULL;
852 
853  if (rep->type == REDIS_REPLY_STRING)
854  value = g_strdup (rep->str);
855  freeReplyObject (rep);
856 
857  return value;
858 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_push_str()

static int redis_push_str ( kb_t  kb,
const char *  name,
const char *  value 
)
static

Push a new entry under a given key.

Parameters
[in]kbKB handle where to store the item.
[in]nameKey to push to.
[in]valueValue to push.
Returns
0 on success, non-null on error.

Definition at line 818 of file kb.c.

819 {
820  struct kb_redis *kbr;
821  redisReply *rep = NULL;
822  int rc = 0;
823 
824  kbr = redis_kb (kb);
825  rep = redis_cmd (kbr, "LPUSH %s %s", name, value);
826  if (!rep || rep->type == REDIS_REPLY_ERROR)
827  rc = -1;
828 
829  if (rep)
830  freeReplyObject (rep);
831 
832  return rc;
833 }
#define redis_kb(__kb)
Definition: kb.c:67
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_release_db()

static int redis_release_db ( struct kb_redis *  kbr)
static

Release DB.

Parameters
[in]kbrSubclass of struct kb.
Returns
0 on success, -1 on error.

Definition at line 298 of file kb.c.

299 {
300  int rc;
301  redisContext *ctx = kbr->rctx;
302  redisReply *rep;
303 
304  if (ctx == NULL)
305  return -EINVAL;
306 
307  rep = redisCommand (ctx, "SELECT 0"); /* Management database*/
308  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
309  {
310  rc = -1;
311  goto err_cleanup;
312  }
313  freeReplyObject (rep);
314 
315  rep = redisCommand (ctx, "HDEL %s %d", GLOBAL_DBINDEX_NAME, kbr->db);
316  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER)
317  {
318  rc = -1;
319  goto err_cleanup;
320  }
321 
322  rc = 0;
323 
324 err_cleanup:
325  if (rep != NULL)
326  freeReplyObject (rep);
327 
328  return rc;
329 }
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
Definition: kb.c:50

References kb_redis::db, GLOBAL_DBINDEX_NAME, and kb_redis::rctx.

Referenced by redis_delete(), and redis_flush_all().

Here is the caller graph for this function:

◆ redis_save()

int redis_save ( kb_t  kb)

Save all the elements from the KB.

Parameters
[in]kbKB handle.
Returns
0 on success, -1 on error.

Definition at line 1520 of file kb.c.

1521 {
1522  int rc;
1523  redisReply *rep;
1524  struct kb_redis *kbr;
1525 
1526  kbr = redis_kb (kb);
1527  g_debug ("%s: saving all elements from KB #%u", __func__, kbr->db);
1528  rep = redis_cmd (kbr, "SAVE");
1529  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1530  {
1531  rc = -1;
1532  goto err_cleanup;
1533  }
1534 
1535  rc = 0;
1536 
1537 err_cleanup:
1538  if (rep != NULL)
1539  freeReplyObject (rep);
1540 
1541  return rc;
1542 }
#define redis_kb(__kb)
Definition: kb.c:67
unsigned int db
Definition: kb.c:63
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References kb_redis::db, redis_cmd(), and redis_kb.

Here is the call graph for this function:

◆ redis_set_int()

static int redis_set_int ( kb_t  kb,
const char *  name,
int  val 
)
static

Set (replace) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
Returns
0 on success, non-null on error.

Definition at line 1333 of file kb.c.

1334 {
1335  struct kb_redis *kbr;
1336  redisReply *rep = NULL;
1337  redisContext *ctx;
1338  int rc = 0, i = 4;
1339 
1340  kbr = redis_kb (kb);
1341  if (get_redis_ctx (redis_kb (kb)) < 0)
1342  return -1;
1343  ctx = kbr->rctx;
1344  redisAppendCommand (ctx, "MULTI");
1345  redisAppendCommand (ctx, "DEL %s", name);
1346  redisAppendCommand (ctx, "RPUSH %s %d", name, val);
1347  redisAppendCommand (ctx, "EXEC");
1348  while (i--)
1349  {
1350  redisGetReply (ctx, (void **) &rep);
1351  if (!rep || rep->type == REDIS_REPLY_ERROR)
1352  rc = -1;
1353  if (rep)
1354  freeReplyObject (rep);
1355  }
1356 
1357  return rc;
1358 }
#define redis_kb(__kb)
Definition: kb.c:67
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References get_redis_ctx(), kb_redis::rctx, and redis_kb.

Here is the call graph for this function:

◆ redis_set_str()

static int redis_set_str ( kb_t  kb,
const char *  name,
const char *  val,
size_t  len 
)
static

Set (replace) a new entry under a given name.

Parameters
[in]kbKB handle where to store the item.
[in]nameItem name.
[in]valItem value.
[in]lenValue length. Used for blobs.
Returns
0 on success, non-null on error.

Definition at line 1234 of file kb.c.

1235 {
1236  struct kb_redis *kbr;
1237  redisReply *rep = NULL;
1238  redisContext *ctx;
1239  int rc = 0, i = 4;
1240 
1241  kbr = redis_kb (kb);
1242  if (get_redis_ctx (kbr) < 0)
1243  return -1;
1244  ctx = kbr->rctx;
1245  redisAppendCommand (ctx, "MULTI");
1246  redisAppendCommand (ctx, "DEL %s", name);
1247  if (len == 0)
1248  redisAppendCommand (ctx, "RPUSH %s %s", name, val);
1249  else
1250  redisAppendCommand (ctx, "RPUSH %s %b", name, val, len);
1251  redisAppendCommand (ctx, "EXEC");
1252  while (i--)
1253  {
1254  redisGetReply (ctx, (void **) &rep);
1255  if (!rep || rep->type == REDIS_REPLY_ERROR)
1256  rc = -1;
1257  if (rep)
1258  freeReplyObject (rep);
1259  }
1260 
1261  return rc;
1262 }
#define redis_kb(__kb)
Definition: kb.c:67
redisContext * rctx
Definition: kb.c:64
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59

References get_redis_ctx(), kb_redis::rctx, and redis_kb.

Here is the call graph for this function:

◆ redis_test_connection()

static int redis_test_connection ( struct kb_redis *  kbr)
static

Test redis connection.

Parameters
[in]kbrSubclass of struct kb to test.
Returns
0 on success, negative integer on error.

Definition at line 376 of file kb.c.

377 {
378  int rc = 0;
379  redisReply *rep;
380 
381  rep = redis_cmd (kbr, "PING");
382  if (rep == NULL)
383  {
384  /* not 100% relevant but hiredis doesn't provide us with proper error
385  * codes. */
386  rc = -ECONNREFUSED;
387  goto out;
388  }
389 
390  if (rep->type != REDIS_REPLY_STATUS)
391  {
392  rc = -EINVAL;
393  goto out;
394  }
395 
396  if (g_ascii_strcasecmp (rep->str, "PONG"))
397  {
398  rc = -EPROTO;
399  goto out;
400  }
401 
402 out:
403  if (rep != NULL)
404  freeReplyObject (rep);
405 
406  return rc;
407 }
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714

References redis_cmd().

Referenced by redis_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ select_database()

static int select_database ( struct kb_redis *  kbr)
static

Select DB.

Parameters
[in]kbrSubclass of struct kb where to save the db index.
Returns
0 on success, -1 on error.

WARNING: do not call redis_cmd in here, since our context is not fully acquired yet!

Definition at line 247 of file kb.c.

248 {
249  int rc;
250  redisContext *ctx = kbr->rctx;
251  redisReply *rep = NULL;
252 
253  if (kbr->db == 0)
254  {
255  unsigned i;
256 
257  if (kbr->max_db == 0)
258  fetch_max_db_index (kbr);
259 
260  for (i = 1; i < kbr->max_db; i++)
261  {
262  rc = try_database_index (kbr, i);
263  if (rc == 0)
264  break;
265  }
266  }
267 
268  /* No DB available, give up. */
269  if (kbr->db == 0)
270  {
271  rc = -1;
272  goto err_cleanup;
273  }
274 
275  rep = redisCommand (ctx, "SELECT %u", kbr->db);
276  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
277  {
278  rc = -1;
279  goto err_cleanup;
280  }
281 
282  rc = 0;
283 
284 err_cleanup:
285  if (rep != NULL)
286  freeReplyObject (rep);
287 
288  return rc;
289 }
unsigned int max_db
Definition: kb.c:62
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
static int try_database_index(struct kb_redis *kbr, int index)
Attempt to atomically acquire ownership of a database.
Definition: kb.c:82
static int fetch_max_db_index(struct kb_redis *kbr)
Set the number of databases have been configured into kbr struct.
Definition: kb.c:189

References kb_redis::db, fetch_max_db_index(), kb_redis::max_db, kb_redis::rctx, and try_database_index().

Referenced by get_redis_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_database_index()

static int try_database_index ( struct kb_redis *  kbr,
int  index 
)
static

Attempt to atomically acquire ownership of a database.

Returns
0 on success, negative integer otherwise.

Definition at line 82 of file kb.c.

83 {
84  redisContext *ctx = kbr->rctx;
85  redisReply *rep;
86  int rc = 0;
87 
88  rep = redisCommand (ctx, "HSETNX %s %d 1", GLOBAL_DBINDEX_NAME, index);
89  if (rep == NULL)
90  return -ENOMEM;
91 
92  if (rep->type != REDIS_REPLY_INTEGER)
93  rc = -EPROTO;
94  else if (rep->integer == 0)
95  rc = -EALREADY;
96  else
97  kbr->db = index;
98 
99  freeReplyObject (rep);
100 
101  return rc;
102 }
unsigned int db
Definition: kb.c:63
redisContext * rctx
Definition: kb.c:64
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
Definition: kb.c:50

References kb_redis::db, GLOBAL_DBINDEX_NAME, and kb_redis::rctx.

Referenced by select_database().

Here is the caller graph for this function:

Variable Documentation

◆ KBDefaultOperations

const struct kb_operations* KBDefaultOperations = &KBRedisOperations

Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).

Definition at line 1618 of file kb.c.

Referenced by kb_direct_conn(), kb_find(), and kb_new().

◆ KBRedisOperations

static const struct kb_operations KBRedisOperations
static
Initial value:
= {
.kb_new = redis_new,
.kb_find = redis_find,
.kb_delete = redis_delete,
.kb_get_single = redis_get_single,
.kb_get_str = redis_get_str,
.kb_get_int = redis_get_int,
.kb_get_nvt = redis_get_nvt,
.kb_get_nvt_all = redis_get_nvt_all,
.kb_get_nvt_oids = redis_get_oids,
.kb_push_str = redis_push_str,
.kb_pop_str = redis_pop_str,
.kb_get_all = redis_get_all,
.kb_get_pattern = redis_get_pattern,
.kb_count = redis_count,
.kb_add_str = redis_add_str,
.kb_add_str_unique = redis_add_str_unique,
.kb_set_str = redis_set_str,
.kb_add_int = redis_add_int,
.kb_add_int_unique = redis_add_int_unique,
.kb_set_int = redis_set_int,
.kb_add_nvt = redis_add_nvt,
.kb_del_items = redis_del_items,
.kb_lnk_reset = redis_lnk_reset,
.kb_save = redis_save,
.kb_flush = redis_flush_all,
.kb_direct_conn = redis_direct_conn,
.kb_get_kb_index = redis_get_kb_index,
}
static int redis_push_str(kb_t kb, const char *name, const char *value)
Push a new entry under a given key.
Definition: kb.c:818
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.
Definition: kb.c:1206
static nvti_t * redis_get_nvt_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.c:922
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.c:760
static char * redis_pop_str(kb_t kb, const char *name)
Pops a single KB string item.
Definition: kb.c:842
static struct kb_item * redis_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
Definition: kb.c:971
static int redis_add_nvt(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition: kb.c:1368
static GSList * redis_get_oids(kb_t kb)
Get all NVT OIDs.
Definition: kb.c:1060
static int redis_flush_all(kb_t, const char *)
Flush all the KB's content. Delete all namespaces.
Definition: kb.c:1445
static int redis_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
Definition: kb.c:1333
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...
Definition: kb.c:1423
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.
Definition: kb.c:487
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.
Definition: kb.c:1152
static size_t redis_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition: kb.c:1095
static int redis_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
Definition: kb.c:1311
static char * redis_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.c:793
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.
Definition: kb.c:1234
static int redis_add_int_unique(kb_t kb, const char *name, int val)
Insert (append) a new unique entry under a given name.
Definition: kb.c:1272
int redis_save(kb_t kb)
Save all the elements from the KB.
Definition: kb.c:1520
static int redis_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition: kb.c:415
static char * redis_get_nvt(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition: kb.c:892
static kb_t redis_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition: kb.c:527
static int redis_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.c:456
static int redis_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition: kb.c:868
static int redis_get_kb_index(kb_t kb)
Return the kb index.
Definition: kb.c:440
static struct kb_item * redis_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
Definition: kb.c:998
static int redis_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.c:1125

Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).

Definition at line 52 of file kb.c.

Referenced by redis_direct_conn(), redis_find(), and redis_new().