OpenVAS Scanner  7.0.1~git
exec.c
Go to the documentation of this file.
1 /* Portions Copyright (C) 2009-2019 Greenbone Networks GmbH
2  * Based on work Copyright (C) 2002 - 2004 Tenable Network Security
3  *
4  * SPDX-License-Identifier: GPL-2.0-only
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
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 
20 #define _GNU_SOURCE
21 
22 #include "exec.h"
23 
24 #include "../misc/plugutils.h"
25 #include "nasl.h"
26 #include "nasl_debug.h"
27 #include "nasl_func.h"
28 #include "nasl_global_ctxt.h"
29 #include "nasl_init.h"
30 #include "nasl_lex_ctxt.h"
31 #include "nasl_tree.h"
32 #include "nasl_var.h"
33 
34 #include <glib.h> /* for g_get_current_dir and others */
35 #include <glib/gstdio.h> /* for g_chdir */
36 #include <gvm/base/logging.h>
37 #include <gvm/base/prefs.h> /* for prefs_get */
38 #include <gvm/util/nvticache.h> /* for nvticache_get_kb */
39 #include <regex.h>
40 #include <stdlib.h> /* for srand48 */
41 #include <string.h> /* for strlen */
42 #include <string.h> /* for memmem */
43 #include <unistd.h> /* for getpid */
44 
45 #undef G_LOG_DOMAIN
46 
49 #define G_LOG_DOMAIN "lib nasl"
50 
51 extern int
53 
54 static int
56 {
57  tree_cell *c2;
58  int flag;
59 
60  if (c == NULL || c == FAKE_CELL)
61  return 0;
62 
63  switch (c->type)
64  {
65  case CONST_INT:
66  return c->x.i_val != 0;
67 
68  case CONST_STR:
69  case CONST_DATA:
70  return c->size != 0;
71 
72  case REF_ARRAY:
73  case DYN_ARRAY:
74  /*nasl_perror(lexic, "cell2bool: converting array to boolean does not make
75  * sense!\n"); */
76  return 1;
77 
78  default:
79  c2 = nasl_exec (lexic, c);
80  flag = cell2bool (lexic, c2);
81  deref_cell (c2);
82  return flag;
83  }
84 }
85 
86 static long int
87 cell2int3 (lex_ctxt *lexic, tree_cell *c, int warn, named_nasl_var *v)
88 {
89  tree_cell *c2 = NULL;
90  long int x;
91  char *p = NULL;
92 
93  if (c == NULL || c == FAKE_CELL) /* Do not SEGV on undefined variables */
94  return 0;
95 
96  switch (c->type)
97  {
98  case CONST_INT:
99  return c->x.i_val;
100 
101  case CONST_STR:
102  case CONST_DATA:
103  x = strtol (c->x.str_val, &p, 0);
104  if (*p != '\0' && warn)
105  if (warn)
106  {
107  if (v)
108  nasl_perror (lexic,
109  "Converting the non numeric string '%s' in variable "
110  "'%s' to integer does not make sense in this "
111  "context",
112  c->x.str_val,
113  v->var_name != NULL ? v->var_name : "(null)");
114  else
115  nasl_perror (lexic,
116  "Converting the non numeric string '%s' to "
117  "integer does not make sense in this context",
118  c->x.str_val);
119  }
120  return x;
121 
122  case REF_VAR:
123  v = c->x.ref_val;
124  /* fallthrough */
125 
126  default:
127  c2 = nasl_exec (lexic, c);
128  x = cell2int3 (lexic, c2, warn, v);
129  deref_cell (c2);
130  return x;
131  }
132 }
133 
134 static long int
136 {
137  return cell2int3 (lexic, c, 0, NULL);
138 }
139 
140 static long int
142 {
143  return cell2int3 (lexic, c, 1, NULL);
144 }
145 
146 static tree_cell *
147 int2cell (long int x)
148 {
149  tree_cell *c = alloc_expr_cell (0, CONST_INT, NULL, NULL);
150  c->x.i_val = x;
151  return c;
152 }
153 
154 static tree_cell *
155 bool2cell (long int x)
156 {
157  return int2cell (x != 0);
158 }
159 
160 static char *
162 {
163  char *p;
164  tree_cell *c2;
165  nasl_array *a;
166 
167  if (c == NULL || c == FAKE_CELL)
168  return NULL;
169 
170  switch (c->type)
171  {
172  case CONST_INT:
173  return g_strdup_printf ("%ld", c->x.i_val);
174 
175  case CONST_STR:
176  case CONST_DATA:
177  if (c->x.str_val == NULL)
178  p = g_strdup ("");
179  else
180  {
181  p = g_malloc0 (c->size + 1);
182  memcpy (p, c->x.str_val, c->size);
183  }
184  return p;
185 
186  case REF_ARRAY:
187  case DYN_ARRAY:
188  a = c->x.ref_val;
189  return array2str (a);
190 
191  default:
192  c2 = nasl_exec (lexic, c);
193  p = cell2str (lexic, c2);
194  deref_cell (c2);
195  if (p == NULL)
196  p = g_strdup ("");
197  return p;
198  }
199 }
200 
204 tree_cell *
206 {
207  tree_cell *c2 = NULL, *ret = NULL;
208  if (c1 == NULL || c1 == FAKE_CELL)
209  return c1;
210 
211  switch (c1->type)
212  {
213  case CONST_INT:
214  case CONST_STR:
215  case CONST_DATA:
216  case REF_ARRAY:
217  case DYN_ARRAY:
218  ref_cell (c1);
219  return c1;
220  default:
221  c2 = nasl_exec (lexic, c1);
222  ret = cell2atom (lexic, c2);
223  deref_cell (c2);
224  return ret;
225  }
226 }
227 
228 long int
230 {
231  int flag, typ, typ1, typ2;
232  long int x1, x2;
233  char *s1, *s2;
234  int len_s1, len_s2, len_min;
235 
236  if (c1 == NULL || c1 == FAKE_CELL)
237  nasl_perror (lexic, "cell_cmp: c1 == NULL !\n");
238  if (c2 == NULL || c2 == FAKE_CELL)
239  nasl_perror (lexic, "cell_cmp: c2 == NULL !\n");
240 
241  /* We first convert the cell to atomic types. */
242  c1 = cell2atom (lexic, c1);
243  c2 = cell2atom (lexic, c2);
244 
245  /*
246  * Comparing anything to something else which is entirely different
247  * may lead to unpredictable results.
248  * Here are the rules:
249  * 1. No problem with same types, although we do not compare arrays yet
250  * 2. No problem with CONST_DATA / CONST_STR
251  * 3. When an integer is compared to a string, the integer is converted
252  * 4. When NULL is compared to an integer, it is converted to 0
253  * 5. When NULL is compared to a string, it is converted to ""
254  * 6. NULL is "smaller" than anything else (i.e. an array)
255  * Anything else is an error
256  */
257  typ1 = cell_type (c1);
258  typ2 = cell_type (c2);
259 
260  if (typ1 == 0 && typ2 == 0) /* Two NULL */
261  {
262  deref_cell (c1);
263  deref_cell (c2);
264  return 0;
265  }
266 
267  if (typ1 == typ2) /* Same type, no problem */
268  typ = typ1;
269  else if ((typ1 == CONST_DATA || typ1 == CONST_STR)
270  && (typ2 == CONST_DATA || typ2 == CONST_STR))
271  typ = CONST_DATA; /* Same type in fact (string) */
272  /* We convert an integer into a string before compare */
273  else if ((typ1 == CONST_INT && (typ2 == CONST_DATA || typ2 == CONST_STR))
274  || (typ2 == CONST_INT && (typ1 == CONST_DATA || typ1 == CONST_STR)))
275  typ = CONST_DATA;
276  else if (typ1 == 0) /* 1st argument is null */
277  if (typ2 == CONST_INT || typ2 == CONST_DATA || typ2 == CONST_STR)
278  typ = typ2; /* We convert it to 0 or "" */
279  else
280  {
281  deref_cell (c1);
282  deref_cell (c2);
283  return -1; /* NULL is smaller than anything else */
284  }
285  else if (typ2 == 0) /* 2nd argument is null */
286  if (typ1 == CONST_INT || typ1 == CONST_DATA || typ1 == CONST_STR)
287  typ = typ1; /* We convert it to 0 or "" */
288  else
289  {
290  deref_cell (c1);
291  deref_cell (c2);
292  return 1; /* Anything else is greater than NULL */
293  }
294  else
295  {
296  gchar *n1, *n2;
297 
298  n1 = cell2str (lexic, c1);
299  n2 = cell2str (lexic, c2);
300  nasl_perror (lexic,
301  "cell_cmp: comparing '%s' of type %s and '%s' of "
302  "type %s does not make sense\n",
303  n1, nasl_type_name (typ1), n2, nasl_type_name (typ2));
304  g_free (n1);
305  g_free (n2);
306  deref_cell (c1);
307  deref_cell (c2);
308  return 0;
309  }
310 
311  switch (typ)
312  {
313  case CONST_INT:
314  x1 = cell2int (lexic, c1);
315  x2 = cell2int (lexic, c2);
316  deref_cell (c1);
317  deref_cell (c2);
318  return x1 - x2;
319 
320  case CONST_STR:
321  case CONST_DATA:
322  s1 = cell2str (lexic, c1);
323  if (typ1 == CONST_STR || typ1 == CONST_DATA)
324  len_s1 = c1->size;
325  else if (s1 == NULL)
326  len_s1 = 0;
327  else
328  len_s1 = strlen (s1);
329 
330  s2 = cell2str (lexic, c2);
331  if (typ2 == CONST_STR || typ2 == CONST_DATA)
332  len_s2 = c2->size;
333  else if (s2 == NULL)
334  len_s2 = 0;
335  else
336  len_s2 = strlen (s2);
337 
338  len_min = len_s1 < len_s2 ? len_s1 : len_s2;
339  flag = 0;
340 
341  if (len_min > 0)
342  flag = memcmp (s1, s2, len_min);
343  if (flag == 0)
344  flag = len_s1 - len_s2;
345 
346  g_free (s1);
347  g_free (s2);
348  deref_cell (c1);
349  deref_cell (c2);
350  return flag;
351 
352  case REF_ARRAY:
353  case DYN_ARRAY:
354  g_message ("cell_cmp: cannot compare arrays yet");
355  deref_cell (c1);
356  deref_cell (c2);
357  return 0;
358 
359  default:
360  g_message ("cell_cmp: don't known how to compare %s and %s",
361  nasl_type_name (typ1), nasl_type_name (typ2));
362  deref_cell (c1);
363  deref_cell (c2);
364  return 0;
365  }
366 }
367 
368 FILE *nasl_trace_fp = NULL;
369 
370 lex_ctxt *truc = NULL;
371 
372 static void
373 nasl_dump_expr (FILE *fp, const tree_cell *c)
374 {
375  if (c == NULL)
376  fprintf (fp, "NULL");
377  else if (c == FAKE_CELL)
378  fprintf (fp, "FAKE");
379  else
380  switch (c->type)
381  {
382  case NODE_VAR:
383  fprintf (fp, "%s", c->x.str_val);
384  break;
385  case EXPR_AND:
386  fprintf (fp, "(");
387  nasl_dump_expr (fp, c->link[0]);
388  fprintf (fp, ") && (");
389  nasl_dump_expr (fp, c->link[1]);
390  fprintf (fp, ")");
391  break;
392  case EXPR_OR:
393  fprintf (fp, "(");
394  nasl_dump_expr (fp, c->link[0]);
395  fprintf (fp, ") || (");
396  nasl_dump_expr (fp, c->link[1]);
397  fprintf (fp, ")");
398  break;
399  case EXPR_NOT:
400  fprintf (fp, "! (");
401  nasl_dump_expr (fp, c->link[0]);
402  fprintf (fp, ")");
403  break;
404  case EXPR_PLUS:
405  fprintf (fp, "(");
406  nasl_dump_expr (fp, c->link[0]);
407  fprintf (fp, ") + (");
408  nasl_dump_expr (fp, c->link[1]);
409  fprintf (fp, ")");
410  break;
411  case EXPR_MINUS:
412  fprintf (fp, "(");
413  nasl_dump_expr (fp, c->link[0]);
414  fprintf (fp, ") - (");
415  nasl_dump_expr (fp, c->link[1]);
416  fprintf (fp, ")");
417  break;
418 
419  case EXPR_INCR:
420  if (c->link[0] == NULL)
421  {
422  fprintf (fp, " ++");
423  nasl_dump_expr (fp, c->link[1]);
424  }
425  else
426  {
427  nasl_dump_expr (fp, c->link[0]);
428  fprintf (fp, "++ ");
429  }
430  break;
431  case EXPR_DECR:
432  if (c->link[0] == NULL)
433  {
434  fprintf (fp, " --");
435  nasl_dump_expr (fp, c->link[1]);
436  }
437  else
438  {
439  nasl_dump_expr (fp, c->link[0]);
440  fprintf (fp, "-- ");
441  }
442  break;
443 
445  case EXPR_EXPO:
446  fprintf (fp, "(");
447  nasl_dump_expr (fp, c->link[0]);
448  fprintf (fp, ") ** (");
449  nasl_dump_expr (fp, c->link[1]);
450  fprintf (fp, ")");
451  break;
452 
453  case EXPR_U_MINUS:
454  fprintf (fp, " - (");
455  nasl_dump_expr (fp, c->link[0]);
456  fprintf (fp, ")");
457  break;
458 
459  case EXPR_MULT:
460  fprintf (fp, "(");
461  nasl_dump_expr (fp, c->link[0]);
462  fprintf (fp, ") * (");
463  nasl_dump_expr (fp, c->link[1]);
464  fprintf (fp, ")");
465  break;
466  case EXPR_DIV:
467  fprintf (fp, "(");
468  nasl_dump_expr (fp, c->link[0]);
469  fprintf (fp, ") / (");
470  nasl_dump_expr (fp, c->link[1]);
471  fprintf (fp, ")");
472  break;
473  case EXPR_MODULO:
474  fprintf (fp, "(");
475  nasl_dump_expr (fp, c->link[0]);
476  fprintf (fp, ") %% (");
477  nasl_dump_expr (fp, c->link[1]);
478  fprintf (fp, ")");
479  break;
480  case EXPR_BIT_AND:
481  fprintf (fp, "(");
482  nasl_dump_expr (fp, c->link[0]);
483  fprintf (fp, ") & (");
484  nasl_dump_expr (fp, c->link[1]);
485  fprintf (fp, ")");
486  break;
487  case EXPR_BIT_OR:
488  fprintf (fp, "(");
489  nasl_dump_expr (fp, c->link[0]);
490  fprintf (fp, ") | (");
491  nasl_dump_expr (fp, c->link[1]);
492  fprintf (fp, ")");
493  break;
494  case EXPR_BIT_XOR:
495  fprintf (fp, "(");
496  nasl_dump_expr (fp, c->link[0]);
497  fprintf (fp, ") ^ (");
498  nasl_dump_expr (fp, c->link[1]);
499  fprintf (fp, ")");
500  break;
501  case EXPR_BIT_NOT:
502  fprintf (fp, "~ (");
503  nasl_dump_expr (fp, c->link[0]);
504  fprintf (fp, ")");
505  break;
506  case EXPR_L_SHIFT:
507  fprintf (fp, "(");
508  nasl_dump_expr (fp, c->link[0]);
509  fprintf (fp, ") << (");
510  nasl_dump_expr (fp, c->link[1]);
511  fprintf (fp, ")");
512  break;
513  case EXPR_R_SHIFT:
514  fprintf (fp, "(");
515  nasl_dump_expr (fp, c->link[0]);
516  fprintf (fp, ") >> (");
517  nasl_dump_expr (fp, c->link[1]);
518  fprintf (fp, ")");
519  break;
520  case EXPR_R_USHIFT:
521  fprintf (fp, "(");
522  nasl_dump_expr (fp, c->link[0]);
523  fprintf (fp, ") >>> (");
524  nasl_dump_expr (fp, c->link[1]);
525  fprintf (fp, ")");
526  break;
527  case COMP_MATCH:
528  nasl_dump_expr (fp, c->link[0]);
529  fprintf (fp, " >< ");
530  nasl_dump_expr (fp, c->link[1]);
531  break;
532  case COMP_NOMATCH:
533  nasl_dump_expr (fp, c->link[0]);
534  fprintf (fp, " >!< ");
535  nasl_dump_expr (fp, c->link[1]);
536  break;
537 
538  case COMP_RE_MATCH:
539  nasl_dump_expr (fp, c->link[0]);
540  fprintf (fp, " =~ ");
541  nasl_dump_expr (fp, c->link[1]);
542  break;
543 
544  case COMP_RE_NOMATCH:
545  nasl_dump_expr (fp, c->link[0]);
546  fprintf (fp, " !~ ");
547  nasl_dump_expr (fp, c->link[1]);
548  break;
549 
550  case COMP_LT:
551  nasl_dump_expr (fp, c->link[0]);
552  fprintf (fp, " < ");
553  nasl_dump_expr (fp, c->link[1]);
554  break;
555  case COMP_LE:
556  nasl_dump_expr (fp, c->link[0]);
557  fprintf (fp, " <= ");
558  nasl_dump_expr (fp, c->link[1]);
559  break;
560  case COMP_GT:
561  nasl_dump_expr (fp, c->link[0]);
562  fprintf (fp, " > ");
563  nasl_dump_expr (fp, c->link[1]);
564  break;
565  case COMP_GE:
566  nasl_dump_expr (fp, c->link[0]);
567  fprintf (fp, " >= ");
568  nasl_dump_expr (fp, c->link[1]);
569  break;
570  case COMP_EQ:
571  nasl_dump_expr (fp, c->link[0]);
572  fprintf (fp, " == ");
573  nasl_dump_expr (fp, c->link[1]);
574  break;
575  case CONST_INT:
576  fprintf (fp, "%ld", c->x.i_val);
577  break;
578  case CONST_STR:
579  case CONST_DATA:
580  fprintf (fp, "\"%s\"", c->x.str_val);
581  break;
582 
583  case NODE_ARRAY_EL:
584  fprintf (fp, "%s[", c->x.str_val);
585  nasl_dump_expr (fp, c->link[0]);
586  fprintf (fp, "]");
587  break;
588 
589  case NODE_FUN_CALL:
590  fprintf (fp, "%s(...)", c->x.str_val);
591  break;
592 
593  case NODE_AFF:
594  nasl_dump_expr (fp, c->link[0]);
595  putc ('=', fp);
596  nasl_dump_expr (fp, c->link[1]);
597  break;
598 
599  case NODE_PLUS_EQ:
600  nasl_dump_expr (fp, c->link[0]);
601  fprintf (fp, "+= (");
602  nasl_dump_expr (fp, c->link[1]);
603  fprintf (fp, ")");
604  break;
605 
606  case NODE_MINUS_EQ:
607  nasl_dump_expr (fp, c->link[0]);
608  fprintf (fp, "-= (");
609  nasl_dump_expr (fp, c->link[1]);
610  fprintf (fp, ")");
611  break;
612 
613  case NODE_MULT_EQ:
614  nasl_dump_expr (fp, c->link[0]);
615  fprintf (fp, "*= (");
616  nasl_dump_expr (fp, c->link[1]);
617  fprintf (fp, ")");
618  break;
619 
620  case NODE_DIV_EQ:
621  nasl_dump_expr (fp, c->link[0]);
622  fprintf (fp, "/= (");
623  nasl_dump_expr (fp, c->link[1]);
624  fprintf (fp, ")");
625  break;
626 
627  case NODE_MODULO_EQ:
628  nasl_dump_expr (fp, c->link[0]);
629  fprintf (fp, "%%= (");
630  nasl_dump_expr (fp, c->link[1]);
631  fprintf (fp, ")");
632  break;
633 
634  case NODE_L_SHIFT_EQ:
635  nasl_dump_expr (fp, c->link[0]);
636  fprintf (fp, " <<= (");
637  nasl_dump_expr (fp, c->link[1]);
638  fprintf (fp, ")");
639  break;
640 
641  case NODE_R_SHIFT_EQ:
642  nasl_dump_expr (fp, c->link[0]);
643  fprintf (fp, " >>= (");
644  nasl_dump_expr (fp, c->link[1]);
645  fprintf (fp, ")");
646  break;
647 
648  case NODE_R_USHIFT_EQ:
649  nasl_dump_expr (fp, c->link[0]);
650  fprintf (fp, " >>>= (");
651  nasl_dump_expr (fp, c->link[1]);
652  fprintf (fp, ")");
653  break;
654 
655  default:
656  fprintf (fp, "*%d*", c->type);
657  break;
658  }
659 }
660 
661 static void
662 nasl_short_dump (FILE *fp, const tree_cell *c)
663 {
664  if (c == NULL || c == FAKE_CELL)
665  return;
666 
667  switch (c->type)
668  {
669  case NODE_IF_ELSE:
670  fprintf (fp, "NASL:%04d> if (", c->line_nb);
671  nasl_dump_expr (fp, c->link[0]);
672  fprintf (fp, ") { ... }");
673  if (c->link[2] != NULL)
674  fprintf (fp, " else { ... }");
675  putc ('\n', fp);
676  break;
677 
678  case NODE_FOR:
679  fprintf (fp, "NASL:%04d> for (", c->line_nb);
680  nasl_dump_expr (fp, c->link[0]);
681  fprintf (fp, "; ");
682  nasl_dump_expr (fp, c->link[1]);
683  fprintf (fp, "; ");
684  nasl_dump_expr (fp, c->link[2]);
685  fprintf (fp, ") { ... }\n");
686  break;
687 
688  case NODE_WHILE:
689  fprintf (fp, "NASL:%04d> while (", c->line_nb);
690  nasl_dump_expr (fp, c->link[0]);
691  fprintf (fp, ") { ... }\n");
692  break;
693 
694  case NODE_FOREACH:
695  fprintf (fp, "NASL:%04d> foreach %s (", c->line_nb, c->x.str_val);
696  nasl_dump_expr (fp, c->link[0]);
697  fprintf (fp, ") { ... }\n");
698  break;
699 
700  case NODE_REPEAT_UNTIL:
701  fprintf (fp, "NASL:%04d> repeat { ... } until (", c->line_nb);
702  nasl_dump_expr (fp, c->link[0]);
703  fprintf (fp, ")\n");
704  break;
705 
706  case NODE_REPEATED:
707  fprintf (fp, "NASL:%04d> ... x ", c->line_nb);
708  nasl_dump_expr (fp, c->link[1]);
709  putc ('\n', fp);
710  break;
711 
712  case NODE_RETURN:
713  fprintf (fp, "NASL:%04d> return ", c->line_nb);
714  nasl_dump_expr (fp, c->link[0]);
715  fprintf (fp, ";\n");
716  break;
717 
718  case NODE_BREAK:
719  fprintf (fp, "NASL:%04d> break\n", c->line_nb);
720  break;
721 
722  case NODE_CONTINUE:
723  fprintf (fp, "NASL:%04d> continue\n", c->line_nb);
724  break;
725 
726  case NODE_AFF:
727  case NODE_PLUS_EQ:
728  case NODE_MINUS_EQ:
729  case NODE_MULT_EQ:
730  case NODE_DIV_EQ:
731  case NODE_MODULO_EQ:
732  case NODE_R_SHIFT_EQ:
733  case NODE_R_USHIFT_EQ:
734  case NODE_L_SHIFT_EQ:
735  fprintf (fp, "NASL:%04d> ", c->line_nb);
736  nasl_dump_expr (fp, c);
737  fprintf (fp, ";\n");
738  break;
739 
740  case NODE_FUN_CALL:
741  fprintf (fp, "NASL:%04d> %s(...)\n", c->line_nb, c->x.str_val);
742  break;
743 
744  case NODE_LOCAL:
745  fprintf (fp, "NASL:%04d> local_var ...\n", c->line_nb);
746  break;
747 
748  case NODE_GLOBAL:
749  fprintf (fp, "NASL:%04d> global_var ...\n", c->line_nb);
750  break;
751  }
752 }
753 
755 static long int
756 expo (long int x, long int y)
757 {
758  long int z;
759 
760  if (y == 0)
761  return 1;
762  else if (y < 0)
763  if (x == 1)
764  return 1;
765  else
766  return 0;
767  else if (y == 1)
768  return x;
769 
770  z = expo (x, y / 2);
771  if (y % 2 == 0)
772  return z * z;
773  else
774  return x * z * z;
775 }
776 
780 tree_cell *
782 {
783  tree_cell *ret = NULL, *ret2 = NULL, *tc1 = NULL, *tc2 = NULL, *tc3 = NULL,
784  *idx = NULL, *args;
785  int flag, z;
786  char *s1 = NULL, *s2 = NULL, *s3 = NULL, *p = NULL;
787  char *p1, *p2;
788  int len1, len2;
789  nasl_func *pf = NULL;
790  long int x, y, n;
791  int i, lint_mode = 0;
792 
793  if (st)
794  if (st->line_nb != 0)
795  lexic->line_nb = st->line_nb;
796  /* return */
797  if (lexic->ret_val != NULL)
798  {
799  ref_cell (lexic->ret_val);
800  return lexic->ret_val;
801  }
802 
803  /* break or continue */
804  if (lexic->break_flag || lexic->cont_flag)
805  return FAKE_CELL;
806 
807  if (st == FAKE_CELL)
808  return FAKE_CELL;
809 
810  if (st == NULL)
811  {
812  return NULL;
813  }
814 
815  if (nasl_trace_fp != NULL)
817 
818  switch (st->type)
819  {
820  case NODE_IF_ELSE:
821  ret = nasl_exec (lexic, st->link[0]);
822 #ifdef STOP_AT_FIRST_ERROR
823  if (ret == NULL)
824  return NULL;
825 #endif
826  if (cell2bool (lexic, ret))
827  ret2 = nasl_exec (lexic, st->link[1]);
828  else if (st->link[2] != NULL) /* else branch */
829  ret2 = nasl_exec (lexic, st->link[2]);
830  else /* No else */
831  ret2 = FAKE_CELL;
832  deref_cell (ret);
833  return ret2;
834 
835  case NODE_INSTR_L: /* Block. [0] = first instr, [1] = tail */
836  ret = nasl_exec (lexic, st->link[0]);
837  if (st->link[1] == NULL || lexic->break_flag || lexic->cont_flag)
838  return ret;
839  deref_cell (ret);
840  ret = nasl_exec (lexic, st->link[1]);
841  return ret;
842 
843  case NODE_FOR:
844  /* [0] = start expr, [1] = cond, [2] = end_expr, [3] = block */
845  ret2 = nasl_exec (lexic, st->link[0]);
846 #ifdef STOP_AT_FIRST_ERROR
847  if (ret2 == NULL)
848  return NULL;
849 #endif
850  deref_cell (ret2);
851  for (;;)
852  {
853  /* Break the loop if 'return' */
854  if (lexic->ret_val != NULL)
855  {
856  ref_cell (lexic->ret_val);
857  return lexic->ret_val;
858  }
859 
860  /* condition */
861  if ((ret = nasl_exec (lexic, st->link[1])) == NULL)
862  return NULL; /* We can return here, as NULL is false */
863  flag = cell2bool (lexic, ret);
864  deref_cell (ret);
865  if (!flag)
866  break;
867  /* block */
868  ret = nasl_exec (lexic, st->link[3]);
869 #ifdef STOP_AT_FIRST_ERROR
870  if (ret == NULL)
871  return NULL;
872 #endif
873  deref_cell (ret);
874 
875  /* break */
876  if (lexic->break_flag)
877  {
878  lexic->break_flag = 0;
879  return FAKE_CELL;
880  }
881 
882  lexic->cont_flag = 0; /* No need to test if set */
883 
884  /* end expression */
885  ret = nasl_exec (lexic, st->link[2]);
886 #ifdef STOP_AT_FIRST_ERROR
887  if (ret == NULL)
888  return NULL;
889 #endif
890  deref_cell (ret);
891  }
892  return FAKE_CELL;
893 
894  case NODE_WHILE:
895  /* [0] = cond, [1] = block */
896  for (;;)
897  {
898  /* return? */
899  if (lexic->ret_val != NULL)
900  {
901  ref_cell (lexic->ret_val);
902  return lexic->ret_val;
903  }
904  /* Condition */
905  if ((ret = nasl_exec (lexic, st->link[0])) == NULL)
906  return NULL; /* NULL is false */
907  flag = cell2bool (lexic, ret);
908  deref_cell (ret);
909  if (!flag)
910  break;
911  /* Block */
912  ret = nasl_exec (lexic, st->link[1]);
913 #ifdef STOP_AT_FIRST_ERROR
914  if (ret == NULL)
915  return NULL;
916 #endif
917  deref_cell (ret);
918 
919  /* break */
920  if (lexic->break_flag)
921  {
922  lexic->break_flag = 0;
923  return FAKE_CELL;
924  }
925  lexic->cont_flag = 0;
926  }
927  return FAKE_CELL;
928 
929  case NODE_REPEAT_UNTIL:
930  /* [0] = block, [1] = cond */
931  for (;;)
932  {
933  /* return? */
934  if (lexic->ret_val != NULL)
935  {
936  ref_cell (lexic->ret_val);
937  return lexic->ret_val;
938  }
939  /* Block */
940  ret = nasl_exec (lexic, st->link[0]);
941 #ifdef STOP_AT_FIRST_ERROR
942  if (ret == NULL)
943  return NULL;
944 #endif
945  deref_cell (ret);
946 
947  /* break */
948  if (lexic->break_flag)
949  {
950  lexic->break_flag = 0;
951  return FAKE_CELL;
952  }
953  lexic->cont_flag = 0;
954 
955  /* Condition */
956  ret = nasl_exec (lexic, st->link[1]);
957 #ifdef STOP_AT_FIRST_ERROR
958  if (ret == NULL)
959  return NULL;
960 #endif
961  flag = cell2bool (lexic, ret);
962  deref_cell (ret);
963  if (flag)
964  break;
965  }
966  return FAKE_CELL;
967 
968  case NODE_FOREACH:
969  /* str_val = index name, [0] = array, [1] = block */
970  {
971  nasl_iterator ai;
972  tree_cell *v, *a, *val;
973 
974  v = get_variable_by_name (lexic, st->x.str_val);
975  if (v == NULL)
976  return NULL; /* We cannot go on if we have no variable to iterate */
977  a = nasl_exec (lexic, st->link[0]);
978  ai = nasl_array_iterator (lexic, a);
979  while ((val = nasl_iterate_array (&ai)) != NULL)
980  {
981  tc1 = nasl_affect (v, val);
982  ret = nasl_exec (lexic, st->link[1]);
983  deref_cell (val);
984  deref_cell (tc1);
985 #ifdef STOP_AT_FIRST_ERROR
986  if (ret == NULL)
987  break;
988 #endif
989  deref_cell (ret);
990 
991  /* return */
992  if (lexic->ret_val != NULL)
993  break;
994  /* break */
995  if (lexic->break_flag)
996  {
997  lexic->break_flag = 0;
998  break;
999  }
1000  lexic->cont_flag = 0;
1001  }
1002  free_array (ai.a);
1003  g_free (ai.a);
1004  deref_cell (a);
1005  deref_cell (v);
1006  }
1007  return FAKE_CELL;
1008 
1009  case NODE_FUN_DEF:
1010  /* x.str_val = function name, [0] = argdecl, [1] = block */
1011  /* 3rd arg is only for lint. Hier is always 0 */
1012  ret = decl_nasl_func (lexic, st, lint_mode);
1013  return ret;
1014 
1015  case NODE_FUN_CALL:
1016  pf = get_func_ref_by_name (lexic, st->x.str_val);
1017  if (pf == NULL)
1018  {
1019  nasl_perror (lexic, "Undefined function '%s'\n", st->x.str_val);
1020  return NULL;
1021  }
1022  args = st->link[0];
1023  ret = nasl_func_call (lexic, pf, args);
1024  return ret;
1025 
1026  case NODE_REPEATED:
1027  n = cell2intW (lexic, st->link[1]);
1028  if (n <= 0)
1029  return NULL;
1030 
1031 #ifdef STOP_AT_FIRST_ERROR
1032  for (tc1 = NULL, i = 1; i <= n; i++)
1033  {
1034  deref_cell (tc1);
1035  if ((tc1 = nasl_exec (lexic, st->link[0])) == NULL)
1036  return NULL;
1037  }
1038  return tc1;
1039 #else
1040  for (i = 1; i <= n; i++)
1041  {
1042  tc1 = nasl_exec (lexic, st->link[0]);
1043  deref_cell (tc1);
1044  }
1045  return FAKE_CELL;
1046 #endif
1047 
1048  /*
1049  * I wonder...
1050  * Will nasl_exec be really called with NODE_EXEC or NODE_ARG?
1051  */
1052  case NODE_DECL: /* Used in function declarations */
1053  /* [0] = next arg in list */
1054  /* TBD? */
1055  return st; /* ? */
1056 
1057  case NODE_ARG: /* Used function calls */
1058  /* val = name can be NULL, [0] = val, [1] = next arg */
1059  ret = nasl_exec (lexic, st->link[0]); /* Is this wise? */
1060  return ret;
1061 
1062  case NODE_RETURN:
1063  /* [0] = ret val */
1064  ret = nasl_return (lexic, st->link[0]);
1065  return ret;
1066 
1067  case NODE_BREAK:
1068  lexic->break_flag = 1;
1069  return FAKE_CELL;
1070 
1071  case NODE_CONTINUE:
1072  lexic->cont_flag = 1;
1073  return FAKE_CELL;
1074 
1075  case NODE_ARRAY_EL: /* val = array name, [0] = index */
1076  idx = cell2atom (lexic, st->link[0]);
1077  ret = get_array_elem (lexic, st->x.str_val, idx);
1078  deref_cell (idx);
1079  return ret;
1080 
1083  case NODE_AFF:
1084  /* [0] = lvalue, [1] = rvalue */
1085  tc1 = nasl_exec (lexic, st->link[0]);
1086  tc2 = nasl_exec (lexic, st->link[1]);
1087  ret = nasl_affect (tc1, tc2);
1088  deref_cell (tc1); /* Must free VAR_REF */
1089  deref_cell (ret);
1090  return tc2; /* So that "a = b = e;" works */
1091 
1092  case NODE_PLUS_EQ:
1093  tc1 = nasl_exec (lexic, st->link[0]);
1094  tc2 = nasl_exec (lexic, st->link[1]);
1095  tc3 = alloc_expr_cell (0, EXPR_PLUS, tc1, tc2);
1096  ret2 = nasl_exec (lexic, tc3);
1097  ret = nasl_affect (tc1, ret2);
1098  deref_cell (tc3); /* Frees tc1 and tc2 */
1099  deref_cell (ret);
1100  return ret2; /* So that "a = b += e;" works */
1101 
1102  case NODE_MINUS_EQ:
1103  tc1 = nasl_exec (lexic, st->link[0]);
1104  tc2 = nasl_exec (lexic, st->link[1]);
1105  tc3 = alloc_expr_cell (0, EXPR_MINUS, tc1, tc2);
1106  ret2 = nasl_exec (lexic, tc3);
1107  ret = nasl_affect (tc1, ret2);
1108  deref_cell (tc3); /* Frees tc1 and tc2 */
1109  deref_cell (ret);
1110  return ret2; /* So that "a = b -= e;" works */
1111 
1112  case NODE_MULT_EQ:
1113  tc1 = nasl_exec (lexic, st->link[0]);
1114  tc2 = nasl_exec (lexic, st->link[1]);
1115  tc3 = alloc_expr_cell (0, EXPR_MULT, tc1, tc2);
1116  ret2 = nasl_exec (lexic, tc3);
1117  ret = nasl_affect (tc1, ret2);
1118  deref_cell (tc3); /* Frees tc1 and tc2 */
1119  deref_cell (ret);
1120  return ret2;
1121 
1122  case NODE_DIV_EQ:
1123  tc1 = nasl_exec (lexic, st->link[0]);
1124  tc2 = nasl_exec (lexic, st->link[1]);
1125  tc3 = alloc_expr_cell (0, EXPR_DIV, tc1, tc2);
1126  ret2 = nasl_exec (lexic, tc3);
1127  ret = nasl_affect (tc1, ret2);
1128  deref_cell (tc3); /* Frees tc1 and tc2 */
1129  deref_cell (ret);
1130  return ret2;
1131 
1132  case NODE_MODULO_EQ:
1133  tc1 = nasl_exec (lexic, st->link[0]);
1134  tc2 = nasl_exec (lexic, st->link[1]);
1135  tc3 = alloc_expr_cell (0, EXPR_MODULO, tc1, tc2);
1136  ret2 = nasl_exec (lexic, tc3);
1137  ret = nasl_affect (tc1, ret2);
1138  deref_cell (tc3); /* Frees tc1 and tc2 */
1139  deref_cell (ret);
1140  return ret2;
1141 
1142  case NODE_L_SHIFT_EQ:
1143  tc1 = nasl_exec (lexic, st->link[0]);
1144  tc2 = nasl_exec (lexic, st->link[1]);
1145  tc3 = alloc_expr_cell (0, EXPR_L_SHIFT, tc1, tc2);
1146  ret2 = nasl_exec (lexic, tc3);
1147  ret = nasl_affect (tc1, ret2);
1148  deref_cell (tc3); /* Frees tc1 and tc2 */
1149  deref_cell (ret);
1150  return ret2;
1151 
1152  case NODE_R_SHIFT_EQ:
1153  tc1 = nasl_exec (lexic, st->link[0]);
1154  tc2 = nasl_exec (lexic, st->link[1]);
1155  tc3 = alloc_expr_cell (0, EXPR_R_SHIFT, tc1, tc2);
1156  ret2 = nasl_exec (lexic, tc3);
1157  ret = nasl_affect (tc1, ret2);
1158  deref_cell (tc3); /* Frees tc1 and tc2 */
1159  deref_cell (ret);
1160  return ret2;
1161 
1162  case NODE_R_USHIFT_EQ:
1163  tc1 = nasl_exec (lexic, st->link[0]);
1164  tc2 = nasl_exec (lexic, st->link[1]);
1165  tc3 = alloc_expr_cell (0, EXPR_R_USHIFT, tc1, tc2);
1166  ret2 = nasl_exec (lexic, tc3);
1167  ret = nasl_affect (tc1, ret2);
1168  deref_cell (tc3); /* Frees tc1 and tc2 */
1169  deref_cell (ret);
1170  return ret2;
1171 
1172  case NODE_VAR:
1173  /* val = variable name */
1174  ret = get_variable_by_name (lexic, st->x.str_val);
1175  return ret;
1176 
1177  case NODE_LOCAL: /* [0] = argdecl */
1178  ret = decl_local_variables (lexic, st->link[0]);
1179  return ret;
1180 
1181  case NODE_GLOBAL: /* [0] = argdecl */
1182  ret = decl_global_variables (lexic, st->link[0]);
1183  return ret;
1184 
1185  case EXPR_AND:
1186  x = cell2bool (lexic, st->link[0]);
1187  if (!x)
1188  return bool2cell (0);
1189 
1190  y = cell2bool (lexic, st->link[1]);
1191  return bool2cell (y);
1192 
1193  case EXPR_OR:
1194  x = cell2bool (lexic, st->link[0]);
1195  if (x)
1196  return bool2cell (x);
1197  y = cell2bool (lexic, st->link[1]);
1198  return bool2cell (y);
1199 
1200  case EXPR_NOT:
1201  x = cell2bool (lexic, st->link[0]);
1202  return bool2cell (!x);
1203 
1204  case EXPR_INCR:
1205  case EXPR_DECR:
1206  x = (st->type == EXPR_INCR) ? 1 : -1;
1207  if (st->link[0] == NULL)
1208  {
1209  y = 1; /* pre */
1210  tc1 = st->link[1];
1211  }
1212  else
1213  {
1214  y = 0; /* post */
1215  tc1 = st->link[0];
1216  }
1217  tc2 = nasl_exec (lexic, tc1);
1218  if (tc2 == NULL)
1219  return NULL;
1220  ret = nasl_incr_variable (lexic, tc2, y, x);
1221  deref_cell (tc2);
1222  return ret;
1223 
1224  case EXPR_PLUS:
1225  s1 = s2 = NULL;
1226  tc1 = cell2atom (lexic, st->link[0]);
1227 #ifdef STOP_AT_FIRST_ERROR
1228  if (tc1 == NULL || tc1 == FAKE_CELL)
1229  return NULL;
1230 #endif
1231  tc2 = cell2atom (lexic, st->link[1]);
1232  if (tc2 == NULL || tc2 == FAKE_CELL)
1233  {
1234 #ifdef STOP_AT_FIRST_ERROR
1235  deref_cell (tc1);
1236  return NULL;
1237 #else
1238  return tc1;
1239 #endif
1240  }
1241 
1242  if (tc1 == NULL || tc1 == FAKE_CELL)
1243  return tc2;
1244 
1245  /*
1246  * Anything added to a string is converted to a string
1247  * Otherwise anything added to an intger is converted into an integer
1248  */
1249  if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1250  flag = CONST_DATA;
1251  else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1252  flag = CONST_STR;
1253  else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1254  flag = CONST_INT;
1255  else
1256  flag = NODE_EMPTY;
1257  switch (flag)
1258  {
1259  long sz;
1260  case CONST_INT:
1261  x = tc1->x.i_val;
1262  y = cell2int (lexic, tc2);
1263  ret = int2cell (x + y);
1264  break;
1265 
1266  case CONST_STR:
1267  case CONST_DATA:
1268  s1 = s2 = NULL;
1269  if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1270  len1 = tc1->size;
1271  else
1272  {
1273  s1 = cell2str (lexic, tc1);
1274  len1 = (s1 == NULL ? 0 : strlen (s1));
1275  }
1276 
1277  if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1278  len2 = tc2->size;
1279  else
1280  {
1281  s2 = cell2str (lexic, tc2);
1282  len2 = (s2 == NULL ? 0 : strlen (s2));
1283  }
1284 
1285  sz = len1 + len2;
1286  s3 = g_malloc0 (sz + 1);
1287  if (len1 > 0)
1288  memcpy (s3, s1 != NULL ? s1 : tc1->x.str_val, len1);
1289  if (len2 > 0)
1290  memcpy (s3 + len1, s2 != NULL ? s2 : tc2->x.str_val, len2);
1291  g_free (s1);
1292  g_free (s2);
1293  ret = alloc_typed_cell (flag);
1294  ret->x.str_val = s3;
1295  ret->size = sz;
1296  break;
1297 
1298  default:
1299  ret = NULL;
1300  break;
1301  }
1302  deref_cell (tc1);
1303  deref_cell (tc2);
1304  return ret;
1305 
1306  case EXPR_MINUS: /* Infamous duplicated code */
1307  s1 = s2 = NULL;
1308  tc1 = cell2atom (lexic, st->link[0]);
1309 #ifdef STOP_AT_FIRST_ERROR
1310  if (tc1 == NULL || tc1 == FAKE_CELL)
1311  return NULL;
1312 #endif
1313  tc2 = cell2atom (lexic, st->link[1]);
1314  if (tc2 == NULL || tc2 == FAKE_CELL)
1315  {
1316 #ifdef STOP_AT_FIRST_ERROR
1317  deref_cell (tc1);
1318  return NULL;
1319 #else
1320  return tc1;
1321 #endif
1322  }
1323 
1324  if (tc1 == NULL || tc1 == FAKE_CELL)
1325  {
1326  if (tc2->type == CONST_INT)
1327  {
1328  y = cell2int (lexic, tc2);
1329  ret = int2cell (-y);
1330  }
1331  else
1332  ret = NULL;
1333  deref_cell (tc2);
1334  return ret;
1335  }
1336 
1337  /*
1338  * Anything subtracted from a string is converted to a string
1339  * Otherwise anything subtracted from integer is converted into an
1340  * integer
1341  */
1342  if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1343  flag = CONST_DATA;
1344  else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1345  flag = CONST_STR;
1346  else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1347  flag = CONST_INT;
1348  else
1349  flag = NODE_EMPTY;
1350  switch (flag)
1351  {
1352  case CONST_INT:
1353  x = cell2int (lexic, tc1);
1354  y = cell2int (lexic, tc2);
1355  ret = int2cell (x - y);
1356  break;
1357 
1358  case CONST_STR:
1359  case CONST_DATA:
1360  if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1361  {
1362  p1 = tc1->x.str_val;
1363  len1 = tc1->size;
1364  }
1365  else
1366  {
1367  p1 = s1 = cell2str (lexic, tc1);
1368  len1 = (s1 == NULL ? 0 : strlen (s1));
1369  }
1370 
1371  if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1372  {
1373  p2 = tc2->x.str_val;
1374  len2 = tc2->size;
1375  }
1376  else
1377  {
1378  p2 = s2 = cell2str (lexic, tc2);
1379  len2 = (s2 == NULL ? 0 : strlen (s2));
1380  }
1381 
1382  if (len2 == 0 || len1 < len2
1383  || (p = memmem (p1, len1, p2, len2)) == NULL)
1384  {
1385  s3 = g_malloc0 (len1 + 1);
1386  memcpy (s3, p1, len1);
1387  ret = alloc_typed_cell (flag);
1388  ret->x.str_val = s3;
1389  ret->size = len1;
1390  }
1391  else
1392  {
1393  long sz = len1 - len2;
1394  if (sz <= 0)
1395  {
1396  sz = 0;
1397  s3 = g_strdup ("");
1398  }
1399  else
1400  {
1401  s3 = g_malloc0 (sz + 1);
1402  if (p - p1 > 0)
1403  memcpy (s3, p1, p - p1);
1404  if (sz > p - p1)
1405  memcpy (s3 + (p - p1), p + len2, sz - (p - p1));
1406  }
1407  ret = alloc_typed_cell (flag);
1408  ret->x.str_val = s3;
1409  ret->size = sz;
1410  }
1411 
1412  g_free (s1);
1413  g_free (s2);
1414  break;
1415 
1416  default:
1417  ret = NULL;
1418  break;
1419  }
1420  deref_cell (tc1);
1421  deref_cell (tc2);
1422  return ret;
1423 
1424  case EXPR_MULT:
1425  x = cell2intW (lexic, st->link[0]);
1426  y = cell2intW (lexic, st->link[1]);
1427  return int2cell (x * y);
1428 
1429  case EXPR_DIV:
1430  x = cell2intW (lexic, st->link[0]);
1431  y = cell2intW (lexic, st->link[1]);
1432  if (y != 0)
1433  return int2cell (x / y);
1434  else
1435  return int2cell (0);
1436 
1437  case EXPR_EXPO:
1438  x = cell2intW (lexic, st->link[0]);
1439  y = cell2intW (lexic, st->link[1]);
1440  return int2cell (expo (x, y));
1441 
1442  case EXPR_MODULO:
1443  x = cell2intW (lexic, st->link[0]);
1444  y = cell2intW (lexic, st->link[1]);
1445  if (y != 0)
1446  return int2cell (x % y);
1447  else
1448  return int2cell (0);
1449 
1450  case EXPR_BIT_AND:
1451  x = cell2intW (lexic, st->link[0]);
1452  y = cell2intW (lexic, st->link[1]);
1453  return int2cell (x & y);
1454 
1455  case EXPR_BIT_OR:
1456  x = cell2intW (lexic, st->link[0]);
1457  y = cell2intW (lexic, st->link[1]);
1458  return int2cell (x | y);
1459 
1460  case EXPR_BIT_XOR:
1461  x = cell2intW (lexic, st->link[0]);
1462  y = cell2intW (lexic, st->link[1]);
1463  return int2cell (x ^ y);
1464 
1465  case EXPR_BIT_NOT:
1466  x = cell2intW (lexic, st->link[0]);
1467  return int2cell (~x);
1468 
1469  case EXPR_U_MINUS:
1470  x = cell2intW (lexic, st->link[0]);
1471  return int2cell (-x);
1472 
1473  /* TBD: Handle shift for strings and arrays */
1474  case EXPR_L_SHIFT:
1475  x = cell2intW (lexic, st->link[0]);
1476  y = cell2intW (lexic, st->link[1]);
1477  return int2cell (x << y);
1478 
1479  case EXPR_R_SHIFT: /* arithmetic right shift */
1480  x = cell2intW (lexic, st->link[0]);
1481  y = cell2intW (lexic, st->link[1]);
1482  z = x >> y;
1483 #ifndef __GNUC__
1484  if (x < 0 && z >= 0) /* Fix it */
1485  z |= (~0) << (sizeof (x) * 8 - y);
1486 #endif
1487  return int2cell (z);
1488 
1489  case EXPR_R_USHIFT:
1490  x = cell2intW (lexic, st->link[0]);
1491  y = cell2intW (lexic, st->link[1]);
1492  z = (unsigned) x >> (unsigned) y;
1493 #ifndef __GNUC__
1494  if (x < 0 && z <= 0) /* Fix it! */
1495  z &= ~((~0) << (sizeof (x) * 8 - y));
1496 #endif
1497  return int2cell (z);
1498 
1499  case COMP_MATCH:
1500  case COMP_NOMATCH:
1501  tc1 = cell2atom (lexic, st->link[0]);
1502  tc2 = cell2atom (lexic, st->link[1]);
1503  s1 = s2 = NULL;
1504 
1505  if (tc1 == NULL || tc1 == FAKE_CELL)
1506  {
1507  p1 = "";
1508  len1 = 0;
1509  }
1510  else if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1511  {
1512  p1 = tc1->x.str_val;
1513  len1 = tc1->size;
1514  }
1515  else
1516  {
1517  p1 = s1 = cell2str (lexic, tc1);
1518  len1 = strlen (s1);
1519  }
1520 
1521  if (tc2 == NULL || tc2 == FAKE_CELL)
1522  {
1523  p2 = "";
1524  len2 = 0;
1525  }
1526  else if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1527  {
1528  p2 = tc2->x.str_val;
1529  len2 = tc2->size;
1530  }
1531  else
1532  {
1533  p2 = s2 = cell2str (lexic, tc2);
1534  len2 = strlen (s2);
1535  }
1536 
1537  if (len1 <= len2)
1538  flag = (memmem (p2, len2, p1, len1) != NULL);
1539  else
1540  flag = 0;
1541 
1542  g_free (s1);
1543  g_free (s2);
1544  deref_cell (tc1);
1545  deref_cell (tc2);
1546  if (st->type == COMP_MATCH)
1547  return bool2cell (flag);
1548  else
1549  return bool2cell (!flag);
1550 
1551  case COMP_RE_MATCH:
1552  case COMP_RE_NOMATCH:
1553  if (st->x.ref_val == NULL)
1554  {
1555  nasl_perror (lexic, "nasl_exec: bad regex at or near line %d\n",
1556  st->line_nb);
1557  return NULL;
1558  }
1559  s1 = cell2str (lexic, st->link[0]);
1560  if (s1 == NULL)
1561  return 0;
1562  flag = regexec (st->x.ref_val, s1, 0, NULL, 0);
1563  g_free (s1);
1564  if (st->type == COMP_RE_MATCH)
1565  return bool2cell (flag != REG_NOMATCH);
1566  else
1567  return bool2cell (flag == REG_NOMATCH);
1568 
1569  case COMP_LT:
1570  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) < 0);
1571 
1572  case COMP_LE:
1573  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) <= 0);
1574 
1575  case COMP_EQ:
1576  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) == 0);
1577 
1578  case COMP_NE:
1579  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) != 0);
1580 
1581  case COMP_GT:
1582  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) > 0);
1583 
1584  case COMP_GE:
1585  return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) >= 0);
1586 
1587  case REF_ARRAY:
1588  case DYN_ARRAY:
1589  case CONST_INT:
1590  case CONST_STR:
1591  case CONST_DATA:
1592  ref_cell (st); /* nasl_exec returns a cell that should be deref-ed */
1593  return st;
1594 
1595  case REF_VAR:
1596  ret = nasl_read_var_ref (lexic, st);
1597  return ret;
1598 
1599  default:
1600  nasl_perror (lexic, "nasl_exec: unhandled node type %d\n", st->type);
1601  abort ();
1602  return NULL;
1603  }
1604 }
1605 
1606 extern tree_cell *
1608 
1623 int
1625 {
1626  naslctxt ctx;
1627  nasl_func *pf;
1628  int err = 0, to, process_id;
1629  tree_cell *ret;
1630  lex_ctxt *lexic;
1631  gchar *old_dir;
1632  gchar *newdir;
1633  tree_cell tc;
1634  const char *str, *name = script_infos->name, *oid = script_infos->oid;
1635  gchar *short_name = g_path_get_basename (name);
1636 
1637  nasl_set_plugin_filename (short_name);
1638  g_free (short_name);
1639 
1640  srand48 (getpid () + getppid () + (long) time (NULL));
1641 
1642  old_dir = g_get_current_dir ();
1643 
1644  newdir = g_path_get_dirname (name);
1645 
1646  if (g_chdir (newdir) != 0)
1647  {
1648  g_message ("%s: Not able to open nor to locate it in include paths",
1649  name);
1650  g_free (old_dir);
1651  g_free (newdir);
1652  return -1;
1653  }
1654  g_free (newdir);
1655 
1656  bzero (&ctx, sizeof (ctx));
1657  if (mode & NASL_ALWAYS_SIGNED)
1658  ctx.always_signed = 1;
1659  if (nvticache_initialized ())
1660  ctx.kb = nvticache_get_kb ();
1661  else
1662  ctx.kb = plug_get_kb (script_infos);
1663 
1664  if (init_nasl_ctx (&ctx, name) == 0)
1665  {
1666  if (naslparse (&ctx))
1667  {
1668  g_message ("%s: Parse error at or near line %d", name, ctx.line_nb);
1669  nasl_clean_ctx (&ctx);
1670  g_chdir (old_dir);
1671  g_free (old_dir);
1672  return -1;
1673  }
1674  }
1675  else
1676  {
1677  g_chdir (old_dir);
1678  g_free (old_dir);
1679  return -1;
1680  }
1681 
1682  lexic = init_empty_lex_ctxt ();
1683  lexic->script_infos = script_infos;
1684  lexic->oid = oid;
1686 
1687  str = prefs_get ("checks_read_timeout");
1688  if (str != NULL)
1689  to = atoi (str);
1690  else
1691  to = 5;
1692 
1693  if (to <= 0)
1694  to = 5;
1695 
1696  lexic->recv_timeout = to;
1697 
1698  process_id = getpid ();
1699  if (mode & NASL_LINT)
1700  {
1701  if (nasl_lint (lexic, ctx.tree) == NULL)
1702  err--;
1703  }
1704  else if (!(mode & NASL_EXEC_PARSE_ONLY))
1705  {
1706  char *p;
1707 
1708  bzero (&tc, sizeof (tc));
1709  tc.type = CONST_INT;
1710  tc.x.i_val = (mode & NASL_COMMAND_LINE) != 0;
1711  add_named_var_to_ctxt (lexic, "COMMAND_LINE", &tc);
1712 
1713  bzero (&tc, sizeof (tc));
1714  tc.type = CONST_INT;
1715  tc.x.i_val = (mode & NASL_EXEC_DESCR) != 0;
1716  add_named_var_to_ctxt (lexic, "description", &tc);
1717 
1718  tc.type = CONST_DATA;
1719  p = strrchr (name, '/');
1720  if (p == NULL)
1721  p = (char *) name;
1722  else
1723  p++;
1724  tc.x.str_val = p;
1725  tc.size = strlen (p);
1726  add_named_var_to_ctxt (lexic, "SCRIPT_NAME", &tc);
1727 
1728  truc = (lex_ctxt *) ctx.tree;
1729  if ((ret = nasl_exec (lexic, ctx.tree)) == NULL)
1730  err = -1;
1731  else
1732  deref_cell (ret);
1733 
1734  if ((pf = get_func_ref_by_name (lexic, "on_exit")) != NULL)
1735  nasl_func_call (lexic, pf, NULL);
1736  }
1737 
1738  if (g_chdir (old_dir) != 0)
1739  {
1740  g_free (old_dir);
1741  return -1;
1742  }
1743  g_free (old_dir);
1744 
1745  nasl_clean_ctx (&ctx);
1746  free_lex_ctxt (lexic);
1747  if (process_id != getpid ())
1748  exit (0);
1749 
1750  return err;
1751 }
NODE_LOCAL
@ NODE_LOCAL
Definition: nasl_tree.h:43
EXPR_AND
@ EXPR_AND
Definition: nasl_tree.h:56
nasl_type_name
const char * nasl_type_name(int t)
Definition: nasl_tree.c:357
free_array
void free_array(nasl_array *a)
Definition: nasl_var.c:354
script_infos
Definition: scanneraux.h:43
get_func_ref_by_name
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
Definition: nasl_func.c:94
EXPR_BIT_XOR
@ EXPR_BIT_XOR
Definition: nasl_tree.h:70
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:93
struct_lex_ctxt::line_nb
int line_nb
Definition: nasl_lex_ctxt.h:44
cell2str
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:161
nasl_return
tree_cell * nasl_return(lex_ctxt *ctxt, tree_cell *retv)
Definition: nasl_func.c:239
add_named_var_to_ctxt
named_nasl_var * add_named_var_to_ctxt(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:825
NODE_MINUS_EQ
@ NODE_MINUS_EQ
Definition: nasl_tree.h:47
NODE_WHILE
@ NODE_WHILE
Definition: nasl_tree.h:28
plug_get_kb
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
NODE_DECL
@ NODE_DECL
Definition: nasl_tree.h:34
NODE_RETURN
@ NODE_RETURN
Definition: nasl_tree.h:36
TC::str_val
char * str_val
Definition: nasl_tree.h:112
NODE_GLOBAL
@ NODE_GLOBAL
Definition: nasl_tree.h:44
NODE_AFF
@ NODE_AFF
Definition: nasl_tree.h:41
NODE_DIV_EQ
@ NODE_DIV_EQ
Definition: nasl_tree.h:49
struct_lex_ctxt::cont_flag
unsigned cont_flag
Definition: nasl_lex_ctxt.h:39
NODE_L_SHIFT_EQ
@ NODE_L_SHIFT_EQ
Definition: nasl_tree.h:52
cell_type
int cell_type(const tree_cell *c)
Definition: nasl_tree.c:418
nasl_iterate_array
tree_cell * nasl_iterate_array(nasl_iterator *it)
Definition: nasl_var.c:1214
EXPR_R_USHIFT
@ EXPR_R_USHIFT
Definition: nasl_tree.h:76
CONST_STR
@ CONST_STR
Definition: nasl_tree.h:91
st_n_nasl_var
Definition: nasl_var.h:65
int2cell
static tree_cell * int2cell(long int x)
Definition: exec.c:147
naslctxt
Definition: nasl_global_ctxt.h:26
array2str
char * array2str(const nasl_array *a)
Definition: nasl_var.c:1004
NODE_REPEAT_UNTIL
@ NODE_REPEAT_UNTIL
Definition: nasl_tree.h:30
NODE_ARRAY_EL
@ NODE_ARRAY_EL
Definition: nasl_tree.h:40
script_infos::name
char * name
Definition: scanneraux.h:49
DYN_ARRAY
@ DYN_ARRAY
Definition: nasl_tree.h:101
nasl_exec
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition: exec.c:781
st_nasl_func
Definition: nasl_func.h:25
decl_global_variables
tree_cell * decl_global_variables(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:786
FAKE_CELL
#define FAKE_CELL
Definition: nasl_tree.h:119
TC::x
union TC::@2 x
nasl_read_var_ref
tree_cell * nasl_read_var_ref(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:847
st_n_nasl_var::var_name
char * var_name
Definition: nasl_var.h:69
EXPR_BIT_NOT
@ EXPR_BIT_NOT
Definition: nasl_tree.h:71
NODE_FUN_DEF
@ NODE_FUN_DEF
Definition: nasl_tree.h:32
exec.h
EXPR_U_MINUS
@ EXPR_U_MINUS
Definition: nasl_tree.h:62
nasl_dump_expr
static void nasl_dump_expr(FILE *fp, const tree_cell *c)
Definition: exec.c:373
name
const char * name
Definition: nasl_init.c:377
naslctxt::kb
kb_t kb
Definition: nasl_global_ctxt.h:33
st_nasl_array
Definition: nasl_var.h:43
nasl_lint
tree_cell * nasl_lint(lex_ctxt *, tree_cell *)
Search for errors in a nasl script.
Definition: lint.c:497
naslparse
int naslparse(naslctxt *)
nasl_affect
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
Definition: nasl_var.c:712
NODE_R_SHIFT_EQ
@ NODE_R_SHIFT_EQ
Definition: nasl_tree.h:53
EXPR_MODULO
@ EXPR_MODULO
Definition: nasl_tree.h:65
NASL_EXEC_DESCR
#define NASL_EXEC_DESCR
Definition: nasl.h:57
cell2atom
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition: exec.c:205
oid
const char * oid
Definition: nasl_builtin_find_service.c:57
NODE_FUN_CALL
@ NODE_FUN_CALL
Definition: nasl_tree.h:33
nasl_debug.h
COMP_LE
@ COMP_LE
Definition: nasl_tree.h:84
NODE_FOREACH
@ NODE_FOREACH
Definition: nasl_tree.h:29
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
EXPR_DECR
@ EXPR_DECR
Definition: nasl_tree.h:73
decl_nasl_func
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
Definition: nasl_func.c:78
struct_lex_ctxt::break_flag
unsigned break_flag
Definition: nasl_lex_ctxt.h:38
NODE_VAR
@ NODE_VAR
Definition: nasl_tree.h:42
bool2cell
static tree_cell * bool2cell(long int x)
Definition: exec.c:155
TC::size
int size
Definition: nasl_tree.h:109
cell2intW
static long int cell2intW(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:141
EXPR_PLUS
@ EXPR_PLUS
Definition: nasl_tree.h:60
NODE_INSTR_L
@ NODE_INSTR_L
Definition: nasl_tree.h:26
nasl_lex_ctxt.h
nasl_array_iterator
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
Definition: nasl_var.c:1178
TC::line_nb
short line_nb
Definition: nasl_tree.h:107
nasl_clean_ctx
void nasl_clean_ctx(naslctxt *)
Definition: nasl_grammar.tab.c:2996
nasl_trace_fp
FILE * nasl_trace_fp
Definition: exec.c:368
NODE_REPEATED
@ NODE_REPEATED
Definition: nasl_tree.h:31
EXPR_R_SHIFT
@ EXPR_R_SHIFT
Definition: nasl_tree.h:75
EXPR_NOT
@ EXPR_NOT
Definition: nasl_tree.h:58
struct_lex_ctxt::oid
const char * oid
Definition: nasl_lex_ctxt.h:42
free_lex_ctxt
void free_lex_ctxt(lex_ctxt *c)
Definition: nasl_lex_ctxt.c:55
nasl.h
EXPR_DIV
@ EXPR_DIV
Definition: nasl_tree.h:64
script_infos::oid
char * oid
Definition: scanneraux.h:48
nasl_func.h
TC::ref_val
void * ref_val
Definition: nasl_tree.h:114
init_nasl_ctx
int init_nasl_ctx(naslctxt *, const char *)
Initialize a NASL context for a NASL file.
Definition: nasl_grammar.tab.c:2894
COMP_RE_NOMATCH
@ COMP_RE_NOMATCH
Definition: nasl_tree.h:81
struct_lex_ctxt::script_infos
struct script_infos * script_infos
Definition: nasl_lex_ctxt.h:41
nasl_short_dump
static void nasl_short_dump(FILE *fp, const tree_cell *c)
Definition: exec.c:662
alloc_expr_cell
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition: nasl_tree.c:74
cell2int3
static long int cell2int3(lex_ctxt *lexic, tree_cell *c, int warn, named_nasl_var *v)
Definition: exec.c:87
NASL_COMMAND_LINE
#define NASL_COMMAND_LINE
Definition: nasl.h:60
get_array_elem
tree_cell * get_array_elem(lex_ctxt *, const char *, tree_cell *)
Definition: nasl_var.c:223
EXPR_L_SHIFT
@ EXPR_L_SHIFT
Definition: nasl_tree.h:74
EXPR_BIT_AND
@ EXPR_BIT_AND
Definition: nasl_tree.h:68
TC
Definition: nasl_tree.h:104
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:33
COMP_MATCH
@ COMP_MATCH
Definition: nasl_tree.h:78
TC::type
short type
Definition: nasl_tree.h:106
TC::link
struct TC * link[4]
Definition: nasl_tree.h:116
naslctxt::tree
tree_cell * tree
Definition: nasl_global_ctxt.h:31
nasl_var.h
naslctxt::always_signed
int always_signed
Definition: nasl_global_ctxt.h:29
cell_cmp
long int cell_cmp(lex_ctxt *lexic, tree_cell *c1, tree_cell *c2)
Definition: exec.c:229
ref_cell
void ref_cell(tree_cell *c)
Definition: nasl_tree.c:178
NODE_R_USHIFT_EQ
@ NODE_R_USHIFT_EQ
Definition: nasl_tree.h:54
nasl_set_plugin_filename
void nasl_set_plugin_filename(const char *filename)
Set the current launched plugin filename.
Definition: nasl_debug.c:63
COMP_GE
@ COMP_GE
Definition: nasl_tree.h:88
NODE_MULT_EQ
@ NODE_MULT_EQ
Definition: nasl_tree.h:48
EXPR_MINUS
@ EXPR_MINUS
Definition: nasl_tree.h:61
REF_VAR
@ REF_VAR
Definition: nasl_tree.h:99
nasl_global_ctxt.h
EXPR_INCR
@ EXPR_INCR
Definition: nasl_tree.h:72
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:90
nasl_func_call
tree_cell * nasl_func_call(lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
Definition: nasl_func.c:107
val
const char * val
Definition: nasl_init.c:378
COMP_LT
@ COMP_LT
Definition: nasl_tree.h:83
struct_lex_ctxt::recv_timeout
int recv_timeout
Definition: nasl_lex_ctxt.h:43
NODE_IF_ELSE
@ NODE_IF_ELSE
Definition: nasl_tree.h:25
nasl_iterator::a
nasl_array * a
Definition: nasl_var.h:78
COMP_NE
@ COMP_NE
Definition: nasl_tree.h:86
nasl_iterator
Definition: nasl_var.h:76
EXPR_OR
@ EXPR_OR
Definition: nasl_tree.h:57
NODE_CONTINUE
@ NODE_CONTINUE
Definition: nasl_tree.h:38
NODE_FOR
@ NODE_FOR
Definition: nasl_tree.h:27
COMP_RE_MATCH
@ COMP_RE_MATCH
Definition: nasl_tree.h:80
nasl_init.h
NASL_ALWAYS_SIGNED
#define NASL_ALWAYS_SIGNED
Definition: nasl.h:59
nasl_incr_variable
tree_cell * nasl_incr_variable(lex_ctxt *, tree_cell *, int, int)
Definition: nasl_var.c:933
NODE_MODULO_EQ
@ NODE_MODULO_EQ
Definition: nasl_tree.h:50
exec_nasl_script
int exec_nasl_script(struct script_infos *script_infos, int mode)
Execute a NASL script.
Definition: exec.c:1624
REF_ARRAY
@ REF_ARRAY
Definition: nasl_tree.h:100
nasl_set_filename
void nasl_set_filename(const char *filename)
Definition: nasl_debug.c:97
NODE_BREAK
@ NODE_BREAK
Definition: nasl_tree.h:37
cell2int
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:135
NASL_EXEC_PARSE_ONLY
#define NASL_EXEC_PARSE_ONLY
Definition: nasl.h:58
NODE_PLUS_EQ
@ NODE_PLUS_EQ
Definition: nasl_tree.h:46
deref_cell
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:192
truc
lex_ctxt * truc
Definition: exec.c:370
cell2bool
static int cell2bool(lex_ctxt *lexic, tree_cell *c)
Definition: exec.c:55
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
get_variable_by_name
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:191
COMP_NOMATCH
@ COMP_NOMATCH
Definition: nasl_tree.h:79
init_empty_lex_ctxt
lex_ctxt * init_empty_lex_ctxt()
Definition: nasl_lex_ctxt.c:32
NODE_EMPTY
@ NODE_EMPTY
Definition: nasl_tree.h:24
NASL_LINT
#define NASL_LINT
Definition: nasl.h:61
NODE_ARG
@ NODE_ARG
Definition: nasl_tree.h:35
EXPR_EXPO
@ EXPR_EXPO
Definition: nasl_tree.h:66
nasl_tree.h
decl_local_variables
tree_cell * decl_local_variables(lex_ctxt *, tree_cell *)
Definition: nasl_var.c:773
EXPR_BIT_OR
@ EXPR_BIT_OR
Definition: nasl_tree.h:69
EXPR_MULT
@ EXPR_MULT
Definition: nasl_tree.h:63
COMP_EQ
@ COMP_EQ
Definition: nasl_tree.h:85
naslctxt::line_nb
int line_nb
Definition: nasl_global_ctxt.h:28
expo
static long int expo(long int x, long int y)
Definition: exec.c:756
struct_lex_ctxt::ret_val
tree_cell * ret_val
Definition: nasl_lex_ctxt.h:36
TC::i_val
long int i_val
Definition: nasl_tree.h:113
COMP_GT
@ COMP_GT
Definition: nasl_tree.h:87