OpenVAS Scanner  22.7.9
nasl-lint.c File Reference

Source of the NASL linter of OpenVAS. More...

#include "lint.h"
#include "nasl.h"
#include <gio/gio.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for nasl-lint.c:

Go to the source code of this file.

Functions

static GDataInputStream * get_DIS_from_filename (const gchar *filename)
 Returns a GDataInputStream* for a given filepath. More...
 
static int process_file (const gchar *filepath, int mode, struct script_infos *script_args)
 Process a file through the linter. More...
 
static int process_file_list (const gchar *list_file, int mode, struct script_infos *script_args)
 Process each files in the list_file through the linter. More...
 
static int process_files (const gchar **files, int mode, struct script_infos *script_args)
 Process each given files through the linter. More...
 
static void custom_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
 custom log handler More...
 
int main (int argc, char **argv)
 Main of the nasl QA linter. More...
 

Detailed Description

Source of the NASL linter of OpenVAS.

Definition in file nasl-lint.c.

Function Documentation

◆ custom_log_handler()

static void custom_log_handler ( const gchar *  log_domain,
GLogLevelFlags  log_level,
const gchar *  message,
gpointer  user_data 
)
static

custom log handler

This handler absorb each log_level not present in the log_mask, and forward the other ones to the default handler.

Definition at line 137 of file nasl-lint.c.

139 {
140  gint log_mask = GPOINTER_TO_INT (user_data);
141  if ((log_level & log_mask) != 0)
142  g_log_default_handler (log_domain, log_level, message, user_data);
143 }

Referenced by main().

Here is the caller graph for this function:

◆ get_DIS_from_filename()

static GDataInputStream* get_DIS_from_filename ( const gchar *  filename)
static

Returns a GDataInputStream* for a given filepath.

Parameters
filenamethe path to the file to open
Returns
a GDataInputStream corresponding to the filepath

Definition at line 25 of file nasl-lint.c.

26 {
27  GFile *file = NULL;
28  GFileInputStream *fis = NULL;
29  GDataInputStream *dis = NULL;
30  GError *error = NULL;
31 
32  file = g_file_new_for_path (filename);
33  fis = g_file_read (file, NULL, &error);
34  if (error != NULL)
35  {
36  if (fis != NULL)
37  g_object_unref (fis);
38 
39  g_error ("%s\n\n", error->message);
40  }
41  dis = g_data_input_stream_new (G_INPUT_STREAM (fis));
42  g_object_unref (fis);
43  return dis;
44 }

Referenced by process_file_list().

Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Main of the nasl QA linter.

Returns
0 on success

Definition at line 150 of file nasl-lint.c.

151 {
152  int mode = 0;
153  int err = 0;
154  int fflag = 0;
155  static gboolean debug = FALSE;
156  static gchar *include_dir = NULL;
157  static gchar *nvt_file_list = NULL;
158  static unsigned char strict_includes = 0;
159  static const gchar **nvt_files = NULL;
160  struct script_infos *script_infos = g_malloc0 (sizeof (struct script_infos));
161  GError *error = NULL;
162  GOptionContext *option_context;
163 
164  static GOptionEntry entries[] = {
165  {"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Output debug log messages.",
166  NULL},
167  {"nvt-list", 'l', 0, G_OPTION_ARG_STRING, &nvt_file_list,
168  "Process files from <file>", "<file>"},
169  {"include-dir", 'i', 0, G_OPTION_ARG_STRING, &include_dir,
170  "Search for includes in <dir>", "<dir>"},
171  {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &nvt_files,
172  "Absolute path to one or more nasl scripts", "NASL_FILE..."},
173  {"strict-includes", 0, 0, G_OPTION_ARG_NONE, &strict_includes,
174  "Enables check for strict include order.", NULL},
175  {NULL, 0, 0, 0, NULL, NULL, NULL}};
176 
177  option_context =
178  g_option_context_new ("- standalone NASL linter for OpenVAS");
179  g_option_context_add_main_entries (option_context, entries, NULL);
180  if (!g_option_context_parse (option_context, &argc, &argv, &error))
181  {
182  g_error ("%s\n\n", error->message);
183  }
184  g_option_context_free (option_context);
185  fflag |= strict_includes;
186  nasl_lint_feature_flags (fflag);
187 
188  mode |= NASL_COMMAND_LINE;
189  /* signing mode */
190  mode |= NASL_ALWAYS_SIGNED;
191  /* linter on */
192  mode |= NASL_LINT;
193 
194  /* For relative include */
195  add_nasl_inc_dir ("");
196  /* For absolute include (if given on command line) */
197  if (include_dir != NULL)
198  add_nasl_inc_dir (include_dir);
199 
200  if (debug)
201  g_log_set_handler (
202  NULL, G_LOG_LEVEL_MASK, custom_log_handler,
203  GINT_TO_POINTER (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO
204  | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING
205  | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR));
206  else
207  g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler,
208  GINT_TO_POINTER (G_LOG_LEVEL_WARNING
209  | G_LOG_LEVEL_CRITICAL
210  | G_LOG_LEVEL_ERROR));
211 
212  /* Process the files from the list */
213  if (nvt_file_list != NULL)
214  err += process_file_list (nvt_file_list, mode, script_infos);
215 
216  /* process the files from the command line */
217  if (nvt_files != NULL)
218  err += process_files (nvt_files, mode, script_infos);
219 
220  g_print ("%d scripts with one or more errors found\n", err);
221 
222  g_free (script_infos);
223 
224  return err;
225 }

References add_nasl_inc_dir(), custom_log_handler(), NASL_ALWAYS_SIGNED, NASL_COMMAND_LINE, NASL_LINT, process_file_list(), and process_files().

Here is the call graph for this function:

◆ process_file()

static int process_file ( const gchar *  filepath,
int  mode,
struct script_infos script_args 
)
static

Process a file through the linter.

Parameters
filepaththe path of the file to be processed
mode,script_argsThe parameters to be given to the linter
Returns
0 if no error was found, 1 if errors were found.

Definition at line 53 of file nasl-lint.c.

54 {
55  int ret;
56 
57  g_debug ("Processing %s", filepath);
58  script_args->name = (char *) filepath;
59  ret = exec_nasl_script (script_args, mode);
60  if (ret != 0)
61  {
62  // Although there is a technical difference between negative and
63  // positive ret value we do not make a distinction in this error message
64  // because details are already provided in exec_nasl_script()
65  g_print ("%d errors while processing %s.\n", ret == -1 ? 1 : ret,
66  filepath);
67  return 1;
68  }
69  return 0;
70 }

References exec_nasl_script(), and script_infos::name.

Referenced by process_file_list(), and process_files().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_file_list()

static int process_file_list ( const gchar *  list_file,
int  mode,
struct script_infos script_args 
)
static

Process each files in the list_file through the linter.

Parameters
list_filethe path to a text file containing path to the files to process, one per line
mode,script_argsParameters for the linter
Returns
The amount of scripts with errors

Definition at line 80 of file nasl-lint.c.

82 {
83  int err = 0;
84  GError *error = NULL;
85  GDataInputStream *nvt_list = get_DIS_from_filename (list_file);
86 
87  while (TRUE)
88  {
89  gchar *line =
90  g_data_input_stream_read_line (nvt_list, NULL, NULL, &error);
91  if (error != NULL)
92  {
93  if (line != NULL)
94  g_free (line);
95 
96  g_error ("%s\n\n", error->message);
97  break;
98  }
99  if (line == NULL)
100  break;
101 
102  err += process_file (line, mode, script_args);
103 
104  g_free (line);
105  }
106  g_object_unref (nvt_list);
107 
108  return err;
109 }

References get_DIS_from_filename(), and process_file().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_files()

static int process_files ( const gchar **  files,
int  mode,
struct script_infos script_args 
)
static

Process each given files through the linter.

Parameters
filesThe path to the files to be processed
mode,script_argsParameters to be given to the linter
Returns
The amount scripts with errors

Definition at line 118 of file nasl-lint.c.

119 {
120  int n = 0;
121  int err = 0;
122  while (files[n])
123  {
124  err += process_file (files[n], mode, script_args);
125  n++;
126  }
127  return err;
128 }

References process_file().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:
custom_log_handler
static void custom_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
custom log handler
Definition: nasl-lint.c:137
script_infos
Definition: scanneraux.h:29
get_DIS_from_filename
static GDataInputStream * get_DIS_from_filename(const gchar *filename)
Returns a GDataInputStream* for a given filepath.
Definition: nasl-lint.c:25
add_nasl_inc_dir
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
Definition: nasl_grammar.tab.c:2539
process_file_list
static int process_file_list(const gchar *list_file, int mode, struct script_infos *script_args)
Process each files in the list_file through the linter.
Definition: nasl-lint.c:80
script_infos::name
char * name
Definition: scanneraux.h:35
process_files
static int process_files(const gchar **files, int mode, struct script_infos *script_args)
Process each given files through the linter.
Definition: nasl-lint.c:118
NASL_COMMAND_LINE
#define NASL_COMMAND_LINE
Definition: nasl.h:48
NASL_ALWAYS_SIGNED
#define NASL_ALWAYS_SIGNED
Definition: nasl.h:47
exec_nasl_script
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
Definition: exec.c:1614
nasl_lint_feature_flags
nasl_lint_feature_flags
Definition: lint.h:14
NASL_LINT
#define NASL_LINT
Definition: nasl.h:49
process_file
static int process_file(const gchar *filepath, int mode, struct script_infos *script_args)
Process a file through the linter.
Definition: nasl-lint.c:53