"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "util/kb.c" between
gvm-libs-11.0.0.tar.gz and gvm-libs-11.0.1.tar.gz

About: GVM Libraries for the Greenbone Vulnerability Management (GVM-10) framework (derived from the former openvas-libraries).

kb.c  (gvm-libs-11.0.0):kb.c  (gvm-libs-11.0.1)
skipping to change at line 79 skipping to change at line 79
static int static int
redis_delete_all (struct kb_redis *); redis_delete_all (struct kb_redis *);
static int redis_lnk_reset (kb_t); static int redis_lnk_reset (kb_t);
static int static int
redis_flush_all (kb_t, const char *); redis_flush_all (kb_t, const char *);
static redisReply * static redisReply *
redis_cmd (struct kb_redis *kbr, const char *fmt, ...); redis_cmd (struct kb_redis *kbr, const char *fmt, ...);
/** /**
* @brief Attempt to atomically acquire ownership of a database. * @brief Attempt to atomically acquire ownership of a database.
*
* @return 0 on success, negative integer otherwise. * @return 0 on success, negative integer otherwise.
*/ */
static int static int
try_database_index (struct kb_redis *kbr, int index) try_database_index (struct kb_redis *kbr, int index)
{ {
redisContext *ctx = kbr->rctx; redisContext *ctx = kbr->rctx;
redisReply *rep; redisReply *rep;
int rc = 0; int rc = 0;
rep = redisCommand (ctx, "HSETNX %s %d 1", GLOBAL_DBINDEX_NAME, index); rep = redisCommand (ctx, "HSETNX %s %d 1", GLOBAL_DBINDEX_NAME, index);
skipping to change at line 107 skipping to change at line 108
kbr->db = index; kbr->db = index;
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Set the number of databases have been configured * @brief Set the number of databases have been configured
* into kbr struct. * into kbr struct.
*
* @param[in] kbr Subclass of struct kb where to save the max db index founded. * @param[in] kbr Subclass of struct kb where to save the max db index founded.
*
* @return 0 on success, -1 on error. * @return 0 on success, -1 on error.
*/ */
static int static int
fetch_max_db_index (struct kb_redis *kbr) fetch_max_db_index (struct kb_redis *kbr)
{ {
int rc = 0; int rc = 0;
redisContext *ctx = kbr->rctx; redisContext *ctx = kbr->rctx;
redisReply *rep = NULL; redisReply *rep = NULL;
rep = redisCommand (ctx, "CONFIG GET databases"); rep = redisCommand (ctx, "CONFIG GET databases");
skipping to change at line 157 skipping to change at line 160
err_cleanup: err_cleanup:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Select DB. * @brief Select DB.
* @param[in] kbr Subclass of struct kb where to save the db index.
* @return 0 on success, -1 on error.
* *
* WARNING: do not call redis_cmd in here, since our context is not fully * WARNING: do not call redis_cmd in here, since our context is not fully
* acquired yet! * acquired yet!
*
* @param[in] kbr Subclass of struct kb where to save the db index.
*
* @return 0 on success, -1 on error.
*/ */
static int static int
select_database (struct kb_redis *kbr) select_database (struct kb_redis *kbr)
{ {
int rc; int rc;
redisContext *ctx = kbr->rctx; redisContext *ctx = kbr->rctx;
redisReply *rep = NULL; redisReply *rep = NULL;
if (kbr->db == 0) if (kbr->db == 0)
{ {
skipping to change at line 210 skipping to change at line 215
err_cleanup: err_cleanup:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Release DB. * @brief Release DB.
*
* @param[in] kbr Subclass of struct kb. * @param[in] kbr Subclass of struct kb.
* *
* @return 0 on success, -1 on error. * @return 0 on success, -1 on error.
*/ */
static int static int
redis_release_db (struct kb_redis *kbr) redis_release_db (struct kb_redis *kbr)
{ {
int rc; int rc;
redisContext *ctx = kbr->rctx; redisContext *ctx = kbr->rctx;
redisReply *rep; redisReply *rep;
skipping to change at line 251 skipping to change at line 257
err_cleanup: err_cleanup:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Get redis context if it is already connected or do a * @brief Get redis context if it is already connected or do a
* a connection. * a connection.
*
* @param[in] kbr Subclass of struct kb where to fetch the context. * @param[in] kbr Subclass of struct kb where to fetch the context.
* or where it is saved in case of a new connection. * or where it is saved in case of a new connection.
*
* @return 0 on success, -1 on connection error, -2 on unavailable DB slot. * @return 0 on success, -1 on connection error, -2 on unavailable DB slot.
*/ */
static int static int
get_redis_ctx (struct kb_redis *kbr) get_redis_ctx (struct kb_redis *kbr)
{ {
int rc; int rc;
if (kbr->rctx != NULL) if (kbr->rctx != NULL)
return 0; return 0;
skipping to change at line 289 skipping to change at line 297
kbr->rctx = NULL; kbr->rctx = NULL;
return -2; return -2;
} }
g_debug ("%s: connected to redis://%s/%d", __func__, kbr->path, kbr->db); g_debug ("%s: connected to redis://%s/%d", __func__, kbr->path, kbr->db);
return 0; return 0;
} }
/** /**
* @brief Test redis connection. * @brief Test redis connection.
*
* @param[in] kbr Subclass of struct kb to test. * @param[in] kbr Subclass of struct kb to test.
*
* @return 0 on success, negative integer on error. * @return 0 on success, negative integer on error.
*/ */
static int static int
redis_test_connection (struct kb_redis *kbr) redis_test_connection (struct kb_redis *kbr)
{ {
int rc = 0; int rc = 0;
redisReply *rep; redisReply *rep;
rep = redis_cmd (kbr, "PING"); rep = redis_cmd (kbr, "PING");
if (rep == NULL) if (rep == NULL)
skipping to change at line 328 skipping to change at line 338
out: out:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Delete all entries and release ownership on the namespace. * @brief Delete all entries and release ownership on the namespace.
*
* @param[in] kb KB handle to release. * @param[in] kb KB handle to release.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_delete (kb_t kb) redis_delete (kb_t kb)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
kbr = redis_kb (kb); kbr = redis_kb (kb);
redis_delete_all (kbr); redis_delete_all (kbr);
skipping to change at line 353 skipping to change at line 365
redisFree (kbr->rctx); redisFree (kbr->rctx);
kbr->rctx = NULL; kbr->rctx = NULL;
} }
g_free (kb); g_free (kb);
return 0; return 0;
} }
/** /**
* @brief Return the kb index * @brief Return the kb index
*
* @param[in] kb KB handle. * @param[in] kb KB handle.
*
* @return kb_index on success, null on error. * @return kb_index on success, null on error.
*/ */
static int static int
redis_get_kb_index (kb_t kb) redis_get_kb_index (kb_t kb)
{ {
int i; int i;
i = ((struct kb_redis *) kb)->db; i = ((struct kb_redis *) kb)->db;
if (i > 0) if (i > 0)
return i; return i;
return -1; return -1;
} }
/** /**
* @brief Initialize a new Knowledge Base object. * @brief Initialize a new Knowledge Base object.
*
* @param[in] kb Reference to a kb_t to initialize. * @param[in] kb Reference to a kb_t to initialize.
* @param[in] kb_path Path to KB. * @param[in] kb_path Path to KB.
*
* @return 0 on success, -1 on connection error, -2 when no DB is available. * @return 0 on success, -1 on connection error, -2 when no DB is available.
*/ */
static int static int
redis_new (kb_t *kb, const char *kb_path) redis_new (kb_t *kb, const char *kb_path)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
int rc = 0; int rc = 0;
kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1); kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
kbr->kb.kb_ops = &KBRedisOperations; kbr->kb.kb_ops = &KBRedisOperations;
skipping to change at line 399 skipping to change at line 415
kbr = NULL; kbr = NULL;
rc = -1; rc = -1;
} }
*kb = (kb_t) kbr; *kb = (kb_t) kbr;
return rc; return rc;
} }
/** /**
* @brief Connect to a Knowledge Base object with the given kb_index. * @brief Connect to a Knowledge Base object with the given kb_index.
*
* @param[in] kb_path Path to KB. * @param[in] kb_path Path to KB.
* @param[in] kb_index DB index * @param[in] kb_index DB index
*
* @return Knowledge Base object, NULL otherwise. * @return Knowledge Base object, NULL otherwise.
*/ */
static kb_t static kb_t
redis_direct_conn (const char *kb_path, const int kb_index) redis_direct_conn (const char *kb_path, const int kb_index)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1); kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
kbr->kb.kb_ops = &KBRedisOperations; kbr->kb.kb_ops = &KBRedisOperations;
skipping to change at line 439 skipping to change at line 457
redisFree (kbr->rctx); redisFree (kbr->rctx);
kbr->rctx = NULL; kbr->rctx = NULL;
return NULL; return NULL;
} }
freeReplyObject (rep); freeReplyObject (rep);
return (kb_t) kbr; return (kb_t) kbr;
} }
/** /**
* @brief Find an existing Knowledge Base object with key. * @brief Find an existing Knowledge Base object with key.
*
* @param[in] kb_path Path to KB. * @param[in] kb_path Path to KB.
* @param[in] key Marker key to search for in KB objects. * @param[in] key Marker key to search for in KB objects.
*
* @return Knowledge Base object, NULL otherwise. * @return Knowledge Base object, NULL otherwise.
*/ */
static kb_t static kb_t
redis_find (const char *kb_path, const char *key) redis_find (const char *kb_path, const char *key)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
unsigned int i = 1; unsigned int i = 1;
kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1); kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
kbr->kb.kb_ops = &KBRedisOperations; kbr->kb.kb_ops = &KBRedisOperations;
skipping to change at line 513 skipping to change at line 533
i++; i++;
} }
while (i < kbr->max_db); while (i < kbr->max_db);
g_free (kbr); g_free (kbr);
return NULL; return NULL;
} }
/** /**
* @brief Release a KB item (or a list). * @brief Release a KB item (or a list).
*
* @param[in] item Item or list to be release * @param[in] item Item or list to be release
*/ */
void void
kb_item_free (struct kb_item *item) kb_item_free (struct kb_item *item)
{ {
while (item != NULL) while (item != NULL)
{ {
struct kb_item *next; struct kb_item *next;
next = item->next; next = item->next;
if (item->type == KB_TYPE_STR && item->v_str != NULL) if (item->type == KB_TYPE_STR && item->v_str != NULL)
g_free (item->v_str); g_free (item->v_str);
g_free (item); g_free (item);
item = next; item = next;
} }
} }
/** /**
* @brief Give a single KB item. * @brief Give a single KB item.
*
* @param[in] name Name of the item. * @param[in] name Name of the item.
* @param[in] elt A redisReply element where to fetch the item. * @param[in] elt A redisReply element where to fetch the item.
* @param[in] force_int To force string to integer conversion. * @param[in] force_int To force string to integer conversion.
*
* @return Single retrieve kb_item on success, NULL otherwise. * @return Single retrieve kb_item on success, NULL otherwise.
*/ */
static struct kb_item * static struct kb_item *
redis2kbitem_single (const char *name, const redisReply *elt, int force_int) redis2kbitem_single (const char *name, const redisReply *elt, int force_int)
{ {
struct kb_item *item; struct kb_item *item;
size_t namelen; size_t namelen;
if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER) if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER)
return NULL; return NULL;
skipping to change at line 575 skipping to change at line 598
item->next = NULL; item->next = NULL;
item->namelen = namelen; item->namelen = namelen;
strncpy (item->name, name, namelen); strncpy (item->name, name, namelen);
return item; return item;
} }
/** /**
* @brief Fetch a KB item or list from a redis Reply. * @brief Fetch a KB item or list from a redis Reply.
*
* @param[in] name Name of the item. * @param[in] name Name of the item.
* @param[in] rep A redisReply element where to fetch the item. * @param[in] rep A redisReply element where to fetch the item.
*
* @return kb_item or list on success, NULL otherwise. * @return kb_item or list on success, NULL otherwise.
*/ */
static struct kb_item * static struct kb_item *
redis2kbitem (const char *name, const redisReply *rep) redis2kbitem (const char *name, const redisReply *rep)
{ {
struct kb_item *kbi; struct kb_item *kbi;
kbi = NULL; kbi = NULL;
switch (rep->type) switch (rep->type)
skipping to change at line 626 skipping to change at line 651
case REDIS_REPLY_ERROR: case REDIS_REPLY_ERROR:
default: default:
break; break;
} }
return kbi; return kbi;
} }
/** /**
* @brief Execute a redis command and get a redis reply. * @brief Execute a redis command and get a redis reply.
*
* @param[in] kbr Subclass of struct kb to connect to. * @param[in] kbr Subclass of struct kb to connect to.
* @param[in] fmt Formatted variable argument list with the cmd to be executed. * @param[in] fmt Formatted variable argument list with the cmd to be executed.
*
* @return Redis reply on success, NULL otherwise. * @return Redis reply on success, NULL otherwise.
*/ */
static redisReply * static redisReply *
redis_cmd (struct kb_redis *kbr, const char *fmt, ...) redis_cmd (struct kb_redis *kbr, const char *fmt, ...)
{ {
redisReply *rep; redisReply *rep;
va_list ap, aq; va_list ap, aq;
int retry = 0; int retry = 0;
va_start (ap, fmt); va_start (ap, fmt);
skipping to change at line 670 skipping to change at line 697
} }
while (retry); while (retry);
va_end (ap); va_end (ap);
return rep; return rep;
} }
/** /**
* @brief Get a single KB element. * @brief Get a single KB element.
*
* @param[in] kb KB handle where to fetch the item. * @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve. * @param[in] name Name of the element to retrieve.
* @param[in] type Desired element type. * @param[in] type Desired element type.
*
* @return A struct kb_item to be freed with kb_item_free() or NULL if no * @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error. * element was found or on error.
*/ */
static struct kb_item * static struct kb_item *
redis_get_single (kb_t kb, const char *name, enum kb_item_type type) redis_get_single (kb_t kb, const char *name, enum kb_item_type type)
{ {
struct kb_item *kbi; struct kb_item *kbi;
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
skipping to change at line 704 skipping to change at line 733
out: out:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return kbi; return kbi;
} }
/** /**
* @brief Get a single KB string item. * @brief Get a single KB string item.
*
* @param[in] kb KB handle where to fetch the item. * @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve. * @param[in] name Name of the element to retrieve.
*
* @return A struct kb_item to be freed with kb_item_free() or NULL if no * @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error. * element was found or on error.
*/ */
static char * static char *
redis_get_str (kb_t kb, const char *name) redis_get_str (kb_t kb, const char *name)
{ {
struct kb_item *kbi; struct kb_item *kbi;
kbi = redis_get_single (kb, name, KB_TYPE_STR); kbi = redis_get_single (kb, name, KB_TYPE_STR);
if (kbi != NULL) if (kbi != NULL)
skipping to change at line 729 skipping to change at line 760
res = kbi->v_str; res = kbi->v_str;
kbi->v_str = NULL; kbi->v_str = NULL;
kb_item_free (kbi); kb_item_free (kbi);
return res; return res;
} }
return NULL; return NULL;
} }
/** /**
* @brief Push a new entry under a given key. * @brief Push a new entry under a given key.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Key to push to. * @param[in] name Key to push to.
* @param[in] value Value to push. * @param[in] value Value to push.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_push_str (kb_t kb, const char *name, const char *value) redis_push_str (kb_t kb, const char *name, const char *value)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep = NULL; redisReply *rep = NULL;
int rc = 0; int rc = 0;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 754 skipping to change at line 787
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Pops a single KB string item. * @brief Pops a single KB string item.
*
* @param[in] kb KB handle where to fetch the item. * @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the key from where to retrieve. * @param[in] name Name of the key from where to retrieve.
*
* @return A string to be freed or NULL if list is empty or on error. * @return A string to be freed or NULL if list is empty or on error.
*/ */
static char * static char *
redis_pop_str (kb_t kb, const char *name) redis_pop_str (kb_t kb, const char *name)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
char *value = NULL; char *value = NULL;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 779 skipping to change at line 814
if (rep->type == REDIS_REPLY_STRING) if (rep->type == REDIS_REPLY_STRING)
value = g_strdup (rep->str); value = g_strdup (rep->str);
freeReplyObject (rep); freeReplyObject (rep);
return value; return value;
} }
/** /**
* @brief Get a single KB integer item. * @brief Get a single KB integer item.
*
* @param[in] kb KB handle where to fetch the item. * @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve. * @param[in] name Name of the element to retrieve.
*
* @return A struct kb_item to be freed with kb_item_free() or NULL if no * @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error. * element was found or on error.
*/ */
static int static int
redis_get_int (kb_t kb, const char *name) redis_get_int (kb_t kb, const char *name)
{ {
struct kb_item *kbi; struct kb_item *kbi;
kbi = redis_get_single (kb, name, KB_TYPE_INT); kbi = redis_get_single (kb, name, KB_TYPE_INT);
if (kbi != NULL) if (kbi != NULL)
skipping to change at line 803 skipping to change at line 840
res = kbi->v_int; res = kbi->v_int;
kb_item_free (kbi); kb_item_free (kbi);
return res; return res;
} }
return -1; return -1;
} }
/** /**
* @brief Get field of a NVT. * @brief Get field of a NVT.
*
* @param[in] kb KB handle where to store the nvt. * @param[in] kb KB handle where to store the nvt.
* @param[in] oid OID of NVT to get from. * @param[in] oid OID of NVT to get from.
* @param[in] position Position of field to get. * @param[in] position Position of field to get.
*
* @return Value of field, NULL otherwise. * @return Value of field, NULL otherwise.
*/ */
static char * static char *
redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position) redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
char *res = NULL; char *res = NULL;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 834 skipping to change at line 873
res = g_strdup_printf ("%lld", rep->integer); res = g_strdup_printf ("%lld", rep->integer);
else if (rep->type == REDIS_REPLY_STRING) else if (rep->type == REDIS_REPLY_STRING)
res = g_strdup (rep->str); res = g_strdup (rep->str);
freeReplyObject (rep); freeReplyObject (rep);
return res; return res;
} }
/** /**
* @brief Get a full NVT. * @brief Get a full NVT.
*
* @param[in] kb KB handle where to store the nvt. * @param[in] kb KB handle where to store the nvt.
* @param[in] oid OID of NVT to get. * @param[in] oid OID of NVT to get.
*
* @return nvti_t of NVT, NULL otherwise. * @return nvti_t of NVT, NULL otherwise.
*/ */
static nvti_t * static nvti_t *
redis_get_nvt_all (kb_t kb, const char *oid) redis_get_nvt_all (kb_t kb, const char *oid)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
kbr = redis_kb (kb); kbr = redis_kb (kb);
rep = rep =
skipping to change at line 882 skipping to change at line 923
nvti_set_family (nvti, rep->element[NVT_FAMILY_POS]->str); nvti_set_family (nvti, rep->element[NVT_FAMILY_POS]->str);
nvti_set_name (nvti, rep->element[NVT_NAME_POS]->str); nvti_set_name (nvti, rep->element[NVT_NAME_POS]->str);
freeReplyObject (rep); freeReplyObject (rep);
return nvti; return nvti;
} }
} }
/** /**
* @brief Get all items stored under a given name. * @brief Get all items stored under a given name.
*
* @param[in] kb KB handle where to fetch the items. * @param[in] kb KB handle where to fetch the items.
* @param[in] name Name of the elements to retrieve. * @param[in] name Name of the elements to retrieve.
*
* @return Linked struct kb_item instances to be freed with kb_item_free() or * @return Linked struct kb_item instances to be freed with kb_item_free() or
* NULL if no element was found or on error. * NULL if no element was found or on error.
*/ */
static struct kb_item * static struct kb_item *
redis_get_all (kb_t kb, const char *name) redis_get_all (kb_t kb, const char *name)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
struct kb_item *kbi; struct kb_item *kbi;
redisReply *rep; redisReply *rep;
skipping to change at line 909 skipping to change at line 952
kbi = redis2kbitem (name, rep); kbi = redis2kbitem (name, rep);
freeReplyObject (rep); freeReplyObject (rep);
return kbi; return kbi;
} }
/** /**
* @brief Get all items stored under a given pattern. * @brief Get all items stored under a given pattern.
*
* @param[in] kb KB handle where to fetch the items. * @param[in] kb KB handle where to fetch the items.
* @param[in] pattern '*' pattern of the elements to retrieve. * @param[in] pattern '*' pattern of the elements to retrieve.
*
* @return Linked struct kb_item instances to be freed with kb_item_free() or * @return Linked struct kb_item instances to be freed with kb_item_free() or
* NULL if no element was found or on error. * NULL if no element was found or on error.
*/ */
static struct kb_item * static struct kb_item *
redis_get_pattern (kb_t kb, const char *pattern) redis_get_pattern (kb_t kb, const char *pattern)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
struct kb_item *kbi = NULL; struct kb_item *kbi = NULL;
redisReply *rep; redisReply *rep;
unsigned int i; unsigned int i;
skipping to change at line 973 skipping to change at line 1018
kbi = tmp; kbi = tmp;
freeReplyObject (rep_range); freeReplyObject (rep_range);
} }
freeReplyObject (rep); freeReplyObject (rep);
return kbi; return kbi;
} }
/** /**
* @brief Get all NVT OIDs. * @brief Get all NVT OIDs.
*
* @param[in] kb KB handle where to fetch the items. * @param[in] kb KB handle where to fetch the items.
*
* @return Linked list of all OIDs or NULL. * @return Linked list of all OIDs or NULL.
*/ */
static GSList * static GSList *
redis_get_oids (kb_t kb) redis_get_oids (kb_t kb)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
GSList *list = NULL; GSList *list = NULL;
size_t i; size_t i;
skipping to change at line 1037 skipping to change at line 1084
return 0; return 0;
} }
count = rep->elements; count = rep->elements;
freeReplyObject (rep); freeReplyObject (rep);
return count; return count;
} }
/** /**
* @brief Delete all entries under a given name. * @brief Delete all entries under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_del_items (kb_t kb, const char *name) redis_del_items (kb_t kb, const char *name)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
int rc = 0; int rc = 0;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 1062 skipping to change at line 1111
rc = -1; rc = -1;
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Insert (append) a new unique entry under a given name. * @brief Insert (append) a new unique entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] str Item value. * @param[in] str Item value.
* @param[in] len Value length. Used for blobs. * @param[in] len Value length. Used for blobs.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len) redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep = NULL; redisReply *rep = NULL;
int rc = 0; int rc = 0;
redisContext *ctx; redisContext *ctx;
skipping to change at line 1116 skipping to change at line 1167
rc = -1; rc = -1;
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Insert (append) a new entry under a given name. * @brief Insert (append) a new entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] str Item value. * @param[in] str Item value.
* @param[in] len Value length. Used for blobs. * @param[in] len Value length. Used for blobs.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_add_str (kb_t kb, const char *name, const char *str, size_t len) redis_add_str (kb_t kb, const char *name, const char *str, size_t len)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
int rc = 0; int rc = 0;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 1144 skipping to change at line 1197
if (!rep || rep->type == REDIS_REPLY_ERROR) if (!rep || rep->type == REDIS_REPLY_ERROR)
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Set (replace) a new entry under a given name. * @brief Set (replace) a new entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] val Item value. * @param[in] val Item value.
* @param[in] len Value length. Used for blobs. * @param[in] len Value length. Used for blobs.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_set_str (kb_t kb, const char *name, const char *val, size_t len) redis_set_str (kb_t kb, const char *name, const char *val, size_t len)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep = NULL; redisReply *rep = NULL;
redisContext *ctx; redisContext *ctx;
int rc = 0, i = 4; int rc = 0, i = 4;
skipping to change at line 1183 skipping to change at line 1238
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
} }
return rc; return rc;
} }
/** /**
* @brief Insert (append) a new unique entry under a given name. * @brief Insert (append) a new unique entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] val Item value. * @param[in] val Item value.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_add_int_unique (kb_t kb, const char *name, int val) redis_add_int_unique (kb_t kb, const char *name, int val)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep; redisReply *rep;
int rc = 0; int rc = 0;
redisContext *ctx; redisContext *ctx;
skipping to change at line 1222 skipping to change at line 1279
out: out:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Insert (append) a new entry under a given name. * @brief Insert (append) a new entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] val Item value. * @param[in] val Item value.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_add_int (kb_t kb, const char *name, int val) redis_add_int (kb_t kb, const char *name, int val)
{ {
redisReply *rep; redisReply *rep;
int rc = 0; int rc = 0;
rep = redis_cmd (redis_kb (kb), "RPUSH %s %d", name, val); rep = redis_cmd (redis_kb (kb), "RPUSH %s %d", name, val);
if (!rep || rep->type == REDIS_REPLY_ERROR) if (!rep || rep->type == REDIS_REPLY_ERROR)
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Set (replace) a new entry under a given name. * @brief Set (replace) a new entry under a given name.
*
* @param[in] kb KB handle where to store the item. * @param[in] kb KB handle where to store the item.
* @param[in] name Item name. * @param[in] name Item name.
* @param[in] val Item value. * @param[in] val Item value.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_set_int (kb_t kb, const char *name, int val) redis_set_int (kb_t kb, const char *name, int val)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep = NULL; redisReply *rep = NULL;
redisContext *ctx; redisContext *ctx;
int rc = 0, i = 4; int rc = 0, i = 4;
skipping to change at line 1279 skipping to change at line 1340
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
} }
return rc; return rc;
} }
/** /**
* @brief Insert a new nvt. * @brief Insert a new nvt.
*
* @param[in] kb KB handle where to store the nvt. * @param[in] kb KB handle where to store the nvt.
* @param[in] nvt nvt to store. * @param[in] nvt nvt to store.
* @param[in] filename Path to nvt to store. * @param[in] filename Path to nvt to store.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename) redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
redisReply *rep = NULL; redisReply *rep = NULL;
int rc = 0; int rc = 0;
unsigned int i; unsigned int i;
gchar *cves, *bids, *xrefs; gchar *cves, *bids, *xrefs;
skipping to change at line 1343 skipping to change at line 1406
if (!rep || rep->type == REDIS_REPLY_ERROR) if (!rep || rep->type == REDIS_REPLY_ERROR)
rc = -1; rc = -1;
if (rep) if (rep)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Reset connection to the KB. This is called after each fork() to make * @brief Reset connection to the KB. This is called after each fork() to make
* sure connections aren't shared between concurrent processes. * sure connections aren't shared between concurrent processes.
*
* @param[in] kb KB handle. * @param[in] kb KB handle.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_lnk_reset (kb_t kb) redis_lnk_reset (kb_t kb)
{ {
struct kb_redis *kbr; struct kb_redis *kbr;
kbr = redis_kb (kb); kbr = redis_kb (kb);
if (kbr->rctx != NULL) if (kbr->rctx != NULL)
{ {
redisFree (kbr->rctx); redisFree (kbr->rctx);
kbr->rctx = NULL; kbr->rctx = NULL;
} }
return 0; return 0;
} }
/** /**
* @brief Flush all the KB's content. Delete all namespaces. * @brief Flush all the KB's content. Delete all namespaces.
*
* @param[in] kb KB handle. * @param[in] kb KB handle.
* @param[in] except Don't flush DB with except key. * @param[in] except Don't flush DB with except key.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
static int static int
redis_flush_all (kb_t kb, const char *except) redis_flush_all (kb_t kb, const char *except)
{ {
unsigned int i = 1; unsigned int i = 1;
struct kb_redis *kbr; struct kb_redis *kbr;
kbr = redis_kb (kb); kbr = redis_kb (kb);
if (kbr->rctx) if (kbr->rctx)
skipping to change at line 1440 skipping to change at line 1507
i++; i++;
} }
while (i < kbr->max_db); while (i < kbr->max_db);
g_free (kb); g_free (kb);
return 0; return 0;
} }
/** /**
* @brief Save all the elements from the KB. * @brief Save all the elements from the KB.
*
* @param[in] kb KB handle. * @param[in] kb KB handle.
*
* @return 0 on success, -1 on error. * @return 0 on success, -1 on error.
*/ */
int int
redis_save (kb_t kb) redis_save (kb_t kb)
{ {
int rc; int rc;
redisReply *rep; redisReply *rep;
struct kb_redis *kbr; struct kb_redis *kbr;
kbr = redis_kb (kb); kbr = redis_kb (kb);
skipping to change at line 1470 skipping to change at line 1539
err_cleanup: err_cleanup:
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Delete all the KB's content. * @brief Delete all the KB's content.
*
* @param[in] kbr Subclass of struct kb. * @param[in] kbr Subclass of struct kb.
*
* @return 0 on success, non-null on error. * @return 0 on success, non-null on error.
*/ */
int int
redis_delete_all (struct kb_redis *kbr) redis_delete_all (struct kb_redis *kbr)
{ {
int rc; int rc;
redisReply *rep; redisReply *rep;
struct sigaction new_action, original_action; struct sigaction new_action, original_action;
/* Ignore SIGPIPE, in case of a lost connection. */ /* Ignore SIGPIPE, in case of a lost connection. */
skipping to change at line 1509 skipping to change at line 1580
if (sigaction (SIGPIPE, &original_action, NULL)) if (sigaction (SIGPIPE, &original_action, NULL))
return -1; return -1;
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/** /**
* @brief Default KB operations. * @brief Default KB operations.
* No selection mechanism is provided yet since there's only one *
* implementation (redis-based). * No selection mechanism is provided yet since there's only one
* implementation (redis-based).
*/ */
static const struct kb_operations KBRedisOperations = { static const struct kb_operations KBRedisOperations = {
.kb_new = redis_new, .kb_new = redis_new,
.kb_find = redis_find, .kb_find = redis_find,
.kb_delete = redis_delete, .kb_delete = redis_delete,
.kb_get_single = redis_get_single, .kb_get_single = redis_get_single,
.kb_get_str = redis_get_str, .kb_get_str = redis_get_str,
.kb_get_int = redis_get_int, .kb_get_int = redis_get_int,
.kb_get_nvt = redis_get_nvt, .kb_get_nvt = redis_get_nvt,
.kb_get_nvt_all = redis_get_nvt_all, .kb_get_nvt_all = redis_get_nvt_all,
 End of changes. 72 change blocks. 
4 lines changed or deleted 76 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)