Greenbone Vulnerability Management Libraries  10.0.0
nvticache.c
Go to the documentation of this file.
1 /* Copyright (C) 2009-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 
31 #include "nvticache.h"
32 
33 #include "kb.h" /* for kb_del_items, kb_item_get_str, kb_item_add_int */
34 
35 #include <assert.h> /* for assert */
36 #include <errno.h>
37 #include <stdio.h> /* for fopen */
38 #include <stdlib.h> /* for atoi */
39 #include <string.h> /* for strcmp */
40 #include <sys/stat.h> /* for stat, st_mtime */
41 #include <time.h> /* for time, time_t */
42 
43 #undef G_LOG_DOMAIN
44 
47 #define G_LOG_DOMAIN "lib nvticache"
48 
49 char *src_path = NULL;
50 kb_t cache_kb = NULL;
51 int cache_saved = 1;
58 int
60 {
61  return !!cache_kb;
62 }
63 
72 int
73 nvticache_init (const char *src, const char *kb_path)
74 {
75  assert (src);
76 
77  if (src_path)
78  g_free (src_path);
79  src_path = g_strdup (src);
80  if (cache_kb)
82  cache_kb = kb_find (kb_path, NVTICACHE_STR);
83  if (cache_kb)
84  return 0;
85 
86  if (kb_new (&cache_kb, kb_path)
88  return -1;
89  return 0;
90 }
91 
97 kb_t
99 {
100  assert (cache_kb);
101  return cache_kb;
102 }
103 
114 int
115 nvticache_check (const gchar *filename)
116 {
117  assert (cache_kb);
118  char *src_file, *time_s;
119  struct stat src_stat;
120  int ret = 0;
121 
122  src_file = g_build_filename (src_path, filename, NULL);
123  time_s = kb_nvt_get (cache_kb, filename, NVT_TIMESTAMP_POS);
124  if (time_s && src_file && stat (src_file, &src_stat) >= 0
125  && atoi (time_s) > src_stat.st_mtime)
126  ret = 1;
127  g_free (time_s);
128  g_free (src_file);
129  return ret;
130 }
131 
135 void
137 {
138  if (cache_kb)
140 }
141 
147 static char *
149 {
150  char filename[2048], *fcontent = NULL, *plugin_set;
151  GError *error = NULL;
152 
153  g_snprintf (filename, sizeof (filename), "%s/plugin_feed_info.inc", src_path);
154  if (!g_file_get_contents (filename, &fcontent, NULL, &error))
155  {
156  if (error)
157  g_warning ("nvt_feed_version: %s", error->message);
158  g_error_free (error);
159  return NULL;
160  }
161  plugin_set = g_strrstr (fcontent, "PLUGIN_SET = ");
162  if (!plugin_set)
163  {
164  g_warning ("nvt_feed_version: Erroneous %s format", filename);
165  g_free (fcontent);
166  return NULL;
167  }
168 
169  plugin_set = g_strndup (plugin_set + 14, 12);
170  g_free (fcontent);
171  return plugin_set;
172 }
173 
177 void
179 {
180  char *feed_version;
181  if (cache_kb && !cache_saved)
182  {
183  kb_save (cache_kb);
184  cache_saved = 1;
185  }
186  if ((feed_version = nvt_feed_version ()))
187  kb_item_set_str (cache_kb, NVTICACHE_STR, feed_version, 0);
188  g_free (feed_version);
189 }
190 
203 int
204 nvticache_add (const nvti_t *nvti, const char *filename)
205 {
206  char *oid, *dummy;
207 
208  assert (cache_kb);
209  /* Check for duplicate OID. */
210  oid = nvti_oid (nvti);
211  dummy = nvticache_get_filename (oid);
212  if (dummy && strcmp (filename, dummy))
213  {
214  struct stat src_stat;
215  char *src_file = g_build_filename (src_path, dummy, NULL);
216 
217  /* If .nasl file was duplicated, not moved. */
218  if (src_file && stat (src_file, &src_stat) >= 0)
219  g_warning ("NVT %s with duplicate OID %s will be replaced with %s",
220  src_file, oid, filename);
221  g_free (src_file);
222  }
223  if (dummy)
224  nvticache_delete (oid);
225 
226  g_free (dummy);
227 
228  if (kb_nvt_add (cache_kb, nvti, filename))
229  goto kb_fail;
230  cache_saved = 0;
231 
232  return 0;
233 kb_fail:
234  return -1;
235 }
236 
244 char *
245 nvticache_get_src (const char *oid)
246 {
247  char *filename, *src;
248 
249  assert (cache_kb);
250 
251  filename = kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
252  if (!filename)
253  return NULL;
254  src = g_build_filename (src_path, filename, NULL);
255  g_free (filename);
256  return src;
257 }
258 
266 char *
267 nvticache_get_oid (const char *filename)
268 {
269  assert (cache_kb);
270 
271  return kb_nvt_get (cache_kb, filename, NVT_OID_POS);
272 }
273 
281 char *
282 nvticache_get_filename (const char *oid)
283 {
284  assert (cache_kb);
285  return kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
286 }
287 
295 char *
297 {
298  assert (cache_kb);
300 }
301 
309 char *
311 {
312  assert (cache_kb);
314 }
315 
323 char *
325 {
326  assert (cache_kb);
328 }
329 
337 char *
339 {
340  assert (cache_kb);
342 }
343 
351 char *
353 {
354  assert (cache_kb);
356 }
357 
365 char *
366 nvticache_get_dependencies (const char *oid)
367 {
368  assert (cache_kb);
370 }
371 
379 int
380 nvticache_get_category (const char *oid)
381 {
382  int category;
383  char *category_s;
384 
385  assert (cache_kb);
386  category_s = kb_nvt_get (cache_kb, oid, NVT_CATEGORY_POS);
387  category = atoi (category_s);
388  g_free (category_s);
389  return category;
390 }
391 
399 int
400 nvticache_get_timeout (const char *oid)
401 {
402  int timeout;
403  char *timeout_s;
404 
405  assert (cache_kb);
406  timeout_s = kb_nvt_get (cache_kb, oid, NVT_TIMEOUT_POS);
407  timeout = atoi (timeout_s);
408  g_free (timeout_s);
409  return timeout;
410 }
411 
419 char *
420 nvticache_get_name (const char *oid)
421 {
422  assert (cache_kb);
423  return kb_nvt_get (cache_kb, oid, NVT_NAME_POS);
424 }
425 
433 char *
434 nvticache_get_cves (const char *oid)
435 {
436  assert (cache_kb);
437  return kb_nvt_get (cache_kb, oid, NVT_CVES_POS);
438 }
439 
447 char *
448 nvticache_get_bids (const char *oid)
449 {
450  assert (cache_kb);
451  return kb_nvt_get (cache_kb, oid, NVT_BIDS_POS);
452 }
453 
461 char *
462 nvticache_get_xrefs (const char *oid)
463 {
464  assert (cache_kb);
465  return kb_nvt_get (cache_kb, oid, NVT_XREFS_POS);
466 }
467 
475 char *
476 nvticache_get_family (const char *oid)
477 {
478  assert (cache_kb);
479  return kb_nvt_get (cache_kb, oid, NVT_FAMILY_POS);
480 }
481 
489 char *
490 nvticache_get_tags (const char *oid)
491 {
492  assert (cache_kb);
493  return kb_nvt_get (cache_kb, oid, NVT_TAGS_POS);
494 }
495 
503 nvti_t *
504 nvticache_get_nvt (const char *oid)
505 {
506  assert (cache_kb);
507  return kb_nvt_get_all (cache_kb, oid);
508 }
509 
517 GSList *
518 nvticache_get_prefs (const char *oid)
519 {
520  char pattern[4096];
521  struct kb_item *prefs, *element;
522  GSList *list = NULL;
523 
524  assert (cache_kb);
525 
526  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
527  prefs = element = kb_item_get_all (cache_kb, pattern);
528  while (element)
529  {
530  nvtpref_t *np;
531  char **array = g_strsplit (element->v_str, "|||", -1);
532 
533  assert (array[2]);
534  assert (!array[3]);
535  np = g_malloc0 (sizeof (nvtpref_t));
536  np->name = array[0];
537  np->type = array[1];
538  np->dflt = array[2];
539  g_free (array);
540  list = g_slist_append (list, np);
541  element = element->next;
542  }
543  kb_item_free (prefs);
544 
545  return list;
546 }
547 
553 GSList *
555 {
556  assert (cache_kb);
557 
558  return kb_nvt_get_oids (cache_kb);
559 }
560 
566 size_t
568 {
569  assert (cache_kb);
570 
571  return kb_item_count (cache_kb, "nvt:*");
572 }
573 
578 void
579 nvticache_delete (const char *oid)
580 {
581  char pattern[4096];
582  char *filename;
583 
584  assert (cache_kb);
585  assert (oid);
586 
587  filename = nvticache_get_filename (oid);
588  g_snprintf (pattern, sizeof (pattern), "prefs:%s", oid);
589  kb_del_items (cache_kb, pattern);
590  g_snprintf (pattern, sizeof (pattern), "nvt:%s", oid);
591  kb_del_items (cache_kb, pattern);
592 
593  if (filename)
594  {
595  g_snprintf (pattern, sizeof (pattern), "filename:%s", filename);
596  kb_del_items (cache_kb, pattern);
597  }
598  g_free (filename);
599 }
600 
606 char *
608 {
610 }
611 
617 int
619 {
620  char *cached, *current;
621  int ret;
622 
623  if (!(current = nvt_feed_version ()))
624  return 0;
626  ret = strcmp (cached, current);
627  g_free (cached);
628  g_free (current);
629  return ret;
630 }
char * nvticache_get_excluded_keys(const char *oid)
Get the Excluded Keys from a plugin OID.
Definition: nvticache.c:324
char * nvticache_get_oid(const char *filename)
Get the OID from a plugin filename.
Definition: nvticache.c:267
int nvticache_check(const gchar *filename)
Check if the nvt for the given filename exists in cache.
Definition: nvticache.c:115
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
Definition: kb.c:608
void nvticache_reset()
Reset connection to KB. To be called after a fork().
Definition: nvticache.c:136
char * nvticache_get_required_keys(const char *oid)
Get the Required Keys from a plugin OID.
Definition: nvticache.c:296
char * nvticache_get_filename(const char *oid)
Get the filename from a plugin OID.
Definition: nvticache.c:282
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
int nvticache_add(const nvti_t *nvti, const char *filename)
Add a NVT Information to the cache.
Definition: nvticache.c:204
int nvticache_check_feed(void)
Check if the plugins feed was newer than cached feed.
Definition: nvticache.c:618
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:357
static nvti_t * kb_nvt_get_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.h:581
char * nvticache_get_src(const char *oid)
Get the full source filename of an OID.
Definition: nvticache.c:245
char * nvticache_get_mandatory_keys(const char *oid)
Get the Mandatory Keys from a plugin OID.
Definition: nvticache.c:310
int nvticache_get_timeout(const char *oid)
Get the Timeout from a plugin OID.
Definition: nvticache.c:400
static int kb_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.h:241
gchar * type
Preference type.
Definition: nvti.h:40
int cache_saved
Definition: nvticache.c:51
void nvticache_delete(const char *oid)
Delete NVT from the cache.
Definition: nvticache.c:579
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:101
char * nvticache_get_cves(const char *oid)
Get the cves from a plugin OID.
Definition: nvticache.c:434
static int kb_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.h:612
static int kb_nvt_add(kb_t kb, const nvti_t *nvt, const char *filename)
Insert a new nvt.
Definition: kb.h:548
Knowledge base management API - Redis backend.
kb_t cache_kb
Definition: nvticache.c:50
void nvticache_save()
Save the nvticache to disk.
Definition: nvticache.c:178
int nvticache_initialized(void)
Return whether the nvt cache is initialized.
Definition: nvticache.c:59
gchar * dflt
Default value of the preference.
Definition: nvti.h:42
static GSList * kb_nvt_get_oids(kb_t kb)
Get list of NVT OIDs.
Definition: kb.h:596
char * nvticache_get_required_ports(const char *oid)
Get the Required ports from a plugin OID.
Definition: nvticache.c:352
kb_t nvticache_get_kb(void)
Return the nvticache kb.
Definition: nvticache.c:98
static char * kb_item_get_str(kb_t kb, const char *name)
Get a single KB string item.
Definition: kb.h:323
GSList * nvticache_get_prefs(const char *oid)
Get the prefs from a plugin OID.
Definition: nvticache.c:518
static size_t kb_item_count(kb_t kb, const char *pattern)
Count all items stored under a given pattern.
Definition: kb.h:426
char * nvticache_feed_version(void)
Get the NVT feed version.
Definition: nvticache.c:607
char * nvticache_get_xrefs(const char *oid)
Get the xrefs from a plugin OID.
Definition: nvticache.c:462
#define NVTICACHE_STR
Definition: nvticache.h:36
The structure for a preference of a NVT.
Definition: nvti.h:38
GSList * nvticache_get_oids()
Get the list of nvti OIDs.
Definition: nvticache.c:554
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
Definition: nvticache.c:420
char * nvticache_get_bids(const char *oid)
Get the bids from a plugin OID.
Definition: nvticache.c:448
static int kb_save(kb_t kb)
Save all the KB's content.
Definition: kb.h:627
size_t nvticache_count()
Get the number of nvt's in the cache.
Definition: nvticache.c:567
int nvticache_init(const char *src, const char *kb_path)
Initializes the nvti cache.
Definition: nvticache.c:73
char * nvticache_get_dependencies(const char *oid)
Get the Dependencies from a plugin OID.
Definition: nvticache.c:366
char * src_path
Definition: nvticache.c:49
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:647
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:219
char * nvticache_get_family(const char *oid)
Get the family from a plugin OID.
Definition: nvticache.c:476
static char * nvt_feed_version()
Determine the version of the NVT feed.
Definition: nvticache.c:148
static kb_t kb_find(const char *kb_path, const char *key)
Find an existing Knowledge Base object with key.
Definition: kb.h:274
nvti_t * nvticache_get_nvt(const char *oid)
Get the nvti from a plugin OID.
Definition: nvticache.c:504
static char * kb_nvt_get(kb_t kb, const char *oid, enum kb_nvt_pos position)
Get field of a NVT.
Definition: kb.h:565
Definition: kb.h:72
char * nvticache_get_tags(const char *oid)
Get the tags from a plugin OID.
Definition: nvticache.c:490
char * v_str
Definition: kb.h:85
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:480
char * nvticache_get_required_udp_ports(const char *oid)
Get the Required udp ports from a plugin OID.
Definition: nvticache.c:338
Protos and data structures for NVT Information Cache.
int nvticache_get_category(const char *oid)
Get the Category from a plugin OID.
Definition: nvticache.c:380