|
Greenbone Vulnerability Management Libraries 22.32.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>

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 "libgvm util" |
| GLib logging domain. | |
| #define | memdup g_memdup |
| #define | GLOBAL_DBINDEX_NAME "GVM.__GlobalDBIndex" |
| Name of the namespace usage bitmap in redis. | |
| #define | redis_kb(__kb) |
Functions | |
| static int | redis_delete_all (struct kb_redis *kbr) |
| Delete all the KB's content. | |
| 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. | |
| static int | redis_flush_all (kb_t kb, const char *except) |
| Flush all the KB's content. Delete all namespaces. | |
| static redisReply * | redis_cmd (struct kb_redis *kbr, const char *fmt,...) |
| Execute a redis command and get a redis reply. | |
| static int | try_database_index (struct kb_redis *kbr, int index) |
| Attempt to atomically acquire ownership of a database. | |
| static int | fetch_max_db_index (struct kb_redis *kbr) |
| Set the number of databases have been configured into kbr struct. | |
| static int | select_database (struct kb_redis *kbr) |
| Select DB. | |
| static int | redis_release_db (struct kb_redis *kbr) |
| Release DB. | |
| static char * | parse_port_of_addr (const char *addr, int tcp_indicator_len) |
| static redisContext * | connect_redis (const char *addr, int len) |
| static int | get_redis_ctx (struct kb_redis *kbr) |
| Get redis context if it is already connected or do a a connection. | |
| static int | redis_test_connection (struct kb_redis *kbr) |
| Test redis connection. | |
| static int | redis_delete (kb_t kb) |
| Delete all entries and release ownership on the namespace. | |
| static int | redis_get_kb_index (kb_t kb) |
| Return the kb index. | |
| static int | redis_memory_purge (kb_t kb) |
| Attempt to purge dirty pages. | |
| static int | redis_new (kb_t *kb, const char *kb_path) |
| Initialize a new Knowledge Base object. | |
| 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. | |
| static kb_t | redis_find (const char *kb_path, const char *key) |
| Find an existing Knowledge Base object with key. | |
| void | kb_item_free (struct kb_item *item) |
| Release a KB item (or a list). | |
| static struct kb_item * | redis2kbitem_single (const char *name, const redisReply *elt, int force_int) |
| Give a single KB item. | |
| static struct kb_item * | redis2kbitem (const char *name, const redisReply *rep) |
| Fetch a KB item or list from a redis Reply. | |
| static struct kb_item * | redis_get_single (kb_t kb, const char *name, enum kb_item_type type) |
| Get a single KB element. | |
| static char * | redis_get_str (kb_t kb, const char *name) |
| Get a single KB string item. | |
| static int | redis_push_str (kb_t kb, const char *name, const char *value) |
| Push a new entry under a given key. | |
| static char * | redis_pop_str (kb_t kb, const char *name) |
| Pops a single KB string item. | |
| static int | redis_get_int (kb_t kb, const char *name) |
| Get a single KB integer item. | |
| static char * | redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position) |
| Get field of a NVT. | |
| static nvti_t * | redis_get_nvt_all (kb_t kb, const char *oid) |
| Get a full NVT. | |
| static struct kb_item * | redis_get_all (kb_t kb, const char *name) |
| Get all items stored under a given name. | |
| static struct kb_item * | redis_get_pattern (kb_t kb, const char *pattern) |
| Get all items stored under a given pattern. | |
| static GSList * | redis_get_oids (kb_t kb) |
| Get all NVT OIDs. | |
| static size_t | redis_count (kb_t kb, const char *pattern) |
| Count all items stored under a given pattern. | |
| static int | redis_del_items (kb_t kb, const char *name) |
| Delete all entries under a given name. | |
| static int | redis_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 | redis_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 | redis_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 | redis_set_str (kb_t kb, const char *name, const char *val, size_t len) |
| Set (replace) a new entry under a given name. | |
| static int | redis_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire) |
| Insert (append) a new unique entry under a given name. | |
| static int | redis_add_int_unique (kb_t kb, const char *name, int val) |
| Insert (append) a new unique entry under a given name. | |
| static int | redis_add_int (kb_t kb, const char *name, int val) |
| Insert (append) a new entry under a given name. | |
| static int | redis_set_int (kb_t kb, const char *name, int val) |
| Set (replace) a new entry under a given name. | |
| static int | redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename) |
| Insert a new nvt. | |
| static int | redis_save (kb_t kb) |
| Save all the elements from the KB. | |
Variables | |
| static const struct kb_operations | KBRedisOperations |
| Default KB operations. | |
| const struct kb_operations * | KBDefaultOperations = &KBRedisOperations |
| Default KB operations. No selection mechanism is provided yet since there's only one implementation (redis-based). | |
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" |
Name of the namespace usage bitmap in redis.
Definition at line 45 of file kb.c.
Referenced by redis_find(), redis_flush_all(), redis_release_db(), and try_database_index().
| #define memdup g_memdup |
Definition at line 32 of file kb.c.
Referenced by redis2kbitem_single().
| #define redis_kb | ( | __kb | ) |
Definition at line 62 of file kb.c.
Referenced by redis_add_int(), redis_add_int_unique(), redis_add_int_unique_volatile(), redis_add_nvt(), redis_add_str(), redis_add_str_unique(), redis_add_str_unique_volatile(), redis_count(), redis_del_items(), redis_delete(), redis_flush_all(), redis_get_all(), redis_get_nvt(), redis_get_nvt_all(), redis_get_oids(), redis_get_pattern(), redis_get_single(), redis_lnk_reset(), redis_memory_purge(), redis_pop_str(), redis_push_str(), redis_save(), redis_set_int(), and redis_set_str().
|
static |
Definition at line 264 of file kb.c.
References parse_port_of_addr().
Referenced by get_redis_ctx(), redis_direct_conn(), redis_find(), and redis_flush_all().


