24#include <glib/gstdio.h>
25#include <gvm/base/logging.h>
26#include <gvm/base/prefs.h>
27#include <gvm/util/nvticache.h>
38#define G_LOG_DOMAIN "lib nasl"
93 if (*p !=
'\0' && warn)
98 "Converting the non numeric string '%s' in variable "
99 "'%s' to integer does not make sense in this "
105 "Converting the non numeric string '%s' to "
106 "integer does not make sense in this context",
162 return g_strdup_printf (
"%ld", c->
x.
i_val);
170 p = g_malloc0 (c->
size + 1);
220 int flag, typ, typ1, typ2;
223 int len_s1, len_s2, len_min;
249 if (typ1 == 0 && typ2 == 0)
290 "cell_cmp: comparing '%s' of type %s and '%s' of "
291 "type %s does not make sense\n",
317 len_s1 = strlen (s1);
325 len_s2 = strlen (s2);
327 len_min = len_s1 < len_s2 ? len_s1 : len_s2;
331 flag = memcmp (s1, s2, len_min);
333 flag = len_s1 - len_s2;
343 g_message (
"cell_cmp: cannot compare arrays yet");
349 g_message (
"cell_cmp: don't known how to compare %s and %s",
365 fprintf (fp,
"NULL");
367 fprintf (fp,
"FAKE");
377 fprintf (fp,
") && (");
384 fprintf (fp,
") || (");
396 fprintf (fp,
") + (");
403 fprintf (fp,
") - (");
409 if (c->
link[0] == NULL)
421 if (c->
link[0] == NULL)
437 fprintf (fp,
") ** (");
443 fprintf (fp,
" - (");
451 fprintf (fp,
") * (");
458 fprintf (fp,
") / (");
465 fprintf (fp,
") %% (");
472 fprintf (fp,
") & (");
479 fprintf (fp,
") | (");
486 fprintf (fp,
") ^ (");
498 fprintf (fp,
") << (");
505 fprintf (fp,
") >> (");
512 fprintf (fp,
") >>> (");
518 fprintf (fp,
" >< ");
523 fprintf (fp,
" >!< ");
529 fprintf (fp,
" =~ ");
535 fprintf (fp,
" !~ ");
546 fprintf (fp,
" <= ");
556 fprintf (fp,
" >= ");
561 fprintf (fp,
" == ");
565 fprintf (fp,
"%ld", c->
x.
i_val);
569 fprintf (fp,
"\"%s\"", c->
x.
str_val);
579 fprintf (fp,
"%s(...)", c->
x.
str_val);
590 fprintf (fp,
"+= (");
597 fprintf (fp,
"-= (");
604 fprintf (fp,
"*= (");
611 fprintf (fp,
"/= (");
618 fprintf (fp,
"%%= (");
625 fprintf (fp,
" <<= (");
632 fprintf (fp,
" >>= (");
639 fprintf (fp,
" >>>= (");
645 fprintf (fp,
"*%d*", c->
type);
659 fprintf (fp,
"NASL:%04d> if (", c->
line_nb);
661 fprintf (fp,
") { ... }");
662 if (c->
link[2] != NULL)
663 fprintf (fp,
" else { ... }");
668 fprintf (fp,
"NASL:%04d> for (", c->
line_nb);
674 fprintf (fp,
") { ... }\n");
678 fprintf (fp,
"NASL:%04d> while (", c->
line_nb);
680 fprintf (fp,
") { ... }\n");
686 fprintf (fp,
") { ... }\n");
690 fprintf (fp,
"NASL:%04d> repeat { ... } until (", c->
line_nb);
696 fprintf (fp,
"NASL:%04d> ... x ", c->
line_nb);
702 fprintf (fp,
"NASL:%04d> return ", c->
line_nb);
708 fprintf (fp,
"NASL:%04d> break\n", c->
line_nb);
712 fprintf (fp,
"NASL:%04d> continue\n", c->
line_nb);
724 fprintf (fp,
"NASL:%04d> ", c->
line_nb);
734 fprintf (fp,
"NASL:%04d> local_var ...\n", c->
line_nb);
738 fprintf (fp,
"NASL:%04d> global_var ...\n", c->
line_nb);
772 tree_cell *ret = NULL, *ret2 = NULL, *tc1 = NULL, *tc2 = NULL, *tc3 = NULL,
775 char *s1 = NULL, *s2 = NULL, *s3 = NULL, *p = NULL;
780 int i, lint_mode = 0;
811#ifdef STOP_AT_FIRST_ERROR
817 else if (st->
link[2] != NULL)
835#ifdef STOP_AT_FIRST_ERROR
858#ifdef STOP_AT_FIRST_ERROR
875#ifdef STOP_AT_FIRST_ERROR
902#ifdef STOP_AT_FIRST_ERROR
930#ifdef STOP_AT_FIRST_ERROR
946#ifdef STOP_AT_FIRST_ERROR
974#ifdef STOP_AT_FIRST_ERROR
1020#ifdef STOP_AT_FIRST_ERROR
1021 for (tc1 = NULL, i = 1; i <= n; i++)
1029 for (i = 1; i <= n; i++)
1196 if (st->
link[0] == NULL)
1216#ifdef STOP_AT_FIRST_ERROR
1223#ifdef STOP_AT_FIRST_ERROR
1263 len1 = (s1 == NULL ? 0 : strlen (s1));
1271 len2 = (s2 == NULL ? 0 : strlen (s2));
1275 s3 = g_malloc0 (sz + 1);
1277 memcpy (s3, s1 != NULL ? s1 : tc1->x.str_val, len1);
1279 memcpy (s3 + len1, s2 != NULL ? s2 : tc2->x.str_val, len2);
1298#ifdef STOP_AT_FIRST_ERROR
1305#ifdef STOP_AT_FIRST_ERROR
1351 p1 = tc1->x.str_val;
1357 len1 = (s1 == NULL ? 0 : strlen (s1));
1362 p2 = tc2->x.str_val;
1368 len2 = (s2 == NULL ? 0 : strlen (s2));
1373 if (len2 == 0 || len1 < len2
1374 || (p1 != NULL && (p = memmem (p1, len1, p2, len2)) == NULL))
1376 s3 = g_malloc0 (len1 + 1);
1378 memcpy (s3, p1, len1);
1385 long sz = len1 - len2;
1393 s3 = g_malloc0 (sz + 1);
1395 memcpy (s3, p1, p - p1);
1396 if (p != NULL && sz > p - p1)
1397 memcpy (s3 + (p - p1), p + len2, sz - (p - p1));
1476 if (x < 0 && z >= 0)
1477 z |= (~0) << (
sizeof (x) * 8 - y);
1484 z = (unsigned) x >> (
unsigned) y;
1486 if (x < 0 && z <= 0)
1487 z &= ~((~0) << (
sizeof (x) * 8 - y));
1504 p1 = tc1->x.str_val;
1520 p2 = tc2->x.str_val;
1530 flag = (memmem (p2, len2, p1, len1) != NULL);
1547 nasl_perror (lexic,
"nasl_exec: bad regex at or near line %d\n",
1554 flag = regexec (st->
x.
ref_val, s1, 0, NULL, 0);
1625 gchar *short_name = g_path_get_basename (
name);
1626 int error_counter = 0;
1629 g_free (short_name);
1631 srand48 (getpid () + getppid () + (
long) time (NULL));
1633 old_dir = g_get_current_dir ();
1635 newdir = g_path_get_dirname (
name);
1637 if (g_chdir (newdir) != 0)
1639 g_message (
"%s: Not able to change working directory to %s (%d [%s]).",
1640 __func__, newdir, errno, strerror (errno));
1647 bzero (&ctx,
sizeof (ctx));
1652 if (nvticache_initialized ())
1653 ctx.
kb = nvticache_get_kb ();
1660 if (err != 0 || error_counter > 0)
1662 g_message (
"%s. There were %d parse errors.",
name, error_counter);
1681 str = prefs_get (
"checks_read_timeout");
1699 if (lintret == NULL)
1711 bzero (&tc,
sizeof (tc));
1716 bzero (&tc,
sizeof (tc));
1722 p = strrchr (
name,
'/');
1728 tc.
size = strlen (p);
1741 if (g_chdir (old_dir) != 0)
long int cell_cmp(lex_ctxt *lexic, tree_cell *c1, tree_cell *c2)
static long int cell2intW(lex_ctxt *lexic, tree_cell *c)
static void nasl_short_dump(FILE *fp, const tree_cell *c)
static long int expo(long int x, long int y)
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
static void nasl_dump_expr(FILE *fp, const tree_cell *c)
static tree_cell * bool2cell(long int x)
int naslparse(naslctxt *, int *)
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
static int cell2bool(lex_ctxt *lexic, tree_cell *c)
static tree_cell * int2cell(long int x)
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
static long int cell2int3(lex_ctxt *lexic, tree_cell *c, int warn, named_nasl_var *v)
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
tree_cell * nasl_lint(lex_ctxt *lexic, tree_cell *st)
Search for errors in a nasl script.
#define NASL_ALWAYS_SIGNED
#define NASL_EXEC_PARSE_ONLY
#define NASL_COMMAND_LINE
void nasl_set_filename(const char *filename)
void nasl_set_plugin_filename(const char *filename)
Set the current launched plugin filename.
void nasl_perror(lex_ctxt *lexic, char *msg,...)
tree_cell * nasl_func_call(lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
tree_cell * nasl_return(lex_ctxt *ctxt, tree_cell *retv)
struct st_nasl_func nasl_func
int init_nasl_ctx(naslctxt *, const char *, const char *)
Initialize a NASL context for a NASL file.
void nasl_clean_ctx(naslctxt *)
void free_lex_ctxt(lex_ctxt *c)
lex_ctxt * init_empty_lex_ctxt()
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
tree_cell * nasl_incr_variable(lex_ctxt *, tree_cell *, int, int)
struct struct_lex_ctxt lex_ctxt
tree_cell * get_array_elem(lex_ctxt *, const char *, tree_cell *)
tree_cell * nasl_read_var_ref(lex_ctxt *, tree_cell *)
tree_cell * decl_global_variables(lex_ctxt *, tree_cell *)
tree_cell * decl_local_variables(lex_ctxt *, tree_cell *)
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
void ref_cell(tree_cell *c)
int cell_type(const tree_cell *c)
const char * nasl_type_name(int t)
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
tree_cell * alloc_typed_cell(int typ)
void deref_cell(tree_cell *c)
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
tree_cell * nasl_iterate_array(nasl_iterator *it)
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
char * array2str(const nasl_array *a)
void free_array(nasl_array *a)
struct st_nasl_array nasl_array
struct st_n_nasl_var named_nasl_var
kb_t plug_get_kb(struct script_infos *args)
Header file for module plugutils.
union TC::@332262321161220155002104006201360276211317150140 x
struct script_infos * script_infos