OpenVAS Libraries  9.0.3
nasl_cmd_exec.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include "../misc/plugutils.h"
#include "../misc/popen.h"
#include "nasl_tree.h"
#include "nasl_global_ctxt.h"
#include "nasl_func.h"
#include "nasl_var.h"
#include "nasl_lex_ctxt.h"
#include "nasl_cmd_exec.h"
#include "nasl_debug.h"
Include dependency graph for nasl_cmd_exec.c:

Go to the source code of this file.

Functions

tree_cellnasl_pread (lex_ctxt *lexic)
 
tree_cellnasl_find_in_path (lex_ctxt *lexic)
 
tree_cellnasl_fread (lex_ctxt *lexic)
 Read file. More...
 
tree_cellnasl_unlink (lex_ctxt *lexic)
 Unlink file. More...
 
tree_cellnasl_fwrite (lex_ctxt *lexic)
 Write file. More...
 
tree_cellnasl_get_tmp_dir (lex_ctxt *lexic)
 
tree_cellnasl_file_stat (lex_ctxt *lexic)
 Stat file. More...
 
tree_cellnasl_file_open (lex_ctxt *lexic)
 Open file. More...
 
tree_cellnasl_file_close (lex_ctxt *lexic)
 Close file. More...
 
tree_cellnasl_file_read (lex_ctxt *lexic)
 Read file. More...
 
tree_cellnasl_file_write (lex_ctxt *lexic)
 Write file. More...
 
tree_cellnasl_file_seek (lex_ctxt *lexic)
 Seek in file. More...
 

Variables

static void(*)(*) old_sig_i () = NULL
 
static void(*)(*) old_sig_c = NULL
 

Function Documentation

◆ nasl_file_close()

tree_cell* nasl_file_close ( lex_ctxt lexic)

Close file.

Definition at line 614 of file nasl_cmd_exec.c.

615 {
616  tree_cell *retc;
617  int fd;
618 
619  fd = get_int_var_by_num (lexic, 0, -1);
620  if (fd < 0)
621  {
622  nasl_perror (lexic, "file_close: need file pointer argument\n");
623  return NULL;
624  }
625 
626  if (close (fd) < 0)
627  {
628  nasl_perror (lexic, "file_close: %s\n", strerror (errno));
629  return NULL;
630  }
631 
632  retc = alloc_typed_cell (CONST_INT);
633  retc->x.i_val = 0;
634  return retc;
635 }
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1226
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114

