OpenVAS Scanner 23.40.3
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 *, const char *)
 Initialize a NASL context for a NASL file.
void nasl_clean_ctx (naslctxt *)

Function Documentation

◆ init_nasl_ctx()

int init_nasl_ctx ( naslctxt * pc,
const char * parent,
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 2723 of file nasl_grammar.tab.c.

2724{
2725 char *full_name = NULL, key_path[2048], *checksum;
2726 const char *filename;
2727 GSList * inc_dir = inc_dirs; // iterator for include directories
2728 size_t flen = 0;
2729 time_t timestamp;
2730
2731 // initialize if not yet done (for openvas-server < 2.0.1)
2732 if (! inc_dirs) add_nasl_inc_dir("");
2733
2734 pc->line_nb = 1;
2735 pc->name = (char *) name;
2737 pc->tree = NULL;
2738 if (!parse_len)
2739 {
2740 parse_len = 9092;
2741 parse_buffer = g_malloc0 (parse_len);
2742 }
2743 else
2744 parse_buffer[0] = '\0';
2745
2746
2748 while (inc_dir != NULL) {
2749 if (full_name)
2750 g_free (full_name);
2751 full_name = g_build_filename(inc_dir->data, name, NULL);
2752
2753 if ((g_file_get_contents (full_name, &pc->buffer, &flen, NULL)))
2754 break;
2755
2756 inc_dir = g_slist_next(inc_dir);
2757 }
2758
2759
2760verify:
2761 if (!full_name || !pc->buffer) {
2762 g_message ("%s: Not able to open nor to locate it in include paths",
2763 name);
2764 g_free(full_name);
2765 return -1;
2766 }
2767
2768 if (pc->always_signed)
2769 {
2770 g_free(full_name);
2771 return 0;
2772 }
2773 /* Cache the checksum of signature verified files, so that commonly included
2774 * files are not verified multiple times per scan. */
2775 // filename should be always without base as we store everything as defined in sha256sums
2776 filename = remove_base(full_name);
2777
2778
2779 snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2780 timestamp = kb_item_get_int (pc->kb, key_path);
2781
2782 /* We never use the mtime of a .nasl/.inc file as integrity check during
2783 * the script load up. A complete verification is done in this case.
2784 * Once it has been uploaded in the nvticache it is enough to just check
2785 * the mtime. */
2786 if (timestamp > 0 && pc->exec_descr == 0)
2787 {
2788 struct stat file_stat;
2789
2790 if (stat (full_name, &file_stat) >= 0 && timestamp > file_stat.st_mtime)
2791 {
2792 /* Already checked. No need to check again. */
2793 g_free (full_name);
2794 return 0;
2795 }
2796 }
2797
2798 load_checksums (pc->kb);
2799 if (checksum_algorithm == GCRY_MD_NONE)
2800 return -1;
2801 else if (checksum_algorithm == GCRY_MD_SHA256)
2802 snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2803 else
2804 abort ();
2805 checksum = kb_item_get_str (pc->kb, key_path);
2806 if (!checksum)
2807 {
2808 // try one more time, but set the parent dir as a base
2809 // this can happen when shorthand includes are used for an example:
2810 // in a nasl file of a/test.nasl -> include("test.inc")
2811 // to include a/test.inc instead of writing include("a/test.inc");
2812 if (parent == NULL) {
2813 g_warning ("No checksum for %s (%s)", full_name, filename);
2814 g_free(full_name);
2815 return -1;
2816 }
2817 g_free(full_name);
2818 full_name = fullname_based_on_parent(parent, name);
2819 parent = NULL;
2820 goto verify;
2821 }
2822 else
2823 {
2824 int ret;
2825 char *check = file_checksum (full_name, checksum_algorithm);
2826
2827 snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2828 ret = strcmp (check, checksum);
2829 if (ret)
2830 {
2831 kb_del_items (pc->kb, key_path);
2832 g_warning ("checksum for %s not matching (%s)", full_name, key_path);
2833 }
2834 else
2835 {
2836 kb_del_items (pc->kb, key_path);
2837 kb_item_add_int (pc->kb, key_path, time (NULL));
2838 }
2839
2840 g_free (full_name);
2841 g_free (checksum);
2842 g_free (check);
2843 return ret;
2844 }
2845}
void nasl_set_filename(const char *filename)
Definition nasl_debug.c:82
static char * file_checksum(const char *filename, int algorithm)
Get the checksum of a file.
static int checksum_algorithm
static char * parse_buffer
static char * fullname_based_on_parent(const char *path, const char *filename)
static const char * remove_base(const char *path)
static int parse_len
int add_nasl_inc_dir(const char *dir)
Adds the given string as directory for searching for includes.
static int include_order
static void load_checksums(kb_t kb)
static GSList * inc_dirs
const char * name
Definition nasl_init.c:440
unsigned int include_order
tree_cell * tree

References add_nasl_inc_dir(), naslctxt::always_signed, naslctxt::buffer, checksum_algorithm, naslctxt::exec_descr, file_checksum(), fullname_based_on_parent(), inc_dirs, include_order, naslctxt::include_order, naslctxt::kb, naslctxt::line_nb, load_checksums(), name, naslctxt::name, nasl_set_filename(), parse_buffer, parse_len, remove_base(), 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 * c)

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

2849{
2850 deref_cell(c->tree);
2851 g_free (c->buffer);
2852}
void deref_cell(tree_cell *c)
Definition nasl_tree.c:178

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: