Greenbone Vulnerability Management Libraries  10.0.1
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;
185  if (cache_kb && !cache_saved)
186  {
187  kb_save (cache_kb);
188  cache_saved = 1;
189  }
190  if ((feed_version = nvt_feed_version ()))
191  kb_item_set_str (cache_kb, NVTICACHE_STR, feed_version, 0);
192  g_free (feed_version);
193 }
194 
207 int
208 nvticache_add (const nvti_t *nvti, const char *filename)
209 {
210  char *oid, *dummy;
211 
212  assert (cache_kb);
213  /* Check for duplicate OID. */
214  oid = nvti_oid (nvti);
215  dummy = nvticache_get_filename (oid);
216  if (dummy && strcmp (filename, dummy))
217  {
218  struct stat src_stat;
219  char *src_file = g_build_filename (src_path, dummy, NULL);
220 
221  /* If .nasl file was duplicated, not moved. */
222  if (src_file && stat (src_file, &src_stat) >= 0)
223  g_warning ("NVT %s with duplicate OID %s will be replaced with %s",
224  src_file, oid, filename);
225  g_free (src_file);
226  }
227  if (dummy)
228  nvticache_delete (oid);
229 
230  g_free (dummy);
231 
232  if (kb_nvt_add (cache_kb, nvti, filename))
233  goto kb_fail;
234  cache_saved = 0;
235 
236  return 0;
237 kb_fail:
238  return -1;
239 }
240 
248 char *
249 nvticache_get_src (const char *oid)
250 {
251  char *filename, *src;
252 
253  assert (cache_kb);
254 
255  filename = kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
256  if (!filename)
257  return NULL;
258  src = g_build_filename (src_path, filename, NULL);
259  g_free (filename);
260  return src;
261 }
262 
270 char *
271 nvticache_get_oid (const char *filename)
272 {
273  assert (cache_kb);
274 
275  return kb_nvt_get (cache_kb, filename, NVT_OID_POS);
276 }
277 
285 char *
286 nvticache_get_filename (const char *oid)
287 {
288  assert (cache_kb);
289  return kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
290 }
291 
299 char *
301 {
302  assert (cache_kb);
304 }
305 
313 char *
315 {
316  assert (cache_kb);
318 }
319 
327 char *
329 {
330  assert (cache_kb);
332 }
333 
341 char *
343 {
344  assert (cache_kb);
346 }
347 
355 char *
357 {
358  assert (cache_kb);
360 }
361 
369 char *
370 nvticache_get_dependencies (const char *oid)
371 {
372  assert (cache_kb);
374 }
375 
383 int
384 nvticache_get_category (const char *oid)
385 {
386  int category;
387  char *category_s;
388 
389  assert (cache_kb);
390  category_s = kb_nvt_get (cache_kb, oid, NVT_CATEGORY_POS);
391  category = atoi (category_s);
392  g_free (category_s);
393  return category;
394 }
395 
403 int
404 nvticache_get_timeout (const char *oid)
405 {
406  int timeout;
407  char *timeout_s;
408 
409  assert (cache_kb);
410  timeout_s = kb_nvt_get (cache_kb, oid, NVT_TIMEOUT_POS);
411  timeout = atoi (timeout_s);
412  g_free (timeout_s);
413  return timeout;
414 }
415 
423 char *
424 nvticache_get_name (const char *oid)
425 {
426  assert (cache_kb);
427  return kb_nvt_get (cache_kb, oid, NVT_NAME_POS);
428 }
429 
437 char *
438 nvticache_get_cves (const char *oid)
439 {
440  assert (cache_kb);
441  return kb_nvt_get (cache_kb, oid, NVT_CVES_POS);
442 }
443 
451 char *
452 nvticache_get_bids (const char *oid)
453 {
454  assert (cache_kb);
455  return kb_nvt_get (cache_kb, oid, NVT_BIDS_POS);
456 }
457 
465 char *
466 nvticache_get_xrefs (const char *oid)
467 {
468  assert (cache_kb);
469  return kb_nvt_get (cache_kb, oid, NVT_XREFS_POS);
470 }
471 
479 char *
480 nvticache_get_family (const char *oid)
481 {
482  assert (cache_kb);
483  return kb_nvt_get (cache_kb, oid, NVT_FAMILY_POS);
484 }
485 
493 char *
494 nvticache_get_tags (const char *oid)
495 {
496  assert (cache_kb);
497  return kb_nvt_get (cache_kb, oid, NVT_TAGS_POS);
498 }
499 
507 nvti_t *
508 nvticache_get_nvt (const char *oid)
509 {
510  assert (cache_kb);
511  return kb_nvt_get_all (cache_kb, oid);
512 }
513 
521 GSList *
522 nvticache_get_prefs (const char *oid)
523 {
524  char pattern[4096];
525  struct kb_item *prefs, *element;
526  GSList *list = NULL;
527 
528  assert (cache_kb);
529 
530  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
531  prefs = element = kb_item_get_all (cache_kb, pattern);
532  while (element)
533  {
534  nvtpref_t *np;
535  char **array = g_strsplit (element->v_str, "|||", -1);
536 
537  assert (array[2]);
538  assert (!array[3]);
539  np = g_malloc0 (sizeof (nvtpref_t));
540  np->name = array[0];
541  np->type = array[1];
542  np->dflt = array[2];
543  g_free (array);
544  list = g_slist_append (list, np);
545  element = element->next;
546  }
547  kb_item_free (prefs);
548 
549  return list;
550 }
551 
557 GSList *
559 {
560  assert (cache_kb);
561 
562  return kb_nvt_get_oids (cache_kb);
563 }
564 
570 size_t
572 {
573  assert (cache_kb);
574 
575  return kb_item_count (cache_kb, "nvt:*");
576 }
577 
582 void
583 nvticache_delete (const char *oid)
584 {
585  char pattern[4096];
586  char *filename;
587 
588  assert (cache_kb);
589  assert (oid);
590 
591  filename = nvticache_get_filename (oid);
592  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
593  kb_del_items (cache_kb, pattern);
594  g_snprintf (pattern, sizeof (pattern), "nvt:%s", oid);
595  kb_del_items (cache_kb, pattern);
596 
597  if (filename)
598  {
599  g_snprintf (pattern, sizeof (pattern), "filename:%s", filename);
600  kb_del_items (cache_kb, pattern);
601  }
602  g_free (filename);
603 }
604 
610 char *
612 {
614 }
615 
621 int
623 {
624  char *cached, *current;
625  int ret;
626 
627  if (!(current = nvt_feed_version ()))
628  return 0;
630  ret = strcmp (cached, current);
631  g_free (cached);
632  g_free (current);
633  return ret;
634 }
char * nvticache_get_excluded_keys(const char *oid)
Get the Excluded Keys from a plugin OID.
Definition: nvticache.c:328
char * nvticache_get_oid(const char *filename)
Get the OID from a plugin filename.
Definition: nvticache.c:271
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:599
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:300
char * nvticache_get_filename(const char *oid)
Get the filename from a plugin OID.
Definition: nvticache.c:286
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:208
int nvticache_check_feed(void)
Check if the plugins feed was newer than cached feed.
Definition: nvticache.c:622
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:249
char * nvticache_get_mandatory_keys(const char *oid)
Get the Mandatory Keys from a plugin OID.
Definition: nvticache.c:314
int nvticache_get_timeout(const char *oid)
Get the Timeout from a plugin OID.
Definition: nvticache.c:404
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:583
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:438
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:182
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:356
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:522
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:611
char * nvticache_get_xrefs(const char *oid)
Get the xrefs from a plugin OID.
Definition: nvticache.c:466
#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:558
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
Definition: nvticache.c:424
char * nvticache_get_bids(const char *oid)
Get the bids from a plugin OID.
Definition: nvticache.c:452
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:571
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:370
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:480
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:508
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:494
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:342
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:384