OpenVAS Scanner  22.7.9
nasl_global_ctxt.h File Reference
#include "nasl_tree.h"
#include <gvm/util/kb.h>
#include <stdio.h>
Include dependency graph for nasl_global_ctxt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  naslctxt
 

Functions

int init_nasl_ctx (naslctxt *, const char *)
 Initialize a NASL context for a NASL file. More...
 
void nasl_clean_ctx (naslctxt *)
 

Function Documentation

◆ init_nasl_ctx()

int init_nasl_ctx ( naslctxt pc,
const char *  name 
)

Initialize a NASL context for a NASL file.

Parameters
pcThe NASL context handler.
nameThe filename of the NASL script.
Returns
0 in case of success. Then, file content is set in pc->buffer. -1 if either the filename was not found/accessible or the signature verification failed (provided signature checking is enabled. In any case, various elements of pc are modified (initialized);

Definition at line 2684 of file nasl_grammar.tab.c.

2685 {
2686  char *full_name = NULL, key_path[2048], *checksum, *filename;
2687  GSList * inc_dir = inc_dirs; // iterator for include directories
2688  size_t flen = 0;
2689  time_t timestamp;
2690 
2691  // initialize if not yet done (for openvas-server < 2.0.1)
2692  if (! inc_dirs) add_nasl_inc_dir("");
2693 
2694  pc->line_nb = 1;
2695  pc->name = (char *) name;
2696  pc->include_order = include_order++;
2697  pc->tree = NULL;
2698  if (!parse_len)
2699  {
2700  parse_len = 9092;
2701  parse_buffer = g_malloc0 (parse_len);
2702  }
2703  else
2704  parse_buffer[0] = '\0';
2705 
2706 
2708  while (inc_dir != NULL) {
2709  if (full_name)
2710  g_free (full_name);
2711  full_name = g_build_filename(inc_dir->data, name, NULL);
2712 
2713  if ((g_file_get_contents (full_name, &pc->buffer, &flen, NULL)))
2714  break;
2715 
2716  inc_dir = g_slist_next(inc_dir);
2717  }
2718 
2719  if (!full_name || !pc->buffer) {
2720  g_message ("%s: Not able to open nor to locate it in include paths",
2721  name);
2722  g_free(full_name);
2723  return -1;
2724  }
2725 
2726  if (pc->always_signed)
2727  {
2728  g_free(full_name);
2729  return 0;
2730  }
2731  /* Cache the checksum of signature verified files, so that commonly included
2732  * files are not verified multiple times per scan. */
2733  if (strstr (full_name, ".inc"))
2734  filename = basename (full_name);
2735  else
2736  filename = full_name;
2737  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2738  timestamp = kb_item_get_int (pc->kb, key_path);
2739 
2740  /* We never use the mtime of a .nasl/.inc file as integrity check during
2741  * the script load up. A complete verification is done in this case.
2742  * Once it has been uploaded in the nvticache it is enough to just check
2743  * the mtime. */
2744  if (timestamp > 0 && pc->exec_descr == 0)
2745  {
2746  struct stat file_stat;
2747 
2748  if (stat (full_name, &file_stat) >= 0 && timestamp > file_stat.st_mtime)
2749  {
2750  /* Already checked. No need to check again. */
2751  g_free (full_name);
2752  return 0;
2753  }
2754  }
2755 
2756  load_checksums (pc->kb);
2757  if (checksum_algorithm == GCRY_MD_NONE)
2758  return -1;
2759  else if (checksum_algorithm == GCRY_MD_SHA256)
2760  snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2761  else
2762  abort ();
2763  checksum = kb_item_get_str (pc->kb, key_path);
2764  if (!checksum)
2765  {
2766  g_warning ("No checksum for %s", full_name);
2767  g_free (full_name);
2768  return -1;
2769  }
2770  else
2771  {
2772  int ret;
2773  char *check = file_checksum (full_name, checksum_algorithm);
2774 
2775  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2776  ret = strcmp (check, checksum);
2777  if (ret)
2778  {
2779  kb_del_items (pc->kb, key_path);
2780  g_warning ("checksum for %s not matching", full_name);
2781  }
2782  else
2783  {
2784  kb_del_items (pc->kb, key_path);
2785  kb_item_add_int (pc->kb, key_path, time (NULL));
2786  }
2787 
2788  g_free (full_name);
2789  g_free (checksum);
2790  g_free (check);
2791  return ret;
2792  }
2793 }

References add_nasl_inc_dir(), naslctxt::always_signed, naslctxt::buffer, checksum_algorithm, naslctxt::exec_descr, file_checksum(), inc_dirs, naslctxt::include_order, include_order, naslctxt::kb, naslctxt::line_nb, load_checksums(), naslctxt::name, name, nasl_set_filename(), parse_buffer, parse_len, and naslctxt::tree.

Referenced by exec_nasl_script(), and yyparse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_clean_ctx()

void nasl_clean_ctx ( naslctxt )

Definition at line 2796 of file nasl_grammar.tab.c.

2797 {
2798  deref_cell(c->tree);
2799  g_free (c->buffer);
2800 }

References naslctxt::buffer, deref_cell(), and naslctxt::tree.

Referenced by exec_nasl_script().

Here is the call graph for this function:
Here is the caller graph for this function:
naslctxt::include_order
unsigned int include_order
Definition: nasl_global_ctxt.h:26
load_checksums
static void load_checksums(kb_t kb)
Definition: nasl_grammar.tab.c:2570
inc_dirs
static GSList * inc_dirs
Definition: nasl_grammar.tab.c:2522
include_order
static int include_order
Definition: nasl_grammar.tab.c:112
checksum_algorithm
static int checksum_algorithm
Definition: nasl_grammar.tab.c:2567
name
const char * name
Definition: nasl_init.c:411
naslctxt::kb
kb_t kb
Definition: nasl_global_ctxt.h:29
naslctxt::exec_descr
int exec_descr
Definition: nasl_global_ctxt.h:23
naslctxt::buffer
char * buffer
Definition: nasl_global_ctxt.h:28
file_checksum
static char * file_checksum(const char *filename, int algorithm)
Get the checksum of a file.
Definition: nasl_grammar.tab.c:2649
parse_buffer
static char * parse_buffer
Definition: nasl_grammar.tab.c:109
naslctxt::tree
tree_cell * tree
Definition: nasl_global_ctxt.h:27
naslctxt::always_signed
int always_signed
Definition: nasl_global_ctxt.h:21
add_nasl_inc_dir
int add_nasl_inc_dir(const char *dir)
Adds the given string as directory for searching for includes.
Definition: nasl_grammar.tab.c:2539
parse_len
static int parse_len
Definition: nasl_grammar.tab.c:111
nasl_set_filename
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:88
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:181
naslctxt::line_nb
int line_nb
Definition: nasl_global_ctxt.h:19
naslctxt::name
char * name
Definition: nasl_global_ctxt.h:20