References alloc_typed_cell(), CONST_INT, get_int_var_by_num(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_file_open()

tree_cell* nasl_file_open ( lex_ctxt lexic)

Open file.

Definition at line 528 of file nasl_cmd_exec.c.

529 {
530  tree_cell *retc;
531  char *fname, *mode;
532  struct stat lstat_info, fstat_info;
533  int fd;
534  int imode = O_RDONLY;
535 
536  fname = get_str_local_var_by_name (lexic, "name");
537  if (fname == NULL)
538  {
539  nasl_perror (lexic, "file_open: need file name argument\n");
540  return NULL;
541  }
542 
543  mode = get_str_local_var_by_name (lexic, "mode");
544  if (mode == NULL)
545  {
546  nasl_perror (lexic, "file_open: need file mode argument\n");
547  return NULL;
548  }
549 
550  if (strcmp (mode, "r") == 0)
551  imode = O_RDONLY;
552  else if (strcmp (mode, "w") == 0)
553  imode = O_WRONLY | O_CREAT;
554  else if (strcmp (mode, "w+") == 0)
555  imode = O_WRONLY | O_TRUNC | O_CREAT;
556  else if (strcmp (mode, "a") == 0)
557  imode = O_WRONLY | O_APPEND | O_CREAT;
558  else if (strcmp (mode, "a+") == 0)
559  imode = O_RDWR | O_APPEND | O_CREAT;
560 
561  if (lstat (fname, &lstat_info) == -1)
562  {
563  if (errno != ENOENT)
564  {
565  nasl_perror (lexic, "file_open: %s: %s\n", fname, strerror (errno));
566  return NULL;
567  }
568  fd = open (fname, imode, 0600);
569  if (fd < 0)
570  {
571  nasl_perror (lexic, "file_open: %s: %s\n", fname, strerror (errno));
572  return NULL;
573  }
574  }
575  else
576  {
577  fd = open (fname, imode, 0600);
578  if (fd < 0)
579  {
580  nasl_perror (lexic, "file_open: %s: possible symlink attack!?! %s\n",
581  fname, strerror (errno));
582  return NULL;
583  }
584  if (fstat (fd, &fstat_info) == -1)
585  {
586  close (fd);
587  nasl_perror (lexic, "fread: %s: possible symlink attack!?! %s\n",
588  fname, strerror (errno));
589  return NULL;
590  }
591  else
592  {
593  if (lstat_info.st_mode != fstat_info.st_mode
594  || lstat_info.st_ino != fstat_info.st_ino
595  || lstat_info.st_dev != fstat_info.st_dev)
596  {
597  close (fd);
598  nasl_perror (lexic, "fread: %s: possible symlink attack!?!\n",
599  fname);
600  return NULL;
601  }
602  }
603  }
604 
605  retc = alloc_typed_cell (CONST_INT);
606  retc->x.i_val = fd;
607  return retc;
608 }
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114

References alloc_typed_cell(), CONST_INT, get_str_local_var_by_name(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_file_read()

tree_cell* nasl_file_read ( lex_ctxt lexic)

Read file.

Definition at line 642 of file nasl_cmd_exec.c.

643 {
644  tree_cell *retc;
645  char *buf;
646  int fd;
647  int flength;
648  int n;
649 
650  fd = get_int_local_var_by_name (lexic, "fp", -1);
651  if (fd < 0)
652  {
653  nasl_perror (lexic, "file_read: need file pointer argument\n");
654  return NULL;
655  }
656 
657  flength = get_int_local_var_by_name (lexic, "length", 0);
658 
659  buf = g_malloc0 (flength + 1);
660 
661  for (n = 0; n < flength;)
662  {
663  int e;
664  errno = 0;
665  e = read (fd, buf + n, flength - n);
666  if (e < 0 && errno == EINTR)
667  continue;
668  else if (e <= 0)
669  break;
670  else
671  n += e;
672  }
673 
674  retc = alloc_typed_cell (CONST_DATA);
675  retc->size = n;
676  retc->x.str_val = buf;
677  return retc;
678 }
char * str_val
Definition: nasl_tree.h:113
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int size
Definition: nasl_tree.h:110

References alloc_typed_cell(), CONST_DATA, get_int_local_var_by_name(), nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_file_seek()

tree_cell* nasl_file_seek ( lex_ctxt lexic)

Seek in file.

Definition at line 731 of file nasl_cmd_exec.c.

732 {
733  tree_cell *retc;
734  int fd;
735  int foffset;
736 
737  foffset = get_int_local_var_by_name (lexic, "offset", 0);
738  fd = get_int_local_var_by_name (lexic, "fp", -1);
739  if (fd < 0)
740  {
741  nasl_perror (lexic, "file_seek: need one arguments 'fp'\n");
742  return NULL;
743  }
744 
745  if (lseek (fd, foffset, SEEK_SET) < 0)
746  {
747  nasl_perror (lexic, "fseek: %s\n", strerror (errno));
748  return NULL;
749  }
750 
751  retc = alloc_typed_cell (CONST_INT);
752  retc->x.i_val = 0;
753  return retc;
754 }
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114

References alloc_typed_cell(), CONST_INT, get_int_local_var_by_name(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_file_stat()

tree_cell* nasl_file_stat ( lex_ctxt lexic)

Stat file.

Definition at line 503 of file nasl_cmd_exec.c.

504 {
505  tree_cell *retc;
506  char *fname;
507  struct stat st;
508 
509  fname = get_str_var_by_num (lexic, 0);
510  if (fname == NULL)
511  {
512  nasl_perror (lexic, "file_stat: need one argument (file name)\n");
513  return NULL;
514  }
515 
516  if (stat (fname, &st) < 0)
517  return NULL;
518 
519  retc = alloc_typed_cell (CONST_INT);
520  retc->x.i_val = (int) st.st_size;
521  return retc;
522 }
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
long int i_val
Definition: nasl_tree.h:114

References alloc_typed_cell(), CONST_INT, get_str_var_by_num(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_file_write()

tree_cell* nasl_file_write ( lex_ctxt lexic)

Write file.

Definition at line 685 of file nasl_cmd_exec.c.

686 {
687  tree_cell *retc;
688  char *content;
689  int len;
690  int fd;
691  int n;
692 
693  content = get_str_local_var_by_name (lexic, "data");
694  fd = get_int_local_var_by_name (lexic, "fp", -1);
695  if (content == NULL || fd < 0)
696  {
697  nasl_perror (lexic, "file_write: need two arguments 'fp' and 'data'\n");
698  return NULL;
699  }
700  len = get_var_size_by_name (lexic, "data");
701 
702 
703  for (n = 0; n < len;)
704  {
705  int e;
706  errno = 0;
707  e = write (fd, content + n, len - n);
708  if (e < 0 && errno == EINTR)
709  continue;
710  else if (e <= 0)
711  {
712  nasl_perror (lexic, "file_write: write() failed - %s\n",
713  strerror (errno));
714  break;
715  }
716  else
717  n += e;
718  }
719 
720 
721 
722  retc = alloc_typed_cell (CONST_INT);
723  retc->x.i_val = n;
724  return retc;
725 }
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1291

References alloc_typed_cell(), CONST_INT, get_int_local_var_by_name(), get_str_local_var_by_name(), get_var_size_by_name(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_find_in_path()

tree_cell* nasl_find_in_path ( lex_ctxt lexic)

Definition at line 215 of file nasl_cmd_exec.c.

216 {
217  tree_cell *retc;
218  char *cmd;
219 
220  cmd = get_str_var_by_num (lexic, 0);
221  if (cmd == NULL)
222  {
223  nasl_perror (lexic, "find_in_path() usage: cmd\n");
224  return NULL;
225  }
226 
227  retc = alloc_typed_cell (CONST_INT);
228  retc->x.i_val = (find_in_path (cmd, 0) != NULL);
229  return retc;
230 }
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
char * find_in_path(char *name, int safe)
Definition: plugutils.c:1041
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
long int i_val
Definition: nasl_tree.h:114

References alloc_typed_cell(), CONST_INT, find_in_path(), get_str_var_by_num(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_fread()

tree_cell* nasl_fread ( lex_ctxt lexic)

Read file.

Definition at line 240 of file nasl_cmd_exec.c.

241 {
242  tree_cell *retc;
243  char *fname;
244  struct stat lstat_info, fstat_info;
245  int fd;
246  char *buf, *p;
247  int alen, len, n;
248  FILE *fp;
249 
250  fname = get_str_var_by_num (lexic, 0);
251  if (fname == NULL)
252  {
253  nasl_perror (lexic, "fread: need one argument (file name)\n");
254  return NULL;
255  }
256 
257  if (lstat (fname, &lstat_info) == -1)
258  {
259  if (errno != ENOENT)
260  {
261  nasl_perror (lexic, "fread: %s: %s\n", fname, strerror (errno));
262  return NULL;
263  }
264  fd = open (fname, O_RDONLY | O_EXCL, 0600);
265  if (fd < 0)
266  {
267  nasl_perror (lexic, "fread: %s: %s\n", fname, strerror (errno));
268  return NULL;
269  }
270  }
271  else
272  {
273  fd = open (fname, O_RDONLY | O_EXCL, 0600);
274  if (fd < 0)
275  {
276  nasl_perror (lexic, "fread: %s: possible symlink attack!?! %s\n",
277  fname, strerror (errno));
278  return NULL;
279  }
280  if (fstat (fd, &fstat_info) == -1)
281  {
282  close (fd);
283  nasl_perror (lexic, "fread: %s: possible symlink attack!?! %s\n",
284  fname, strerror (errno));
285  return NULL;
286  }
287  else
288  {
289  if (lstat_info.st_mode != fstat_info.st_mode
290  || lstat_info.st_ino != fstat_info.st_ino
291  || lstat_info.st_dev != fstat_info.st_dev)
292  {
293  close (fd);
294  nasl_perror (lexic, "fread: %s: possible symlink attack!?!\n",
295  fname);
296  return NULL;
297  }
298  }
299  }
300  fp = fdopen (fd, "r");
301  if (fp == NULL)
302  {
303  close (fd);
304  nasl_perror (lexic, "fread: %s: %s\n", fname, strerror (errno));
305  return NULL;
306  }
307 
308  alen = lstat_info.st_size + 1;
309  buf = g_malloc0 (alen);
310  len = 0;
311  while ((n = fread (buf + len, 1, alen - len, fp)) > 0)
312  {
313  len += n;
314  if (alen <= len)
315  {
316  alen += 4096;
317  p = g_realloc (buf, alen);
318  buf = p;
319  }
320  }
321 
322  buf[len] = '\0';
323  if (alen > len + 1)
324  {
325  p = g_realloc (buf, len + 1);
326  buf = p;
327  }
328 
329  retc = alloc_typed_cell (CONST_DATA);
330  retc->size = len;
331  retc->x.str_val = buf;
332  fclose (fp);
333  return retc;
334 }
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248
int size
Definition: nasl_tree.h:110

References alloc_typed_cell(), CONST_DATA, get_str_var_by_num(), nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_fwrite()

tree_cell* nasl_fwrite ( lex_ctxt lexic)

Write file.

Definition at line 369 of file nasl_cmd_exec.c.

370 {
371  tree_cell *retc;
372  char *content, *fname;
373  struct stat lstat_info, fstat_info;
374  int fd;
375  int len, i, x;
376  FILE *fp;
377 
378  content = get_str_local_var_by_name (lexic, "data");
379  fname = get_str_local_var_by_name (lexic, "file");
380  if (content == NULL || fname == NULL)
381  {
382  nasl_perror (lexic, "fwrite: need two arguments 'data' and 'file'\n");
383  return NULL;
384  }
385  len = get_var_size_by_name (lexic, "data");
386 
387  if (lstat (fname, &lstat_info) == -1)
388  {
389  if (errno != ENOENT)
390  {
391  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
392  return NULL;
393  }
394  fd = open (fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
395  if (fd < 0)
396  {
397  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
398  return NULL;
399  }
400  }
401  else
402  {
403  fd = open (fname, O_WRONLY | O_CREAT, 0600);
404  if (fd < 0)
405  {
406  nasl_perror (lexic, "fwrite: %s: possible symlink attack!?! %s\n",
407  fname, strerror (errno));
408  return NULL;
409  }
410  if (fstat (fd, &fstat_info) == -1)
411  {
412  close (fd);
413  nasl_perror (lexic, "fwrite: %s: possible symlink attack!?! %s\n",
414  fname, strerror (errno));
415  return NULL;
416  }
417  else
418  {
419  if (lstat_info.st_mode != fstat_info.st_mode
420  || lstat_info.st_ino != fstat_info.st_ino
421  || lstat_info.st_dev != fstat_info.st_dev)
422  {
423  close (fd);
424  nasl_perror (lexic, "fwrite: %s: possible symlink attack!?!\n",
425  fname);
426  return NULL;
427  }
428  }
429  }
430  if (ftruncate (fd, 0) == -1)
431  {
432  close (fd);
433  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
434  return NULL;
435  }
436  fp = fdopen (fd, "w");
437  if (fp == NULL)
438  {
439  close (fd);
440  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
441  return NULL;
442  }
443 
444  for (i = 0; i < len;)
445  {
446  x = fwrite (content + i, 1, len - i, fp);
447  if (x > 0)
448  i += x;
449  else
450  {
451  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
452  (void) fclose (fp);
453  unlink (fname);
454  return NULL;
455  }
456  }
457 
458  if (fclose (fp) < 0)
459  {
460  nasl_perror (lexic, "fwrite: %s: %s\n", fname, strerror (errno));
461  unlink (fname);
462  return NULL;
463  }
464  retc = alloc_typed_cell (CONST_INT);
465  retc->x.i_val = len;
466  return retc;
467 }
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
long int i_val
Definition: nasl_tree.h:114
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1291

References alloc_typed_cell(), CONST_INT, get_str_local_var_by_name(), get_var_size_by_name(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_get_tmp_dir()

tree_cell* nasl_get_tmp_dir ( lex_ctxt lexic)

Definition at line 472 of file nasl_cmd_exec.c.

473 {
474  tree_cell *retc;
475  char path[MAXPATHLEN];
476 
477  snprintf (path, sizeof (path), "%s/", g_get_tmp_dir ());
478  if (access (path, R_OK | W_OK | X_OK) < 0)
479  {
480  nasl_perror (lexic,
481  "get_tmp_dir(): %s not available - check your OpenVAS installation\n",
482  path);
483  return NULL;
484  }
485 
486  retc = alloc_typed_cell (CONST_DATA);
487  retc->x.str_val = strdup (path);
488  retc->size = strlen (retc->x.str_val);
489 
490  return retc;
491 }
char * str_val
Definition: nasl_tree.h:113
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
Definition: nasl_tree.h:105
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
int size
Definition: nasl_tree.h:110

References alloc_typed_cell(), CONST_DATA, nasl_perror(), TC::size, TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_pread()

tree_cell* nasl_pread ( lex_ctxt lexic)
Todo:
Supspects to glib replacements, all path related stuff.

Definition at line 66 of file nasl_cmd_exec.c.

67 {
68  tree_cell *retc = NULL, *a;
69  anon_nasl_var *v;
70  nasl_array *av;
71  int i, j, n, sz, sz2, cd, nice;
72  char **args = NULL, *cmd, *str, *str2, buf[8192];
73  FILE *fp;
74  char cwd[MAXPATHLEN], newdir[MAXPATHLEN];
75 
76  if (pid != 0)
77  {
78  nasl_perror (lexic, "nasl_pread is not reentrant!\n");
79  return NULL;
80  }
81 
82  a = get_variable_by_name (lexic, "argv");
83  cmd = get_str_local_var_by_name (lexic, "cmd");
84  if (cmd == NULL || a == NULL || (v = a->x.ref_val) == NULL)
85  {
86  deref_cell (a);
87  nasl_perror (lexic, "pread() usage: cmd:..., argv:...\n");
88  return NULL;
89  }
90  deref_cell (a);
91 
92  nice = get_int_local_var_by_name (lexic, "nice", 0);
93 
94  if (v->var_type == VAR2_ARRAY)
95  av = &v->v.v_arr;
96  else
97  {
98  nasl_perror (lexic, "pread: argv element must be an array (0x%x)\n",
99  v->var_type);
100  return NULL;
101  }
102 
103  cd = get_int_local_var_by_name (lexic, "cd", 0);
104 
105  cwd[0] = '\0';
106  if (cd)
107  {
108  char *p;
109 
110  if (cmd[0] == '/')
111  {
112  strncpy (newdir, cmd, sizeof (newdir) - 1);
113  p = strrchr (newdir, '/');
114  if (p != newdir)
115  *p = '\0';
116  }
117  else
118  {
119  p = find_in_path (cmd, 0);
120  if (p != NULL)
121  strncpy (newdir, p, sizeof (newdir) - 1);
122  else
123  {
124  nasl_perror (lexic, "pread: '%s' not found in $PATH\n", cmd);
125  return NULL;
126  }
127 
128  }
129  newdir[sizeof (newdir) - 1] = '\0';
130 
131  if (getcwd (cwd, sizeof (cwd)) == NULL)
132  {
133  nasl_perror (lexic, "pread(): getcwd: %s\n", strerror (errno));
134  *cwd = '\0';
135  }
136 
137  if (chdir (newdir) < 0)
138  {
139  nasl_perror (lexic, "pread: could not chdir to %s\n", newdir);
140  return NULL;
141  }
142  if (cmd[0] != '/' && strlen (newdir) + strlen (cmd) + 1 < sizeof (newdir))
143  {
144  strcat (newdir, "/");
145  strcat (newdir, cmd);
146  cmd = newdir;
147  }
148  }
149 
150  if (av->hash_elt != NULL)
151  nasl_perror (lexic, "pread: named elements in 'cmd' are ignored!\n");
152  n = av->max_idx;
153  args = g_malloc0 (sizeof (char **) * (n + 2)); /* Last arg is NULL */
154  for (j = 0, i = 0; i < n; i++)
155  {
156  str = (char *) var2str (av->num_elt[i]);
157  if (str != NULL)
158  args[j++] = g_strdup (str);
159  }
160  args[j++] = NULL;
161 
162  old_sig_t = signal (SIGTERM, sig_h);
163  old_sig_i = signal (SIGINT, sig_h);
164  old_sig_c = signal (SIGCHLD, sig_c);
165 
166  fp = openvas_popen4 ((const char *) cmd, args, &pid, nice);
167 
168  for (i = 0; i < n; i++)
169  g_free (args[i]);
170  g_free (args);
171 
172  if (fp != NULL)
173  {
174  sz = 0;
175  str = g_malloc0 (1);
176 
177  errno = 0;
178  while ((n = fread (buf, 1, sizeof (buf), fp)) > 0 || errno == EINTR) /* && kill(pid, 0) >= 0) */
179  {
180  if (errno == EINTR)
181  {
182  errno = 0;
183  continue;
184  }
185  sz2 = sz + n;
186  str2 = g_realloc (str, sz2);
187  str = str2;
188  memcpy (str + sz, buf, n);
189  sz = sz2;
190  }
191  if (ferror (fp) && errno != EINTR)
192  nasl_perror (lexic, "nasl_pread: fread(): %s\n", strerror (errno));
193 
194  (void) openvas_pclose (fp, pid);
195  pid = 0;
196 
197  if (*cwd != '\0')
198  if (chdir (cwd) < 0)
199  nasl_perror (lexic, "pread(): chdir(%s): %s\n", cwd,
200  strerror (errno));
201 
202  retc = alloc_typed_cell (CONST_DATA);
203  retc->x.str_val = str;
204  retc->size = sz;
205  }
206 
207  signal (SIGINT, old_sig_i);
208  signal (SIGTERM, old_sig_t);
209  signal (SIGCHLD, old_sig_c);
210 
211  return retc;
212 }
FILE * openvas_popen4(const char *cmd, char *const args[], pid_t *ppid, int inice)
Definition: popen.c:36
union st_a_nasl_var::@9 v
char * str_val
Definition: nasl_tree.h:113
void deref_cell(tree_cell *c)
Definition: nasl_tree.c:202
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1240
nasl_array v_arr
Definition: nasl_var.h:62
char * get_str_local_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1262
union TC::@7 x
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:53
int var_type
Definition: nasl_var.h:54
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:206
Definition: nasl_tree.h:105
int openvas_pclose(FILE *fp, pid_t pid)
Definition: popen.c:136
struct st_a_nasl_var ** num_elt
Definition: nasl_var.h:44
static void(*)(*) old_sig_i()
Definition: nasl_cmd_exec.c:48
char * find_in_path(char *name, int safe)
Definition: plugutils.c:1041
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
static void(*)(*) old_sig_c
Definition: nasl_cmd_exec.c:48
struct st_n_nasl_var ** hash_elt
Definition: nasl_var.h:45
const char * var2str(const anon_nasl_var *v)
Definition: nasl_var.c:1189
int size
Definition: nasl_tree.h:110

◆ nasl_unlink()

tree_cell* nasl_unlink ( lex_ctxt lexic)

Unlink file.

Definition at line 344 of file nasl_cmd_exec.c.

345 {
346  char *fname;
347 
348  fname = get_str_var_by_num (lexic, 0);
349  if (fname == NULL)
350  {
351  nasl_perror (lexic, "unlink: need one argument (file name)\n");
352  return NULL;
353  }
354 
355  if (unlink (fname) < 0)
356  {
357  nasl_perror (lexic, "unlink(%s): %s\n", fname, strerror (errno));
358  return NULL;
359  }
360  /* No need to return a value */
361  return FAKE_CELL;
362 }
#define FAKE_CELL
Definition: nasl_tree.h:120
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:94
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1248

References FAKE_CELL, get_str_var_by_num(), and nasl_perror().

Here is the call graph for this function:

Variable Documentation

◆ old_sig_c

void(*)(*) old_sig_c = NULL

Definition at line 48 of file nasl_cmd_exec.c.

◆ old_sig_i

void(*)(*) old_sig_i() = NULL

Definition at line 48 of file nasl_cmd_exec.c.