OpenVAS Scanner  7.0.1~git
nasl_global_ctxt.h File Reference
#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 2792 of file nasl_grammar.tab.c.

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

Referenced by exec_nasl_script(), and yyparse().

2793 {
2794  char *full_name = NULL, key_path[2048], *checksum, *filename;
2795  GSList * inc_dir = inc_dirs; // iterator for include directories
2796  size_t flen = 0;
2797  time_t timestamp;
2798 
2799  // initialize if not yet done (for openvas-server < 2.0.1)
2800  if (! inc_dirs) add_nasl_inc_dir("");
2801 
2802  pc->line_nb = 1;
2803  pc->tree = NULL;
2804  if (!parse_len)
2805  {
2806  parse_len = 9092;
2807  parse_buffer = g_malloc0 (parse_len);
2808  }
2809  else
2810  parse_buffer[0] = '\0';
2811 
2812 
2814  while (inc_dir != NULL) {
2815  if (full_name)
2816  g_free (full_name);
2817  full_name = g_build_filename(inc_dir->data, name, NULL);
2818 
2819  if ((g_file_get_contents (full_name, &pc->buffer, &flen, NULL)))
2820  break;
2821 
2822  inc_dir = g_slist_next(inc_dir);
2823  }
2824 
2825  if (!full_name || !pc->buffer) {
2826  g_message ("%s: Not able to open nor to locate it in include paths",
2827  name);
2828  g_free(full_name);
2829  return -1;
2830  }
2831 
2832  if (pc->always_signed)
2833  {
2834  g_free(full_name);
2835  return 0;
2836  }
2837  /* Cache the checksum of signature verified files, so that commonly included
2838  * files are not verified multiple times per scan. */
2839  if (strstr (full_name, ".inc"))
2840  filename = basename (full_name);
2841  else
2842  filename = full_name;
2843  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2844  timestamp = kb_item_get_int (pc->kb, key_path);
2845  if (timestamp > 0)
2846  {
2847  struct stat file_stat;
2848 
2849  if (stat (full_name, &file_stat) >= 0 && timestamp > file_stat.st_mtime)
2850  {
2851  /* Already checked. No need to check again. */
2852  g_free (full_name);
2853  return 0;
2854  }
2855  }
2856 
2857  load_checksums (pc->kb);
2858  if (checksum_algorithm == GCRY_MD_NONE)
2859  return -1;
2860  else if (checksum_algorithm == GCRY_MD_MD5)
2861  snprintf (key_path, sizeof (key_path), "md5sums:%s", filename);
2862  else if (checksum_algorithm == GCRY_MD_SHA256)
2863  snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2864  else
2865  abort ();
2866  checksum = kb_item_get_str (pc->kb, key_path);
2867  if (!checksum)
2868  {
2869  g_warning ("No checksum for %s", full_name);
2870  g_free (full_name);
2871  return -1;
2872  }
2873  else
2874  {
2875  int ret;
2876  char *check = file_checksum (full_name, checksum_algorithm);
2877 
2878  ret = strcmp (check, checksum);
2879  if (ret)
2880  g_warning ("checksum for %s not matching", full_name);
2881  else
2882  {
2883  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2884  kb_item_add_int (pc->kb, key_path, time (NULL));
2885  }
2886  g_free (full_name);
2887  g_free (checksum);
2888  g_free (check);
2889  return ret;
2890  }
2891 }
static void load_checksums(kb_t kb)
char * buffer
static int checksum_algorithm
tree_cell * tree
static GSList * inc_dirs
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:97
int add_nasl_inc_dir(const char *dir)
Adds the given string as directory for searching for includes.
static char * parse_buffer
const char * name
Definition: nasl_init.c:377
static int parse_len
static char * file_checksum(const char *filename, int algorithm)
Get the checksum of a file.
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 2894 of file nasl_grammar.tab.c.

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

Referenced by exec_nasl_script().

2895 {
2896  deref_cell(c->tree);
2897  g_free (c->buffer);
2898 }
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
Here is the call graph for this function:
Here is the caller graph for this function: