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

2895 {
2896  char *full_name = NULL, key_path[2048], *checksum, *filename;
2897  GSList * inc_dir = inc_dirs; // iterator for include directories
2898  size_t flen = 0;
2899  time_t timestamp;
2900 
2901  // initialize if not yet done (for openvas-server < 2.0.1)
2902  if (! inc_dirs) add_nasl_inc_dir("");
2903 
2904  pc->line_nb = 1;
2905  pc->tree = NULL;
2906  if (!parse_len)
2907  {
2908  parse_len = 9092;
2909  parse_buffer = g_malloc0 (parse_len);
2910  }
2911  else
2912  parse_buffer[0] = '\0';
2913 
2914 
2916  while (inc_dir != NULL) {
2917  if (full_name)
2918  g_free (full_name);
2919  full_name = g_build_filename(inc_dir->data, name, NULL);
2920 
2921  if ((g_file_get_contents (full_name, &pc->buffer, &flen, NULL)))
2922  break;
2923 
2924  inc_dir = g_slist_next(inc_dir);
2925  }
2926 
2927  if (!full_name || !pc->buffer) {
2928  g_message ("%s: Not able to open nor to locate it in include paths",
2929  name);
2930  g_free(full_name);
2931  return -1;
2932  }
2933 
2934  if (pc->always_signed)
2935  {
2936  g_free(full_name);
2937  return 0;
2938  }
2939  /* Cache the checksum of signature verified files, so that commonly included
2940  * files are not verified multiple times per scan. */
2941  if (strstr (full_name, ".inc"))
2942  filename = basename (full_name);
2943  else
2944  filename = full_name;
2945  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2946  timestamp = kb_item_get_int (pc->kb, key_path);
2947  if (timestamp > 0)
2948  {
2949  struct stat file_stat;
2950 
2951  if (stat (full_name, &file_stat) >= 0 && timestamp > file_stat.st_mtime)
2952  {
2953  /* Already checked. No need to check again. */
2954  g_free (full_name);
2955  return 0;
2956  }
2957  }
2958 
2959  load_checksums (pc->kb);
2960  if (checksum_algorithm == GCRY_MD_NONE)
2961  return -1;
2962  else if (checksum_algorithm == GCRY_MD_MD5)
2963  snprintf (key_path, sizeof (key_path), "md5sums:%s", filename);
2964  else if (checksum_algorithm == GCRY_MD_SHA256)
2965  snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2966  else
2967  abort ();
2968  checksum = kb_item_get_str (pc->kb, key_path);
2969  if (!checksum)
2970  {
2971  g_warning ("No checksum for %s", full_name);
2972  g_free (full_name);
2973  return -1;
2974  }
2975  else
2976  {
2977  int ret;
2978  char *check = file_checksum (full_name, checksum_algorithm);
2979 
2980  ret = strcmp (check, checksum);
2981  if (ret)
2982  g_warning ("checksum for %s not matching", full_name);
2983  else
2984  {
2985  snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2986  kb_item_add_int (pc->kb, key_path, time (NULL));
2987  }
2988  g_free (full_name);
2989  g_free (checksum);
2990  g_free (check);
2991  return ret;
2992  }
2993 }

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().

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

2997 {
2998  deref_cell(c->tree);
2999  g_free (c->buffer);
3000 }

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:
load_checksums
static void load_checksums(kb_t kb)
Definition: nasl_grammar.tab.c:2769
inc_dirs
static GSList * inc_dirs
Definition: nasl_grammar.tab.c:2724
checksum_algorithm
static int checksum_algorithm
Definition: nasl_grammar.tab.c:2766
name
const char * name
Definition: nasl_init.c:377
naslctxt::kb
kb_t kb
Definition: nasl_global_ctxt.h:33
naslctxt::buffer
char * buffer
Definition: nasl_global_ctxt.h:32
file_checksum
static char * file_checksum(const char *filename, int algorithm)
Get the checksum of a file.
Definition: nasl_grammar.tab.c:2859
parse_buffer
static char * parse_buffer
Definition: nasl_grammar.tab.c:120
naslctxt::tree
tree_cell * tree
Definition: nasl_global_ctxt.h:31
naslctxt::always_signed
int always_signed
Definition: nasl_global_ctxt.h:29
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:2738
parse_len
static int parse_len
Definition: nasl_grammar.tab.c:122
nasl_set_filename
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:97
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
naslctxt::line_nb
int line_nb
Definition: nasl_global_ctxt.h:28