Greenbone Vulnerability Management Libraries 22.10.0
kb.h File Reference

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

#include "../base/nvti.h"
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>

Go to the source code of this file.

Data Structures

struct  kb_item
 Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked list. More...
 
struct  kb
 Top-level KB. This is to be inherited by KB implementations. More...
 
struct  kb_operations
 KB interface. Functions provided by an implementation. All functions have to be provided, there is no default/fallback. These functions should be called via the corresponding static inline wrappers below. See the wrappers for the documentation. More...
 

Macros

#define KB_PATH_DEFAULT   "/run/redis/redis.sock"
 Default KB location.
 

Typedefs

typedef struct kbkb_t
 type abstraction to hide KB internals.
 

Enumerations

enum  kb_item_type { KB_TYPE_UNSPEC , KB_TYPE_INT , KB_TYPE_STR , KB_TYPE_CNT }
 Possible type of a kb_item. More...
 
enum  kb_nvt_pos {
  NVT_FILENAME_POS , NVT_REQUIRED_KEYS_POS , NVT_MANDATORY_KEYS_POS , NVT_EXCLUDED_KEYS_POS ,
  NVT_REQUIRED_UDP_PORTS_POS , NVT_REQUIRED_PORTS_POS , NVT_DEPENDENCIES_POS , NVT_TAGS_POS ,
  NVT_CVES_POS , NVT_BIDS_POS , NVT_XREFS_POS , NVT_CATEGORY_POS ,
  NVT_FAMILY_POS , NVT_NAME_POS , NVT_TIMESTAMP_POS , NVT_OID_POS
}
 Possible positions of nvt values in cache list. More...
 

Functions

void kb_item_free (struct kb_item *)
 Release a KB item (or a list).
 
static int kb_new (kb_t *kb, const char *kb_path)
 Initialize a new Knowledge Base object.
 
static kb_t kb_direct_conn (const char *kb_path, const int kb_index)
 Connect to a Knowledge Base object which has the given kb_index.
 
static kb_t kb_find (const char *kb_path, const char *key)
 Find an existing Knowledge Base object with key.
 
static int kb_delete (kb_t kb)
 Delete all entries and release ownership on the namespace.
 
static struct kb_itemkb_item_get_single (kb_t kb, const char *name, enum kb_item_type type)
 Get a single KB element.
 
static char * kb_item_get_str (kb_t kb, const char *name)
 Get a single KB string item.
 
static int kb_item_get_int (kb_t kb, const char *name)
 Get a single KB integer item.
 
static struct kb_itemkb_item_get_all (kb_t kb, const char *name)
 Get all items stored under a given name.
 
static struct kb_itemkb_item_get_pattern (kb_t kb, const char *pattern)
 Get all items stored under a given pattern.
 
static int kb_item_push_str (kb_t kb, const char *name, const char *value)
 Push a new value under a given key.
 
static char * kb_item_pop_str (kb_t kb, const char *name)
 Pop a single KB string item.
 
static size_t kb_item_count (kb_t kb, const char *pattern)
 Count all items stored under a given pattern.
 
static int kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
 Insert (append) a new entry under a given name.
 
static int kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len, int pos)
 Insert (append) a new unique entry under a given name.
 
static int kb_add_str_unique_volatile (kb_t kb, const char *name, const char *str, int expire, size_t len, int pos)
 Insert (append) a new unique and volatile entry under a given name.
 
static int kb_item_set_str (kb_t kb, const char *name, const char *str, size_t len)
 Set (replace) a new entry under a given name.
 
static int kb_item_add_int (kb_t kb, const char *name, int val)
 Insert (append) a new entry under a given name.
 
static int kb_item_add_int_unique (kb_t kb, const char *name, int val)
 Insert (append) a new unique entry under a given name.
 
static int kb_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire)
 Insert (append) a new unique and volatile entry under a given name.
 
static int kb_item_set_int (kb_t kb, const char *name, int val)
 Set (replace) a new entry under a given name.
 
static int kb_nvt_add (kb_t kb, const nvti_t *nvt, const char *filename)
 Insert a new nvt.
 
static char * kb_nvt_get (kb_t kb, const char *oid, enum kb_nvt_pos position)
 Get field of a NVT.
 
static nvti_tkb_nvt_get_all (kb_t kb, const char *oid)
 Get a full NVT.
 
static GSList * kb_nvt_get_oids (kb_t kb)
 Get list of NVT OIDs.
 
static int kb_del_items (kb_t kb, const char *name)
 Delete all entries under a given name.
 
static int kb_save (kb_t kb)
 Save all the KB's content.
 
static int kb_lnk_reset (kb_t kb)
 Reset connection to the KB. This is called after each fork() to make.
 
static int kb_flush (kb_t kb, const char *except)
 Flush all the KB's content. Delete all namespaces.
 
static int kb_get_kb_index (kb_t kb)
 Return the kb index.
 

Variables

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

Detailed Description

Knowledge base management API - Redis backend.

Macro Definition Documentation

◆ KB_PATH_DEFAULT

#define KB_PATH_DEFAULT   "/run/redis/redis.sock"

Default KB location.

Typedef Documentation

◆ kb_t

typedef struct kb* kb_t

type abstraction to hide KB internals.

