Greenbone Vulnerability Management Libraries
10.0.0
|
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 <unistd.h>
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 | KB_RETRY_DELAY 60 |
Number of seconds to wait for between two attempts to acquire a KB namespace. 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 redisContext * | 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... | |
Knowledge base management API - Redis backend.
Contains specialized structures and functions to use redis as a KB server.
Definition in file kb.c.
#define GLOBAL_DBINDEX_NAME "GVM.__GlobalDBIndex" |
#define KB_RETRY_DELAY 60 |
|
static |
Set the number of databases have been configured into kbr struct.
[in] | kbr | Subclass of struct kb where to save the max db index founded. |
Definition at line 196 of file kb.c.
References fetch_max_db_index_compat(), G_LOG_DOMAIN, kb_redis::max_db, and kb_redis::rctx.
Referenced by redis_find(), and select_database().
|
static |
Set the number of databases have been configured into kbr struct. (For Redis 2.4.* compatibility).
[in] | kbr | Subclass of struct kb where to save the max db index founded. |
Definition at line 129 of file kb.c.
References G_LOG_DOMAIN, kb_redis::max_db, MAX_DB_INDEX__24, and kb_redis::rctx.
Referenced by fetch_max_db_index().
|
static |
Get redis context if it is already connected or do a a connection.
[in] | kbr | Subclass of struct kb where to fetch the context. or where it is saved in case of a new connection. |
Definition at line 346 of file kb.c.
References kb_redis::db, G_LOG_DOMAIN, KB_RETRY_DELAY, 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_set_int(), and redis_set_str().
void kb_item_free | ( | struct kb_item * | item | ) |
Release a KB item (or a list).
[in] | item | Item or list to be release |
Definition at line 608 of file kb.c.
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().
|
static |
Fetch a KB item or list from a redis Reply.
[in] | name | Name of the item. |
[in] | rep | A redisReply element where to fetch the item. |
Definition at line 672 of file kb.c.
References kb_item::name, kb_item::next, and redis2kbitem_single().
Referenced by redis_get_all(), and redis_get_pattern().
|
static |
Give a single KB item.
[in] | name | Name of the item. |
[in] | elt | A redisReply element where to fetch the item. |
[in] | force_int | To force string to integer conversion. |
Definition at line 630 of file kb.c.
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().
|
static |
Insert (append) a new entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | val | Item value. |
Definition at line 1319 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Insert (append) a new unique entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | val | Item value. |
Definition at line 1282 of file kb.c.
References get_redis_ctx(), and redis_kb.
Insert a new nvt.
[in] | kb | KB handle where to store the nvt. |
[in] | nvt | nvt to store. |
[in] | filename | Path to nvt to store. |
Definition at line 1372 of file kb.c.
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.
|
static |
Insert (append) a new entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | str | Item value. |
[in] | len | Value length. Used for blobs. |
Definition at line 1218 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Insert (append) a new unique entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | str | Item value. |
[in] | len | Value length. Used for blobs. |
Definition at line 1166 of file kb.c.
References get_redis_ctx(), and redis_kb.
|
static |
Execute a redis command and get a redis reply.
[in] | kbr | Subclass of struct kb to connect to. |
[in] | fmt | Formatted variable argument list with the cmd to be executed. |
Definition at line 723 of file kb.c.
References get_redis_ctx(), 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().
|
static |
Count all items stored under a given pattern.
[in] | kb | KB handle where to count the items. |
[in] | pattern | '*' pattern of the elements to count. |
Definition at line 1109 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Delete all entries under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
Definition at line 1139 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Delete all entries and release ownership on the namespace.
[in] | kb | KB handle to release. |
Definition at line 427 of file kb.c.
References kb_redis::rctx, redis_delete_all(), redis_kb, and redis_release_db().
Referenced by redis_new().
|
static |
Delete all the KB's content.
[in] | kbr | Subclass of struct kb. |
Definition at line 1555 of file kb.c.
References kb_redis::db, and redis_cmd().
Referenced by redis_delete(), and redis_flush_all().
|
static |
Connect to a Knowledge Base object with the given kb_index.
[in] | kb_path | Path to KB. |
[in] | kb_index | DB index |
Definition at line 498 of file kb.c.
References kb_redis::db, G_LOG_DOMAIN, kb_redis::kb, kb::kb_ops, KBRedisOperations, kb_redis::path, and kb_redis::rctx.
|
static |
Find an existing Knowledge Base object with key.
[in] | kb_path | Path to KB. |
[in] | key | Marker key to search for in KB objects. |
Definition at line 538 of file kb.c.
References kb_redis::db, fetch_max_db_index(), G_LOG_DOMAIN, GLOBAL_DBINDEX_NAME, kb_redis::kb, kb_item_get_str(), kb::kb_ops, KB_RETRY_DELAY, KBRedisOperations, kb_redis::max_db, kb_redis::path, and kb_redis::rctx.
|
static |
Flush all the KB's content. Delete all namespaces.
[in] | kb | KB handle. |
[in] | except | Don't flush DB with except key. |
Definition at line 1449 of file kb.c.
References kb_redis::db, G_LOG_DOMAIN, GLOBAL_DBINDEX_NAME, kb_item_get_str(), KB_RETRY_DELAY, kb_redis::max_db, kb_redis::path, kb_redis::rctx, redis_delete_all(), redis_kb, and redis_release_db().
Get all items stored under a given name.
[in] | kb | KB handle where to fetch the items. |
[in] | name | Name of the elements to retrieve. |
Definition at line 985 of file kb.c.
References kb_item::name, redis2kbitem(), redis_cmd(), and redis_kb.
|
static |
Get a single KB integer item.
[in] | kb | KB handle where to fetch the item. |
[in] | name | Name of the element to retrieve. |
Definition at line 882 of file kb.c.
References kb_item_free(), KB_TYPE_INT, kb_item::name, redis_get_single(), and kb_item::v_int.
|
static |
|
static |
Get field of a NVT.
[in] | kb | KB handle where to store the nvt. |
[in] | oid | OID of NVT to get from. |
[in] | position | Position of field to get. |
Definition at line 906 of file kb.c.
References NVT_TIMESTAMP_POS, redis_cmd(), and redis_kb.
Get a full NVT.
[in] | kb | KB handle where to store the nvt. |
[in] | oid | OID of NVT to get. |
Definition at line 936 of file kb.c.
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.
|
static |
Get all NVT OIDs.
[in] | kb | KB handle where to fetch the items. |
Definition at line 1074 of file kb.c.
References redis_cmd(), and redis_kb.
Get all items stored under a given pattern.
[in] | kb | KB handle where to fetch the items. |
[in] | pattern | '*' pattern of the elements to retrieve. |
Definition at line 1012 of file kb.c.
References get_redis_ctx(), kb_item::next, redis2kbitem(), redis_cmd(), and redis_kb.
|
static |
Get a single KB element.
[in] | kb | KB handle where to fetch the item. |
[in] | name | Name of the element to retrieve. |
[in] | type | Desired element type. |
Definition at line 774 of file kb.c.
References KB_TYPE_INT, redis2kbitem_single(), redis_cmd(), and redis_kb.
Referenced by redis_get_int(), and redis_get_str().
|
static |
Get a single KB string item.
[in] | kb | KB handle where to fetch the item. |
[in] | name | Name of the element to retrieve. |
Definition at line 807 of file kb.c.
References kb_item_free(), KB_TYPE_STR, kb_item::name, redis_get_single(), and kb_item::v_str.
|
static |
Reset connection to the KB. This is called after each fork() to make sure connections aren't shared between concurrent processes.
[in] | kb | KB handle. |
Definition at line 1427 of file kb.c.
References kb_redis::rctx, and redis_kb.
Referenced by redis_cmd().
|
static |
Initialize a new Knowledge Base object.
[in] | kb | Reference to a kb_t to initialize. |
[in] | kb_path | Path to KB. |
Definition at line 468 of file kb.c.
References G_LOG_DOMAIN, kb_redis::kb, kb::kb_ops, KBRedisOperations, kb_redis::path, redis_delete(), and redis_test_connection().
|
static |
Pops a single KB string item.
[in] | kb | KB handle where to fetch the item. |
[in] | name | Name of the key from where to retrieve. |
Definition at line 856 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Push a new entry under a given key.
[in] | kb | KB handle where to store the item. |
[in] | name | Key to push to. |
[in] | value | Value to push. |
Definition at line 832 of file kb.c.
References redis_cmd(), and redis_kb.
|
static |
Release DB.
[in] | kbr | Subclass of struct kb. |
Definition at line 305 of file kb.c.
References kb_redis::db, GLOBAL_DBINDEX_NAME, and kb_redis::rctx.
Referenced by redis_delete(), and redis_flush_all().
int redis_save | ( | kb_t | kb | ) |
Save all the elements from the KB.
[in] | kb | KB handle. |
Definition at line 1525 of file kb.c.
References kb_redis::db, redis_cmd(), and redis_kb.
|
static |
Set (replace) a new entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | val | Item value. |
Definition at line 1341 of file kb.c.
References get_redis_ctx(), and redis_kb.
|
static |
Set (replace) a new entry under a given name.
[in] | kb | KB handle where to store the item. |
[in] | name | Item name. |
[in] | val | Item value. |
[in] | len | Value length. Used for blobs. |
Definition at line 1246 of file kb.c.
References get_redis_ctx(), and redis_kb.
|
static |
Test redis connection.
[in] | kbr | Subclass of struct kb to test. |
Definition at line 388 of file kb.c.
References redis_cmd().
Referenced by redis_new().
|
static |
Select DB.
[in] | kbr | Subclass of struct kb where to save the db index. |
WARNING: do not call redis_cmd in here, since our context is not fully acquired yet!
Definition at line 254 of file kb.c.
References kb_redis::db, fetch_max_db_index(), kb_redis::max_db, kb_redis::rctx, and try_database_index().
Referenced by get_redis_ctx().
|
static |
Attempt to atomically acquire ownership of a database.
Definition at line 89 of file kb.c.
References kb_redis::db, GLOBAL_DBINDEX_NAME, and kb_redis::rctx.
Referenced by select_database().
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 1623 of file kb.c.
Referenced by kb_direct_conn(), kb_find(), and kb_new().
|
static |
Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based).
Definition at line 59 of file kb.c.
Referenced by redis_direct_conn(), redis_find(), and redis_new().