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 2773 of file nasl_grammar.tab.c.

Referenced by exec_nasl_script().

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

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

Referenced by exec_nasl_script().

2875 {
2876 #if 0
2877  nasl_dump_tree(c->tree);
2878 #endif
2879  deref_cell(c->tree);
2880  g_free(c->buffer);
2881  c->buffer = NULL;
2882  if (c->fp)
2883  {
2884  fclose(c->fp);
2885  c->fp = NULL;
2886  }
2887 }
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: