Greenbone Vulnerability Management Libraries  22.4.1
kb.h
Go to the documentation of this file.
1 /* Copyright (C) 2014-2022 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 #ifndef _GVM_KB_H
26 #define _GVM_KB_H
27 
28 #include "../base/nvti.h" /* for nvti_t */
29 
30 #include <assert.h>
31 #include <stddef.h> /* for NULL */
32 #include <sys/types.h> /* for size_t */
33 
37 #ifdef REDIS_SOCKET_PATH
38 #define KB_PATH_DEFAULT REDIS_SOCKET_PATH
39 #else
40 #define KB_PATH_DEFAULT "/run/redis/redis.sock"
41 #endif
42 
47 {
51  /* -- */
53 };
54 
59 {
76 };
77 
82 struct kb_item
83 {
84  enum kb_item_type type;
86  union
87  {
88  char *v_str;
89  int v_int;
90  };
92  size_t len;
93  struct kb_item *next;
95  size_t namelen;
96  char name[];
97 };
98 
99 struct kb_operations;
100 
104 struct kb
105 {
106  const struct kb_operations *kb_ops;
107 };
108 
112 typedef struct kb *kb_t;
113 
121 {
122  /* ctor/dtor */
123  int (*kb_new) (kb_t *, const char *);
124  int (*kb_delete) (kb_t);
125  kb_t (*kb_find) (const char *, const char *);
126  kb_t (*kb_direct_conn) (const char *, const int);
128  /* Actual kb operations */
132  struct kb_item *(*kb_get_single) (kb_t, const char *, enum kb_item_type);
136  char *(*kb_get_str) (kb_t, const char *);
140  int (*kb_get_int) (kb_t, const char *);
144  char *(*kb_get_nvt) (kb_t, const char *, enum kb_nvt_pos);
148  nvti_t *(*kb_get_nvt_all) (kb_t, const char *);
152  GSList *(*kb_get_nvt_oids) (kb_t);
156  int (*kb_push_str) (kb_t, const char *, const char *);
160  char *(*kb_pop_str) (kb_t, const char *);
165  struct kb_item *(*kb_get_all) (kb_t, const char *);
170  struct kb_item *(*kb_get_pattern) (kb_t, const char *);
175  size_t (*kb_count) (kb_t, const char *);
180  int (*kb_add_str) (kb_t, const char *, const char *, size_t);
185  int (*kb_add_str_unique) (kb_t, const char *, const char *, size_t, int);
190  int (*kb_add_str_unique_volatile) (kb_t, const char *, const char *, int,
191  size_t, int);
196  int (*kb_set_str) (kb_t, const char *, const char *, size_t);
201  int (*kb_add_int) (kb_t, const char *, int);
206  int (*kb_add_int_unique) (kb_t, const char *, int);
211  int (*kb_add_int_unique_volatile) (kb_t, const char *, int, int);
216  int (*kb_set_int) (kb_t, const char *, int);
221  int (*kb_add_nvt) (kb_t, const nvti_t *, const char *);
226  int (*kb_del_items) (kb_t, const char *);
227 
228  /* Utils */
229  int (*kb_save) (kb_t);
230  int (*kb_lnk_reset) (kb_t);
231  int (*kb_flush) (kb_t, const char *);
233 };
234 
240 extern const struct kb_operations *KBDefaultOperations;
241 
245 void
246 kb_item_free (struct kb_item *);
247 
254 static inline int
255 kb_new (kb_t *kb, const char *kb_path)
256 {
257  assert (kb);
258  assert (KBDefaultOperations);
259  assert (KBDefaultOperations->kb_new);
260 
261  *kb = NULL;
262 
263  return KBDefaultOperations->kb_new (kb, kb_path);
264 }
265 
272 static inline kb_t
273 kb_direct_conn (const char *kb_path, const int kb_index)
274 {
275  assert (KBDefaultOperations);
277 
278  return KBDefaultOperations->kb_direct_conn (kb_path, kb_index);
279 }
280 
287 static inline kb_t
288 kb_find (const char *kb_path, const char *key)
289 {
290  assert (KBDefaultOperations);
291  assert (KBDefaultOperations->kb_find);
292 
293  return KBDefaultOperations->kb_find (kb_path, key);
294 }
295 
301 static inline int
303 {
304  assert (kb);
305  assert (kb->kb_ops);
306  assert (kb->kb_ops->kb_delete);
307 
308  return kb->kb_ops->kb_delete (kb);
309 }
310 
319 static inline struct kb_item *
321 {
322  assert (kb);
323  assert (kb->kb_ops);
324  assert (kb->kb_ops->kb_get_single);
325 
326  return kb->kb_ops->kb_get_single (kb, name, type);
327 }
328 
335 static inline char *
336 kb_item_get_str (kb_t kb, const char *name)
337 {
338  assert (kb);
339  assert (kb->kb_ops);
340  assert (kb->kb_ops->kb_get_str);
341 
342  return kb->kb_ops->kb_get_str (kb, name);
343 }
344 
351 static inline int
352 kb_item_get_int (kb_t kb, const char *name)
353 {
354  assert (kb);
355  assert (kb->kb_ops);
356  assert (kb->kb_ops->kb_get_int);
357 
358  return kb->kb_ops->kb_get_int (kb, name);
359 }
360 
368 static inline struct kb_item *
369 kb_item_get_all (kb_t kb, const char *name)
370 {
371  assert (kb);
372  assert (kb->kb_ops);
373  assert (kb->kb_ops->kb_get_all);
374 
375  return kb->kb_ops->kb_get_all (kb, name);
376 }
377 
385 static inline struct kb_item *
386 kb_item_get_pattern (kb_t kb, const char *pattern)
387 {
388  assert (kb);
389  assert (kb->kb_ops);
390  assert (kb->kb_ops->kb_get_pattern);
391 
392  return kb->kb_ops->kb_get_pattern (kb, pattern);
393 }
394 
402 static inline int
403 kb_item_push_str (kb_t kb, const char *name, const char *value)
404 {
405  assert (kb);
406  assert (kb->kb_ops);
407  assert (kb->kb_ops->kb_push_str);
408 
409  return kb->kb_ops->kb_push_str (kb, name, value);
410 }
411 
419 static inline char *
420 kb_item_pop_str (kb_t kb, const char *name)
421 {
422  assert (kb);
423  assert (kb->kb_ops);
424  assert (kb->kb_ops->kb_pop_str);
425 
426  return kb->kb_ops->kb_pop_str (kb, name);
427 }
428 
437 static inline size_t
438 kb_item_count (kb_t kb, const char *pattern)
439 {
440  assert (kb);
441  assert (kb->kb_ops);
442  assert (kb->kb_ops->kb_count);
443 
444  return kb->kb_ops->kb_count (kb, pattern);
445 }
446 
455 static inline int
456 kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
457 {
458  assert (kb);
459  assert (kb->kb_ops);
460  assert (kb->kb_ops->kb_add_str);
461 
462  return kb->kb_ops->kb_add_str (kb, name, str, len);
463 }
464 
475 static inline int
476 kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len,
477  int pos)
478 {
479  assert (kb);
480  assert (kb->kb_ops);
481  assert (kb->kb_ops->kb_add_str_unique);
482 
483  return kb->kb_ops->kb_add_str_unique (kb, name, str, len, pos);
484 }
485 
497 static inline int
498 kb_add_str_unique_volatile (kb_t kb, const char *name, const char *str,
499  int expire, size_t len, int pos)
500 {
501  assert (kb);
502  assert (KBDefaultOperations);
504 
505  return KBDefaultOperations->kb_add_str_unique_volatile (kb, name, str, expire,
506  len, pos);
507 }
508 
517 static inline int
518 kb_item_set_str (kb_t kb, const char *name, const char *str, size_t len)
519 {
520  assert (kb);
521  assert (kb->kb_ops);
522  assert (kb->kb_ops->kb_set_str);
523 
524  return kb->kb_ops->kb_set_str (kb, name, str, len);
525 }
526 
534 static inline int
535 kb_item_add_int (kb_t kb, const char *name, int val)
536 {
537  assert (kb);
538  assert (kb->kb_ops);
539  assert (kb->kb_ops->kb_add_int);
540 
541  return kb->kb_ops->kb_add_int (kb, name, val);
542 }
543 
551 static inline int
552 kb_item_add_int_unique (kb_t kb, const char *name, int val)
553 {
554  assert (kb);
555  assert (kb->kb_ops);
556  assert (kb->kb_ops->kb_add_int_unique);
557 
558  return kb->kb_ops->kb_add_int_unique (kb, name, val);
559 }
560 
570 static inline int
571 kb_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire)
572 {
573  assert (kb);
574  assert (KBDefaultOperations);
576 
578  expire);
579 }
580 
588 static inline int
589 kb_item_set_int (kb_t kb, const char *name, int val)
590 {
591  assert (kb);
592  assert (kb->kb_ops);
593  assert (kb->kb_ops->kb_set_int);
594 
595  return kb->kb_ops->kb_set_int (kb, name, val);
596 }
597 
605 static inline int
606 kb_nvt_add (kb_t kb, const nvti_t *nvt, const char *filename)
607 {
608  assert (kb);
609  assert (kb->kb_ops);
610  assert (kb->kb_ops->kb_add_nvt);
611 
612  return kb->kb_ops->kb_add_nvt (kb, nvt, filename);
613 }
614 
622 static inline char *
623 kb_nvt_get (kb_t kb, const char *oid, enum kb_nvt_pos position)
624 {
625  assert (kb);
626  assert (kb->kb_ops);
627  assert (kb->kb_ops->kb_get_nvt);
628 
629  return kb->kb_ops->kb_get_nvt (kb, oid, position);
630 }
631 
638 static inline nvti_t *
639 kb_nvt_get_all (kb_t kb, const char *oid)
640 {
641  assert (kb);
642  assert (kb->kb_ops);
643  assert (kb->kb_ops->kb_get_nvt_all);
644 
645  return kb->kb_ops->kb_get_nvt_all (kb, oid);
646 }
647 
653 static inline GSList *
655 {
656  assert (kb);
657  assert (kb->kb_ops);
658  assert (kb->kb_ops->kb_get_nvt_oids);
659 
660  return kb->kb_ops->kb_get_nvt_oids (kb);
661 }
662 
669 static inline int
670 kb_del_items (kb_t kb, const char *name)
671 {
672  assert (kb);
673  assert (kb->kb_ops);
674  assert (kb->kb_ops->kb_del_items);
675 
676  return kb->kb_ops->kb_del_items (kb, name);
677 }
678 
684 static inline int
686 {
687  int rc = 0;
688 
689  assert (kb);
690  assert (kb->kb_ops);
691 
692  if (kb->kb_ops->kb_save != NULL)
693  rc = kb->kb_ops->kb_save (kb);
694 
695  return rc;
696 }
697 
704 static inline int
706 {
707  int rc = 0;
708 
709  assert (kb);
710  assert (kb->kb_ops);
711 
712  if (kb->kb_ops->kb_lnk_reset != NULL)
713  rc = kb->kb_ops->kb_lnk_reset (kb);
714 
715  return rc;
716 }
717 
724 static inline int
725 kb_flush (kb_t kb, const char *except)
726 {
727  int rc = 0;
728 
729  assert (kb);
730  assert (kb->kb_ops);
731 
732  if (kb->kb_ops->kb_flush != NULL)
733  rc = kb->kb_ops->kb_flush (kb, except);
734 
735  return rc;
736 }
737 
743 static inline int
745 {
746  assert (kb);
747  assert (kb->kb_ops);
748  assert (kb->kb_ops->kb_get_kb_index);
749 
750  return kb->kb_ops->kb_get_kb_index (kb);
751 }
752 
753 #endif
static nvti_t * kb_nvt_get_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.h:639
static int kb_item_set_int(kb_t kb, const char *name, int val)
Set (replace) a new entry under a given name.
Definition: kb.h:589
static int kb_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.h:670
static int kb_item_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.
Definition: kb.h:476
static int kb_nvt_add(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition: kb.h:606
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.h:336
static int kb_item_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.h:456
static struct kb_item * kb_item_get_all(kb_t kb, const char *name)
Get all items stored under a given name.
Definition: kb.h:369
kb_nvt_pos
Possible positions of nvt values in cache list.
Definition: kb.h:59
@ NVT_FAMILY_POS
Definition: kb.h:72
@ NVT_CATEGORY_POS
Definition: kb.h:71
@ NVT_TIMESTAMP_POS
Definition: kb.h:74
@ NVT_NAME_POS
Definition: kb.h:73
@ NVT_TAGS_POS
Definition: kb.h:67
@ NVT_BIDS_POS
Definition: kb.h:69
@ NVT_EXCLUDED_KEYS_POS
Definition: kb.h:63
@ NVT_REQUIRED_PORTS_POS
Definition: kb.h:65
@ NVT_REQUIRED_UDP_PORTS_POS
Definition: kb.h:64
@ NVT_FILENAME_POS
Definition: kb.h:60
@ NVT_OID_POS
Definition: kb.h:75
@ NVT_DEPENDENCIES_POS
Definition: kb.h:66
@ NVT_CVES_POS
Definition: kb.h:68
@ NVT_REQUIRED_KEYS_POS
Definition: kb.h:61
@ NVT_XREFS_POS
Definition: kb.h:70
@ NVT_MANDATORY_KEYS_POS
Definition: kb.h:62
static int kb_item_push_str(kb_t kb, const char *name, const char *value)
Push a new value under a given key.
Definition: kb.h:403
static int kb_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.h:255
static char * kb_nvt_get(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition: kb.h:623
static size_t kb_item_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition: kb.h:438
static int kb_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition: kb.h:302
static int kb_item_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition: kb.h:352
struct kb * kb_t
type abstraction to hide KB internals.
Definition: kb.h:112
static char * kb_item_pop_str(kb_t kb, const char *name)
Pop a single KB string item.
Definition: kb.h:420
static kb_t kb_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition: kb.h:288
static int kb_item_set_str(kb_t kb, const char *name, const char *str, size_t len)
Set (replace) a new entry under a given name.
Definition: kb.h:518
static int kb_add_int_unique_volatile(kb_t kb, const char *name, int val, int expire)
Insert (append) a new unique and volatile entry under a given name.
Definition: kb.h:571
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
Definition: kb.c:647
static int kb_save(kb_t kb)
Save all the KB's content.
Definition: kb.h:685
static int kb_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.
Definition: kb.h:498
static int kb_item_add_int(kb_t kb, const char *name, int val)
Insert (append) a new entry under a given name.
Definition: kb.h:535
static int kb_lnk_reset(kb_t kb)
Reset connection to the KB. This is called after each fork() to make sure connections aren't shared b...
Definition: kb.h:705
const struct kb_operations * KBDefaultOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition: kb.c:1888
static GSList * kb_nvt_get_oids(kb_t kb)
Get list of NVT OIDs.
Definition: kb.h:654
kb_item_type
Possible type of a kb_item.
Definition: kb.h:47
@ KB_TYPE_INT
Definition: kb.h:49
@ KB_TYPE_CNT
Definition: kb.h:52
@ KB_TYPE_UNSPEC
Definition: kb.h:48
@ KB_TYPE_STR
Definition: kb.h:50
static struct kb_item * kb_item_get_pattern(kb_t kb, const char *pattern)
Get all items stored under a given pattern.
Definition: kb.h:386
static struct kb_item * kb_item_get_single(kb_t kb, const char *name, enum kb_item_type type)
Get a single KB element.
Definition: kb.h:320
static int kb_flush(kb_t kb, const char *except)
Flush all the KB's content. Delete all namespaces.
Definition: kb.h:725
static int kb_item_add_int_unique(kb_t kb, const char *name, int val)
Insert (append) a new unique entry under a given name.
Definition: kb.h:552
static kb_t kb_direct_conn(const char *kb_path, const int kb_index)
Connect to a Knowledge Base object which has the given kb_index.
Definition: kb.h:273
static int kb_get_kb_index(kb_t kb)
Return the kb index.
Definition: kb.h:744
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:83
char name[]
Definition: kb.h:96
int v_int
Definition: kb.h:89
enum kb_item_type type
Definition: kb.h:84
char * v_str
Definition: kb.h:88
struct kb_item * next
Definition: kb.h:93
size_t len
Definition: kb.h:92
size_t namelen
Definition: kb.h:95
KB interface. Functions provided by an implementation. All functions have to be provided,...
Definition: kb.h:121
GSList *(* kb_get_nvt_oids)(kb_t)
Definition: kb.h:152
struct kb_item *(* kb_get_single)(kb_t, const char *, enum kb_item_type)
Definition: kb.h:132
struct kb_item *(* kb_get_pattern)(kb_t, const char *)
Definition: kb.h:170
int(* kb_set_int)(kb_t, const char *, int)
Definition: kb.h:216
int(* kb_flush)(kb_t, const char *)
Definition: kb.h:231
int(* kb_get_int)(kb_t, const char *)
Definition: kb.h:140
int(* kb_add_str)(kb_t, const char *, const char *, size_t)
Definition: kb.h:180
struct kb_item *(* kb_get_all)(kb_t, const char *)
Definition: kb.h:165
int(* kb_add_str_unique_volatile)(kb_t, const char *, const char *, int, size_t, int)
Definition: kb.h:190
int(* kb_add_nvt)(kb_t, const nvti_t *, const char *)
Definition: kb.h:221
int(* kb_lnk_reset)(kb_t)
Definition: kb.h:230
char *(* kb_get_nvt)(kb_t, const char *, enum kb_nvt_pos)
Definition: kb.h:144
int(* kb_new)(kb_t *, const char *)
Definition: kb.h:123
int(* kb_push_str)(kb_t, const char *, const char *)
Definition: kb.h:156
char *(* kb_pop_str)(kb_t, const char *)
Definition: kb.h:160
nvti_t *(* kb_get_nvt_all)(kb_t, const char *)
Definition: kb.h:148
int(* kb_delete)(kb_t)
Definition: kb.h:124
size_t(* kb_count)(kb_t, const char *)
Definition: kb.h:175
int(* kb_add_int_unique)(kb_t, const char *, int)
Definition: kb.h:206
int(* kb_add_str_unique)(kb_t, const char *, const char *, size_t, int)
Definition: kb.h:185
int(* kb_get_kb_index)(kb_t)
Definition: kb.h:232
int(* kb_add_int)(kb_t, const char *, int)
Definition: kb.h:201
kb_t(* kb_direct_conn)(const char *, const int)
Definition: kb.h:126
char *(* kb_get_str)(kb_t, const char *)
Definition: kb.h:136
int(* kb_save)(kb_t)
Definition: kb.h:229
kb_t(* kb_find)(const char *, const char *)
Definition: kb.h:125
int(* kb_set_str)(kb_t, const char *, const char *, size_t)
Definition: kb.h:196
int(* kb_del_items)(kb_t, const char *)
Definition: kb.h:226
int(* kb_add_int_unique_volatile)(kb_t, const char *, int, int)
Definition: kb.h:211
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:105
const struct kb_operations * kb_ops
Definition: kb.h:106
The structure of a information record that corresponds to a NVT.
Definition: nvti.c:408