|
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 109 of file kb.c.
References G_LOG_DOMAIN, kb_redis::max_db, and kb_redis::rctx.
Referenced by redis_find(), and select_database().

|
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 314 of file kb.c.
References connect_redis(), kb_redis::db, G_LOG_DOMAIN, kb_redis::path, kb_redis::rctx, and select_database().
Referenced by redis_add_int_unique(), redis_add_int_unique_volatile(), redis_add_str_unique(), redis_add_str_unique_volatile(), redis_cmd(), redis_get_pattern(), redis_new(), 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 642 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().

|
inlinestatic |
Definition at line 250 of file kb.c.
Referenced by connect_redis(), Ensure(), Ensure(), Ensure(), and main().

|
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 711 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 666 of file kb.c.
References KB_TYPE_INT, KB_TYPE_STR, kb_item::len, memdup, 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 1546 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 1505 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, 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. |
| [in] | expire | Item expire. |
Definition at line 1452 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, 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 1607 of file kb.c.
References nvti_category(), nvti_dependencies(), nvti_excluded_keys(), nvti_family(), nvti_mandatory_keys(), nvti_name(), nvti_oid(), nvti_pref(), nvti_pref_len(), nvti_refs(), nvti_required_keys(), nvti_required_ports(), nvti_required_udp_ports(), nvti_tag(), nvtpref_default(), nvtpref_id(), nvtpref_name(), nvtpref_type(), redis_cmd(), and redis_kb.

|
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 1381 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. |
| [in] | pos | Which position the value is appended to. 0 for right, 1 for left position in the list. |
Definition at line 1323 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, and redis_kb.

|
static |
Insert (append) a new unique and volatile entry under a given name.
| [in] | kb | KB handle where to store the item. |
| [in] | name | Item name. |
| [in] | str | Item value. |
| [in] | expire | Item expire. |
| [in] | len | Value length. Used for blobs. |
| [in] | pos | Which position the value is appended to. 0 for right, 1 for left position in the list. |
Definition at line 1230 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, 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 764 of file kb.c.
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_memory_purge(), 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 1166 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 1198 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 394 of file kb.c.
References kb_redis::path, 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 1809 of file kb.c.
References kb_redis::db, and redis_cmd().
Referenced by redis_delete(), redis_flush_all(), and redis_new().


|
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 516 of file kb.c.
References connect_redis(), 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 564 of file kb.c.
References connect_redis(), 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.

|
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 1699 of file kb.c.
References connect_redis(), 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().

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 1038 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 930 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 956 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 988 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_XREFS_POS, nvti_add_refs(), nvti_new(), nvti_set_category(), 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(), redis_cmd(), and redis_kb.

|
static |
Get all NVT OIDs.
| [in] | kb | KB handle where to fetch the items. |
Definition at line 1131 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 1067 of file kb.c.
References get_redis_ctx(), kb_item::next, kb_redis::rctx, 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 812 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 847 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 1675 of file kb.c.
References kb_redis::rctx, and redis_kb.
Referenced by redis_cmd().

|
static |
Attempt to purge dirty pages.
Attempt to purge dirty pages so these can be reclaimed by the allocator. This command only works when using jemalloc as an allocator, and evaluates to a benign NOOP for all others. Command is applied to complete redis instance and not only single db.
| [in] | kb | KB handle where to run the command. |
Definition at line 444 of file kb.c.
References redis_cmd(), and redis_kb.
Referenced by redis_new().


|
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, get_redis_ctx(), kb_redis::kb, kb::kb_ops, KBRedisOperations, kb_redis::path, redis_delete(), redis_delete_all(), redis_memory_purge(), 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 903 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 874 of file kb.c.
References redis_cmd(), and redis_kb.

|
static |
Release DB.
| [in] | kbr | Subclass of struct kb. |
Definition at line 216 of file kb.c.
References kb_redis::db, GLOBAL_DBINDEX_NAME, and kb_redis::rctx.
Referenced by redis_delete(), and redis_flush_all().

|
static |
Save all the elements from the KB.
| [in] | kb | KB handle. |
Definition at line 1777 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 1570 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, 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 1411 of file kb.c.
References get_redis_ctx(), kb_redis::rctx, and redis_kb.

|
static |
Test redis connection.
| [in] | kbr | Subclass of struct kb to test. |
Definition at line 353 of file kb.c.
References redis_cmd().
Referenced by redis_new().


|
static |
Select DB.
WARNING: do not call redis_cmd in here, since our context is not fully acquired yet!
| [in] | kbr | Subclass of struct kb where to save the db index. |
Definition at line 164 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 78 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 1880 of file kb.c.
Referenced by kb_add_int_unique_volatile(), kb_add_str_unique_volatile(), 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 47 of file kb.c.
Referenced by redis_direct_conn(), redis_find(), and redis_new().