Greenbone Vulnerability Management Libraries: util/kb.h Source File

Greenbone Vulnerability Management Libraries 23.1.1

kb.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#ifndef _GVM_UTIL_KB_H
12#define _GVM_UTIL_KB_H
13
14#include "../base/nvti.h" /* for nvti_t */
15
16#include <assert.h>
17#include <stddef.h> /* for NULL */
18#include <sys/types.h> /* for size_t */
19
23#ifdef REDIS_SOCKET_PATH
24#define KB_PATH_DEFAULT REDIS_SOCKET_PATH
25#else
26#define KB_PATH_DEFAULT "/run/redis/redis.sock"
27#endif
28
32 enum kb_item_type
33{
34 KB_TYPE_UNSPEC,
35 KB_TYPE_INT,
36 KB_TYPE_STR,
37 /* -- */
38 KB_TYPE_CNT,
39};
40
44 enum kb_nvt_pos
45{
46 NVT_FILENAME_POS,
47 NVT_REQUIRED_KEYS_POS,
48 NVT_MANDATORY_KEYS_POS,
49 NVT_EXCLUDED_KEYS_POS,
50 NVT_REQUIRED_UDP_PORTS_POS,
51 NVT_REQUIRED_PORTS_POS,
52 NVT_DEPENDENCIES_POS,
53 NVT_TAGS_POS,
54 NVT_CVES_POS,
55 NVT_BIDS_POS,
56 NVT_XREFS_POS,
57 NVT_CATEGORY_POS,
58 NVT_FAMILY_POS,
59 NVT_NAME_POS,
60 NVT_TIMESTAMP_POS,
61 NVT_OID_POS,
62};
63
68 struct kb_item
69{
70 enum kb_item_type type;
71
72 union
73 {
74 char *v_str;
75 int v_int;
76 };
77
78 size_t len;
79 struct kb_item *next;
80
81 size_t namelen;
82 char name[];
83};
84
85struct kb_operations;
86
90 struct kb
91{
92 const struct kb_operations *kb_ops;
93};
94
98 typedef struct kb *kb_t;
99
106 struct kb_operations
107{
108 /* ctor/dtor */
109 int (*kb_new) (kb_t *, const char *);
110 int (*kb_delete) (kb_t);
111 kb_t (*kb_find) (const char *, const char *);
112 kb_t (*kb_direct_conn) (const char *, const int);
113
114 /* Actual kb operations */
118 struct kb_item *(*kb_get_single) (kb_t, const char *, enum kb_item_type);
122 char *(*kb_get_str) (kb_t, const char *);
126 int (*kb_get_int) (kb_t, const char *);
130 char *(*kb_get_nvt) (kb_t, const char *, enum kb_nvt_pos);
134 nvti_t *(*kb_get_nvt_all) (kb_t, const char *);
138 GSList *(*kb_get_nvt_oids) (kb_t);
142 int (*kb_push_str) (kb_t, const char *, const char *);
146 char *(*kb_pop_str) (kb_t, const char *);
151 struct kb_item *(*kb_get_all) (kb_t, const char *);
156 struct kb_item *(*kb_get_pattern) (kb_t, const char *);
161 size_t (*kb_count) (kb_t, const char *);
166 int (*kb_add_str) (kb_t, const char *, const char *, size_t);
171 int (*kb_add_str_unique) (kb_t, const char *, const char *, size_t, int);
176 int (*kb_add_str_unique_volatile) (kb_t, const char *, const char *, int,
177 size_t, int);
182 int (*kb_set_str) (kb_t, const char *, const char *, size_t);
187 int (*kb_add_int) (kb_t, const char *, int);
192 int (*kb_add_int_unique) (kb_t, const char *, int);
197 int (*kb_add_int_unique_volatile) (kb_t, const char *, int, int);
202 int (*kb_set_int) (kb_t, const char *, int);
207 int (*kb_add_nvt) (kb_t, const nvti_t *, const char *);
212 int (*kb_del_items) (kb_t, const char *);
213
214 /* Utils */
215 int (*kb_save) (kb_t);
216 int (*kb_lnk_reset) (kb_t);
217 int (*kb_flush) (kb_t, const char *);
218 int (*kb_get_kb_index) (kb_t);
219};
220
226extern const struct kb_operations *KBDefaultOperations;
227
231void
232 kb_item_free (struct kb_item *);
233
242static inline int
243kb_new (kb_t *kb, const char *kb_path)
244{
245 assert (kb);
246 assert (KBDefaultOperations);
247 assert (KBDefaultOperations->kb_new);
248
249 *kb = NULL;
250
251 return KBDefaultOperations->kb_new (kb, kb_path);
252}
253
262static inline kb_t
263kb_direct_conn (const char *kb_path, const int kb_index)
264{
265 assert (KBDefaultOperations);
266 assert (KBDefaultOperations->kb_direct_conn);
267
268 return KBDefaultOperations->kb_direct_conn (kb_path, kb_index);
269}
270
279static inline kb_t
280kb_find (const char *kb_path, const char *key)
281{
282 assert (KBDefaultOperations);
283 assert (KBDefaultOperations->kb_find);
284
285 return KBDefaultOperations->kb_find (kb_path, key);
286}
287
295static inline int
296kb_delete (kb_t kb)
297{
298 assert (kb);
299 assert (kb->kb_ops);
300 assert (kb->kb_ops->kb_delete);
301
302 return kb->kb_ops->kb_delete (kb);
303}
304
315static inline struct kb_item *
316kb_item_get_single (kb_t kb, const char *name, enum kb_item_type type)
317{
318 assert (kb);
319 assert (kb->kb_ops);
320 assert (kb->kb_ops->kb_get_single);
321
322 return kb->kb_ops->kb_get_single (kb, name, type);
323}
324
333static inline char *
334kb_item_get_str (kb_t kb, const char *name)
335{
336 assert (kb);
337 assert (kb->kb_ops);
338 assert (kb->kb_ops->kb_get_str);
339
340 return kb->kb_ops->kb_get_str (kb, name);
341}
342
351static inline int
352kb_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
370static inline struct kb_item *
371kb_item_get_all (kb_t kb, const char *name)
372{
373 assert (kb);
374 assert (kb->kb_ops);
375 assert (kb->kb_ops->kb_get_all);
376
377 return kb->kb_ops->kb_get_all (kb, name);
378}
379
389static inline struct kb_item *
390kb_item_get_pattern (kb_t kb, const char *pattern)
391{
392 assert (kb);
393 assert (kb->kb_ops);
394 assert (kb->kb_ops->kb_get_pattern);
395
396 return kb->kb_ops->kb_get_pattern (kb, pattern);
397}
398
408static inline int
409kb_item_push_str (kb_t kb, const char *name, const char *value)
410{
411 assert (kb);
412 assert (kb->kb_ops);
413 assert (kb->kb_ops->kb_push_str);
414
415 return kb->kb_ops->kb_push_str (kb, name, value);
416}
417
427static inline char *
428kb_item_pop_str (kb_t kb, const char *name)
429{
430 assert (kb);
431 assert (kb->kb_ops);
432 assert (kb->kb_ops->kb_pop_str);
433
434 return kb->kb_ops->kb_pop_str (kb, name);
435}
436
447static inline size_t
448kb_item_count (kb_t kb, const char *pattern)
449{
450 assert (kb);
451 assert (kb->kb_ops);
452 assert (kb->kb_ops->kb_count);
453
454 return kb->kb_ops->kb_count (kb, pattern);
455}
456
467static inline int
468kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
469{
470 assert (kb);
471 assert (kb->kb_ops);
472 assert (kb->kb_ops->kb_add_str);
473
474 return kb->kb_ops->kb_add_str (kb, name, str, len);
475}
476
489static inline int
490kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len,
491 int pos)
492{
493 assert (kb);
494 assert (kb->kb_ops);
495 assert (kb->kb_ops->kb_add_str_unique);
496
497 return kb->kb_ops->kb_add_str_unique (kb, name, str, len, pos);
498}
499
515static inline int
516kb_add_str_unique_volatile (kb_t kb, const char *name, const char *str,
517 int expire, size_t len, int pos)
518{
519 assert (kb);
520 assert (KBDefaultOperations);
521 assert (KBDefaultOperations->kb_add_str_unique_volatile);
522
523 return KBDefaultOperations->kb_add_str_unique_volatile (kb, name, str, expire,
524 len, pos);
525}
526
537static inline int
538kb_item_set_str (kb_t kb, const char *name, const char *str, size_t len)
539{
540 assert (kb);
541 assert (kb->kb_ops);
542 assert (kb->kb_ops->kb_set_str);
543
544 return kb->kb_ops->kb_set_str (kb, name, str, len);
545}
546
556static inline int
557kb_item_add_int (kb_t kb, const char *name, int val)
558{
559 assert (kb);
560 assert (kb->kb_ops);
561 assert (kb->kb_ops->kb_add_int);
562
563 return kb->kb_ops->kb_add_int (kb, name, val);
564}
565
575static inline int
576kb_item_add_int_unique (kb_t kb, const char *name, int val)
577{
578 assert (kb);
579 assert (kb->kb_ops);
580 assert (kb->kb_ops->kb_add_int_unique);
581
582 return kb->kb_ops->kb_add_int_unique (kb, name, val);
583}
584
596static inline int
597kb_add_int_unique_volatile (kb_t kb, const char *name, int val, int expire)
598{
599 assert (kb);
600 assert (KBDefaultOperations);
601 assert (KBDefaultOperations->kb_add_int_unique_volatile);
602
603 return KBDefaultOperations->kb_add_int_unique_volatile (kb, name, val,
604 expire);
605}
606
616static inline int
617kb_item_set_int (kb_t kb, const char *name, int val)
618{
619 assert (kb);
620 assert (kb->kb_ops);
621 assert (kb->kb_ops->kb_set_int);
622
623 return kb->kb_ops->kb_set_int (kb, name, val);
624}
625
635static inline int
636kb_nvt_add (kb_t kb, const nvti_t *nvt, const char *filename)
637{
638 assert (kb);
639 assert (kb->kb_ops);
640 assert (kb->kb_ops->kb_add_nvt);
641
642 return kb->kb_ops->kb_add_nvt (kb, nvt, filename);
643}
644
654static inline char *
655kb_nvt_get (kb_t kb, const char *oid, enum kb_nvt_pos position)
656{
657 assert (kb);
658 assert (kb->kb_ops);
659 assert (kb->kb_ops->kb_get_nvt);
660
661 return kb->kb_ops->kb_get_nvt (kb, oid, position);
662}
663
672static inline nvti_t *
673kb_nvt_get_all (kb_t kb, const char *oid)
674{
675 assert (kb);
676 assert (kb->kb_ops);
677 assert (kb->kb_ops->kb_get_nvt_all);
678
679 return kb->kb_ops->kb_get_nvt_all (kb, oid);
680}
681
689static inline GSList *
690kb_nvt_get_oids (kb_t kb)
691{
692 assert (kb);
693 assert (kb->kb_ops);
694 assert (kb->kb_ops->kb_get_nvt_oids);
695
696 return kb->kb_ops->kb_get_nvt_oids (kb);
697}
698
707static inline int
708kb_del_items (kb_t kb, const char *name)
709{
710 assert (kb);
711 assert (kb->kb_ops);
712 assert (kb->kb_ops->kb_del_items);
713
714 return kb->kb_ops->kb_del_items (kb, name);
715}
716
724static inline int
725kb_save (kb_t kb)
726{
727 int rc = 0;
728
729 assert (kb);
730 assert (kb->kb_ops);
731
732 if (kb->kb_ops->kb_save != NULL)
733 rc = kb->kb_ops->kb_save (kb);
734
735 return rc;
736}
737
746static inline int
747kb_lnk_reset (kb_t kb)
748{
749 int rc = 0;
750
751 assert (kb);
752 assert (kb->kb_ops);
753
754 if (kb->kb_ops->kb_lnk_reset != NULL)
755 rc = kb->kb_ops->kb_lnk_reset (kb);
756
757 return rc;
758}
759
768static inline int
769kb_flush (kb_t kb, const char *except)
770{
771 int rc = 0;
772
773 assert (kb);
774 assert (kb->kb_ops);
775
776 if (kb->kb_ops->kb_flush != NULL)
777 rc = kb->kb_ops->kb_flush (kb, except);
778
779 return rc;
780}
781
789static inline int
790kb_get_kb_index (kb_t kb)
791{
792 assert (kb);
793 assert (kb->kb_ops);
794 assert (kb->kb_ops->kb_get_kb_index);
795
796 return kb->kb_ops->kb_get_kb_index (kb);
797}
798
799#endif
KBDefaultOperations
const struct kb_operations * KBDefaultOperations
Default KB operations. No selection mechanism is provided yet since there's only one implementation (...
Definition kb.c:1881
kb_item_set_int
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:617
kb_del_items
static int kb_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition kb.h:708
kb_item_add_str_unique
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:490
kb_nvt_add
static int kb_nvt_add(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition kb.h:636
kb_item_add_str
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:468
kb_nvt_pos
kb_nvt_pos
Possible positions of nvt values in cache list.
Definition kb.h:45
NVT_FAMILY_POS
@ NVT_FAMILY_POS
Definition kb.h:58
NVT_CATEGORY_POS
@ NVT_CATEGORY_POS
Definition kb.h:57
NVT_TIMESTAMP_POS
@ NVT_TIMESTAMP_POS
Definition kb.h:60
NVT_NAME_POS
@ NVT_NAME_POS
Definition kb.h:59
NVT_TAGS_POS
@ NVT_TAGS_POS
Definition kb.h:53
NVT_BIDS_POS
@ NVT_BIDS_POS
Definition kb.h:55
NVT_EXCLUDED_KEYS_POS
@ NVT_EXCLUDED_KEYS_POS
Definition kb.h:49
NVT_REQUIRED_PORTS_POS
@ NVT_REQUIRED_PORTS_POS
Definition kb.h:51
NVT_REQUIRED_UDP_PORTS_POS
@ NVT_REQUIRED_UDP_PORTS_POS
Definition kb.h:50
NVT_FILENAME_POS
@ NVT_FILENAME_POS
Definition kb.h:46
NVT_OID_POS
@ NVT_OID_POS
Definition kb.h:61
NVT_DEPENDENCIES_POS
@ NVT_DEPENDENCIES_POS
Definition kb.h:52
NVT_CVES_POS
@ NVT_CVES_POS
Definition kb.h:54
NVT_REQUIRED_KEYS_POS
@ NVT_REQUIRED_KEYS_POS
Definition kb.h:47
NVT_XREFS_POS
@ NVT_XREFS_POS
Definition kb.h:56
NVT_MANDATORY_KEYS_POS
@ NVT_MANDATORY_KEYS_POS
Definition kb.h:48
kb_item_push_str
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:409
kb_new
static int kb_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition kb.h:243
kb_item_count
static size_t kb_item_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition kb.h:448
kb_delete
static int kb_delete(kb_t kb)
Delete all entries and release ownership on the namespace.
Definition kb.h:296
kb_item_get_int
static int kb_item_get_int(kb_t kb, const char *name)
Get a single KB integer item.
Definition kb.h:352
kb_t
struct kb * kb_t
type abstraction to hide KB internals.
Definition kb.h:98
kb_nvt_get_all
static nvti_t * kb_nvt_get_all(kb_t kb, const char *oid)
Get a full NVT.
Definition kb.h:673
kb_find
static kb_t kb_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition kb.h:280
kb_nvt_get_oids
static GSList * kb_nvt_get_oids(kb_t kb)
Get list of NVT OIDs.
Definition kb.h:690
kb_item_pop_str
static char * kb_item_pop_str(kb_t kb, const char *name)
Pop a single KB string item.
Definition kb.h:428
kb_item_set_str
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:538
kb_add_int_unique_volatile
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:597
kb_nvt_get
static char * kb_nvt_get(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition kb.h:655
kb_item_free
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
Definition kb.c:643
kb_save
static int kb_save(kb_t kb)
Save all the KB's content.
Definition kb.h:725
kb_item_get_single
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:316
kb_add_str_unique_volatile
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:516
kb_item_add_int
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:557
kb_lnk_reset
static int kb_lnk_reset(kb_t kb)
Reset connection to the KB. This is called after each fork() to make.
Definition kb.h:747
kb_item_get_pattern
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:390
kb_item_type
kb_item_type
Possible type of a kb_item.
Definition kb.h:33
KB_TYPE_INT
@ KB_TYPE_INT
Definition kb.h:35
KB_TYPE_CNT
@ KB_TYPE_CNT
Definition kb.h:38
KB_TYPE_UNSPEC
@ KB_TYPE_UNSPEC
Definition kb.h:34
KB_TYPE_STR
@ KB_TYPE_STR
Definition kb.h:36
kb_flush
static int kb_flush(kb_t kb, const char *except)
Flush all the KB's content. Delete all namespaces.
Definition kb.h:769
kb_item_get_all
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:371
kb_item_add_int_unique
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:576
kb_item_get_str
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition kb.h:334
kb_direct_conn
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:263
kb_get_kb_index
static int kb_get_kb_index(kb_t kb)
Return the kb index.
Definition kb.h:790
nvti_t
struct nvti nvti_t
The structure of a information record that corresponds to a NVT.
nvti.h
Protos and data structures for NVT Information data sets.
kb_item
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition kb.h:69
kb_item::name
char name[]
Definition kb.h:82
kb_item::v_int
int v_int
Definition kb.h:75
kb_item::type
enum kb_item_type type
Definition kb.h:70
kb_item::v_str
char * v_str
Definition kb.h:74
kb_item::next
struct kb_item * next
Definition kb.h:79
kb_item::len
size_t len
Definition kb.h:78
kb_item::namelen
size_t namelen
Definition kb.h:81
kb_operations
KB interface. Functions provided by an implementation. All functions have to be provided,...
Definition kb.h:107
kb_operations::kb_get_single
struct kb_item *(* kb_get_single)(kb_t, const char *, enum kb_item_type)
Definition kb.h:118
kb_operations::kb_set_int
int(* kb_set_int)(kb_t, const char *, int)
Definition kb.h:202
kb_operations::kb_flush
int(* kb_flush)(kb_t, const char *)
Definition kb.h:217
kb_operations::kb_get_int
int(* kb_get_int)(kb_t, const char *)
Definition kb.h:126
kb_operations::kb_add_str
int(* kb_add_str)(kb_t, const char *, const char *, size_t)
Definition kb.h:166
kb_operations::kb_get_nvt_all
nvti_t *(* kb_get_nvt_all)(kb_t, const char *)
Definition kb.h:134
kb_operations::kb_add_str_unique_volatile
int(* kb_add_str_unique_volatile)(kb_t, const char *, const char *, int, size_t, int)
Definition kb.h:176
kb_operations::kb_get_nvt
char *(* kb_get_nvt)(kb_t, const char *, enum kb_nvt_pos)
Definition kb.h:130
kb_operations::kb_get_nvt_oids
GSList *(* kb_get_nvt_oids)(kb_t)
Definition kb.h:138
kb_operations::kb_add_nvt
int(* kb_add_nvt)(kb_t, const nvti_t *, const char *)
Definition kb.h:207
kb_operations::kb_get_pattern
struct kb_item *(* kb_get_pattern)(kb_t, const char *)
Definition kb.h:156
kb_operations::kb_lnk_reset
int(* kb_lnk_reset)(kb_t)
Definition kb.h:216
kb_operations::kb_new
int(* kb_new)(kb_t *, const char *)
Definition kb.h:109
kb_operations::kb_push_str
int(* kb_push_str)(kb_t, const char *, const char *)
Definition kb.h:142
kb_operations::kb_delete
int(* kb_delete)(kb_t)
Definition kb.h:110
kb_operations::kb_get_str
char *(* kb_get_str)(kb_t, const char *)
Definition kb.h:122
kb_operations::kb_count
size_t(* kb_count)(kb_t, const char *)
Definition kb.h:161
kb_operations::kb_pop_str
char *(* kb_pop_str)(kb_t, const char *)
Definition kb.h:146
kb_operations::kb_add_int_unique
int(* kb_add_int_unique)(kb_t, const char *, int)
Definition kb.h:192
kb_operations::kb_add_str_unique
int(* kb_add_str_unique)(kb_t, const char *, const char *, size_t, int)
Definition kb.h:171
kb_operations::kb_get_kb_index
int(* kb_get_kb_index)(kb_t)
Definition kb.h:218
kb_operations::kb_add_int
int(* kb_add_int)(kb_t, const char *, int)
Definition kb.h:187
kb_operations::kb_direct_conn
kb_t(* kb_direct_conn)(const char *, const int)
Definition kb.h:112
kb_operations::kb_save
int(* kb_save)(kb_t)
Definition kb.h:215
kb_operations::kb_find
kb_t(* kb_find)(const char *, const char *)
Definition kb.h:111
kb_operations::kb_set_str
int(* kb_set_str)(kb_t, const char *, const char *, size_t)
Definition kb.h:182
kb_operations::kb_del_items
int(* kb_del_items)(kb_t, const char *)
Definition kb.h:212
kb_operations::kb_add_int_unique_volatile
int(* kb_add_int_unique_volatile)(kb_t, const char *, int, int)
Definition kb.h:197
kb_operations::kb_get_all
struct kb_item *(* kb_get_all)(kb_t, const char *)
Definition kb.h:151
kb
Top-level KB. This is to be inherited by KB implementations.
Definition kb.h:91
kb::kb_ops
const struct kb_operations * kb_ops
Definition kb.h:92