/usr/include/openvas/base/kb.h is in libopenvas-dev 9.0.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /* OpenVAS Libraries
*
* Authors:
* Henri Doreau <henri.doreau@gmail.com>
*
* Copyright:
* Copyright (C) 2014 - Greenbone Networks GmbH.
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Knowledge base management API - Redis backend.
*/
#ifndef OPENVAS_KB_H
#define OPENVAS_KB_H
#include <assert.h>
/**
* @brief Default KB location.
*
* TODO This should eventually be expressed as an URI when/if multiple KB
* backends are supported (e.g.: redis:///tmp/redis.sock).
*/
#define KB_PATH_DEFAULT "/tmp/redis.sock"
/**
* @brief Possible type of a kb_item.
*/
enum kb_item_type {
KB_TYPE_UNSPEC, /**< Ignore the value (name/presence test). */
KB_TYPE_INT, /**< The kb_items v should then be interpreted as int. */
KB_TYPE_STR, /**< The kb_items v should then be interpreted as char*. */
/* -- */
KB_TYPE_CNT,
};
/**
* @brief Knowledge base item (defined by name, type (int/char*) and value).
* Implemented as a singly linked list
*/
struct kb_item
{
enum kb_item_type type; /**< One of KB_TYPE_INT or KB_TYPE_STR. */
union
{
char *v_str;
int v_int;
}; /**< Value of this knowledge base item. */
struct kb_item *next; /**< Next item in list. */
size_t namelen; /**< Name length (including final NULL byte). */
char name[0]; /**< Name of this knowledge base item. */
};
struct kb_operations;
/**
* @brief Top-level KB. This is to be inherited by KB implementations.
*/
struct kb
{
const struct kb_operations *kb_ops; /**< KB vtable. */
};
/**
* @brief type abstraction to hide KB internals.
*/
typedef struct kb *kb_t;
/**
* @brief KB interface. Functions provided by an implementation. All functions
* have to be provided, there is no default/fallback. These functions
* should be called via the corresponding static inline wrappers below.
* See the wrappers for the documentation.
*/
struct kb_operations
{
/* ctor/dtor */
int (*kb_new) (kb_t *, const char *);
int (*kb_delete) (kb_t);
/* Actual kb operations */
struct kb_item *(*kb_get_single) (kb_t, const char *, enum kb_item_type);
char *(*kb_get_str) (kb_t, const char *);
int (*kb_get_int) (kb_t, const char *);
struct kb_item * (*kb_get_all) (kb_t, const char *);
struct kb_item * (*kb_get_pattern) (kb_t, const char *);
int (*kb_add_str) (kb_t, const char *, const char *);
int (*kb_set_str) (kb_t, const char *, const char *);
int (*kb_add_int) (kb_t, const char *, int);
int (*kb_set_int) (kb_t, const char *, int);
int (*kb_del_items) (kb_t, const char *);
/* Utils */
int (*kb_lnk_reset) (kb_t);
int (*kb_flush) (kb_t);
};
/**
* @brief Default KB operations.
* No selection mechanism is provided yet since there's only one
* implementation (redis-based).
*/
extern const struct kb_operations *KBDefaultOperations;
/**
* @brief Release a KB item (or a list).
*/
void kb_item_free (struct kb_item *);
/**
* @brief Initialize a new Knowledge Base object.
* @param[in] kb Reference to a kb_t to initialize.
* @return 0 on success, non-null on error.
*/
static inline int kb_new (kb_t *kb, const char *kb_path)
{
assert (kb);
assert (KBDefaultOperations);
assert (KBDefaultOperations->kb_new);
*kb = NULL;
return KBDefaultOperations->kb_new (kb, kb_path);
}
/**
* @brief Delete all entries and release ownership on the namespace.
* @param[in] kb KB handle to release.
* @return 0 on success, non-null on error.
*/
static inline int kb_delete (kb_t kb)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_delete);
return kb->kb_ops->kb_delete (kb);
}
/**
* @brief Get a single KB element.
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
* @param[in] type Desired element type.
* @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error.
*/
static inline struct kb_item *
kb_item_get_single (kb_t kb, const char *name, enum kb_item_type type)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_single);
return kb->kb_ops->kb_get_single (kb, name, type);
}
/**
* @brief Get a single KB string item.
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
* @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error.
*/
static inline char *
kb_item_get_str (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_str);
return kb->kb_ops->kb_get_str (kb, name);
}
/**
* @brief Get a single KB integer item.
* @param[in] kb KB handle where to fetch the item.
* @param[in] name Name of the element to retrieve.
* @return A struct kb_item to be freed with kb_item_free() or NULL if no
* element was found or on error.
*/
static inline int
kb_item_get_int (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_int);
return kb->kb_ops->kb_get_int (kb, name);
}
/**
* @brief Get all items stored under a given name.
* @param[in] kb KB handle where to fetch the items.
* @param[in] name Name of the elements to retrieve.
* @return Linked struct kb_item instances to be freed with kb_item_free() or
* NULL if no element was found or on error.
*/
static inline struct kb_item *
kb_item_get_all (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_all);
return kb->kb_ops->kb_get_all (kb, name);
}
/**
* @brief Get all items stored under a given pattern.
* @param[in] kb KB handle where to fetch the items.
* @param[in] pattern '*' pattern of the elements to retrieve.
* @return Linked struct kb_item instances to be freed with kb_item_free() or
* NULL if no element was found or on error.
*/
static inline struct kb_item *
kb_item_get_pattern (kb_t kb, const char *pattern)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_get_pattern);
return kb->kb_ops->kb_get_pattern (kb, pattern);
}
/**
* @brief Insert (append) a new entry under a given name.
* @param[in] kb KB handle where to store the item.
* @param[in] name Item name.
* @param[in] str Item value.
* @return 0 on success, non-null on error.
*/
static inline int
kb_item_add_str (kb_t kb, const char *name, const char *str)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_str);
return kb->kb_ops->kb_add_str (kb, name, str);
}
/**
* @brief Set (replace) a new entry under a given name.
* @param[in] kb KB handle where to store the item.
* @param[in] name Item name.
* @param[in] str Item value.
* @return 0 on success, non-null on error.
*/
static inline int
kb_item_set_str (kb_t kb, const char *name, const char *str)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_set_str);
return kb->kb_ops->kb_set_str (kb, name, str);
}
/**
* @brief Insert (append) a new entry under a given name.
* @param[in] kb KB handle where to store the item.
* @param[in] name Item name.
* @param[in] val Item value.
* @return 0 on success, non-null on error.
*/
static inline int
kb_item_add_int (kb_t kb, const char *name, int val)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_int);
return kb->kb_ops->kb_add_int (kb, name, val);
}
/**
* @brief Set (replace) a new entry under a given name.
* @param[in] kb KB handle where to store the item.
* @param[in] name Item name.
* @param[in] val Item value.
* @return 0 on success, non-null on error.
*/
static inline int
kb_item_set_int (kb_t kb, const char *name, int val)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_set_int);
return kb->kb_ops->kb_set_int (kb, name, val);
}
/**
* @brief Delete all entries under a given name.
* @param[in] kb KB handle where to store the item.
* @param[in] name Item name.
* @return 0 on success, non-null on error.
*/
static inline int
kb_del_items (kb_t kb, const char *name)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_del_items);
return kb->kb_ops->kb_del_items (kb, name);
}
/**
* @brief Reset connection to the KB. This is called after each fork() to make
* sure connections aren't shared between concurrent processes.
* @param[in] kb KB handle.
* @return 0 on success, non-null on error.
*/
static inline int kb_lnk_reset (kb_t kb)
{
int rc = 0;
assert (kb);
assert (kb->kb_ops);
if (kb->kb_ops->kb_lnk_reset != NULL)
rc = kb->kb_ops->kb_lnk_reset (kb);
return rc;
}
/**
* @brief Flush all the KB's content. Delete all namespaces.
* @param[in] kb KB handle.
* @return 0 on success, non-null on error.
*/
static inline int kb_flush (kb_t kb)
{
int rc = 0;
assert (kb);
assert (kb->kb_ops);
if (kb->kb_ops->kb_flush != NULL)
rc = kb->kb_ops->kb_flush (kb);
return rc;
}
#endif
|