"Fossies" - the Fresh Open Source Software Archive

Member "gvm-libs-11.0.1/util/nvticache.c" (12 May 2020, 13950 Bytes) of package /linux/misc/openvas/gvm-libs-11.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nvticache.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 11.0.0_vs_11.0.1.

    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 
   20 /**
   21  * @file
   22  * @brief Implementation of API to handle NVT Info Cache
   23  *
   24  * This file contains all methods to handle NVT Information Cache
   25  * (nvticache_t).
   26  *
   27  * The module consequently uses glib datatypes and api for memory
   28  * management etc.
   29  */
   30 
   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 /**
   45  * @brief GLib log domain.
   46  */
   47 #define G_LOG_DOMAIN "lib  nvticache"
   48 
   49 char *src_path = NULL; /**< The directory of the source files. */
   50 kb_t cache_kb = NULL;  /**< Cache KB handler. */
   51 int cache_saved = 1;   /**< If cache was saved. */
   52 
   53 /**
   54  * @brief Return whether the nvt cache is initialized.
   55  *
   56  * @return 1 if cache is initialized, 0 otherwise.
   57  */
   58 int
   59 nvticache_initialized (void)
   60 {
   61   return !!cache_kb;
   62 }
   63 
   64 /**
   65  * @brief Initializes the nvti cache.
   66  *
   67  * @param src           The directory that contains the nvt files.
   68  * @param kb_path       Path to kb socket.
   69  *
   70  * @return 0 in case of success, anything else indicates an error.
   71  */
   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_STR);
   83   if (cache_kb)
   84     return 0;
   85 
   86   if (kb_new (&cache_kb, kb_path)
   87       || kb_item_set_str (cache_kb, NVTICACHE_STR, "0", 0))
   88     return -1;
   89   return 0;
   90 }
   91 
   92 /**
   93  * @brief Return the nvticache kb.
   94  *
   95  * @return Cache kb.
   96  */
   97 kb_t
   98 nvticache_get_kb (void)
   99 {
  100   assert (cache_kb);
  101   return cache_kb;
  102 }
  103 
  104 /**
  105  * @brief Check if the nvt for the given filename exists in cache.
  106  *
  107  * @param filename The name of the original NVT without the path
  108  *                 to the base location of NVTs (e.g.
  109  *                 "scriptname1.nasl" or even
  110  *                 "subdir1/subdir2/scriptname2.nasl" )
  111  *
  112  * @return 1 if nvt is in cache and up to date, 0 otherwise.
  113  */
  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 
  132 /**
  133  * @brief Reset connection to KB. To be called after a fork().
  134  */
  135 void
  136 nvticache_reset ()
  137 {
  138   if (cache_kb)
  139     kb_lnk_reset (cache_kb);
  140 }
  141 
  142 /**
  143  * @brief Determine the version of the NVT feed.
  144  *
  145  * @return Feed version string if success, NULL otherwise.
  146  */
  147 static char *
  148 nvt_feed_version ()
  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 
  178 /**
  179  * @brief Save the nvticache to disk.
  180  */
  181 void
  182 nvticache_save ()
  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 (feed_version && 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 
  202 /**
  203  * @brief Add a NVT Information to the cache.
  204  *
  205  * @param nvti     The NVT Information to add
  206  *
  207  * @param filename The name of the original NVT without the path
  208  *                 to the base location of NVTs (e.g.
  209  *                 "scriptname1.nasl" or even
  210  *                 "subdir1/subdir2/scriptname2.nasl" )
  211  *
  212  * @return 0 in case of success, anything else indicates an error.
  213  */
  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 
  248 /**
  249  * @brief Get the full source filename of an OID.
  250  *
  251  * @param oid      The OID to look up.
  252  *
  253  * @return Filename with full path matching OID if found, NULL otherwise.
  254  */
  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 
  270 /**
  271  * @brief Get the OID from a plugin filename.
  272  *
  273  * @param filename      Filename to lookup.
  274  *
  275  * @return OID matching filename if found, NULL otherwise.
  276  */
  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 
  285 /**
  286  * @brief Get the filename from a plugin OID.
  287  *
  288  * @param[in]   oid     OID to match.
  289  *
  290  * @return Filanem matching OID, NULL otherwise.
  291  */
  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 
  299 /**
  300  * @brief Get the Required Keys from a plugin OID.
  301  *
  302  * @param[in]   oid     OID to match.
  303  *
  304  * @return Required Keys matching OID, NULL otherwise.
  305  */
  306 char *
  307 nvticache_get_required_keys (const char *oid)
  308 {
  309   assert (cache_kb);
  310   return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_KEYS_POS);
  311 }
  312 
  313 /**
  314  * @brief Get the Mandatory Keys from a plugin OID.
  315  *
  316  * @param[in]   oid     OID to match.
  317  *
  318  * @return Mandatory Keys matching OID, NULL otherwise.
  319  */
  320 char *
  321 nvticache_get_mandatory_keys (const char *oid)
  322 {
  323   assert (cache_kb);
  324   return kb_nvt_get (cache_kb, oid, NVT_MANDATORY_KEYS_POS);
  325 }
  326 
  327 /**
  328  * @brief Get the Excluded Keys from a plugin OID.
  329  *
  330  * @param[in]   oid     OID to match.
  331  *
  332  * @return Excluded Keys matching OID, NULL otherwise.
  333  */
  334 char *
  335 nvticache_get_excluded_keys (const char *oid)
  336 {
  337   assert (cache_kb);
  338   return kb_nvt_get (cache_kb, oid, NVT_EXCLUDED_KEYS_POS);
  339 }
  340 
  341 /**
  342  * @brief Get the Required udp ports from a plugin OID.
  343  *
  344  * @param[in]   oid     OID to match.
  345  *
  346  * @return Required udp ports matching OID, NULL otherwise.
  347  */
  348 char *
  349 nvticache_get_required_udp_ports (const char *oid)
  350 {
  351   assert (cache_kb);
  352   return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_UDP_PORTS_POS);
  353 }
  354 
  355 /**
  356  * @brief Get the Required ports from a plugin OID.
  357  *
  358  * @param[in]   oid     OID to match.
  359  *
  360  * @return Required ports matching OID, NULL otherwise.
  361  */
  362 char *
  363 nvticache_get_required_ports (const char *oid)
  364 {
  365   assert (cache_kb);
  366   return kb_nvt_get (cache_kb, oid, NVT_REQUIRED_PORTS_POS);
  367 }
  368 
  369 /**
  370  * @brief Get the Dependencies from a plugin OID.
  371  *
  372  * @param[in]   oid     OID to match.
  373  *
  374  * @return Dependencies matching OID, NULL otherwise.
  375  */
  376 char *
  377 nvticache_get_dependencies (const char *oid)
  378 {
  379   assert (cache_kb);
  380   return kb_nvt_get (cache_kb, oid, NVT_DEPENDENCIES_POS);
  381 }
  382 
  383 /**
  384  * @brief Get the Category from a plugin OID.
  385  *
  386  * @param[in]   oid     OID to match.
  387  *
  388  * @return Category matching OID, -1 otherwise.
  389  */
  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 
  403 /**
  404  * @brief Get the Timeout from a plugin OID.
  405  *
  406  * @param[in]   oid     OID to match.
  407  *
  408  * @return Timeout matching OID, -1 otherwise.
  409  */
  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 
  423 /**
  424  * @brief Get the name from a plugin OID.
  425  *
  426  * @param[in]   oid     OID to match.
  427  *
  428  * @return Name matching OID, NULL otherwise.
  429  */
  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 
  437 /**
  438  * @brief Get the cves from a plugin OID.
  439  *
  440  * @param[in]   oid     OID to match.
  441  *
  442  * @return CVEs matching OID, NULL otherwise.
  443  */
  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 
  451 /**
  452  * @brief Get the bids from a plugin OID.
  453  *
  454  * @param[in]   oid     OID to match.
  455  *
  456  * @return BIDs matching OID, NULL otherwise.
  457  */
  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 
  465 /**
  466  * @brief Get the xrefs from a plugin OID.
  467  *
  468  * @param[in]   oid     OID to match.
  469  *
  470  * @return XREFs matching OID, NULL otherwise.
  471  */
  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 
  479 /**
  480  * @brief Get the family from a plugin OID.
  481  *
  482  * @param[in]   oid     OID to match.
  483  *
  484  * @return Family matching OID, NULL otherwise.
  485  */
  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 
  493 /**
  494  * @brief Get the tags from a plugin OID.
  495  *
  496  * @param[in]   oid     OID to match.
  497  *
  498  * @return Tags matching OID, NULL otherwise.
  499  */
  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 
  507 /**
  508  * @brief Get the nvti from a plugin OID.
  509  *
  510  * @param[in]   oid     OID to match.
  511  *
  512  * @return Full nvti matching OID, NULL otherwise.
  513  */
  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 
  521 /**
  522  * @brief Get the prefs from a plugin OID.
  523  *
  524  * @param[in]   oid     OID to match.
  525  *
  526  * @return Prefs matching OID, NULL otherwise.
  527  */
  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 
  556 /**
  557  * @brief Get the list of nvti OIDs.
  558  *
  559  * @return OIDs list.
  560  */
  561 GSList *
  562 nvticache_get_oids ()
  563 {
  564   assert (cache_kb);
  565 
  566   return kb_nvt_get_oids (cache_kb);
  567 }
  568 
  569 /**
  570  * @brief Get the number of nvt's in the cache.
  571  *
  572  * @return Number of nvt's.
  573  */
  574 size_t
  575 nvticache_count ()
  576 {
  577   assert (cache_kb);
  578 
  579   return kb_item_count (cache_kb, "nvt:*");
  580 }
  581 
  582 /**
  583  * @brief Delete NVT from the cache.
  584  *
  585  * @param[in] oid OID to match.
  586  */
  587 void
  588 nvticache_delete (const char *oid)
  589 {
  590   char pattern[4096];
  591   char *filename;
  592 
  593   assert (cache_kb);
  594   assert (oid);
  595 
  596   filename = nvticache_get_filename (oid);
  597   g_snprintf (pattern, sizeof (pattern), "oid:%s:prefs", oid);
  598   kb_del_items (cache_kb, pattern);
  599   g_snprintf (pattern, sizeof (pattern), "nvt:%s", oid);
  600   kb_del_items (cache_kb, pattern);
  601 
  602   if (filename)
  603     {
  604       g_snprintf (pattern, sizeof (pattern), "filename:%s", filename);
  605       kb_del_items (cache_kb, pattern);
  606     }
  607   g_free (filename);
  608 }
  609 
  610 /**
  611  * @brief Get the NVT feed version.
  612  *
  613  * @return Feed version.
  614  */
  615 char *
  616 nvticache_feed_version (void)
  617 {
  618   return kb_item_get_str (cache_kb, NVTICACHE_STR);
  619 }
  620 
  621 /**
  622  * @brief Check if the plugins feed was newer than cached feed.
  623  *
  624  * @return 1 if new feed, 0 if matching feeds or error.
  625  */
  626 int
  627 nvticache_check_feed (void)
  628 {
  629   char *cached, *current;
  630   int ret;
  631 
  632   if (!(current = nvt_feed_version ()))
  633     return 0;
  634   cached = kb_item_get_str (cache_kb, NVTICACHE_STR);
  635   ret = strcmp (cached, current);
  636   g_free (cached);
  637   g_free (current);
  638   return ret;
  639 }