Greenbone Vulnerability Management Libraries  10.0.1
kb.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #define _GNU_SOURCE
26 
27 #include "kb.h"
28 
29 #include <errno.h> /* for ENOMEM, EINVAL, EPROTO, EALREADY, ECONN... */
30 #include <glib.h> /* for g_log, g_free */
31 #include <hiredis/hiredis.h> /* for redisReply, freeReplyObject, redisCommand */
32 #include <stdbool.h> /* for bool, true, false */
33 #include <stdio.h>
34 #include <stdlib.h> /* for atoi */
35 #include <string.h> /* for strlen, strerror, strncpy, memset */
36 
37 #undef G_LOG_DOMAIN
38 #define G_LOG_DOMAIN "lib kb"
39 
50 #define GLOBAL_DBINDEX_NAME "GVM.__GlobalDBIndex"
51 
52 static const struct kb_operations KBRedisOperations;
53 
59 struct kb_redis
60 {
61  struct kb kb;
62  unsigned int max_db;
63  unsigned int db;
64  redisContext *rctx;
65  char path[0];
66 };
67 #define redis_kb(__kb) ((struct kb_redis *) (__kb))
68 
69 static int
70 redis_delete_all (struct kb_redis *);
71 static int redis_lnk_reset (kb_t);
72 static int
73 redis_flush_all (kb_t, const char *);
74 static redisReply *
75 redis_cmd (struct kb_redis *kbr, const char *fmt, ...);
76 
81 static int
82 try_database_index (struct kb_redis *kbr, int index)
83 {
84  redisContext *ctx = kbr->rctx;
85  redisReply *rep;
86  int rc = 0;
87 
88  rep = redisCommand (ctx, "HSETNX %s %d 1", GLOBAL_DBINDEX_NAME, index);
89  if (rep == NULL)
90  return -ENOMEM;
91 
92  if (rep->type != REDIS_REPLY_INTEGER)
93  rc = -EPROTO;
94  else if (rep->integer == 0)
95  rc = -EALREADY;
96  else
97  kbr->db = index;
98 
99  freeReplyObject (rep);
100 
101  return rc;
102 }
103 
104 /* Redis 2.4.* compatibility mode.
105  *
106  * Before 2.6.* redis won't tell its clients how many databases have been
107  * configured. We can find it empirically by attempting to select a given
108  * DB and seeing whether we get an error or not.
109  */
113 #define MAX_DB_INDEX__24 1000
114 
121 static int
123 {
124  redisContext *ctx = kbr->rctx;
125  redisReply *rep;
126  int min, max;
127  int rc = 0;
128 
129  min = 1;
130  max = MAX_DB_INDEX__24;
131 
132  while (min < max)
133  {
134  int current;
135 
136  current = min + ((max - min) / 2);
137 
138  rep = redisCommand (ctx, "SELECT %d", current);
139  if (rep == NULL)
140  {
141  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
142  "%s: redis command failed with '%s'", __func__, ctx->errstr);
143  return -1;
144  }
145 
146  switch (rep->type)
147  {
148  case REDIS_REPLY_ERROR:
149  max = current;
150  break;
151 
152  case REDIS_REPLY_STATUS:
153  min = current + 1;
154  break;
155 
156  default:
157  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
158  "%s: unexpected reply of type %d", __func__, rep->type);
159  freeReplyObject (rep);
160  return -1;
161  }
162  freeReplyObject (rep);
163  }
164 
165  kbr->max_db = min;
166 
167  /* Go back to DB #0 */
168  rep = redisCommand (ctx, "SELECT 0");
169  if (rep == NULL)
170  {
171  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
172  "%s: DB selection failed with '%s'", __func__, ctx->errstr);
173  rc = -1;
174  }
175 
176  if (rep)
177  freeReplyObject (rep);
178 
179  return rc;
180 }
181 
188 static int
190 {
191  int rc = 0;
192  redisContext *ctx = kbr->rctx;
193  redisReply *rep = NULL;
194 
195  rep = redisCommand (ctx, "CONFIG GET databases");
196  if (rep == NULL)
197  {
198  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
199  "%s: redis command failed with '%s'", __func__, ctx->errstr);
200  rc = -1;
201  goto err_cleanup;
202  }
203 
204  if (rep->type != REDIS_REPLY_ARRAY)
205  {
206  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
207  "%s: cannot retrieve max DB number: %s", __func__, rep->str);
208  rc = -1;
209  goto err_cleanup;
210  }
211 
212  if (rep->elements == 0)
213  {
214  /* Redis 2.4 compatibility mode. Suboptimal... */
215  rc = fetch_max_db_index_compat (kbr);
216  }
217  else if (rep->elements == 2)
218  {
219  kbr->max_db = (unsigned) atoi (rep->element[1]->str);
220  }
221  else
222  {
223  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
224  "%s: unexpected reply length (%zd)", __func__, rep->elements);
225  rc = -1;
226  goto err_cleanup;
227  }
228 
229  g_debug ("%s: maximum DB number: %u", __func__, kbr->max_db);
230 
231 err_cleanup:
232  if (rep != NULL)
233  freeReplyObject (rep);
234 
235  return rc;
236 }
237 
246 static int
248 {
249  int rc;
250  redisContext *ctx = kbr->rctx;
251  redisReply *rep = NULL;
252 
253  if (kbr->db == 0)
254  {
255  unsigned i;
256 
257  if (kbr->max_db == 0)
258  fetch_max_db_index (kbr);
259 
260  for (i = 1; i < kbr->max_db; i++)
261  {
262  rc = try_database_index (kbr, i);
263  if (rc == 0)
264  break;
265  }
266  }
267 
268  /* No DB available, give up. */
269  if (kbr->db == 0)
270  {
271  rc = -1;
272  goto err_cleanup;
273  }
274 
275  rep = redisCommand (ctx, "SELECT %u", kbr->db);
276  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
277  {
278  rc = -1;
279  goto err_cleanup;
280  }
281 
282  rc = 0;
283 
284 err_cleanup:
285  if (rep != NULL)
286  freeReplyObject (rep);
287 
288  return rc;
289 }
290 
297 static int
299 {
300  int rc;
301  redisContext *ctx = kbr->rctx;
302  redisReply *rep;
303 
304  if (ctx == NULL)
305  return -EINVAL;
306 
307  rep = redisCommand (ctx, "SELECT 0"); /* Management database*/
308  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
309  {
310  rc = -1;
311  goto err_cleanup;
312  }
313  freeReplyObject (rep);
314 
315  rep = redisCommand (ctx, "HDEL %s %d", GLOBAL_DBINDEX_NAME, kbr->db);
316  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER)
317  {
318  rc = -1;
319  goto err_cleanup;
320  }
321 
322  rc = 0;
323 
324 err_cleanup:
325  if (rep != NULL)
326  freeReplyObject (rep);
327 
328  return rc;
329 }
330 
338 static int
339 get_redis_ctx (struct kb_redis *kbr)
340 {
341  int rc;
342 
343  if (kbr->rctx != NULL)
344  return 0;
345 
346  kbr->rctx = redisConnectUnix (kbr->path);
347  if (kbr->rctx == NULL || kbr->rctx->err)
348  {
349  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
350  "%s: redis connection error: %s", __func__,
351  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
352  redisFree (kbr->rctx);
353  kbr->rctx = NULL;
354  return -1;
355  }
356 
357  rc = select_database (kbr);
358  if (rc)
359  {
360  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No redis DB available");
361  redisFree (kbr->rctx);
362  kbr->rctx = NULL;
363  return -2;
364  }
365 
366  g_debug ("%s: connected to redis://%s/%d", __func__, kbr->path, kbr->db);
367  return 0;
368 }
369 
375 static int
377 {
378  int rc = 0;
379  redisReply *rep;
380 
381  rep = redis_cmd (kbr, "PING");
382  if (rep == NULL)
383  {
384  /* not 100% relevant but hiredis doesn't provide us with proper error
385  * codes. */
386  rc = -ECONNREFUSED;
387  goto out;
388  }
389 
390  if (rep->type != REDIS_REPLY_STATUS)
391  {
392  rc = -EINVAL;
393  goto out;
394  }
395 
396  if (g_ascii_strcasecmp (rep->str, "PONG"))
397  {
398  rc = -EPROTO;
399  goto out;
400  }
401 
402 out:
403  if (rep != NULL)
404  freeReplyObject (rep);
405 
406  return rc;
407 }
408 
414 static int
416 {
417  struct kb_redis *kbr;
418 
419  kbr = redis_kb (kb);
420 
421  redis_delete_all (kbr);
422  redis_release_db (kbr);
423 
424  if (kbr->rctx != NULL)
425  {
426  redisFree (kbr->rctx);
427  kbr->rctx = NULL;
428  }
429 
430  g_free (kb);
431  return 0;
432 }
433 
439 static int
441 {
442  int i;
443  i = ((struct kb_redis *) kb)->db;
444  if (i > 0)
445  return i;
446  return -1;
447 }
448 
455 static int
456 redis_new (kb_t *kb, const char *kb_path)
457 {
458  struct kb_redis *kbr;
459  int rc = 0;
460 
461  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
462  kbr->kb.kb_ops = &KBRedisOperations;
463  strncpy (kbr->path, kb_path, strlen (kb_path));
464 
465  if ((rc = get_redis_ctx (kbr)) < 0)
466  return rc;
467  if (redis_test_connection (kbr))
468  {
469  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
470  "%s: cannot access redis at '%s'", __func__, kb_path);
471  redis_delete ((kb_t) kbr);
472  kbr = NULL;
473  rc = -1;
474  }
475 
476  *kb = (kb_t) kbr;
477  return rc;
478 }
479 
486 static kb_t
487 redis_direct_conn (const char *kb_path, const int kb_index)
488 {
489  struct kb_redis *kbr;
490  redisReply *rep;
491 
492  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
493  kbr->kb.kb_ops = &KBRedisOperations;
494  strncpy (kbr->path, kb_path, strlen (kb_path));
495 
496  kbr->rctx = redisConnectUnix (kbr->path);
497  if (kbr->rctx == NULL || kbr->rctx->err)
498  {
499  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
500  "%s: redis connection error: %s", __func__,
501  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
502  redisFree (kbr->rctx);
503  g_free (kbr);
504  return NULL;
505  }
506  kbr->db = kb_index;
507  rep = redisCommand (kbr->rctx, "SELECT %d", kb_index);
508  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
509  {
510  if (rep != NULL)
511  freeReplyObject (rep);
512  redisFree (kbr->rctx);
513  kbr->rctx = NULL;
514  return NULL;
515  }
516  freeReplyObject (rep);
517  return (kb_t) kbr;
518 }
519 
526 static kb_t
527 redis_find (const char *kb_path, const char *key)
528 {
529  struct kb_redis *kbr;
530  unsigned int i = 1;
531 
532  kbr = g_malloc0 (sizeof (struct kb_redis) + strlen (kb_path) + 1);
533  kbr->kb.kb_ops = &KBRedisOperations;
534  strncpy (kbr->path, kb_path, strlen (kb_path));
535 
536  do
537  {
538  redisReply *rep;
539 
540  kbr->rctx = redisConnectUnix (kbr->path);
541  if (kbr->rctx == NULL || kbr->rctx->err)
542  {
543  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
544  "%s: redis connection error: %s", __func__,
545  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
546  redisFree (kbr->rctx);
547  g_free (kbr);
548  return NULL;
549  }
550 
551  if (kbr->max_db == 0)
552  fetch_max_db_index (kbr);
553 
554  kbr->db = i;
555  rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
556  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
557  {
558  if (rep != NULL)
559  freeReplyObject (rep);
560  i++;
561  redisFree (kbr->rctx);
562  kbr->rctx = NULL;
563  continue;
564  }
565  freeReplyObject (rep);
566  rep = redisCommand (kbr->rctx, "SELECT %u", i);
567  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
568  {
569  redisFree (kbr->rctx);
570  kbr->rctx = NULL;
571  }
572  else
573  {
574  freeReplyObject (rep);
575  if (key)
576  {
577  char *tmp = kb_item_get_str (&kbr->kb, key);
578  if (tmp)
579  {
580  g_free (tmp);
581  return (kb_t) kbr;
582  }
583  }
584  redisFree (kbr->rctx);
585  }
586  i++;
587  }
588  while (i < kbr->max_db);
589 
590  g_free (kbr);
591  return NULL;
592 }
593 
598 void
599 kb_item_free (struct kb_item *item)
600 {
601  while (item != NULL)
602  {
603  struct kb_item *next;
604 
605  next = item->next;
606  if (item->type == KB_TYPE_STR && item->v_str != NULL)
607  g_free (item->v_str);
608  g_free (item);
609  item = next;
610  }
611 }
612 
620 static struct kb_item *
621 redis2kbitem_single (const char *name, const redisReply *elt, int force_int)
622 {
623  struct kb_item *item;
624  size_t namelen;
625 
626  if (elt->type != REDIS_REPLY_STRING && elt->type != REDIS_REPLY_INTEGER)
627  return NULL;
628 
629  namelen = strlen (name) + 1;
630 
631  item = g_malloc0 (sizeof (struct kb_item) + namelen);
632  if (elt->type == REDIS_REPLY_INTEGER)
633  {
634  item->type = KB_TYPE_INT;
635  item->v_int = elt->integer;
636  }
637  else if (force_int)
638  {
639  item->type = KB_TYPE_INT;
640  item->v_int = atoi (elt->str);
641  }
642  else
643  {
644  item->type = KB_TYPE_STR;
645  item->v_str = g_memdup (elt->str, elt->len + 1);
646  item->len = elt->len;
647  }
648 
649  item->next = NULL;
650  item->namelen = namelen;
651  strncpy (item->name, name, namelen);
652 
653  return item;
654 }
655 
662 static struct kb_item *
663 redis2kbitem (const char *name, const redisReply *rep)
664 {
665  struct kb_item *kbi;
666 
667  kbi = NULL;
668 
669  switch (rep->type)
670  {
671  unsigned int i;
672 
673  case REDIS_REPLY_STRING:
674  case REDIS_REPLY_INTEGER:
675  kbi = redis2kbitem_single (name, rep, 0);
676  break;
677 
678  case REDIS_REPLY_ARRAY:
679  for (i = 0; i < rep->elements; i++)
680  {
681  struct kb_item *tmpitem;
682 
683  tmpitem = redis2kbitem_single (name, rep->element[i], 0);
684  if (tmpitem == NULL)
685  break;
686 
687  if (kbi != NULL)
688  {
689  tmpitem->next = kbi;
690  kbi = tmpitem;
691  }
692  else
693  kbi = tmpitem;
694  }
695  break;
696 
697  case REDIS_REPLY_NIL:
698  case REDIS_REPLY_STATUS:
699  case REDIS_REPLY_ERROR:
700  default:
701  break;
702  }
703 
704  return kbi;
705 }
706 
713 static redisReply *
714 redis_cmd (struct kb_redis *kbr, const char *fmt, ...)
715 {
716  redisReply *rep;
717  va_list ap, aq;
718  int retry = 0;
719 
720  va_start (ap, fmt);
721  do
722  {
723  if (get_redis_ctx (kbr) < 0)
724  {
725  va_end (ap);
726  return NULL;
727  }
728 
729  va_copy (aq, ap);
730  rep = redisvCommand (kbr->rctx, fmt, aq);
731  va_end (aq);
732 
733  if (kbr->rctx->err)
734  {
735  if (rep != NULL)
736  freeReplyObject (rep);
737 
738  redis_lnk_reset ((kb_t) kbr);
739  retry = !retry;
740  }
741  else
742  retry = 0;
743  }
744  while (retry);
745 
746  va_end (ap);
747 
748  return rep;
749 }
750 
759 static struct kb_item *
761 {
762  struct kb_item *kbi;
763  struct kb_redis *kbr;
764  redisReply *rep;
765 
766  kbr = redis_kb (kb);
767  kbi = NULL;
768 
769  rep = redis_cmd (kbr, "LINDEX %s -1", name);
770  if (rep == NULL || rep->type != REDIS_REPLY_STRING)
771  {
772  kbi = NULL;
773  goto out;
774  }
775 
776  kbi = redis2kbitem_single (name, rep, type == KB_TYPE_INT);
777 
778 out:
779  if (rep != NULL)
780  freeReplyObject (rep);
781 
782  return kbi;
783 }
784 
792 static char *
793 redis_get_str (kb_t kb, const char *name)
794 {
795  struct kb_item *kbi;
796 
798  if (kbi != NULL)
799  {
800  char *res;
801 
802  res = kbi->v_str;
803  kbi->v_str = NULL;
804  kb_item_free (kbi);
805  return res;
806  }
807  return NULL;
808 }
809 
817 static int
818 redis_push_str (kb_t kb, const char *name, const char *value)
819 {
820  struct kb_redis *kbr;
821  redisReply *rep = NULL;
822  int rc = 0;
823 
824  kbr = redis_kb (kb);
825  rep = redis_cmd (kbr, "LPUSH %s %s", name, value);
826  if (!rep || rep->type == REDIS_REPLY_ERROR)
827  rc = -1;
828 
829  if (rep)
830  freeReplyObject (rep);
831 
832  return rc;
833 }
834 
841 static char *
842 redis_pop_str (kb_t kb, const char *name)
843 {
844  struct kb_redis *kbr;
845  redisReply *rep;
846  char *value = NULL;
847 
848  kbr = redis_kb (kb);
849  rep = redis_cmd (kbr, "RPOP %s", name);
850  if (!rep)
851  return NULL;
852 
853  if (rep->type == REDIS_REPLY_STRING)
854  value = g_strdup (rep->str);
855  freeReplyObject (rep);
856 
857  return value;
858 }
859 
867 static int
868 redis_get_int (kb_t kb, const char *name)
869 {
870  struct kb_item *kbi;
871 
873  if (kbi != NULL)
874  {
875  int res;
876 
877  res = kbi->v_int;
878  kb_item_free (kbi);
879  return res;
880  }
881  return -1;
882 }
883 
891 static char *
892 redis_get_nvt (kb_t kb, const char *oid, enum kb_nvt_pos position)
893 {
894  struct kb_redis *kbr;
895  redisReply *rep;
896  char *res = NULL;
897 
898  kbr = redis_kb (kb);
899  if (position >= NVT_TIMESTAMP_POS)
900  rep = redis_cmd (kbr, "LINDEX filename:%s %d", oid,
901  position - NVT_TIMESTAMP_POS);
902  else
903  rep = redis_cmd (kbr, "LINDEX nvt:%s %d", oid, position);
904  if (!rep)
905  return NULL;
906  if (rep->type == REDIS_REPLY_INTEGER)
907  res = g_strdup_printf ("%lld", rep->integer);
908  else if (rep->type == REDIS_REPLY_STRING)
909  res = g_strdup (rep->str);
910  freeReplyObject (rep);
911 
912  return res;
913 }
914 
921 static nvti_t *
922 redis_get_nvt_all (kb_t kb, const char *oid)
923 {
924  struct kb_redis *kbr;
925  redisReply *rep;
926 
927  kbr = redis_kb (kb);
928  rep =
929  redis_cmd (kbr, "LRANGE nvt:%s %d %d", oid, NVT_FILENAME_POS, NVT_NAME_POS);
930  if (!rep)
931  return NULL;
932  if (rep->type != REDIS_REPLY_ARRAY || rep->elements != NVT_NAME_POS + 1)
933  {
934  freeReplyObject (rep);
935  return NULL;
936  }
937  else
938  {
939  nvti_t *nvti = nvti_new ();
940 
941  nvti_set_oid (nvti, oid);
942  nvti_set_required_keys (nvti, rep->element[NVT_REQUIRED_KEYS_POS]->str);
944  nvti_set_excluded_keys (nvti, rep->element[NVT_EXCLUDED_KEYS_POS]->str);
946  nvti, rep->element[NVT_REQUIRED_UDP_PORTS_POS]->str);
948  nvti_set_dependencies (nvti, rep->element[NVT_DEPENDENCIES_POS]->str);
949  nvti_set_tag (nvti, rep->element[NVT_TAGS_POS]->str);
950  nvti_set_cve (nvti, rep->element[NVT_CVES_POS]->str);
951  nvti_set_bid (nvti, rep->element[NVT_BIDS_POS]->str);
952  nvti_set_xref (nvti, rep->element[NVT_XREFS_POS]->str);
953  nvti_set_category (nvti, atoi (rep->element[NVT_CATEGORY_POS]->str));
954  nvti_set_timeout (nvti, atoi (rep->element[NVT_TIMEOUT_POS]->str));
955  nvti_set_family (nvti, rep->element[NVT_FAMILY_POS]->str);
956  nvti_set_name (nvti, rep->element[NVT_NAME_POS]->str);
957 
958  freeReplyObject (rep);
959  return nvti;
960  }
961 }
962 
970 static struct kb_item *
971 redis_get_all (kb_t kb, const char *name)
972 {
973  struct kb_redis *kbr;
974  struct kb_item *kbi;
975  redisReply *rep;
976 
977  kbr = redis_kb (kb);
978 
979  rep = redis_cmd (kbr, "LRANGE %s 0 -1", name);
980  if (rep == NULL)
981  return NULL;
982 
983  kbi = redis2kbitem (name, rep);
984 
985  freeReplyObject (rep);
986 
987  return kbi;
988 }
989 
997 static struct kb_item *
998 redis_get_pattern (kb_t kb, const char *pattern)
999 {
1000  struct kb_redis *kbr;
1001  struct kb_item *kbi = NULL;
1002  redisReply *rep;
1003  unsigned int i;
1004 
1005  kbr = redis_kb (kb);
1006  rep = redis_cmd (kbr, "KEYS %s", pattern);
1007  if (!rep)
1008  return NULL;
1009  if (rep->type != REDIS_REPLY_ARRAY)
1010  {
1011  freeReplyObject (rep);
1012  return NULL;
1013  }
1014 
1015  if (get_redis_ctx (kbr) < 0)
1016  return NULL;
1017  for (i = 0; i < rep->elements; i++)
1018  redisAppendCommand (kbr->rctx, "LRANGE %s 0 -1", rep->element[i]->str);
1019 
1020  for (i = 0; i < rep->elements; i++)
1021  {
1022  struct kb_item *tmp;
1023  redisReply *rep_range;
1024 
1025  redisGetReply (kbr->rctx, (void **) &rep_range);
1026  if (!rep)
1027  continue;
1028  tmp = redis2kbitem (rep->element[i]->str, rep_range);
1029  if (!tmp)
1030  {
1031  freeReplyObject (rep_range);
1032  continue;
1033  }
1034 
1035  if (kbi)
1036  {
1037  struct kb_item *tmp2;
1038 
1039  tmp2 = tmp;
1040  while (tmp->next)
1041  tmp = tmp->next;
1042  tmp->next = kbi;
1043  kbi = tmp2;
1044  }
1045  else
1046  kbi = tmp;
1047  freeReplyObject (rep_range);
1048  }
1049 
1050  freeReplyObject (rep);
1051  return kbi;
1052 }
1053 
1059 static GSList *
1061 {
1062  struct kb_redis *kbr;
1063  redisReply *rep;
1064  GSList *list = NULL;
1065  size_t i;
1066 
1067  kbr = redis_kb (kb);
1068  rep = redis_cmd (kbr, "KEYS nvt:*");
1069  if (!rep)
1070  return NULL;
1071 
1072  if (rep->type != REDIS_REPLY_ARRAY)
1073  {
1074  freeReplyObject (rep);
1075  return NULL;
1076  }
1077 
1078  /* Fetch OID values from key names nvt:OID. */
1079  for (i = 0; i < rep->elements; i++)
1080  list = g_slist_prepend (list, g_strdup (rep->element[i]->str + 4));
1081  freeReplyObject (rep);
1082 
1083  return list;
1084 }
1085 
1094 static size_t
1095 redis_count (kb_t kb, const char *pattern)
1096 {
1097  struct kb_redis *kbr;
1098  redisReply *rep;
1099  size_t count;
1100 
1101  kbr = redis_kb (kb);
1102 
1103  rep = redis_cmd (kbr, "KEYS %s", pattern);
1104  if (rep == NULL)
1105  return 0;
1106 
1107  if (rep->type != REDIS_REPLY_ARRAY)
1108  {
1109  freeReplyObject (rep);
1110  return 0;
1111  }
1112 
1113  count = rep->elements;
1114  freeReplyObject (rep);
1115  return count;
1116 }
1117 
1124 static int
1125 redis_del_items (kb_t kb, const char *name)
1126 {
1127  struct kb_redis *kbr;
1128  redisReply *rep;
1129  int rc = 0;
1130 
1131  kbr = redis_kb (kb);
1132 
1133  rep = redis_cmd (kbr, "DEL %s", name);
1134  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1135  rc = -1;
1136 
1137  if (rep != NULL)
1138  freeReplyObject (rep);
1139 
1140  return rc;
1141 }
1142 
1151 static int
1152 redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
1153 {
1154  struct kb_redis *kbr;
1155  redisReply *rep = NULL;
1156  int rc = 0;
1157  redisContext *ctx;
1158 
1159  kbr = redis_kb (kb);
1160  if (get_redis_ctx (kbr) < 0)
1161  return -1;
1162  ctx = kbr->rctx;
1163 
1164  /* Some VTs still rely on values being unique (ie. a value inserted multiple
1165  * times, will only be present once.)
1166  * Once these are fixed, the LREM becomes redundant and should be removed.
1167  */
1168  if (len == 0)
1169  {
1170  redisAppendCommand (ctx, "LREM %s 1 %s", name, str);
1171  redisAppendCommand (ctx, "RPUSH %s %s", name, str);
1172  redisGetReply (ctx, (void **) &rep);
1173  if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1174  g_debug ("Key '%s' already contained value '%s'", name, str);
1175  freeReplyObject (rep);
1176  redisGetReply (ctx, (void **) &rep);
1177  }
1178  else
1179  {
1180  redisAppendCommand (ctx, "LREM %s 1 %b", name, str, len);
1181  redisAppendCommand (ctx, "RPUSH %s %b", name, str, len);
1182  redisGetReply (ctx, (void **) &rep);
1183  if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1184  g_debug ("Key '%s' already contained string '%s'", name, str);
1185  freeReplyObject (rep);
1186  redisGetReply (ctx, (void **) &rep);
1187  }
1188  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1189  rc = -1;
1190 
1191  if (rep != NULL)
1192  freeReplyObject (rep);
1193 
1194  return rc;
1195 }
1196 
1205 static int
1206 redis_add_str (kb_t kb, const char *name, const char *str, size_t len)
1207 {
1208  struct kb_redis *kbr;
1209  redisReply *rep;
1210  int rc = 0;
1211 
1212  kbr = redis_kb (kb);
1213  if (len == 0)
1214  rep = redis_cmd (kbr, "RPUSH %s %s", name, str);
1215  else
1216  rep = redis_cmd (kbr, "RPUSH %s %b", name, str, len);
1217  if (!rep || rep->type == REDIS_REPLY_ERROR)
1218  rc = -1;
1219 
1220  if (rep)
1221  freeReplyObject (rep);
1222  return rc;
1223 }
1224 
1233 static int
1234 redis_set_str (kb_t kb, const char *name, const char *val, size_t len)
1235 {
1236  struct kb_redis *kbr;
1237  redisReply *rep = NULL;
1238  redisContext *ctx;
1239  int rc = 0, i = 4;
1240 
1241  kbr = redis_kb (kb);
1242  if (get_redis_ctx (kbr) < 0)
1243  return -1;
1244  ctx = kbr->rctx;
1245  redisAppendCommand (ctx, "MULTI");
1246  redisAppendCommand (ctx, "DEL %s", name);
1247  if (len == 0)
1248  redisAppendCommand (ctx, "RPUSH %s %s", name, val);
1249  else
1250  redisAppendCommand (ctx, "RPUSH %s %b", name, val, len);
1251  redisAppendCommand (ctx, "EXEC");
1252  while (i--)
1253  {
1254  redisGetReply (ctx, (void **) &rep);
1255  if (!rep || rep->type == REDIS_REPLY_ERROR)
1256  rc = -1;
1257  if (rep)
1258  freeReplyObject (rep);
1259  }
1260 
1261  return rc;
1262 }
1263 
1271 static int
1272 redis_add_int_unique (kb_t kb, const char *name, int val)
1273 {
1274  struct kb_redis *kbr;
1275  redisReply *rep;
1276  int rc = 0;
1277  redisContext *ctx;
1278 
1279  kbr = redis_kb (kb);
1280  if (get_redis_ctx (kbr) < 0)
1281  return -1;
1282  ctx = kbr->rctx;
1283  redisAppendCommand (ctx, "LREM %s 1 %d", name, val);
1284  redisAppendCommand (ctx, "RPUSH %s %d", name, val);
1285  redisGetReply (ctx, (void **) &rep);
1286  if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
1287  g_debug ("Key '%s' already contained integer '%d'", name, val);
1288  freeReplyObject (rep);
1289  redisGetReply (ctx, (void **) &rep);
1290  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1291  {
1292  rc = -1;
1293  goto out;
1294  }
1295 
1296 out:
1297  if (rep != NULL)
1298  freeReplyObject (rep);
1299 
1300  return rc;
1301 }
1302 
1310 static int
1311 redis_add_int (kb_t kb, const char *name, int val)
1312 {
1313  redisReply *rep;
1314  int rc = 0;
1315 
1316  rep = redis_cmd (redis_kb (kb), "RPUSH %s %d", name, val);
1317  if (!rep || rep->type == REDIS_REPLY_ERROR)
1318  rc = -1;
1319  if (rep)
1320  freeReplyObject (rep);
1321 
1322  return rc;
1323 }
1324 
1332 static int
1333 redis_set_int (kb_t kb, const char *name, int val)
1334 {
1335  struct kb_redis *kbr;
1336  redisReply *rep = NULL;
1337  redisContext *ctx;
1338  int rc = 0, i = 4;
1339 
1340  kbr = redis_kb (kb);
1341  if (get_redis_ctx (redis_kb (kb)) < 0)
1342  return -1;
1343  ctx = kbr->rctx;
1344  redisAppendCommand (ctx, "MULTI");
1345  redisAppendCommand (ctx, "DEL %s", name);
1346  redisAppendCommand (ctx, "RPUSH %s %d", name, val);
1347  redisAppendCommand (ctx, "EXEC");
1348  while (i--)
1349  {
1350  redisGetReply (ctx, (void **) &rep);
1351  if (!rep || rep->type == REDIS_REPLY_ERROR)
1352  rc = -1;
1353  if (rep)
1354  freeReplyObject (rep);
1355  }
1356 
1357  return rc;
1358 }
1359 
1367 static int
1368 redis_add_nvt (kb_t kb, const nvti_t *nvt, const char *filename)
1369 {
1370  struct kb_redis *kbr;
1371  redisReply *rep = NULL;
1372  int rc = 0;
1373  GSList *element;
1374 
1375  if (!nvt || !filename)
1376  return -1;
1377 
1378  kbr = redis_kb (kb);
1379  rep = redis_cmd (
1380  kbr, "RPUSH nvt:%s %s %s %s %s %s %s %s %s %s %s %s %d %d %s %s",
1381  nvti_oid (nvt), filename, nvti_required_keys (nvt) ?: "",
1382  nvti_mandatory_keys (nvt) ?: "", nvti_excluded_keys (nvt) ?: "",
1383  nvti_required_udp_ports (nvt) ?: "", nvti_required_ports (nvt) ?: "",
1384  nvti_dependencies (nvt) ?: "", nvti_tag (nvt) ?: "", nvti_cve (nvt) ?: "",
1385  nvti_bid (nvt) ?: "", nvti_xref (nvt) ?: "", nvti_category (nvt),
1386  nvti_timeout (nvt), nvti_family (nvt), nvti_name (nvt));
1387  if (rep == NULL || rep->type == REDIS_REPLY_ERROR)
1388  rc = -1;
1389  if (rep != NULL)
1390  freeReplyObject (rep);
1391 
1392  element = nvt->prefs;
1393  if (g_slist_length (element))
1394  rep = redis_cmd (kbr, "DEL oid:%s:prefs", nvti_oid (nvt));
1395  while (element)
1396  {
1397  nvtpref_t *pref = element->data;
1398 
1399  rep = redis_cmd (kbr, "RPUSH oid:%s:prefs %s|||%s|||%s", nvti_oid (nvt),
1400  pref->name, pref->type, pref->dflt);
1401  if (!rep || rep->type == REDIS_REPLY_ERROR)
1402  rc = -1;
1403  if (rep)
1404  freeReplyObject (rep);
1405  element = element->next;
1406  }
1407  rep = redis_cmd (kbr, "RPUSH filename:%s %lu %s", filename, time (NULL),
1408  nvti_oid (nvt));
1409  if (!rep || rep->type == REDIS_REPLY_ERROR)
1410  rc = -1;
1411  if (rep)
1412  freeReplyObject (rep);
1413  return rc;
1414 }
1415 
1422 static int
1424 {
1425  struct kb_redis *kbr;
1426 
1427  kbr = redis_kb (kb);
1428 
1429  if (kbr->rctx != NULL)
1430  {
1431  redisFree (kbr->rctx);
1432  kbr->rctx = NULL;
1433  }
1434 
1435  return 0;
1436 }
1437 
1444 static int
1445 redis_flush_all (kb_t kb, const char *except)
1446 {
1447  unsigned int i = 1;
1448  struct kb_redis *kbr;
1449 
1450  kbr = redis_kb (kb);
1451  if (kbr->rctx)
1452  redisFree (kbr->rctx);
1453 
1454  g_debug ("%s: deleting all DBs at %s except %s", __func__, kbr->path, except);
1455  do
1456  {
1457  redisReply *rep;
1458 
1459  kbr->rctx = redisConnectUnix (kbr->path);
1460  if (kbr->rctx == NULL || kbr->rctx->err)
1461  {
1462  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
1463  "%s: redis connection error: %s", __func__,
1464  kbr->rctx ? kbr->rctx->errstr : strerror (ENOMEM));
1465  redisFree (kbr->rctx);
1466  kbr->rctx = NULL;
1467  return -1;
1468  }
1469 
1470  kbr->db = i;
1471  rep = redisCommand (kbr->rctx, "HEXISTS %s %d", GLOBAL_DBINDEX_NAME, i);
1472  if (rep == NULL || rep->type != REDIS_REPLY_INTEGER || rep->integer != 1)
1473  {
1474  freeReplyObject (rep);
1475  redisFree (kbr->rctx);
1476  i++;
1477  continue;
1478  }
1479  freeReplyObject (rep);
1480  rep = redisCommand (kbr->rctx, "SELECT %u", i);
1481  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1482  {
1483  freeReplyObject (rep);
1484  redisFree (kbr->rctx);
1485  kbr->rctx = NULL;
1486  }
1487  else
1488  {
1489  freeReplyObject (rep);
1490  /* Don't remove DB if it has "except" key. */
1491  if (except)
1492  {
1493  char *tmp = kb_item_get_str (kb, except);
1494  if (tmp)
1495  {
1496  g_free (tmp);
1497  i++;
1498  redisFree (kbr->rctx);
1499  continue;
1500  }
1501  }
1502  redis_delete_all (kbr);
1503  redis_release_db (kbr);
1504  redisFree (kbr->rctx);
1505  }
1506  i++;
1507  }
1508  while (i < kbr->max_db);
1509 
1510  g_free (kb);
1511  return 0;
1512 }
1513 
1519 int
1521 {
1522  int rc;
1523  redisReply *rep;
1524  struct kb_redis *kbr;
1525 
1526  kbr = redis_kb (kb);
1527  g_debug ("%s: saving all elements from KB #%u", __func__, kbr->db);
1528  rep = redis_cmd (kbr, "SAVE");
1529  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1530  {
1531  rc = -1;
1532  goto err_cleanup;
1533  }
1534 
1535  rc = 0;
1536 
1537 err_cleanup:
1538  if (rep != NULL)
1539  freeReplyObject (rep);
1540 
1541  return rc;
1542 }
1543 
1549 int
1551 {
1552  int rc;
1553  redisReply *rep;
1554  struct sigaction new_action, original_action;
1555 
1556  /* Ignore SIGPIPE, in case of a lost connection. */
1557  new_action.sa_flags = 0;
1558  if (sigemptyset (&new_action.sa_mask))
1559  return -1;
1560  new_action.sa_handler = SIG_IGN;
1561  if (sigaction (SIGPIPE, &new_action, &original_action))
1562  return -1;
1563 
1564  g_debug ("%s: deleting all elements from KB #%u", __func__, kbr->db);
1565  rep = redis_cmd (kbr, "FLUSHDB");
1566  if (rep == NULL || rep->type != REDIS_REPLY_STATUS)
1567  {
1568  rc = -1;
1569  goto err_cleanup;
1570  }
1571 
1572  rc = 0;
1573 
1574 err_cleanup:
1575  if (sigaction (SIGPIPE, &original_action, NULL))
1576  return -1;
1577  if (rep != NULL)
1578  freeReplyObject (rep);
1579 
1580  return rc;
1581 }
1582 
1588 static const struct kb_operations KBRedisOperations = {
1589  .kb_new = redis_new,
1590  .kb_find = redis_find,
1591  .kb_delete = redis_delete,
1592  .kb_get_single = redis_get_single,
1593  .kb_get_str = redis_get_str,
1594  .kb_get_int = redis_get_int,
1595  .kb_get_nvt = redis_get_nvt,
1596  .kb_get_nvt_all = redis_get_nvt_all,
1597  .kb_get_nvt_oids = redis_get_oids,
1598  .kb_push_str = redis_push_str,
1599  .kb_pop_str = redis_pop_str,
1600  .kb_get_all = redis_get_all,
1601  .kb_get_pattern = redis_get_pattern,
1602  .kb_count = redis_count,
1603  .kb_add_str = redis_add_str,
1604  .kb_add_str_unique = redis_add_str_unique,
1605  .kb_set_str = redis_set_str,
1606  .kb_add_int = redis_add_int,
1607  .kb_add_int_unique = redis_add_int_unique,
1608  .kb_set_int = redis_set_int,
1609  .kb_add_nvt = redis_add_nvt,
1610  .kb_del_items = redis_del_items,
1611  .kb_lnk_reset = redis_lnk_reset,
1612  .kb_save = redis_save,
1613  .kb_flush = redis_flush_all,
1614  .kb_direct_conn = redis_direct_conn,
1615  .kb_get_kb_index = redis_get_kb_index,
1616 };
1617 
static int redis_push_str(kb_t kb, const char *name, const char *value)
Push a new entry under a given key.
Definition: kb.c:818
int nvti_set_category(nvti_t *n, const gint category)
Set the category type of a NVT Info.
Definition: nvti.c:817
#define redis_kb(__kb)
Definition: kb.c:67
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.
Definition: kb.c:1206
kb_nvt_pos
Possible positions of nvt values in cache list.
Definition: kb.h:54
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
Definition: nvti.c:275
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).
Definition: kb.c:122
static nvti_t * redis_get_nvt_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.c:922
Definition: kb.h:46
gchar * nvti_required_ports(const nvti_t *n)
Get the required ports list.
Definition: nvti.c:373
static int redis_test_connection(struct kb_redis *kbr)
Test redis connection.
Definition: kb.c:376
static struct kb_item * redis_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.c:760
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
Definition: kb.c:599
static int redis_delete_all(struct kb_redis *)
Delete all the KB's content.
Definition: kb.c:1550
nvti_t * nvti_new(void)
Create a new (empty) nvti structure.
Definition: nvti.c:156
int nvti_set_family(nvti_t *n, const gchar *family)
Set the family of a NVT.
Definition: nvti.c:777
#define G_LOG_DOMAIN
Definition: kb.c:38
int nvti_set_required_ports(nvti_t *n, const gchar *required_ports)
Set the required ports of a NVT.
Definition: nvti.c:728
static char * redis_pop_str(kb_t kb, const char *name)
Pops a single KB string item.
Definition: kb.c:842
unsigned int max_db
Definition: kb.c:62
gchar * nvti_cve(const nvti_t *n)
Get the CVE references.
Definition: nvti.c:247
gchar * name
Name of the preference.
Definition: nvti.h:41
The structure of a information record that corresponds to a NVT.
Definition: nvti.h:62
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:79
struct kb_item * next
Definition: kb.h:90
enum kb_item_type type
Definition: kb.h:81
static struct kb_item * redis_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
Definition: kb.c:971
unsigned int db
Definition: kb.c:63
KB interface. Functions provided by an implementation. All functions have to be provided,...
Definition: kb.h:117
gint nvti_category(const nvti_t *n)
Get the category for this NVT.
Definition: nvti.c:456
int nvti_set_timeout(nvti_t *n, const gint timeout)
Set the timeout of a NVT Info.
Definition: nvti.c:798
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:579
redisContext * rctx
Definition: kb.c:64
gint nvti_timeout(const nvti_t *n)
Get the timeout for this NVT.
Definition: nvti.c:443
int nvti_set_cve(nvti_t *n, const gchar *cve)
Set the CVE references of a NVT.
Definition: nvti.c:513
static int redis_add_nvt(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition: kb.c:1368
static int redis_release_db(struct kb_redis *kbr)
Release DB.
Definition: kb.c:298
gchar * nvti_tag(const nvti_t *n)
Get the tag.
Definition: nvti.c:289
static GSList * redis_get_oids(kb_t kb)
Get all NVT OIDs.
Definition: kb.c:1060
gchar * nvti_family(const nvti_t *n)
Get the family name.
Definition: nvti.c:401
static int redis_flush_all(kb_t, const char *)
Flush all the KB's content. Delete all namespaces.
Definition: kb.c:1445
static struct kb_item * redis2kbitem_single(const char *name, const redisReply *elt, int force_int)
Give a single KB item.
Definition: kb.c:621
gchar * type
Preference type.
Definition: nvti.h:40
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition: nvti.c:317
Definition: kb.h:45
static int redis_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
Definition: kb.c:1333
char path[0]
Definition: kb.c:65
gchar * nvti_excluded_keys(const nvti_t *n)
Get the excluded keys list.
Definition: nvti.c:359
static int select_database(struct kb_redis *kbr)
Select DB.
Definition: kb.c:247
gchar * nvti_mandatory_keys(const nvti_t *n)
Get the mandatory keys list.
Definition: nvti.c:345
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
#define GLOBAL_DBINDEX_NAME
Name of the namespace usage bitmap in redis.
Definition: kb.c:50
const struct kb_operations * kb_ops
Definition: kb.h:103
static int redis_lnk_reset(kb_t)
Reset connection to the KB. This is called after each fork() to make sure connections aren't shared b...
Definition: kb.c:1423
int nvti_set_oid(nvti_t *n, const gchar *oid)
Set the OID of a NVT Info.
Definition: nvti.c:471
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.
Definition: kb.c:487
int nvti_set_mandatory_keys(nvti_t *n, const gchar *mandatory_keys)
Set the mandatory keys of a NVT.
Definition: nvti.c:678
int nvti_set_name(nvti_t *n, const gchar *name)
Set the name of a NVT.
Definition: nvti.c:492
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
Definition: nvti.c:555
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.
Definition: kb.c:1152
Knowledge base management API - Redis backend.
size_t len
Definition: kb.h:89
static struct kb_item * redis2kbitem(const char *name, const redisReply *rep)
Fetch a KB item or list from a redis Reply.
Definition: kb.c:663
static size_t redis_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition: kb.c:1095
int nvti_set_bid(nvti_t *n, const gchar *bid)
Set the bid references of a NVT.
Definition: nvti.c:534
gchar * nvti_required_udp_ports(const nvti_t *n)
Get the required udp ports list.
Definition: nvti.c:387
struct kb kb
Definition: kb.c:61
static int get_redis_ctx(struct kb_redis *kbr)
Get redis context if it is already connected or do a a connection.
Definition: kb.c:339
int nvti_set_required_udp_ports(nvti_t *n, const gchar *required_udp_ports)
Set the required udp ports of a NVT.
Definition: nvti.c:753
gchar * dflt
Default value of the preference.
Definition: nvti.h:42
static int try_database_index(struct kb_redis *kbr, int index)
Attempt to atomically acquire ownership of a database.
Definition: kb.c:82
int v_int
Definition: kb.h:86
static redisReply * redis_cmd(struct kb_redis *kbr, const char *fmt,...)
Execute a redis command and get a redis reply.
Definition: kb.c:714
kb_item_type
Possible type of a kb_item.
Definition: kb.h:42
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.h:323
static int redis_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
Definition: kb.c:1311
static char * redis_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.c:793
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.
Definition: kb.c:1234
static int redis_add_int_unique(kb_t kb, const char *name, int val)
Insert (append) a new unique entry under a given name.
Definition: kb.c:1272
size_t namelen
Definition: kb.h:92
int redis_save(kb_t kb)
Save all the elements from the KB.
Definition: kb.c:1520
Subclass of struct kb, it contains the redis-specific fields, such as the redis context,...
Definition: kb.c:59
static int redis_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition: kb.c:415
static const struct kb_operations KBRedisOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:52
int nvti_set_excluded_keys(nvti_t *n, const gchar *excluded_keys)
Set the excluded keys of a NVT.
Definition: nvti.c:703
The structure for a preference of a NVT.
Definition: nvti.h:38
struct kb * kb_t
type abstraction to hide KB internals.
Definition: kb.h:109
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition: nvti.c:628
static char * redis_get_nvt(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition: kb.c:892
gchar * nvti_required_keys(const nvti_t *n)
Get the required keys list.
Definition: nvti.c:331
#define MAX_DB_INDEX__24
Max number of configured DB.
Definition: kb.c:113
char name[0]
Definition: kb.h:93
static kb_t redis_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition: kb.c:527
static int redis_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.c:456
static int redis_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition: kb.c:868
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:219
static int redis_get_kb_index(kb_t kb)
Return the kb index.
Definition: kb.c:440
gchar * nvti_bid(const nvti_t *n)
Get the bid references.
Definition: nvti.c:261
const struct kb_operations * KBDefaultOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:1618
static int fetch_max_db_index(struct kb_redis *kbr)
Set the number of databases have been configured into kbr struct.
Definition: kb.c:189
char * v_str
Definition: kb.h:85
int nvti_set_required_keys(nvti_t *n, const gchar *required_keys)
Set the required keys of a NVT.
Definition: nvti.c:653
static struct kb_item * redis_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
Definition: kb.c:998
int(* kb_new)(kb_t *, const char *)
Definition: kb.h:120
static int redis_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.c:1125
gchar * nvti_name(const nvti_t *n)
Get the name.
Definition: nvti.c:233
GSList * prefs
Collection of NVT preferences.
Definition: nvti.h:83