OpenVAS Scanner 23.32.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 *)
 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 * 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 2681 of file nasl_grammar.tab.c.

2682{
2683 char *full_name = NULL, key_path[2048], *checksum, *filename;
2684 GSList * inc_dir = inc_dirs; // iterator for include directories
2685 size_t flen = 0;
2686 time_t timestamp;
2687
2688 // initialize if not yet done (for openvas-server < 2.0.1)
2689 if (! inc_dirs) add_nasl_inc_dir("");
2690
2691 pc->line_nb = 1;
2692 pc->name = (char *) name;
2694 pc->tree = NULL;
2695 if (!parse_len)
2696 {
2697 parse_len = 9092;
2698 parse_buffer = g_malloc0 (parse_len);
2699 }
2700 else
2701 parse_buffer[0] = '\0';
2702
2703
2705 while (inc_dir != NULL) {
2706 if (full_name)
2707 g_free (full_name);
2708 full_name = g_build_filename(inc_dir->data, name, NULL);
2709
2710 if ((g_file_get_contents (full_name, &pc->buffer, &flen, NULL)))
2711 break;
2712
2713 inc_dir = g_slist_next(inc_dir);
2714 }
2715
2716 if (!full_name || !pc->buffer) {
2717 g_message ("%s: Not able to open nor to locate it in include paths",
2718 name);
2719 g_free(full_name);
2720 return -1;
2721 }
2722
2723 if (pc->always_signed)
2724 {
2725 g_free(full_name);
2726 return 0;
2727 }
2728 /* Cache the checksum of signature verified files, so that commonly included
2729 * files are not verified multiple times per scan. */
2730 if (strstr (full_name, ".inc"))
2731 filename = basename (full_name);
2732 else
2733 filename = full_name;
2734 snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2735 timestamp = kb_item_get_int (pc->kb, key_path);
2736
2737 /* We never use the mtime of a .nasl/.inc file as integrity check during
2738 * the script load up. A complete verification is done in this case.
2739 * Once it has been uploaded in the nvticache it is enough to just check
2740 * the mtime. */
2741 if (timestamp > 0 && pc->exec_descr == 0)
2742 {
2743 struct stat file_stat;
2744
2745 if (stat (full_name, &file_stat) >= 0 && timestamp > file_stat.st_mtime)
2746 {
2747 /* Already checked. No need to check again. */
2748 g_free (full_name);
2749 return 0;
2750 }
2751 }
2752
2753 load_checksums (pc->kb);
2754 if (checksum_algorithm == GCRY_MD_NONE)
2755 return -1;
2756 else if (checksum_algorithm == GCRY_MD_SHA256)
2757 snprintf (key_path, sizeof (key_path), "sha256sums:%s", filename);
2758 else
2759 abort ();
2760 checksum = kb_item_get_str (pc->kb, key_path);
2761 if (!checksum)
2762 {
2763 g_warning ("No checksum for %s", full_name);
2764 g_free (full_name);
2765 return -1;
2766 }
2767 else
2768 {
2769 int ret;
2770 char *check = file_checksum (full_name, checksum_algorithm);
2771
2772 snprintf (key_path, sizeof (key_path), "signaturecheck:%s", filename);
2773 ret = strcmp (check, checksum);
2774 if (ret)
2775 {
2776 kb_del_items (pc->kb, key_path);
2777 g_warning ("checksum for %s not matching", full_name);
2778 }
2779 else
2780 {
2781 kb_del_items (pc->kb, key_path);
2782 kb_item_add_int (pc->kb, key_path, time (NULL));
2783 }
2784
2785 g_free (full_name);
2786 g_free (checksum);
2787 g_free (check);
2788 return ret;
2789 }
2790}
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 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:439
unsigned int include_order
tree_cell * tree

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

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

2794{
2795 deref_cell(c->tree);
2796 g_free (c->buffer);
2797}
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: