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