OpenVAS Scanner  7.0.1~git
nasl-lint.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #include "nasl.h" /* exec_nasl_script */
26 
27 #include <gio/gio.h> /* g_file_... */
28 #include <glib.h> /* gchar, g_malloc, g_error, g_print, ... */
29 #include <stdio.h> /* for printf */
30 #include <stdlib.h> /* for exit */
31 
37 static GDataInputStream *
38 get_DIS_from_filename (const gchar *filename)
39 {
40  GFile *file = NULL;
41  GFileInputStream *fis = NULL;
42  GDataInputStream *dis = NULL;
43  GError *error = NULL;
44 
45  file = g_file_new_for_path (filename);
46  fis = g_file_read (file, NULL, &error);
47  if (error != NULL)
48  {
49  if (fis != NULL)
50  g_object_unref (fis);
51 
52  g_error ("%s\n\n", error->message);
53  }
54  dis = g_data_input_stream_new (G_INPUT_STREAM (fis));
55  g_object_unref (fis);
56  return dis;
57 }
58 
65 static gboolean
66 process_file (const gchar *filepath, int mode, struct script_infos *script_args)
67 {
68  g_debug ("Processing %s", filepath);
69  script_args->name = (char *) filepath;
70  if (exec_nasl_script (script_args, mode) < 0)
71  {
72  g_print ("Error while processing %s.\n", filepath);
73  return TRUE;
74  }
75  return FALSE;
76 }
77 
85 static int
86 process_file_list (const gchar *list_file, int mode,
87  struct script_infos *script_args)
88 {
89  int err = 0;
90  GError *error = NULL;
91  GDataInputStream *nvt_list = get_DIS_from_filename (list_file);
92 
93  while (TRUE)
94  {
95  gchar *line =
96  g_data_input_stream_read_line (nvt_list, NULL, NULL, &error);
97  if (error != NULL)
98  {
99  if (line != NULL)
100  g_free (line);
101 
102  g_error ("%s\n\n", error->message);
103  break;
104  }
105  if (line == NULL)
106  break;
107 
108  if (process_file (line, mode, script_args))
109  err++;
110 
111  g_free (line);
112  }
113  g_object_unref (nvt_list);
114 
115  return err;
116 }
117 
124 static int
125 process_files (const gchar **files, int mode, struct script_infos *script_args)
126 {
127  int n = 0;
128  int err = 0;
129  while (files[n])
130  {
131  if (process_file (files[n], mode, script_args))
132  err++;
133  n++;
134  }
135  return err;
136 }
137 
144 static void
145 custom_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
146  const gchar *message, gpointer user_data)
147 {
148  gint log_mask = GPOINTER_TO_INT (user_data);
149  if ((log_level & log_mask) != 0)
150  g_log_default_handler (log_domain, log_level, message, user_data);
151 }
152 
157 int
158 main (int argc, char **argv)
159 {
160  int mode = 0;
161  int err = 0;
162  static gboolean debug = FALSE;
163  static gchar *include_dir = NULL;
164  static gchar *nvt_file_list = NULL;
165  static const gchar **nvt_files = NULL;
166  struct script_infos *script_infos = g_malloc0 (sizeof (struct script_infos));
167  GError *error = NULL;
168  GOptionContext *option_context;
169  static GOptionEntry entries[] = {
170  {"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Output debug log messages.",
171  NULL},
172  {"nvt-list", 'l', 0, G_OPTION_ARG_STRING, &nvt_file_list,
173  "Process files from <file>", "<file>"},
174  {"include-dir", 'i', 0, G_OPTION_ARG_STRING, &include_dir,
175  "Search for includes in <dir>", "<dir>"},
176  {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &nvt_files,
177  "Absolute path to one or more nasl scripts", "NASL_FILE..."},
178  {NULL, 0, 0, 0, NULL, NULL, NULL}};
179 
180  option_context =
181  g_option_context_new ("- standalone NASL linter for OpenVAS");
182  g_option_context_add_main_entries (option_context, entries, NULL);
183  if (!g_option_context_parse (option_context, &argc, &argv, &error))
184  {
185  g_error ("%s\n\n", error->message);
186  }
187  g_option_context_free (option_context);
188 
189  mode |= NASL_COMMAND_LINE;
190  /* signing mode */
191  mode |= NASL_ALWAYS_SIGNED;
192  /* linter on */
193  mode |= NASL_LINT;
194 
195  /* For relative include */
196  add_nasl_inc_dir ("");
197  /* For absolute include (if given on command line) */
198  if (include_dir != NULL)
199  add_nasl_inc_dir (include_dir);
200 
201  if (debug)
202  g_log_set_handler (
203  NULL, G_LOG_LEVEL_MASK, custom_log_handler,
204  GINT_TO_POINTER (G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO
205  | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_WARNING
206  | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR));
207  else
208  g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler,
209  GINT_TO_POINTER (G_LOG_LEVEL_WARNING
210  | G_LOG_LEVEL_CRITICAL
211  | G_LOG_LEVEL_ERROR));
212 
213  /* Process the files from the list */
214  if (nvt_file_list != NULL)
215  err += process_file_list (nvt_file_list, mode, script_infos);
216 
217  /* process the files from the command line */
218  if (nvt_files != NULL)
219  err += process_files (nvt_files, mode, script_infos);
220 
221  g_print ("%d errors found\n", err);
222  return err;
223 }
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:145
script_infos
Definition: scanneraux.h:43
get_DIS_from_filename
static GDataInputStream * get_DIS_from_filename(const gchar *filename)
Returns a GDataInputStream* for a given filepath.
Definition: nasl-lint.c:38
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:2738
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:86
script_infos::name
char * name
Definition: scanneraux.h:49
nasl.h
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:125
NASL_COMMAND_LINE
#define NASL_COMMAND_LINE
Definition: nasl.h:60
main
int main(int argc, char **argv)
Main of the nasl QA linter.
Definition: nasl-lint.c:158
process_file
static gboolean process_file(const gchar *filepath, int mode, struct script_infos *script_args)
Process a file through the linter.
Definition: nasl-lint.c:66
NASL_ALWAYS_SIGNED
#define NASL_ALWAYS_SIGNED
Definition: nasl.h:59
exec_nasl_script
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
Definition: exec.c:1624
NASL_LINT
#define NASL_LINT
Definition: nasl.h:61