Main of the standalone nasl interpreter.
139{
143 static gchar *target = NULL;
144 gchar *default_target = "127.0.0.1";
145 int mode = 0, err = 0, pos;
147 GSList *unresolved;
148
149 static gboolean display_version = FALSE;
150 static gboolean nasl_debug = FALSE;
151 static gboolean description_only = FALSE;
152 static gboolean both_modes = FALSE;
153 static gboolean parse_only = FALSE;
154 static gboolean do_lint = FALSE;
155 static gchar *trace_file = NULL;
156 static gchar *config_file = NULL;
157 static gchar *source_iface = NULL;
158 static gchar *port_range = NULL;
159 static gboolean with_safe_checks = FALSE;
160 static gboolean signing_mode = FALSE;
161 static gchar *include_dir = NULL;
162 static gchar **nasl_filenames = NULL;
163 static gchar **kb_values = NULL;
164 static int debug_tls = 0;
165 GError *error = NULL;
166 GOptionContext *option_context;
167 static GOptionEntry entries[] = {
168 {"version", 'V', 0, G_OPTION_ARG_NONE, &display_version,
169 "Display version information", NULL},
170 {"debug", 'd', 0, G_OPTION_ARG_NONE, &nasl_debug,
171 "Output debug information to stderr.", NULL},
172 {"description", 'D', 0, G_OPTION_ARG_NONE, &description_only,
173 "Only run the 'description' part of the script", NULL},
174 {"both", 'B', 0, G_OPTION_ARG_NONE, &both_modes,
175 "Run in description mode before running the script.", NULL},
176 {"parse", 'p', 0, G_OPTION_ARG_NONE, &parse_only,
177 "Only parse the script, don't execute it", NULL},
178 {"lint", 'L', 0, G_OPTION_ARG_NONE, &do_lint,
179 "'lint' the script (extended checks)", NULL},
180 {"target", 't', 0, G_OPTION_ARG_STRING, &target,
181 "Execute the scripts against <target>", "<target>"},
182 {"trace", 'T', 0, G_OPTION_ARG_FILENAME, &trace_file,
183 "Log actions to <file> (or '-' for stderr)", "<file>"},
184 {"config-file", 'c', 0, G_OPTION_ARG_FILENAME, &config_file,
185 "Configuration file", "<filename>"},
186 {"source-iface", 'e', 0, G_OPTION_ARG_STRING, &source_iface,
187 "Source network interface for established connections.", "<iface_name>"},
188 {"safe", 's', 0, G_OPTION_ARG_NONE, &with_safe_checks,
189 "Specifies that the script should be run with 'safe checks' enabled",
190 NULL},
191 {"disable-signing", 'X', 0, G_OPTION_ARG_NONE, &signing_mode,
192 "Run the script with disabled signature verification", NULL},
193 {"include-dir", 'i', 0, G_OPTION_ARG_STRING, &include_dir,
194 "Search for includes in <dir>", "<dir>"},
195 {"debug-tls", 0, 0, G_OPTION_ARG_INT, &debug_tls,
196 "Enable TLS debugging at <level>", "<level>"},
197 {"kb", 'k', 0, G_OPTION_ARG_STRING_ARRAY, &kb_values,
198 "Set KB key to value. Can be used multiple times", "<key=value>"},
199 {"port-range", 'r', 0, G_OPTION_ARG_STRING, &port_range,
200 "Set the <port-range> used by nasl scripts. ", "<port-range>"},
201 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &nasl_filenames,
202 "Absolute path to one or more nasl scripts", "NASL_FILE..."},
203 {NULL, 0, 0, 0, NULL, NULL, NULL}};
204
205 option_context =
206 g_option_context_new ("- standalone NASL interpreter for OpenVAS");
207 g_option_context_add_main_entries (option_context, entries, NULL);
208 if (!g_option_context_parse (option_context, &argc, &argv, &error))
209 {
210 g_print ("%s\n\n", error->message);
211 exit (0);
212 }
213 g_option_context_free (option_context);
214
215
216
217
218 if (display_version)
219 {
221 if (debug_tls)
222 {
223 printf ("gnutls %s\n", gnutls_check_version (NULL));
224 printf ("libssh %s\n", ssh_version (0));
225 printf ("gpgme %s\n", gpgme_check_version (NULL));
226 }
227 else
228 putchar ('\n');
229 printf ("Copyright (C) 2002 - 2004 Tenable Network Security\n");
230 printf ("Copyright (C) 2024 Greenbone AG\n\n");
231 exit (0);
232 }
233 if (nasl_debug)
236 if (signing_mode)
238 if (description_only)
240 if (do_lint)
242 if (parse_only)
244 if (trace_file)
245 {
246 if (!strcmp (trace_file, "-"))
248 else
249 {
250 FILE *fp = fopen (trace_file, "w");
251 if (fp == NULL)
252 {
253 perror (optarg);
254 exit (2);
255 }
256 setvbuf (fp, NULL, _IOLBF, BUFSIZ);
258 }
259 }
260
263 if (!nasl_filenames)
264 {
265 fprintf (stderr, "Error. No input file(s) specified !\n");
266 exit (1);
267 }
268
270 {
271 fprintf (stderr, "** WARNING : packet forgery will not work\n");
272 fprintf (stderr, "** as NASL is not running as root\n");
273 }
274 signal (SIGPIPE, SIG_IGN);
275
276 if (source_iface && gvm_source_iface_init (source_iface))
277 {
278 fprintf (stderr, "Erroneous network source interface: %s\n",
279 source_iface);
280 exit (1);
281 }
282 if (debug_tls)
283 {
285 gnutls_global_set_log_level (debug_tls);
286 }
287
288 if (!target)
289 target = g_strdup (default_target);
290
291 hosts = gvm_hosts_new (target);
293 {
294 fprintf (stderr, "Erroneous target %s\n", target);
295 exit (1);
296 }
297 unresolved = gvm_hosts_resolve (
hosts);
298 while (unresolved)
299 {
300 g_warning ("Couldn't resolve hostname '%s'", (char *) unresolved->data);
301 unresolved = unresolved->next;
302 }
303 g_slist_free_full (unresolved, g_free);
304 g_free (target);
305
306
308 if (include_dir != NULL)
309 {
311 }
312
313 prefs_config (config_file ? config_file : OPENVAS_CONF);
314
315 if (prefs_get ("vendor_version") != NULL)
317
318 if (port_range != NULL)
319 {
320 prefs_set ("port_range", port_range);
321 g_free (port_range);
322 }
323
324 if (with_safe_checks)
325 prefs_set ("safe_checks", "yes");
326
327 pos = 0;
329 {
330 struct in6_addr ip6;
331 kb_t kb;
332 int rc;
333 int process_id;
334
335 if (prefs_get_bool ("expand_vhosts"))
336 gvm_host_add_reverse_lookup (
host);
337 gvm_vhosts_exclude (
host, prefs_get (
"exclude_hosts"));
338 gvm_host_get_addr6 (
host, &ip6);
339 rc = kb_new (&kb, prefs_get ("db_address") ? prefs_get ("db_address")
340 : KB_PATH_DEFAULT);
341 if (rc)
342 exit (1);
343
345 process_id = getpid ();
346
348 for (int i = 0; nasl_filenames[i] != NULL; i++)
349 {
351 if (both_modes || with_safe_checks)
352 {
354 if (!nvti)
355 {
356 err++;
357 continue;
358 }
359 else if (with_safe_checks
361 {
362 printf ("%s isn't safe\n", nasl_filenames[i]);
363 nvti_free (nvti);
364 err++;
365 continue;
366 }
367 nvti_free (nvti);
368 }
369 if (kb_values)
370 {
371 gchar **kb_values_aux = kb_values;
372 while (*kb_values_aux)
373 {
374 gchar **splits = g_strsplit (*kb_values_aux, "=", -1);
375 if (splits[2] || !splits[1])
376 {
377 fprintf (stderr, "Erroneous --kb entry %s\n",
378 *kb_values_aux);
379 exit (1);
380 }
381 kb_item_add_str_unique (kb, splits[0], splits[1], 0, pos);
382 kb_values_aux++;
383 g_strfreev (splits);
384 }
385 }
386
388 err++;
389
390 if (process_id != getpid ())
391 exit (0);
392 }
395
396 kb_delete (kb);
397 }
398
401
402 gvm_hosts_free (
hosts);
403 return err;
404}
static int nvti_category_is_safe(int category)
Checks that an NVT category is safe.
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
static struct host * hosts
void set_main_kb(kb_t kb)
sets the shared database between ospd and openvas as a main_kb for further usage. @description this s...
char * nasl_version(void)
static nvti_t * parse_script_infos(struct script_infos *infos)
static struct script_infos * init(struct in6_addr *ip, GSList *vhosts, kb_t kb)
static void gcrypt_init()
Initialize Gcrypt.
#define NASL_ALWAYS_SIGNED
#define NASL_EXEC_PARSE_ONLY
#define NASL_COMMAND_LINE
int add_nasl_inc_dir(const char *)
Adds the given string as directory for searching for includes.
int openvas_SSL_init()
Initializes SSL support.
static void my_gnutls_log_func(int level, const char *text)
Host information, implemented as doubly linked list.
void vendor_version_set(const gchar *version)
Set vendor version.