"Fossies" - the Fresh Open Source Software Archive  

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

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

kb.c  (gvm-libs-10.0.1):kb.c  (gvm-libs-11.0.0)
skipping to change at line 104 skipping to change at line 104
else if (rep->integer == 0) else if (rep->integer == 0)
rc = -EALREADY; rc = -EALREADY;
else else
kbr->db = index; kbr->db = index;
freeReplyObject (rep); freeReplyObject (rep);
return rc; return rc;
} }
/* Redis 2.4.* compatibility mode.
*
* Before 2.6.* redis won't tell its clients how many databases have been
* configured. We can find it empirically by attempting to select a given
* DB and seeing whether we get an error or not.
*/
/**
* @brief Max number of configured DB.
*/
#define MAX_DB_INDEX__24 1000
/**
* @brief Set the number of databases have been configured
* into kbr struct. (For Redis 2.4.* compatibility).
* @param[in] kbr Subclass of struct kb where to save the max db index founded.
* @return 0 on success, -1 on error.
*/
static int
fetch_max_db_index_compat (struct kb_redis *kbr)
{
redisContext *ctx = kbr->rctx;
redisReply *rep;
int min, max;
int rc = 0;
min = 1;
max = MAX_DB_INDEX__24;
while (min < max)
{
int current;
current = min + ((max - min) / 2);
rep = redisCommand (ctx, "SELECT %d", current);
if (rep == NULL)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis command failed with '%s'", __func__, ctx->errstr);
return -1;
}
switch (rep->type)
{
case REDIS_REPLY_ERROR:
max = current;
break;
case REDIS_REPLY_STATUS:
min = current + 1;
break;
default:
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: unexpected reply of type %d", __func__, rep->type);
freeReplyObject (rep);
return -1;
}
freeReplyObject (rep);
}
kbr->max_db = min;
/* Go back to DB #0 */
rep = redisCommand (ctx, "SELECT 0");
if (rep == NULL)
{
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: DB selection failed with '%s'", __func__, ctx->errstr);
rc = -1;
}
if (rep)
freeReplyObject (rep);
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;
skipping to change at line 212 skipping to change at line 134
} }
if (rep->type != REDIS_REPLY_ARRAY) if (rep->type != REDIS_REPLY_ARRAY)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: cannot retrieve max DB number: %s", __func__, rep->str); "%s: cannot retrieve max DB number: %s", __func__, rep->str);
rc = -1; rc = -1;
goto err_cleanup; goto err_cleanup;
} }
if (rep->elements == 0) if (rep->elements == 2)
{
/* Redis 2.4 compatibility mode. Suboptimal... */
rc = fetch_max_db_index_compat (kbr);
}
else if (rep->elements == 2)
{ {
kbr->max_db = (unsigned) atoi (rep->element[1]->str); kbr->max_db = (unsigned) atoi (rep->element[1]->str);
} }
else else
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: unexpected reply length (%zd)", __func__, rep->elements); "%s: unexpected reply length (%zd)", __func__, rep->elements);
rc = -1; rc = -1;
goto err_cleanup; goto err_cleanup;
} }
skipping to change at line 350 skipping to change at line 267
{ {
int rc; int rc;
if (kbr->rctx != NULL) if (kbr->rctx != NULL)
return 0; return 0;
kbr->rctx = redisConnectUnix (kbr->path); kbr->rctx = redisConnectUnix (kbr->path);
if (kbr->rctx == NULL || kbr->rctx->err) if (kbr->rctx == NULL || kbr->rctx->err)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error: %s", __func__, "%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM)); kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx); redisFree (kbr->rctx);
kbr->rctx = NULL; kbr->rctx = NULL;
return -1; return -1;
} }
rc = select_database (kbr); rc = select_database (kbr);
if (rc) if (rc)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No redis DB available"); g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No redis DB available");
skipping to change at line 500 skipping to change at line 417
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;
strncpy (kbr->path, kb_path, strlen (kb_path)); strncpy (kbr->path, kb_path, strlen (kb_path));
kbr->rctx = redisConnectUnix (kbr->path); kbr->rctx = redisConnectUnix (kbr->path);
if (kbr->rctx == NULL || kbr->rctx->err) if (kbr->rctx == NULL || kbr->rctx->err)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error: %s", __func__, "%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM)); kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx); redisFree (kbr->rctx);
g_free (kbr); g_free (kbr);
return NULL; return NULL;
} }
kbr->db = kb_index; kbr->db = kb_index;
rep = redisCommand (kbr->rctx, "SELECT %d", kb_index); rep = redisCommand (kbr->rctx, "SELECT %d", kb_index);
if (rep == NULL || rep->type != REDIS_REPLY_STATUS) if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
{ {
if (rep != NULL) if (rep != NULL)
skipping to change at line 544 skipping to change at line 461
strncpy (kbr->path, kb_path, strlen (kb_path)); strncpy (kbr->path, kb_path, strlen (kb_path));
do do
{ {
redisReply *rep; redisReply *rep;
kbr->rctx = redisConnectUnix (kbr->path); kbr->rctx = redisConnectUnix (kbr->path);
if (kbr->rctx == NULL || kbr->rctx->err) if (kbr->rctx == NULL || kbr->rctx->err)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error: %s", __func__, "%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM)); kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx); redisFree (kbr->rctx);
g_free (kbr); g_free (kbr);
return NULL; return NULL;
} }
if (kbr->max_db == 0) if (kbr->max_db == 0)
fetch_max_db_index (kbr); fetch_max_db_index (kbr);
kbr->db = i; kbr->db = i;
skipping to change at line 950 skipping to change at line 867
nvti_set_oid (nvti, oid); nvti_set_oid (nvti, oid);
nvti_set_required_keys (nvti, rep->element[NVT_REQUIRED_KEYS_POS]->str); nvti_set_required_keys (nvti, rep->element[NVT_REQUIRED_KEYS_POS]->str);
nvti_set_mandatory_keys (nvti, rep->element[NVT_MANDATORY_KEYS_POS]->str); nvti_set_mandatory_keys (nvti, rep->element[NVT_MANDATORY_KEYS_POS]->str);
nvti_set_excluded_keys (nvti, rep->element[NVT_EXCLUDED_KEYS_POS]->str); nvti_set_excluded_keys (nvti, rep->element[NVT_EXCLUDED_KEYS_POS]->str);
nvti_set_required_udp_ports ( nvti_set_required_udp_ports (
nvti, rep->element[NVT_REQUIRED_UDP_PORTS_POS]->str); nvti, rep->element[NVT_REQUIRED_UDP_PORTS_POS]->str);
nvti_set_required_ports (nvti, rep->element[NVT_REQUIRED_PORTS_POS]->str); nvti_set_required_ports (nvti, rep->element[NVT_REQUIRED_PORTS_POS]->str);
nvti_set_dependencies (nvti, rep->element[NVT_DEPENDENCIES_POS]->str); nvti_set_dependencies (nvti, rep->element[NVT_DEPENDENCIES_POS]->str);
nvti_set_tag (nvti, rep->element[NVT_TAGS_POS]->str); nvti_set_tag (nvti, rep->element[NVT_TAGS_POS]->str);
nvti_set_cve (nvti, rep->element[NVT_CVES_POS]->str); nvti_add_refs (nvti, "cve", rep->element[NVT_CVES_POS]->str, "");
nvti_set_bid (nvti, rep->element[NVT_BIDS_POS]->str); nvti_add_refs (nvti, "bid", rep->element[NVT_BIDS_POS]->str, "");
nvti_set_xref (nvti, rep->element[NVT_XREFS_POS]->str); nvti_add_refs (nvti, NULL, rep->element[NVT_XREFS_POS]->str, "");
nvti_set_category (nvti, atoi (rep->element[NVT_CATEGORY_POS]->str)); nvti_set_category (nvti, atoi (rep->element[NVT_CATEGORY_POS]->str));
nvti_set_timeout (nvti, atoi (rep->element[NVT_TIMEOUT_POS]->str)); nvti_set_timeout (nvti, atoi (rep->element[NVT_TIMEOUT_POS]->str));
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;
} }
} }
skipping to change at line 1373 skipping to change at line 1290
* @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;
GSList *element; unsigned int i;
gchar *cves, *bids, *xrefs;
if (!nvt || !filename) if (!nvt || !filename)
return -1; return -1;
cves = nvti_refs (nvt, "cve", "", 0);
bids = nvti_refs (nvt, "bid", "", 0);
xrefs = nvti_refs (nvt, NULL, "cve,bid", 1);
kbr = redis_kb (kb); kbr = redis_kb (kb);
rep = redis_cmd ( rep = redis_cmd (
kbr, "RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %d %s %s", kbr, "RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %d %s %s",
nvti_oid (nvt), filename, nvti_required_keys (nvt) ?: "", nvti_oid (nvt), filename, nvti_required_keys (nvt) ?: "",
nvti_mandatory_keys (nvt) ?: "", nvti_excluded_keys (nvt) ?: "", nvti_mandatory_keys (nvt) ?: "", nvti_excluded_keys (nvt) ?: "",
nvti_required_udp_ports (nvt) ?: "", nvti_required_ports (nvt) ?: "", nvti_required_udp_ports (nvt) ?: "", nvti_required_ports (nvt) ?: "",
nvti_dependencies (nvt) ?: "", nvti_tag (nvt) ?: "", nvti_cve (nvt) ?: "", nvti_dependencies (nvt) ?: "", nvti_tag (nvt) ?: "", cves ?: "",
nvti_bid (nvt) ?: "", nvti_xref (nvt) ?: "", nvti_category (nvt), bids ?: "", xrefs ?: "", nvti_category (nvt),
nvti_timeout (nvt), nvti_family (nvt), nvti_name (nvt)); nvti_timeout (nvt), nvti_family (nvt), nvti_name (nvt));
g_free (cves);
g_free (bids);
g_free (xrefs);
if (rep == NULL || rep->type == REDIS_REPLY_ERROR) if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
rc = -1; rc = -1;
if (rep != NULL) if (rep != NULL)
freeReplyObject (rep); freeReplyObject (rep);
element = nvt->prefs; if (nvti_pref_len (nvt))
if (g_slist_length (element)) redis_cmd (kbr, "DEL oid:%s:prefs", nvti_oid (nvt));
rep = redis_cmd (kbr, "DEL oid:%s:prefs", nvti_oid (nvt)); for (i = 0; i < nvti_pref_len (nvt); i++)
while (element) {
{ const nvtpref_t *pref = nvti_pref (nvt, i);
nvtpref_t *pref = element->data;
rep = redis_cmd (kbr, "RPUSH oid:%s:prefs %d|||%s|||%s|||%s",
rep = redis_cmd (kbr, "RPUSH oid:%s:prefs %s|||%s|||%s", nvti_oid (nvt), nvti_oid (nvt), nvtpref_id (pref), nvtpref_name (pref),
pref->name, pref->type, pref->dflt); nvtpref_type (pref), nvtpref_default (pref));
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);
element = element->next;
} }
rep = redis_cmd (kbr, "RPUSH filename:%s %lu %s", filename, time (NULL), rep = redis_cmd (kbr, "RPUSH filename:%s %lu %s", filename, time (NULL),
nvti_oid (nvt)); nvti_oid (nvt));
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;
} }
skipping to change at line 1463 skipping to change at line 1387
g_debug ("%s: deleting all DBs at %s except %s", __func__, kbr->path, except); g_debug ("%s: deleting all DBs at %s except %s", __func__, kbr->path, except);
do do
{ {
redisReply *rep; redisReply *rep;
kbr->rctx = redisConnectUnix (kbr->path); kbr->rctx = redisConnectUnix (kbr->path);
if (kbr->rctx == NULL || kbr->rctx->err) if (kbr->rctx == NULL || kbr->rctx->err)
{ {
g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"%s: redis connection error: %s", __func__, "%s: redis connection error to %s: %s", __func__, kbr->path,
kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM)); kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
redisFree (kbr->rctx); redisFree (kbr->rctx);
kbr->rctx = NULL; kbr->rctx = NULL;
return -1; return -1;
} }
kbr->db = i; kbr->db = i;
rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i); rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1) if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
{ {
 End of changes. 13 change blocks. 
104 lines changed or deleted 28 lines changed or added

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