OpenVAS Scanner 22.7.9
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 2696 of file nasl_grammar.tab.c.

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

References add_nasl_inc_dir(), naslctxt::always_signed, naslctxt::buffer, checksum_algorithm, naslctxt::exec_descr, file_checksum(), inc_dirs, naslctxt::include_order, include_order, naslctxt::kb, naslctxt::line_nb, load_checksums(), naslctxt::name, 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 2808 of file nasl_grammar.tab.c.

2809{
2810 deref_cell(c->tree);
2811 g_free (c->buffer);
2812}
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:181

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: