32 #include <sys/param.h> 35 #include "../misc/plugutils.h" 36 #include "../misc/popen.h" 54 (void) kill (pid, SIGKILL);
61 (void) waitpid (pid, NULL, WNOHANG);
71 int i, j, n, sz, sz2, cd, nice;
72 char **args = NULL, *cmd, *str, *str2, buf[8192];
74 char cwd[MAXPATHLEN], newdir[MAXPATHLEN];
78 nasl_perror (lexic,
"nasl_pread is not reentrant!\n");
84 if (cmd == NULL || a == NULL || (v = a->x.ref_val) == NULL)
87 nasl_perror (lexic,
"pread() usage: cmd:..., argv:...\n");
98 nasl_perror (lexic,
"pread: argv element must be an array (0x%x)\n",
112 strncpy (newdir, cmd,
sizeof (newdir) - 1);
113 p = strrchr (newdir,
'/');
121 strncpy (newdir, p,
sizeof (newdir) - 1);
124 nasl_perror (lexic,
"pread: '%s' not found in $PATH\n", cmd);
129 newdir[
sizeof (newdir) - 1] =
'\0';
131 if (getcwd (cwd,
sizeof (cwd)) == NULL)
133 nasl_perror (lexic,
"pread(): getcwd: %s\n", strerror (errno));
137 if (chdir (newdir) < 0)
139 nasl_perror (lexic,
"pread: could not chdir to %s\n", newdir);
142 if (cmd[0] !=
'/' && strlen (newdir) + strlen (cmd) + 1 <
sizeof (newdir))
144 strcat (newdir,
"/");
145 strcat (newdir, cmd);
151 nasl_perror (lexic,
"pread: named elements in 'cmd' are ignored!\n");
153 args = g_malloc0 (
sizeof (
char **) * (n + 2));
154 for (j = 0, i = 0; i < n; i++)
158 args[j++] = g_strdup (str);
162 old_sig_t = signal (SIGTERM, sig_h);
168 for (i = 0; i < n; i++)
178 while ((n = fread (buf, 1,
sizeof (buf), fp)) > 0 || errno == EINTR)
186 str2 = g_realloc (str, sz2);
188 memcpy (str + sz, buf, n);
191 if (ferror (fp) && errno != EINTR)
192 nasl_perror (lexic,
"nasl_pread: fread(): %s\n", strerror (errno));
199 nasl_perror (lexic,
"pread(): chdir(%s): %s\n", cwd,
208 signal (SIGTERM, old_sig_t);
223 nasl_perror (lexic,
"find_in_path() usage: cmd\n");
244 struct stat lstat_info, fstat_info;
253 nasl_perror (lexic,
"fread: need one argument (file name)\n");
257 if (lstat (fname, &lstat_info) == -1)
261 nasl_perror (lexic,
"fread: %s: %s\n", fname, strerror (errno));
264 fd = open (fname, O_RDONLY | O_EXCL, 0600);
267 nasl_perror (lexic,
"fread: %s: %s\n", fname, strerror (errno));
273 fd = open (fname, O_RDONLY | O_EXCL, 0600);
276 nasl_perror (lexic,
"fread: %s: possible symlink attack!?! %s\n",
277 fname, strerror (errno));
280 if (fstat (fd, &fstat_info) == -1)
283 nasl_perror (lexic,
"fread: %s: possible symlink attack!?! %s\n",
284 fname, strerror (errno));
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)
294 nasl_perror (lexic,
"fread: %s: possible symlink attack!?!\n",
300 fp = fdopen (fd,
"r");
304 nasl_perror (lexic,
"fread: %s: %s\n", fname, strerror (errno));
308 alen = lstat_info.st_size + 1;
309 buf = g_malloc0 (alen);
311 while ((n = fread (buf + len, 1, alen - len, fp)) > 0)
317 p = g_realloc (buf, alen);
325 p = g_realloc (buf, len + 1);
351 nasl_perror (lexic,
"unlink: need one argument (file name)\n");
355 if (unlink (fname) < 0)
357 nasl_perror (lexic,
"unlink(%s): %s\n", fname, strerror (errno));
372 char *content, *fname;
373 struct stat lstat_info, fstat_info;
380 if (content == NULL || fname == NULL)
382 nasl_perror (lexic,
"fwrite: need two arguments 'data' and 'file'\n");
387 if (lstat (fname, &lstat_info) == -1)
391 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
394 fd = open (fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
397 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
403 fd = open (fname, O_WRONLY | O_CREAT, 0600);
406 nasl_perror (lexic,
"fwrite: %s: possible symlink attack!?! %s\n",
407 fname, strerror (errno));
410 if (fstat (fd, &fstat_info) == -1)
413 nasl_perror (lexic,
"fwrite: %s: possible symlink attack!?! %s\n",
414 fname, strerror (errno));
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)
424 nasl_perror (lexic,
"fwrite: %s: possible symlink attack!?!\n",
430 if (ftruncate (fd, 0) == -1)
433 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
436 fp = fdopen (fd,
"w");
440 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
444 for (i = 0; i < len;)
446 x = fwrite (content + i, 1, len - i, fp);
451 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
460 nasl_perror (lexic,
"fwrite: %s: %s\n", fname, strerror (errno));
475 char path[MAXPATHLEN];
477 snprintf (path,
sizeof (path),
"%s/", g_get_tmp_dir ());
478 if (access (path, R_OK | W_OK | X_OK) < 0)
481 "get_tmp_dir(): %s not available - check your OpenVAS installation\n",
512 nasl_perror (lexic,
"file_stat: need one argument (file name)\n");
516 if (stat (fname, &st) < 0)
520 retc->
x.
i_val = (int) st.st_size;
532 struct stat lstat_info, fstat_info;
534 int imode = O_RDONLY;
539 nasl_perror (lexic,
"file_open: need file name argument\n");
546 nasl_perror (lexic,
"file_open: need file mode argument\n");
550 if (strcmp (mode,
"r") == 0)
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;
561 if (lstat (fname, &lstat_info) == -1)
565 nasl_perror (lexic,
"file_open: %s: %s\n", fname, strerror (errno));
568 fd = open (fname, imode, 0600);
571 nasl_perror (lexic,
"file_open: %s: %s\n", fname, strerror (errno));
577 fd = open (fname, imode, 0600);
580 nasl_perror (lexic,
"file_open: %s: possible symlink attack!?! %s\n",
581 fname, strerror (errno));
584 if (fstat (fd, &fstat_info) == -1)
587 nasl_perror (lexic,
"fread: %s: possible symlink attack!?! %s\n",
588 fname, strerror (errno));
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)
598 nasl_perror (lexic,
"fread: %s: possible symlink attack!?!\n",
622 nasl_perror (lexic,
"file_close: need file pointer argument\n");
628 nasl_perror (lexic,
"file_close: %s\n", strerror (errno));
653 nasl_perror (lexic,
"file_read: need file pointer argument\n");
659 buf = g_malloc0 (flength + 1);
661 for (n = 0; n < flength;)
665 e = read (fd, buf + n, flength - n);
666 if (e < 0 && errno == EINTR)
695 if (content == NULL || fd < 0)
697 nasl_perror (lexic,
"file_write: need two arguments 'fp' and 'data'\n");
703 for (n = 0; n < len;)
707 e = write (fd, content + n, len - n);
708 if (e < 0 && errno == EINTR)
712 nasl_perror (lexic,
"file_write: write() failed - %s\n",
741 nasl_perror (lexic,
"file_seek: need one arguments 'fp'\n");
745 if (lseek (fd, foffset, SEEK_SET) < 0)
747 nasl_perror (lexic,
"fseek: %s\n", strerror (errno));
FILE * openvas_popen4(const char *cmd, char *const args[], pid_t *ppid, int inice)
tree_cell * nasl_fread(lex_ctxt *lexic)
Read file.
tree_cell * nasl_file_close(lex_ctxt *lexic)
Close file.
tree_cell * nasl_file_write(lex_ctxt *lexic)
Write file.
union st_a_nasl_var::@9 v
void deref_cell(tree_cell *c)
long int get_int_local_var_by_name(lex_ctxt *, const char *, int)
char * get_str_local_var_by_name(lex_ctxt *, const char *)
tree_cell * nasl_fwrite(lex_ctxt *lexic)
Write file.
tree_cell * alloc_typed_cell(int typ)
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
tree_cell * nasl_file_seek(lex_ctxt *lexic)
Seek in file.
int openvas_pclose(FILE *fp, pid_t pid)
struct st_a_nasl_var ** num_elt
tree_cell * nasl_pread(lex_ctxt *lexic)
static void(*)(*) old_sig_i()
char * find_in_path(char *name, int safe)
tree_cell * nasl_get_tmp_dir(lex_ctxt *lexic)
long int get_int_var_by_num(lex_ctxt *, int, int)
tree_cell * nasl_file_open(lex_ctxt *lexic)
Open file.
void nasl_perror(lex_ctxt *lexic, char *msg,...)
char * get_str_var_by_num(lex_ctxt *, int)
tree_cell * nasl_find_in_path(lex_ctxt *lexic)
tree_cell * nasl_unlink(lex_ctxt *lexic)
Unlink file.
static void(*)(*) old_sig_c
struct st_n_nasl_var ** hash_elt
const char * var2str(const anon_nasl_var *v)
tree_cell * nasl_file_read(lex_ctxt *lexic)
Read file.
tree_cell * nasl_file_stat(lex_ctxt *lexic)
Stat file.
int get_var_size_by_name(lex_ctxt *, const char *)