OpenVAS Libraries  9.0.3
nasl-lint.c
Go to the documentation of this file.
1 /* Nessus Attack Scripting Language Linter
2  *
3  * Copyright (C) 2013 Greenbone Networks GmbH
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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  */
20 
26 #include <stdio.h> /* for printf */
27 #include <stdlib.h> /* for exit */
28 
29 #include "nasl.h" /* exec_nasl_script, arglist */
30 
31 #include <glib.h> /* gchar, g_malloc, g_error, g_print, ... */
32 
33 #include <gio/gio.h> /* g_file_... */
34 
40 static GDataInputStream*
41 get_DIS_from_filename (const gchar* filename)
42 {
43  GFile* file = NULL;
44  GFileInputStream* fis = NULL;
45  GDataInputStream* dis = NULL;
46  GError* error = NULL;
47 
48  file = g_file_new_for_path (filename);
49  fis = g_file_read (file, NULL, &error);
50  if (error != NULL) {
51  if (fis != NULL)
52  g_object_unref (fis);
53 
54  g_error ("%s\n\n", error->message);
55  }
56  dis = g_data_input_stream_new (G_INPUT_STREAM(fis));
57  g_object_unref (fis);
58  return dis;
59 }
60 
67 static gboolean
68 process_file (const gchar* filepath, int mode, struct arglist* script_args)
69 {
70  g_debug("Processing %s", filepath);
71  if (exec_nasl_script (script_args, filepath, NULL, mode) < 0)
72  {
73  g_print ("Error while processing %s.\n", filepath);
74  return TRUE;
75  }
76  return FALSE;
77 }
78 
86 static int
87 process_file_list (const gchar* list_file, int mode,
88  struct arglist* script_args)
89 {
90  int err = 0;
91  GError* error = NULL;
92  GDataInputStream* nvt_list = get_DIS_from_filename(list_file);
93 
94  while(TRUE)
95  {
96  gchar* line = g_data_input_stream_read_line (nvt_list, NULL, NULL,
97  &error);
98  if (error != NULL)
99  {
100  if (line != NULL)
101  g_free (line);
102 
103  g_error ("%s\n\n", error->message);
104  break;
105  }
106  if (line == NULL)
107  break;
108 
109  if (process_file(line, mode, script_args))
110  err++;
111 
112  g_free (line);
113  }
114  g_object_unref (nvt_list);
115 
116  return err;
117 }
118 
125 static int
126 process_files(const gchar** files, int mode, struct arglist* script_args)
127 {
128  int n = 0;
129  int err = 0;
130  while (files[n])
131  {
132  if (process_file(files[n], mode, script_args))
133  err++;
134  n++;
135  }
136  return err;
137 }
138 
145 static void
146 custom_log_handler(const gchar *log_domain,
147  GLogLevelFlags log_level,
148  const gchar *message,
149  gpointer user_data )
150 {
151  gint log_mask = GPOINTER_TO_INT (user_data);
152  if ((log_level & log_mask) != 0)
153  g_log_default_handler(log_domain, log_level, message, user_data);
154 }
155 
160 int
161 main (int argc, char **argv)
162 {
163  int mode = 0;
164  int err = 0;
165  static gboolean debug = FALSE;
166  static gchar *include_dir = NULL;
167  static gchar *nvt_file_list = NULL;
168  static const gchar **nvt_files = NULL;
169  struct arglist *script_infos = g_malloc0 (sizeof (struct arglist));
170  GError *error = NULL;
171  GOptionContext *option_context;
172  static GOptionEntry entries[] = {
173  {"debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
174  "Output debug log messages.", NULL},
175  {"nvt-list", 'l', 0, G_OPTION_ARG_STRING, &nvt_file_list,
176  "Process files from <file>", "<file>"},
177  {"include-dir", 'i', 0, G_OPTION_ARG_STRING, &include_dir,
178  "Search for includes in <dir>", "<dir>"},
179  {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &nvt_files,
180  "Absolute path to one or more nasl scripts", "NASL_FILE..."},
181  {NULL}
182  };
183 
184  option_context =
185  g_option_context_new ("- standalone NASL linter for OpenVAS");
186  g_option_context_add_main_entries (option_context, entries, NULL);
187  if (!g_option_context_parse (option_context, &argc, &argv, &error))
188  {
189  g_error ("%s\n\n", error->message);
190  }
191  g_option_context_free (option_context);
192 
193 #if !GLIB_CHECK_VERSION(2, 35, 0)
194  g_type_init();
195 #endif
196 
197  mode |= NASL_COMMAND_LINE;
198  /* authenticated mode */
199  mode |= NASL_ALWAYS_SIGNED;
200  /* linter on */
201  mode |= NASL_LINT;
202 
203  /* For relative include */
204  add_nasl_inc_dir ("");
205  /* For absolute include (if given on command line) */
206  if (include_dir != NULL)
207  add_nasl_inc_dir (include_dir);
208 
209  if (debug)
210  g_log_set_handler (NULL,
211  G_LOG_LEVEL_MASK,
212  custom_log_handler,
213  GINT_TO_POINTER (G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_INFO|
214  G_LOG_LEVEL_MESSAGE|
215  G_LOG_LEVEL_WARNING|
216  G_LOG_LEVEL_CRITICAL|
217  G_LOG_LEVEL_ERROR));
218  else
219  g_log_set_handler (NULL,
220  G_LOG_LEVEL_MASK,
221  custom_log_handler,
222  GINT_TO_POINTER (G_LOG_LEVEL_WARNING|
223  G_LOG_LEVEL_CRITICAL|
224  G_LOG_LEVEL_ERROR));
225 
226  /* Process the files from the list */
227  if (nvt_file_list != NULL)
228  err += process_file_list(nvt_file_list, mode, script_infos);
229 
230  /* process the files from the command line */
231  if (nvt_files != NULL)
232  err += process_files(nvt_files, mode, script_infos);
233 
234  g_print ("%d errors found\n", err);
235  return err;
236 }
#define err(x)
int exec_nasl_script(struct arglist *script_infos, const char *name, const char *oid, int mode)
Execute a NASL script.
Definition: exec.c:1711
#define NASL_COMMAND_LINE
Definition: nasl.h:63
#define NASL_LINT
Definition: nasl.h:64
#define NASL_ALWAYS_SIGNED
Definition: nasl.h:62
int main(int argc, char **argv)
Main of the nasl QA linter.
Definition: nasl-lint.c:161
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.