OpenVAS Libraries  9.0.3
nasl_global_ctxt.h File Reference
#include <stdio.h>
#include "../base/kb.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, pc->fp is set with the respective file descriptor -1 if either the filename was not found/accessible or the signature verification failed (provided signature checking is enabled. Also, the pc->fp is set to NULL. In any case, various elements of pc are modified (initialized);

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

Referenced by exec_nasl_script().

2773 {
2774  char *full_name = NULL, key_path[2048], *checksum, *filename, *check = NULL;
2775  GSList * inc_dir = inc_dirs; // iterator for include directories
2776 
2777  // initialize if not yet done (for openvas-server < 2.0.1)
2778  if (! inc_dirs) add_nasl_inc_dir("");
2779 
2780  pc->line_nb = 1;
2781  pc->tree = NULL;
2782  pc->buffer = g_malloc0 (80);
2783  pc->maxlen = 80;
2784  pc->fp = NULL;
2786  while (inc_dir != NULL) {
2787  if (full_name)
2788  g_free (full_name);
2789  full_name = g_build_filename(inc_dir->data, name, NULL);
2790 
2791  if ((pc->fp = fopen(full_name, "r")) != NULL)
2792  break;
2793 
2794  inc_dir = g_slist_next(inc_dir);
2795  }
2796 
2797  if (! pc->fp) {
2798  log_legacy_write ("%s: Not able to open nor to locate it in include paths",
2799  name);
2800  g_free(full_name);
2801  return -1;
2802  }
2803 
2804  if (pc->always_authenticated)
2805  {
2806  g_free(full_name);
2807  return 0;
2808  }
2809  /* Cache the checksum of signature verified files, so that commonly included
2810  * files are not verified multiple times per scan. */
2811 
2812  filename = full_name;
2813  if (strstr (full_name, ".inc"))
2814  filename = basename (full_name);
2815  init_checksum_algorithm ();
2816  if (checksum_algorithm == GCRY_MD_NONE)
2817  return -1;
2818 
2819  snprintf (key_path, sizeof (key_path), "checksum:%s", filename);
2820  checksum = kb_item_get_str (pc->kb, key_path);
2821  if (checksum)
2822  {
2823  int ret;
2824  check = file_checksum (full_name, checksum_algorithm);
2825  ret = strcmp (check, checksum);
2826  if (!ret)
2827  {
2828  /* Already checked. No need to check again. */
2829  g_free (full_name);
2830  g_free (checksum);
2831  g_free (check);
2832  return 0;
2833  }
2834  g_free (checksum);
2835  g_free (check);
2836  }
2837 
2838  load_checksums (pc->kb);
2839  if (checksum_algorithm == GCRY_MD_MD5)
2840  snprintf (key_path, sizeof (key_path), "md5sums:%s", filename);
2841  else if (checksum_algorithm == GCRY_MD_SHA256)
2842  snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2843  else
2844  abort ();
2845  checksum = kb_item_get_str (pc->kb, key_path);
2846  if (!checksum)
2847  {
2848  log_legacy_write ("No checksum for %s", full_name);
2849  g_free (full_name);
2850  return -1;
2851  }
2852  else
2853  {
2854  int ret;
2855 
2856  check = file_checksum (full_name, checksum_algorithm);
2857  ret = strcmp (check, checksum);
2858  if (ret)
2859  log_legacy_write ("checksum for %s not matching", full_name);
2860  else
2861  {
2862  snprintf (key_path, sizeof (key_path), "checksum:%s", filename);
2863  kb_item_set_str (pc->kb, key_path, check);
2864  }
2865  g_free (full_name);
2866  g_free (checksum);
2867  g_free (check);
2868  return ret;
2869  }
2870 }
char * buffer
tree_cell * tree
int always_authenticated
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:71
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
int add_nasl_inc_dir(const char *dir)
Adds the given string as directory for searching for includes.
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ nasl_clean_ctx()

void nasl_clean_ctx ( naslctxt )

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

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

Referenced by exec_nasl_script().

2874 {
2875 #if 0
2876  nasl_dump_tree(c->tree);
2877 #endif
2878  deref_cell(c->tree);
2879  g_free(c->buffer);
2880  c->buffer = NULL;
2881  if (c->fp)
2882  {
2883  fclose(c->fp);
2884  c->fp = NULL;
2885  }
2886 }
void nasl_dump_tree(const tree_cell *c)
Definition: nasl_tree.c:439
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
Here is the call graph for this function:
Here is the caller graph for this function: