Greenbone Vulnerability Management Libraries  11.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  static int msg_shown = 0;
153 
154  g_snprintf (filename, sizeof (filename), "%s/plugin_feed_info.inc", src_path);
155  if (!g_file_get_contents (filename, &fcontent, NULL, &error))
156  {
157  if (error && msg_shown == 0)
158  {
159  g_warning ("nvt_feed_version: %s", error->message);
160  msg_shown = 1;
161  }
162  g_error_free (error);
163  return NULL;
164  }
165  plugin_set = g_strrstr (fcontent, "PLUGIN_SET = ");
166  if (!plugin_set)
167  {
168  g_warning ("nvt_feed_version: Erroneous %s format", filename);
169  g_free (fcontent);
170  return NULL;
171  }
172  msg_shown = 0;
173  plugin_set = g_strndup (plugin_set + 14, 12);
174  g_free (fcontent);
175  return plugin_set;
176 }
177 
181 void
183 {
184  char *feed_version, *old_version;
185  if (cache_kb && !cache_saved)
186  {
187  kb_save (cache_kb);
188  cache_saved = 1;
189  }
190  old_version = nvticache_feed_version ();
191  feed_version = nvt_feed_version ();
192  if (g_strcmp0 (old_version, feed_version))
193  {
194  kb_item_set_str (cache_kb, NVTICACHE_STR, feed_version, 0);
195  g_message ("Updated NVT cache from version %s to %s", old_version,
196  feed_version);
197  }
198  g_free (old_version);
199  g_free (feed_version);
200 }
201 
214 int
215 nvticache_add (const nvti_t *nvti, const char *filename)
216 {
217  char *oid, *dummy;
218 
219  assert (cache_kb);
220  /* Check for duplicate OID. */
221  oid = nvti_oid (nvti);
222  dummy = nvticache_get_filename (oid);
223  if (dummy && strcmp (filename, dummy))
224  {
225  struct stat src_stat;
226  char *src_file = g_build_filename (src_path, dummy, NULL);
227 
228  /* If .nasl file was duplicated, not moved. */
229  if (src_file && stat (src_file, &src_stat) >= 0)
230  g_warning ("NVT %s with duplicate OID %s will be replaced with %s",
231  src_file, oid, filename);
232  g_free (src_file);
233  }
234  if (dummy)
235  nvticache_delete (oid);
236 
237  g_free (dummy);
238 
239  if (kb_nvt_add (cache_kb, nvti, filename))
240  goto kb_fail;
241  cache_saved = 0;
242 
243  return 0;
244 kb_fail:
245  return -1;
246 }
247 
255 char *
256 nvticache_get_src (const char *oid)
257 {
258  char *filename, *src;
259 
260  assert (cache_kb);
261 
262  filename = kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
263  if (!filename)
264  return NULL;
265  src = g_build_filename (src_path, filename, NULL);
266  g_free (filename);
267  return src;
268 }
269 
277 char *
278 nvticache_get_oid (const char *filename)
279 {
280  assert (cache_kb);
281 
282  return kb_nvt_get (cache_kb, filename, NVT_OID_POS);
283 }
284 
292 char *
293 nvticache_get_filename (const char *oid)
294 {
295  assert (cache_kb);
296  return kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
297 }
298 
306 char *
308 {
309  assert (cache_kb);
311 }
312 
320 char *
322 {
323  assert (cache_kb);
325 }
326 
334 char *
336 {
337  assert (cache_kb);
339 }
340 
348 char *
350 {
351  assert (cache_kb);
353 }
354 
362 char *
364 {
365  assert (cache_kb);
367 }
368 
376 char *
377 nvticache_get_dependencies (const char *oid)
378 {
379  assert (cache_kb);
381 }
382 
390 int
391 nvticache_get_category (const char *oid)
392 {
393  int category;
394  char *category_s;
395 
396  assert (cache_kb);
397  category_s = kb_nvt_get (cache_kb, oid, NVT_CATEGORY_POS);
398  category = atoi (category_s);
399  g_free (category_s);
400  return category;
401 }
402 
410 int
411 nvticache_get_timeout (const char *oid)
412 {
413  int timeout;
414  char *timeout_s;
415 
416  assert (cache_kb);
417  timeout_s = kb_nvt_get (cache_kb, oid, NVT_TIMEOUT_POS);
418  timeout = atoi (timeout_s);
419  g_free (timeout_s);
420  return timeout;
421 }
422 
430 char *
431 nvticache_get_name (const char *oid)
432 {
433  assert (cache_kb);
434  return kb_nvt_get (cache_kb, oid, NVT_NAME_POS);
435 }
436 
444 char *
445 nvticache_get_cves (const char *oid)
446 {
447  assert (cache_kb);
448  return kb_nvt_get (cache_kb, oid, NVT_CVES_POS);
449 }
450 
458 char *
459 nvticache_get_bids (const char *oid)
460 {
461  assert (cache_kb);
462  return kb_nvt_get (cache_kb, oid, NVT_BIDS_POS);
463 }
464 
472 char *
473 nvticache_get_xrefs (const char *oid)
474 {
475  assert (cache_kb);
476  return kb_nvt_get (cache_kb, oid, NVT_XREFS_POS);
477 }
478 
486 char *
487 nvticache_get_family (const char *oid)
488 {
489  assert (cache_kb);
490  return kb_nvt_get (cache_kb, oid, NVT_FAMILY_POS);
491 }
492 
500 char *
501 nvticache_get_tags (const char *oid)
502 {
503  assert (cache_kb);
504  return kb_nvt_get (cache_kb, oid, NVT_TAGS_POS);
505 }
506 
514 nvti_t *
515 nvticache_get_nvt (const char *oid)
516 {
517  assert (cache_kb);
518  return kb_nvt_get_all (cache_kb, oid);
519 }
520 
528 GSList *
529 nvticache_get_prefs (const char *oid)
530 {
531  char pattern[4096];
532  struct kb_item *prefs, *element;
533  GSList *list = NULL;
534 
535  assert (cache_kb);
536 
537  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
538  prefs = element = kb_item_get_all (cache_kb, pattern);
539  while (element)
540  {
541  nvtpref_t *np;
542  char **array = g_strsplit (element->v_str, "|||", -1);
543 
544  assert (array[3]);
545  assert (!array[4]);
546  np = nvtpref_new (atoi (array[0]), array[1], array[2], array[3]);
547  g_strfreev (array);
548  list = g_slist_append (list, np);
549  element = element->next;
550  }
551  kb_item_free (prefs);
552 
553  return list;
554 }
555 
561 GSList *
563 {
564  assert (cache_kb);
565 
566  return kb_nvt_get_oids (cache_kb);
567 }
568 
574 size_t
576 {
577  assert (cache_kb);
578 
579  return kb_item_count (cache_kb, "nvt:*");
580 }
581 
586 void
587 nvticache_delete (const char *oid)
588 {
589  char pattern[4096];
590  char *filename;
591 
592  assert (cache_kb);
593  assert (oid);
594 
595  filename = nvticache_get_filename (oid);
596  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
597  kb_del_items (cache_kb, pattern);
598  g_snprintf (pattern, sizeof (pattern), "nvt:%s", oid);
599  kb_del_items (cache_kb, pattern);
600 
601  if (filename)
602  {
603  g_snprintf (pattern, sizeof (pattern), "filename:%s", filename);
604  kb_del_items (cache_kb, pattern);
605  }
606  g_free (filename);
607 }
608 
614 char *
616 {
618 }
619 
625 int
627 {
628  char *cached, *current;
629  int ret;
630 
631  if (!(current = nvt_feed_version ()))
632  return 0;
634  ret = strcmp (cached, current);
635  g_free (cached);
636  g_free (current);
637  return ret;
638 }
NVT_EXCLUDED_KEYS_POS
@ NVT_EXCLUDED_KEYS_POS
Definition: kb.h:63
kb.h
Knowledge base management API - Redis backend.
NVT_BIDS_POS
@ NVT_BIDS_POS
Definition: kb.h:69
nvticache_feed_version
char * nvticache_feed_version(void)
Get the NVT feed version.
Definition: nvticache.c:615
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:278
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:327
nvticache.h
Protos and data structures for NVT Information Cache.
nvticache_get_src
char * nvticache_get_src(const char *oid)
Get the full source filename of an OID.
Definition: nvticache.c:256
nvticache_delete
void nvticache_delete(const char *oid)
Delete NVT from the cache.
Definition: nvticache.c:587
NVT_NAME_POS
@ NVT_NAME_POS
Definition: kb.h:74
nvticache_get_mandatory_keys
char * nvticache_get_mandatory_keys(const char *oid)
Get the Mandatory Keys from a plugin OID.
Definition: nvticache.c:321
nvticache_get_oid
char * nvticache_get_oid(const char *filename)
Get the OID from a plugin filename.
Definition: nvticache.c:278
kb_lnk_reset
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:651
nvti_oid
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:503
nvticache_get_timeout
int nvticache_get_timeout(const char *oid)
Get the Timeout from a plugin OID.
Definition: nvticache.c:411
kb_save
static int kb_save(kb_t kb)
Save all the KB's content.
Definition: kb.h:631
nvticache_count
size_t nvticache_count()
Get the number of nvt's in the cache.
Definition: nvticache.c:575
nvticache_reset
void nvticache_reset()
Reset connection to KB. To be called after a fork().
Definition: nvticache.c:136
nvt_feed_version
static char * nvt_feed_version()
Determine the version of the NVT feed.
Definition: nvticache.c:148
nvticache_get_family
char * nvticache_get_family(const char *oid)
Get the family from a plugin OID.
Definition: nvticache.c:487
kb_nvt_get_all
static nvti_t * kb_nvt_get_all(kb_t kb, const char *oid)
Get a full NVT.
Definition: kb.h:585
nvticache_get_cves
char * nvticache_get_cves(const char *oid)
Get the cves from a plugin OID.
Definition: nvticache.c:445
nvticache_save
void nvticache_save()
Save the nvticache to disk.
Definition: nvticache.c:182
NVT_FILENAME_POS
@ NVT_FILENAME_POS
Definition: kb.h:60
nvticache_get_xrefs
char * nvticache_get_xrefs(const char *oid)
Get the xrefs from a plugin OID.
Definition: nvticache.c:473
NVT_TIMEOUT_POS
@ NVT_TIMEOUT_POS
Definition: kb.h:72
kb_nvt_get_oids
static GSList * kb_nvt_get_oids(kb_t kb)
Get list of NVT OIDs.
Definition: kb.h:600
nvticache_get_category
int nvticache_get_category(const char *oid)
Get the Category from a plugin OID.
Definition: nvticache.c:391
nvticache_get_required_udp_ports
char * nvticache_get_required_udp_ports(const char *oid)
Get the Required udp ports from a plugin OID.
Definition: nvticache.c:349
nvticache_initialized
int nvticache_initialized(void)
Return whether the nvt cache is initialized.
Definition: nvticache.c:59
kb_item
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:83
nvticache_get_name
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
Definition: nvticache.c:431
nvticache_get_oids
GSList * nvticache_get_oids()
Get the list of nvti OIDs.
Definition: nvticache.c:562
nvti
The structure of a information record that corresponds to a NVT.
Definition: nvti.c:270
NVT_REQUIRED_UDP_PORTS_POS
@ NVT_REQUIRED_UDP_PORTS_POS
Definition: kb.h:64
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:484
nvticache_get_prefs
GSList * nvticache_get_prefs(const char *oid)
Get the prefs from a plugin OID.
Definition: nvticache.c:529
NVT_TIMESTAMP_POS
@ NVT_TIMESTAMP_POS
Definition: kb.h:75
nvticache_check
int nvticache_check(const gchar *filename)
Check if the nvt for the given filename exists in cache.
Definition: nvticache.c:115
nvticache_init
int nvticache_init(const char *src, const char *kb_path)
Initializes the nvti cache.
Definition: nvticache.c:73
kb_item::v_str
char * v_str
Definition: kb.h:89
NVT_OID_POS
@ NVT_OID_POS
Definition: kb.h:76
NVT_XREFS_POS
@ NVT_XREFS_POS
Definition: kb.h:70
kb_item_free
void kb_item_free(struct kb_item *item)
Release a KB item (or a list).
Definition: kb.c:516
nvticache_get_filename
char * nvticache_get_filename(const char *oid)
Get the filename from a plugin OID.
Definition: nvticache.c:293
cache_kb
kb_t cache_kb
Definition: nvticache.c:50
nvticache_get_required_keys
char * nvticache_get_required_keys(const char *oid)
Get the Required Keys from a plugin OID.
Definition: nvticache.c:307
nvtpref
The structure for a preference of a NVT.
Definition: nvti.c:333
nvticache_add
int nvticache_add(const nvti_t *nvti, const char *filename)
Add a NVT Information to the cache.
Definition: nvticache.c:215
NVT_CATEGORY_POS
@ NVT_CATEGORY_POS
Definition: kb.h:71
nvticache_check_feed
int nvticache_check_feed(void)
Check if the plugins feed was newer than cached feed.
Definition: nvticache.c:626
NVT_DEPENDENCIES_POS
@ NVT_DEPENDENCIES_POS
Definition: kb.h:66
kb_item::next
struct kb_item * next
Definition: kb.h:94
src_path
char * src_path
Definition: nvticache.c:49
NVT_FAMILY_POS
@ NVT_FAMILY_POS
Definition: kb.h:73
NVTICACHE_STR
#define NVTICACHE_STR
Definition: nvticache.h:36
nvtpref_new
nvtpref_t * nvtpref_new(int id, gchar *name, gchar *type, gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition: nvti.c:357
kb
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:105
kb_del_items
static int kb_del_items(kb_t kb, const char *name)
Delete all entries under a given name.
Definition: kb.h:616
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:361
NVT_CVES_POS
@ NVT_CVES_POS
Definition: kb.h:68
NVT_MANDATORY_KEYS_POS
@ NVT_MANDATORY_KEYS_POS
Definition: kb.h:62
cache_saved
int cache_saved
Definition: nvticache.c:51
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:552
nvticache_get_bids
char * nvticache_get_bids(const char *oid)
Get the bids from a plugin OID.
Definition: nvticache.c:459
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:430
nvticache_get_dependencies
char * nvticache_get_dependencies(const char *oid)
Get the Dependencies from a plugin OID.
Definition: nvticache.c:377
NVT_REQUIRED_KEYS_POS
@ NVT_REQUIRED_KEYS_POS
Definition: kb.h:61
kb_new
static int kb_new(kb_t *kb, const char *kb_path)
Initialize a new Knowledge Base object.
Definition: kb.h:245
nvticache_get_nvt
nvti_t * nvticache_get_nvt(const char *oid)
Get the nvti from a plugin OID.
Definition: nvticache.c:515
NVT_TAGS_POS
@ NVT_TAGS_POS
Definition: kb.h:67
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:569
nvticache_get_tags
char * nvticache_get_tags(const char *oid)
Get the tags from a plugin OID.
Definition: nvticache.c:501
NVT_REQUIRED_PORTS_POS
@ NVT_REQUIRED_PORTS_POS
Definition: kb.h:65
nvticache_get_excluded_keys
char * nvticache_get_excluded_keys(const char *oid)
Get the Excluded Keys from a plugin OID.
Definition: nvticache.c:335
nvticache_get_kb
kb_t nvticache_get_kb(void)
Return the nvticache kb.
Definition: nvticache.c:98
nvticache_get_required_ports
char * nvticache_get_required_ports(const char *oid)
Get the Required ports from a plugin OID.
Definition: nvticache.c:363