kb.h
Go to the documentation of this file.
/* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef _GVM_UTIL_KB_H
#define _GVM_UTIL_KB_H
#include "../base/nvti.h" /* for nvti_t */
#include <assert.h>
#include <stddef.h> /* for NULL */
#include <sys/types.h> /* for size_t */
#ifdef REDIS_SOCKET_PATH
#define KB_PATH_DEFAULT REDIS_SOCKET_PATH
#else
#define KB_PATH_DEFAULT "/run/redis/redis.sock"
#endif
enum kb_item_type
{
KB_TYPE_UNSPEC,
KB_TYPE_INT,
KB_TYPE_STR,
/* -- */
KB_TYPE_CNT,
};
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,
};
struct kb_item
{
enum kb_item_type type;
union
{
char *v_str;
int v_int;
};
size_t len;
struct kb_item *next;
size_t namelen;
char name[];
};
struct kb_operations;
struct kb
{
const struct kb_operations *kb_ops;
};
typedef struct kb *kb_t;
struct kb_operations
{
/* ctor/dtor */
int (*kb_new) (kb_t *, const char *);
int (*kb_delete) (kb_t);
kb_t (*kb_find) (const char *, const char *);
kb_t (*kb_direct_conn) (const char *, const int);
/* Actual kb operations */
struct kb_item *(*kb_get_single) (kb_t, const char *, enum kb_item_type);
char *(*kb_get_str) (kb_t, const char *);
int (*kb_get_int) (kb_t, const char *);
char *(*kb_get_nvt) (kb_t, const char *, enum kb_nvt_pos);
nvti_t *(*kb_get_nvt_all) (kb_t, const char *);
GSList *(*kb_get_nvt_oids) (kb_t);
int (*kb_push_str) (kb_t, const char *, const char *);
char *(*kb_pop_str) (kb_t, const char *);
struct kb_item *(*kb_get_all) (kb_t, const char *);
struct kb_item *(*kb_get_pattern) (kb_t, const char *);
size_t (*kb_count) (kb_t, const char *);
int (*kb_add_str) (kb_t, const char *, const char *, size_t);
int (*kb_add_str_unique) (kb_t, const char *, const char *, size_t, int);
int (*kb_add_str_unique_volatile) (kb_t, const char *, const char *, int,
size_t, int);
int (*kb_set_str) (kb_t, const char *, const char *, size_t);
int (*kb_add_int) (kb_t, const char *, int);
int (*kb_add_int_unique) (kb_t, const char *, int);
int (*kb_add_int_unique_volatile) (kb_t, const char *, int, int);
int (*kb_set_int) (kb_t, const char *, int);
int (*kb_add_nvt) (kb_t, const nvti_t *, const char *);
int (*kb_del_items) (kb_t, const char *);
/* Utils */
int (*kb_save) (kb_t);
int (*kb_lnk_reset) (kb_t);
int (*kb_flush) (kb_t, const char *);
int (*kb_get_kb_index) (kb_t);
};
extern const struct kb_operations *KBDefaultOperations;
void
kb_item_free (struct kb_item *);
static inline int
kb_new (kb_t *kb, const char *kb_path)
{
assert (kb);
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_new);
*kb = NULL;
return KBDefaultOperations->kb_new (kb, kb_path);
}
static inline kb_t
kb_direct_conn (const char *kb_path, const int kb_index)
{
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_direct_conn);
return KBDefaultOperations->kb_direct_conn (kb_path, kb_index);
}
static inline kb_t
kb_find (const char *kb_path, const char *key)
{
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_find);
return KBDefaultOperations->kb_find (kb_path, key);
}
static inline int
kb_delete (kb_t kb)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_delete);
return kb->kb_ops->kb_delete (kb);
}
static inline struct kb_item *
kb_item_get_single (kb_t kb, const char *name, enum kb_item_type type)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_single);
return kb->kb_ops->kb_get_single (kb, name, type);
}
static inline char *
kb_item_get_str (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_str);
return kb->kb_ops->kb_get_str (kb, name);
}
static inline int
kb_item_get_int (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_int);
return kb->kb_ops->kb_get_int (kb, name);
}
static inline struct kb_item *
kb_item_get_all (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_all);
return kb->kb_ops->kb_get_all (kb, name);
}
static inline struct kb_item *
kb_item_get_pattern (kb_t kb, const char *pattern)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_pattern);
return kb->kb_ops->kb_get_pattern (kb, pattern);
}
static inline int
kb_item_push_str (kb_t kb, const char *name, const char *value)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_push_str);
return kb->kb_ops->kb_push_str (kb, name, value);
}
static inline char *
kb_item_pop_str (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_pop_str);
return kb->kb_ops->kb_pop_str (kb, name);
}
static inline size_t
kb_item_count (kb_t kb, const char *pattern)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_count);
return kb->kb_ops->kb_count (kb, pattern);
}
static inline int
kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_str);
return kb->kb_ops->kb_add_str (kb, name, str, len);
}
static inline int
kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len,
int pos)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_str_unique);
return kb->kb_ops->kb_add_str_unique (kb, name, str, len, pos);
}
static inline int
kb_add_str_unique_volatile (kb_t kb, const char *name, const char *str,
int expire, size_t len, int pos)
{
assert (kb);
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_add_str_unique_volatile);
return KBDefaultOperations->kb_add_str_unique_volatile (kb, name, str, expire,
len, pos);
}
static inline int
kb_item_set_str (kb_t kb, const char *name, const char *str, size_t len)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_set_str);
return kb->kb_ops->kb_set_str (kb, name, str, len);
}
static inline int
kb_item_add_int (kb_t kb, const char *name, int val)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_int);
return kb->kb_ops->kb_add_int (kb, name, val);
}
static inline int
kb_item_add_int_unique (kb_t kb, const char *name, int val)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_int_unique);
return kb->kb_ops->kb_add_int_unique (kb, name, val);
}
static inline int
kb_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire)
{
assert (kb);
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_add_int_unique_volatile);
return KBDefaultOperations->kb_add_int_unique_volatile (kb, name, val,
expire);
}
static inline int
kb_item_set_int (kb_t kb, const char *name, int val)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_set_int);
return kb->kb_ops->kb_set_int (kb, name, val);
}
static inline int
kb_nvt_add (kb_t kb, const nvti_t *nvt, const char *filename)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_nvt);
return kb->kb_ops->kb_add_nvt (kb, nvt, filename);
}
static inline char *
kb_nvt_get (kb_t kb, const char *oid, enum kb_nvt_pos position)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_nvt);
return kb->kb_ops->kb_get_nvt (kb, oid, position);
}
static inline nvti_t *
kb_nvt_get_all (kb_t kb, const char *oid)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_nvt_all);
return kb->kb_ops->kb_get_nvt_all (kb, oid);
}
static inline GSList *
kb_nvt_get_oids (kb_t kb)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_nvt_oids);
return kb->kb_ops->kb_get_nvt_oids (kb);
}
static inline int
kb_del_items (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_del_items);
return kb->kb_ops->kb_del_items (kb, name);
}
static inline int
kb_save (kb_t kb)
{
int rc = 0;
assert (kb);
assert (kb->kb_ops);
if (kb->kb_ops->kb_save != NULL)
rc = kb->kb_ops->kb_save (kb);
return rc;
}
static inline int
kb_lnk_reset (kb_t kb)
{
int rc = 0;
assert (kb);
assert (kb->kb_ops);
if (kb->kb_ops->kb_lnk_reset != NULL)
rc = kb->kb_ops->kb_lnk_reset (kb);
return rc;
}
static inline int
kb_flush (kb_t kb, const char *except)
{
int rc = 0;
assert (kb);
assert (kb->kb_ops);
if (kb->kb_ops->kb_flush != NULL)
rc = kb->kb_ops->kb_flush (kb, except);
return rc;
}
static inline int
kb_get_kb_index (kb_t kb)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_kb_index);
return kb->kb_ops->kb_get_kb_index (kb);
}
#endif
KBDefaultOperations
const struct kb_operations * KBDefaultOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition kb.c:1880
kb_item_set_int
static int kb_item_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
Definition kb.h:617
kb_del_items
static int kb_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition kb.h:708
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)
Insert (append) a new unique entry under a given name.
Definition kb.h:490
kb_nvt_add
static int kb_nvt_add(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition kb.h:636
kb_item_add_str
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.
Definition kb.h:468
kb_nvt_pos
kb_nvt_pos
Possible positions of nvt values in cache list.
Definition kb.h:45
NVT_FAMILY_POS
@ NVT_FAMILY_POS
Definition kb.h:58
NVT_CATEGORY_POS
@ NVT_CATEGORY_POS
Definition kb.h:57
NVT_TIMESTAMP_POS
@ NVT_TIMESTAMP_POS
Definition kb.h:60
NVT_NAME_POS
@ NVT_NAME_POS
Definition kb.h:59
NVT_TAGS_POS
@ NVT_TAGS_POS
Definition kb.h:53
NVT_BIDS_POS
@ NVT_BIDS_POS
Definition kb.h:55
NVT_EXCLUDED_KEYS_POS
@ NVT_EXCLUDED_KEYS_POS
Definition kb.h:49
NVT_REQUIRED_PORTS_POS
@ NVT_REQUIRED_PORTS_POS
Definition kb.h:51
NVT_REQUIRED_UDP_PORTS_POS
@ NVT_REQUIRED_UDP_PORTS_POS
Definition kb.h:50
NVT_FILENAME_POS
@ NVT_FILENAME_POS
Definition kb.h:46
NVT_OID_POS
@ NVT_OID_POS
Definition kb.h:61
NVT_DEPENDENCIES_POS
@ NVT_DEPENDENCIES_POS
Definition kb.h:52
NVT_CVES_POS
@ NVT_CVES_POS
Definition kb.h:54
NVT_REQUIRED_KEYS_POS
@ NVT_REQUIRED_KEYS_POS
Definition kb.h:47
NVT_XREFS_POS
@ NVT_XREFS_POS
Definition kb.h:56
NVT_MANDATORY_KEYS_POS
@ NVT_MANDATORY_KEYS_POS
Definition kb.h:48
kb_item_push_str
static int kb_item_push_str(kb_t kb, const char *name, const char *value)
Push a new value under a given key.
Definition kb.h:409
kb_new
static int kb_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition kb.h:243
kb_item_count
static size_t kb_item_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition kb.h:448
kb_delete
static int kb_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition kb.h:296
kb_item_get_int
static int kb_item_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition kb.h:352
kb_t
struct kb * kb_t
type abstraction to hide KB internals.
Definition kb.h:98
kb_nvt_get_all
static nvti_t * kb_nvt_get_all(kb_t kb, const char *oid)
Get a full NVT.
Definition kb.h:673
kb_find
static kb_t kb_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition kb.h:280
kb_nvt_get_oids
static GSList * kb_nvt_get_oids(kb_t kb)
Get list of NVT OIDs.
Definition kb.h:690
kb_item_pop_str
static char * kb_item_pop_str(kb_t kb, const char *name)
Pop a single KB string item.
Definition kb.h:428
kb_item_set_str
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.
Definition kb.h:538
kb_add_int_unique_volatile
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.
Definition kb.h:597
kb_nvt_get
static char * kb_nvt_get(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition kb.h:655
kb_item_free
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
Definition kb.c:642
kb_save
static int kb_save(kb_t kb)
Save all the KB's content.
Definition kb.h:725
kb_item_get_single
static struct kb_item * kb_item_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition kb.h:316
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)
Insert (append) a new unique and volatile entry under a given name.
Definition kb.h:516
kb_item_add_int
static int kb_item_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
Definition kb.h:557
kb_lnk_reset
static int kb_lnk_reset(kb_t kb)
Reset connection to the KB. This is called after each fork() to make.
Definition kb.h:747
kb_item_get_pattern
static struct kb_item * kb_item_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
Definition kb.h:390
kb_item_type
kb_item_type
Possible type of a kb_item.
Definition kb.h:33
KB_TYPE_INT
@ KB_TYPE_INT
Definition kb.h:35
KB_TYPE_CNT
@ KB_TYPE_CNT
Definition kb.h:38
KB_TYPE_UNSPEC
@ KB_TYPE_UNSPEC
Definition kb.h:34
KB_TYPE_STR
@ KB_TYPE_STR
Definition kb.h:36
kb_flush
static int kb_flush(kb_t kb, const char *except)
Flush all the KB's content. Delete all namespaces.
Definition kb.h:769
kb_item_get_all
static struct kb_item * kb_item_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
Definition kb.h:371
kb_item_add_int_unique
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.
Definition kb.h:576
kb_item_get_str
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition kb.h:334
kb_direct_conn
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.
Definition kb.h:263
kb_get_kb_index
static int kb_get_kb_index(kb_t kb)
Return the kb index.
Definition kb.h:790
nvti_t
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
nvti.h
Protos and data structures for NVT Information data sets.
kb_item
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition kb.h:69
kb_item::name
char name[]
Definition kb.h:82
kb_item::v_int
int v_int
Definition kb.h:75
kb_item::type
enum kb_item_type type
Definition kb.h:70
kb_item::v_str
char * v_str
Definition kb.h:74
kb_item::next
struct kb_item * next
Definition kb.h:79
kb_item::len
size_t len
Definition kb.h:78
kb_item::namelen
size_t namelen
Definition kb.h:81
kb_operations
KB interface. Functions provided by an implementation. All functions have to be provided,...
Definition kb.h:107
kb_operations::kb_get_single
struct kb_item *(* kb_get_single)(kb_t, const char *, enum kb_item_type)
Definition kb.h:118
kb_operations::kb_set_int
int(* kb_set_int)(kb_t, const char *, int)
Definition kb.h:202
kb_operations::kb_flush
int(* kb_flush)(kb_t, const char *)
Definition kb.h:217
kb_operations::kb_get_int
int(* kb_get_int)(kb_t, const char *)
Definition kb.h:126
kb_operations::kb_add_str
int(* kb_add_str)(kb_t, const char *, const char *, size_t)
Definition kb.h:166
kb_operations::kb_get_nvt_all
nvti_t *(* kb_get_nvt_all)(kb_t, const char *)
Definition kb.h:134
kb_operations::kb_add_str_unique_volatile
int(* kb_add_str_unique_volatile)(kb_t, const char *, const char *, int, size_t, int)
Definition kb.h:176
kb_operations::kb_get_nvt
char *(* kb_get_nvt)(kb_t, const char *, enum kb_nvt_pos)
Definition kb.h:130
kb_operations::kb_get_nvt_oids
GSList *(* kb_get_nvt_oids)(kb_t)
Definition kb.h:138
kb_operations::kb_add_nvt
int(* kb_add_nvt)(kb_t, const nvti_t *, const char *)
Definition kb.h:207
kb_operations::kb_get_pattern
struct kb_item *(* kb_get_pattern)(kb_t, const char *)
Definition kb.h:156
kb_operations::kb_lnk_reset
int(* kb_lnk_reset)(kb_t)
Definition kb.h:216
kb_operations::kb_new
int(* kb_new)(kb_t *, const char *)
Definition kb.h:109
kb_operations::kb_push_str
int(* kb_push_str)(kb_t, const char *, const char *)
Definition kb.h:142
kb_operations::kb_delete
int(* kb_delete)(kb_t)
Definition kb.h:110
kb_operations::kb_get_str
char *(* kb_get_str)(kb_t, const char *)
Definition kb.h:122
kb_operations::kb_count
size_t(* kb_count)(kb_t, const char *)
Definition kb.h:161
kb_operations::kb_pop_str
char *(* kb_pop_str)(kb_t, const char *)
Definition kb.h:146
kb_operations::kb_add_int_unique
int(* kb_add_int_unique)(kb_t, const char *, int)
Definition kb.h:192
kb_operations::kb_add_str_unique
int(* kb_add_str_unique)(kb_t, const char *, const char *, size_t, int)
Definition kb.h:171
kb_operations::kb_get_kb_index
int(* kb_get_kb_index)(kb_t)
Definition kb.h:218
kb_operations::kb_add_int
int(* kb_add_int)(kb_t, const char *, int)
Definition kb.h:187
kb_operations::kb_direct_conn
kb_t(* kb_direct_conn)(const char *, const int)
Definition kb.h:112
kb_operations::kb_save
int(* kb_save)(kb_t)
Definition kb.h:215
kb_operations::kb_find
kb_t(* kb_find)(const char *, const char *)
Definition kb.h:111
kb_operations::kb_set_str
int(* kb_set_str)(kb_t, const char *, const char *, size_t)
Definition kb.h:182
kb_operations::kb_del_items
int(* kb_del_items)(kb_t, const char *)
Definition kb.h:212
kb_operations::kb_add_int_unique_volatile
int(* kb_add_int_unique_volatile)(kb_t, const char *, int, int)
Definition kb.h:197
kb_operations::kb_get_all
struct kb_item *(* kb_get_all)(kb_t, const char *)
Definition kb.h:151
kb
Top-level KB. This is to be inherited by KB implementations.
Definition kb.h:91
kb::kb_ops
const struct kb_operations * kb_ops
Definition kb.h:92