OpenVAS Libraries  9.0.3
nvticache.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: Implementation of API to handle NVT Info Cache
4  *
5  * Authors:
6  * Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2009 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
37 /* for struct stat */
38 #include <sys/stat.h>
39 
40 /* for nvticache_t */
41 #include "nvticache.h"
42 #include "kb.h"
43 
44 #include <string.h> // for strlen
45 #include <assert.h>
46 #include <stdlib.h> /* for atoi */
47 
48 #undef G_LOG_DOMAIN
49 #define G_LOG_DOMAIN "lib nvticache"
50 
51 char *src_path = NULL; /* The directory of the source files. */
52 kb_t cache_kb = NULL;
53 
59 int
61 {
62  return !!cache_kb;
63 }
64 
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)
81  kb_lnk_reset (cache_kb);
82  cache_kb = kb_find (kb_path, "nvticache");
83  if (cache_kb)
84  return 0;
85 
86  if (kb_new (&cache_kb, kb_path) || kb_item_set_int (cache_kb, "nvticache", 1))
87  return -1;
88  return 0;
89 }
90 
96 kb_t
98 {
99  assert (cache_kb);
100  return cache_kb;
101 }
102 
113 int
114 nvticache_check (const gchar *filename)
115 {
116  assert (cache_kb);
117  char pattern[2048], *src_file;
118  time_t timestamp;
119  struct stat src_stat;
120 
121  src_file = g_build_filename (src_path, filename, NULL);
122  g_snprintf (pattern, sizeof (pattern), "filename:%s:timestamp", filename);
123  timestamp = kb_item_get_int (cache_kb, pattern);
124  if (timestamp && src_file && stat (src_file, &src_stat) >= 0
125  && timestamp > src_stat.st_mtime)
126  {
127  g_free (src_file);
128  return 1;
129  }
130  g_free (src_file);
131  return 0;
132 }
133 
137 void
139 {
140  if (cache_kb)
141  kb_lnk_reset (cache_kb);
142 }
143 
156 int
157 nvticache_add (const nvti_t *nvti, const char *filename)
158 {
159  char *oid, *dummy, pattern[4096];
160  GSList *element;
161 
162  assert (cache_kb);
163 
164  /* Check for duplicate OID. */
165  oid = nvti_oid (nvti);
166  dummy = nvticache_get_filename (oid);
167  if (dummy && strcmp (filename, dummy))
168  {
169  struct stat src_stat;
170  char *src_file = g_build_filename (src_path, dummy, NULL);
171 
172  /* If .nasl file was duplicated, not moved. */
173  if (src_file && stat (src_file, &src_stat) >= 0)
174  g_warning ("NVT %s with duplicate OID %s will be replaced with %s",
175  src_file, oid, filename);
176  g_free (src_file);
177  }
178  if (dummy)
180 
181  g_free (dummy);
182  if (kb_nvt_add (cache_kb, nvti, filename))
183  goto kb_fail;
184  element = nvti->prefs;
185  while (element)
186  {
187  char value[4096];
188  nvtpref_t *pref = element->data;
189 
190  g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
191  g_snprintf (value, sizeof (value), "%s|||%s|||%s", pref->name, pref->type,
192  pref->dflt);
193  if (kb_item_add_str (cache_kb, pattern, value))
194  goto kb_fail;
195  element = element->next;
196  }
197  g_snprintf (pattern, sizeof (pattern), "filename:%s:timestamp", filename);
198  if (kb_item_set_int (cache_kb, pattern, time (NULL)))
199  goto kb_fail;
200 
201  return 0;
202 
203 kb_fail:
204  return -1;
205 }
206 
214 char *
215 nvticache_get_src (const char *oid)
216 {
217  char *filename, *src;
218 
219  assert (cache_kb);
220 
221  filename = kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
222  if (!filename)
223  return NULL;
224  src = g_build_filename (src_path, filename, NULL);
225  g_free (filename);
226  return src;
227 }
228 
236 char *
237 nvticache_get_oid (const char *filename)
238 {
239  char *ret, pattern[2048];
240  struct kb_item *kbi;
241 
242  assert (cache_kb);
243 
244  g_snprintf (pattern, sizeof (pattern), "filename:%s:oid", filename);
245  ret = kb_item_get_str (cache_kb, pattern);
246  if (ret)
247  return ret;
248 
249  /* NVT filename in subfolder case. */
250  g_snprintf (pattern, sizeof (pattern), "filename:*/%s:oid", filename);
251  kbi = kb_item_get_pattern (cache_kb, pattern);
252  if (!kbi)
253  return NULL;
254 
255  ret = g_strdup (kbi->v_str);
256  kb_item_free (kbi);
257  return ret;
258 }
259 
267 char *
269 {
270  assert (cache_kb);
271  return kb_nvt_get (cache_kb, oid, NVT_FILENAME_POS);
272 }
273 
281 char *
283 {
284  assert (cache_kb);
285  return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_KEYS_POS);
286 }
287 
295 char *
297 {
298  assert (cache_kb);
299  return kb_nvt_get (cache_kb, oid, NVT_MANDATORY_KEYS_POS);
300 }
301 
309 char *
311 {
312  assert (cache_kb);
313  return kb_nvt_get (cache_kb, oid, NVT_EXCLUDED_KEYS_POS);
314 }
315 
323 char *
325 {
326  assert (cache_kb);
327  return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_UDP_PORTS_POS);
328 }
329 
337 char *
339 {
340  assert (cache_kb);
341  return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_PORTS_POS);
342 }
343 
351 char *
353 {
354  assert (cache_kb);
355  return kb_nvt_get (cache_kb, oid, NVT_DEPENDENCIES_POS);
356 }
357 
365 int
367 {
368  int category;
369  char *category_s;
370 
371  assert (cache_kb);
372  category_s = kb_nvt_get (cache_kb, oid, NVT_CATEGORY_POS);
373  category = atoi (category_s);
374  g_free (category_s);
375  return category;
376 }
377 
385 int
387 {
388  int timeout;
389  char *timeout_s;
390 
391  assert (cache_kb);
392  timeout_s = kb_nvt_get (cache_kb, oid, NVT_TIMEOUT_POS);
393  timeout = atoi (timeout_s);
394  g_free (timeout_s);
395  return timeout;
396 }
397 
405 char *
406 nvticache_get_name (const char *oid)
407 {
408  assert (cache_kb);
409  return kb_nvt_get (cache_kb, oid, NVT_NAME_POS);
410 }
411 
419 char *
421 {
422  assert (cache_kb);
423  return kb_nvt_get (cache_kb, oid, NVT_VERSION_POS);
424 }
425 
433 char *
435 {
436  assert (cache_kb);
437  return kb_nvt_get (cache_kb, oid, NVT_COPYRIGHT_POS);
438 }
439 
447 char *
448 nvticache_get_cves (const char *oid)
449 {
450  assert (cache_kb);
451  return kb_nvt_get (cache_kb, oid, NVT_CVES_POS);
452 }
453 
461 char *
462 nvticache_get_bids (const char *oid)
463 {
464  assert (cache_kb);
465  return kb_nvt_get (cache_kb, oid, NVT_BIDS_POS);
466 }
467 
475 char *
477 {
478  assert (cache_kb);
479  return kb_nvt_get (cache_kb, oid, NVT_XREFS_POS);
480 }
481 
489 char *
491 {
492  assert (cache_kb);
493  return kb_nvt_get (cache_kb, oid, NVT_FAMILY_POS);
494 }
495 
503 char *
504 nvticache_get_tags (const char *oid)
505 {
506  assert (cache_kb);
507  return kb_nvt_get (cache_kb, oid, NVT_TAGS_POS);
508 }
509 
517 GSList *
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  list = g_slist_append (list, np);
540  element = element->next;
541  }
542  kb_item_free (prefs);
543 
544  return list;
545 }
546 
552 GSList *
554 {
555  struct kb_item *kbi, *item;
556  GSList *list = NULL;
557 
558  assert (cache_kb);
559 
560  kbi = item = kb_item_get_pattern (cache_kb, "filename:*:oid");
561  if (!kbi)
562  return NULL;
563 
564  while (item)
565  {
566  list = g_slist_prepend (list, g_strdup (item->v_str));
567  item = item->next;
568  }
569  kb_item_free (kbi);
570  return list;
571 }
572 
578 size_t
580 {
581  assert (cache_kb);
582 
583  return kb_item_count (cache_kb, "nvt:*");
584 }
585 
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:timestamp", filename);
604  kb_del_items (cache_kb, pattern);
605  g_snprintf (pattern, sizeof (pattern), "filename:%s:oid", filename);
606  kb_del_items (cache_kb, pattern);
607  }
608  g_free (filename);
609 }
char * nvticache_get_excluded_keys(const char *oid)
Get the Excluded Keys from a plugin OID.
Definition: nvticache.c:310
char * nvticache_get_oid(const char *filename)
Get the OID from a plugin filename.
Definition: nvticache.c:237
int nvticache_check(const gchar *filename)
Check if the nvt for the given filename exists in cache.
Definition: nvticache.c:114
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
Definition: kb_redis.c:501
void nvticache_reset()
Reset connection to KB. To be called after a fork().
Definition: nvticache.c:138
char * nvticache_get_required_keys(const char *oid)
Get the Required Keys from a plugin OID.
Definition: nvticache.c:282
char * nvticache_get_filename(const char *oid)
Get the filename from a plugin OID.
Definition: nvticache.c:268
gchar * name
Name of the preference.
Definition: nvti.h:48
The structure of a information record that corresponds to a NVT.
Definition: nvti.h:64
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:81
struct kb_item * next
Definition: kb.h:91
int nvticache_add(const nvti_t *nvti, const char *filename)
Add a NVT Information to the cache.
Definition: nvticache.c:157
const char * oid
char * nvticache_get_src(const char *oid)
Get the full source filename of an OID.
Definition: nvticache.c:215
char * nvticache_get_mandatory_keys(const char *oid)
Get the Mandatory Keys from a plugin OID.
Definition: nvticache.c:296
int nvticache_get_timeout(const char *oid)
Get the Timeout from a plugin OID.
Definition: nvticache.c:386
char * nvticache_get_version(const char *oid)
Get the version from a plugin OID.
Definition: nvticache.c:420
char * nvticache_get_copyright(const char *oid)
Get the copyright from a plugin OID.
Definition: nvticache.c:434
gchar * type
Preference type.
Definition: nvti.h:47
void nvticache_delete(const char *oid)
Definition: nvticache.c:587
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:102
char * nvticache_get_cves(const char *oid)
Get the cves from a plugin OID.
Definition: nvticache.c:448
kb_t cache_kb
Definition: nvticache.c:52
int nvticache_initialized(void)
Return whether the nvt cache is initialized.
Definition: nvticache.c:60
gchar * dflt
Default value of the preference.
Definition: nvti.h:49
char * nvticache_get_required_ports(const char *oid)
Get the Required ports from a plugin OID.
Definition: nvticache.c:338
kb_t nvticache_get_kb(void)
Return the nvticache kb.
Definition: nvticache.c:97
GSList * nvticache_get_prefs(const char *oid)
Get the prefs from a plugin OID.
Definition: nvticache.c:518
char * nvticache_get_xrefs(const char *oid)
Get the xrefs from a plugin OID.
Definition: nvticache.c:476
The structure for a preference of a NVT.
Definition: nvti.h:45
GSList * nvticache_get_oids()
Get the list of nvti OIDs.
Definition: nvticache.c:553
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
Definition: nvticache.c:406
char * nvticache_get_bids(const char *oid)
Get the bids from a plugin OID.
Definition: nvticache.c:462
size_t nvticache_count()
Get the number of nvt&#39;s in the cache.
Definition: nvticache.c:579
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:352
char * src_path
Definition: nvticache.c:51
gchar * nvti_oid(const nvti_t *n)
Get the OID string.
Definition: nvti.c:218
char * nvticache_get_family(const char *oid)
Get the family from a plugin OID.
Definition: nvticache.c:490
char * nvticache_get_tags(const char *oid)
Get the tags from a plugin OID.
Definition: nvticache.c:504
char * v_str
Definition: kb.h:87
char * nvticache_get_required_udp_ports(const char *oid)
Get the Required udp ports from a plugin OID.
Definition: nvticache.c:324
GSList * prefs
Collection of NVT preferences.
Definition: nvti.h:86
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:366