Enumeration Type Documentation

◆ kb_item_type

Possible type of a kb_item.

Enumerator
KB_TYPE_UNSPEC 

Ignore the value (name/presence test).

KB_TYPE_INT 

The kb_items v should then be interpreted as int.

KB_TYPE_STR 

The kb_items v should then be interpreted as char*.

KB_TYPE_CNT 

◆ kb_nvt_pos

enum kb_nvt_pos

Possible positions of nvt values in cache list.

Enumerator
NVT_FILENAME_POS 
NVT_REQUIRED_KEYS_POS 
NVT_MANDATORY_KEYS_POS 
NVT_EXCLUDED_KEYS_POS 
NVT_REQUIRED_UDP_PORTS_POS 
NVT_REQUIRED_PORTS_POS 
NVT_DEPENDENCIES_POS 
NVT_TAGS_POS 
NVT_CVES_POS 
NVT_BIDS_POS 
NVT_XREFS_POS 
NVT_CATEGORY_POS 
NVT_FAMILY_POS 
NVT_NAME_POS 
NVT_TIMESTAMP_POS 
NVT_OID_POS 

Function Documentation

◆ kb_add_int_unique_volatile()

static int kb_add_int_unique_volatile ( kb_t kb,
const char * name,
int val,
int expire )
inlinestatic

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

Parameters
[in]kbReference to a kb_t to initialize.
[in]nameItem name.
[in]valItem value.
[in]expireItem expire.
Returns
0 on success, -1 on error.

◆ kb_add_str_unique_volatile()

static int kb_add_str_unique_volatile ( kb_t kb,
const char * name,
const char * str,
int expire,
size_t len,
int pos )
inlinestatic

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

Parameters
[in]kbReference to a kb_t to initialize.
[in]nameItem name.
[in]strString to add.
[in]lenValue length. Used for blobs.
[in]expireItem expire.
[in]posWhich position the value is appended to. 0 for right, 1 for left position in the list.
Returns
0 on success, -1 on error.

◆ kb_del_items()

static int kb_del_items ( kb_t kb,
const char * name )
inlinestatic

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.

◆ kb_delete()

static int kb_delete ( kb_t kb)
inlinestatic

Delete all entries and release ownership on the namespace.

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

◆ kb_direct_conn()

static kb_t kb_direct_conn ( const char * kb_path,
const int kb_index )
inlinestatic

Connect to a Knowledge Base object which has the given kb_index.

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

◆ kb_find()

static kb_t kb_find ( const char * kb_path,
const char * key )
inlinestatic

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.

◆ kb_flush()

static int kb_flush ( kb_t kb,
const char * except )
inlinestatic

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.

◆ kb_get_kb_index()

static int kb_get_kb_index ( kb_t kb)
inlinestatic

Return the kb index.

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

◆ kb_item_add_int()

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

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.

◆ kb_item_add_int_unique()

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

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.

◆ kb_item_add_str()

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

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.

◆ kb_item_add_str_unique()

static int kb_item_add_str_unique ( kb_t kb,
const char * name,
const char * str,
size_t len,
int pos )
inlinestatic

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.
[in]posWhich position the value is appended to. 0 for right, 1 for left position in the list.
Returns
0 on success, non-null on error.

◆ kb_item_count()

static size_t kb_item_count ( kb_t kb,
const char * pattern )
inlinestatic

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.

◆ 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

◆ kb_item_get_all()

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

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.

◆ kb_item_get_int()

static int kb_item_get_int ( kb_t kb,
const char * name )
inlinestatic

Get a single KB integer item.

Parameters
[in]kbKB handle where to fetch the item.
[in]nameName of the element to retrieve.
Returns
An integer.

◆ kb_item_get_pattern()

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

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.

◆ kb_item_get_single()

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

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.

◆ kb_item_get_str()

static char * kb_item_get_str ( kb_t kb,
const char * name )
inlinestatic

Get a single KB string item.

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

◆ kb_item_pop_str()

static char * kb_item_pop_str ( kb_t kb,
const char * name )
inlinestatic

Pop 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.

◆ kb_item_push_str()

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

Push a new value 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.

◆ kb_item_set_int()

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

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.

◆ kb_item_set_str()

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

Set (replace) 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.

◆ kb_lnk_reset()

static int kb_lnk_reset ( kb_t kb)
inlinestatic

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.

◆ kb_new()

static int kb_new ( kb_t * kb,
const char * kb_path )
inlinestatic

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 on unavailable DB spot.

◆ kb_nvt_add()

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

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.

◆ kb_nvt_get()

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

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.

◆ kb_nvt_get_all()

static nvti_t * kb_nvt_get_all ( kb_t kb,
const char * oid )
inlinestatic

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.

◆ kb_nvt_get_oids()

static GSList * kb_nvt_get_oids ( kb_t kb)
inlinestatic

Get list of NVT OIDs.

Parameters
[in]kbKB handle where NVTs are stored.
Returns
Linked-list of OIDs, NULL otherwise.

◆ kb_save()

static int kb_save ( kb_t kb)
inlinestatic

Save all the KB's content.

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

Variable Documentation

◆ KBDefaultOperations

const struct kb_operations* KBDefaultOperations
extern

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