OpenVAS Scanner 23.32.3
nasl-lint.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "lint.h"
12#include "nasl.h" /* exec_nasl_script */
13
14#include <gio/gio.h> /* g_file_... */
15#include <glib.h> /* gchar, g_malloc, g_error, g_print, ... */
16#include <stdio.h> /* for printf */
17#include <stdlib.h> /* for exit */
18
24static GDataInputStream *
25get_DIS_from_filename (const gchar *filename)
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}
45
52static int
53process_file (const gchar *filepath, int mode, struct script_infos *script_args)
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}
71
79static int
80process_file_list (const gchar *list_file, int mode,
81 struct script_infos *script_args)
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}
110
117static int
118process_files (const gchar **files, int mode, struct script_infos *script_args)
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}
129
136static void
137custom_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
138 const gchar *message, gpointer user_data)
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}
144
149int
150main (int argc, char **argv)
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;
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}
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
Definition exec.c:1614
nasl_lint_feature_flags
Definition lint.h:14
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
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
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
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
int main(int argc, char **argv)
Main of the nasl QA linter.
Definition nasl-lint.c:150
static GDataInputStream * get_DIS_from_filename(const gchar *filename)
Returns a GDataInputStream* for a given filepath.
Definition nasl-lint.c:25
#define NASL_ALWAYS_SIGNED
Definition nasl.h:47
#define NASL_COMMAND_LINE
Definition nasl.h:48
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
#define NASL_LINT
Definition nasl.h:49
char * name
Definition scanneraux.h:35