25#include <gvm/base/drop_privileges.h>
26#include <gvm/base/prefs.h>
36#define MAXPATHLEN 4096
46 str = g_string_new (
"");
52 int ret, ret_out = 0, ret_err = 0;
53 int maxfd = fdout > fderr ? fdout : fderr;
59 ret = select (maxfd + 1, &fds, NULL, NULL, NULL);
66 bzero (buf,
sizeof (buf));
67 if (FD_ISSET (fdout, &fds))
69 ret_out = read (fdout, buf,
sizeof (buf));
71 g_string_append (str, buf);
73 if (FD_ISSET (fderr, &fds))
75 ret_err = read (fderr, buf,
sizeof (buf));
77 g_string_append (str, buf);
79 if (ret_out <= 0 && ret_err <= 0)
83 return g_string_free (str, FALSE);
105 int i, j, n, cd, fdout = 0, fderr = 0;
106 char **args = NULL, *cmd, *str, *new_user;
108 GError *error = NULL;
112 nasl_perror (lexic,
"nasl_pread is not reentrant!\n");
117 if (new_user && !prefs_get_bool (
"drop_privileges"))
119 if (drop_privileges (new_user, &error))
123 nasl_perror (lexic,
"%s: %s\n", __func__, error->message);
124 g_error_free (error);
132 if (cmd == NULL || a == NULL || (v = a->x.ref_val) == NULL)
135 nasl_perror (lexic,
"pread() usage: cmd:..., argv:...\n");
144 nasl_perror (lexic,
"pread: argv element must be an array (0x%x)\n",
156 memset (newdir,
'\0',
sizeof (newdir));
158 strncpy (newdir, cmd,
sizeof (newdir) - 1);
161 p = g_find_program_in_path (cmd);
163 strncpy (newdir, p,
sizeof (newdir) - 1);
166 nasl_perror (lexic,
"pread: '%s' not found in $PATH\n", cmd);
171 p = strrchr (newdir,
'/');
172 if (p && p != newdir)
174 if (getcwd (cwd,
sizeof (cwd)) == NULL)
176 nasl_perror (lexic,
"pread(): getcwd: %s\n", strerror (errno));
180 if (chdir (newdir) < 0)
182 nasl_perror (lexic,
"pread: could not chdir to %s\n", newdir);
185 if (cmd[0] !=
'/' && strlen (newdir) + strlen (cmd) + 1 <
sizeof (newdir))
187 strcat (newdir,
"/");
188 strcat (newdir, cmd);
193 nasl_perror (lexic,
"pread: named elements in 'cmd' are ignored!\n");
195 args = g_malloc0 (
sizeof (
char *) * (n + 2));
196 for (j = 0, i = 0; i < n; i++)
200 args[j++] = g_strdup (str);
204 if (g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL,
205 NULL, &
pid, NULL, &fdout, &fderr, &error)
210 g_warning (
"%s: %s", __func__, error->message);
211 g_error_free (error);
221 retc->
size = strlen (str);
223 else if (errno && errno != EINTR)
224 nasl_perror (lexic,
"nasl_pread: fread(): %s\n", strerror (errno));
229 nasl_perror (lexic,
"pread(): chdir(%s): %s\n", cwd, strerror (errno));
232 for (i = 0; i < n; i++)
236 g_spawn_close_pid (
pid);
251 nasl_perror (lexic,
"find_in_path() usage: cmd\n");
256 result = g_find_program_in_path (cmd);
273 char *fname, *fcontent;
275 GError *ferror = NULL;
280 nasl_perror (lexic,
"fread: need one argument (file name)\n");
284 if (!g_file_get_contents (fname, &fcontent, &flen, &ferror))
286 nasl_perror (lexic,
"fread: %s", ferror ? ferror->message :
"Error");
288 g_error_free (ferror);
313 nasl_perror (lexic,
"unlink: need one argument (file name)\n");
317 if (unlink (fname) < 0)
319 nasl_perror (lexic,
"unlink(%s): %s\n", fname, strerror (errno));
334 char *fcontent, *fname;
336 GError *ferror = NULL;
340 if (!fcontent || !fname)
342 nasl_perror (lexic,
"fwrite: need two arguments 'data' and 'file'\n");
347 if (!g_file_set_contents (fname, fcontent, flen, &ferror))
349 nasl_perror (lexic,
"fwrite: %s", ferror ? ferror->message :
"Error");
351 g_error_free (ferror);
365 snprintf (path,
sizeof (path),
"%s/", g_get_tmp_dir ());
366 if (access (path, R_OK | W_OK | X_OK) < 0)
370 "get_tmp_dir(): %s not available - check your OpenVAS installation\n",
400 nasl_perror (lexic,
"file_stat: need one argument (file name)\n");
404 if (stat (fname, &st) < 0)
408 retc->
x.
i_val = (int) st.st_size;
420 struct stat fstat_info;
422 int imode = O_RDONLY;
427 nasl_perror (lexic,
"file_open: need file name argument\n");
434 nasl_perror (lexic,
"file_open: need file mode argument\n");
438 if (strcmp (mode,
"r") == 0)
440 else if (strcmp (mode,
"w") == 0)
441 imode = O_WRONLY | O_CREAT;
442 else if (strcmp (mode,
"w+") == 0)
443 imode = O_WRONLY | O_TRUNC | O_CREAT;
444 else if (strcmp (mode,
"a") == 0)
445 imode = O_WRONLY | O_APPEND | O_CREAT;
446 else if (strcmp (mode,
"a+") == 0)
447 imode = O_RDWR | O_APPEND | O_CREAT;
449 fd = open (fname, imode, 0600);
452 nasl_perror (lexic,
"file_open: %s: possible symlink attack!?! %s\n",
453 fname, strerror (errno));
457 if (fstat (fd, &fstat_info) == -1)
460 nasl_perror (lexic,
"fread: %s: possible symlink attack!?! %s\n", fname,
482 nasl_perror (lexic,
"file_close: need file pointer argument\n");
488 nasl_perror (lexic,
"file_close: %s\n", strerror (errno));
512 nasl_perror (lexic,
"file_read: need file pointer argument\n");
518 buf = g_malloc0 (flength + 1);
520 for (n = 0; n < flength;)
524 e = read (fd, buf + n, flength - n);
525 if (e < 0 && errno == EINTR)
553 if (content == NULL || fd < 0)
555 nasl_perror (lexic,
"file_write: need two arguments 'fp' and 'data'\n");
560 for (n = 0; n <
len;)
564 e = write (fd, content + n,
len - n);
565 if (e < 0 && errno == EINTR)
569 nasl_perror (lexic,
"file_write: write() failed - %s\n",
596 nasl_perror (lexic,
"file_seek: need one arguments 'fp'\n");
600 if (lseek (fd, foffset, SEEK_SET) < 0)
602 nasl_perror (lexic,
"fseek: %s\n", strerror (errno));
tree_cell * nasl_pread(lex_ctxt *lexic)
Spawn a process.
tree_cell * nasl_file_seek(lex_ctxt *lexic)
Seek in file.
tree_cell * nasl_unlink(lex_ctxt *lexic)
Unlink file.
tree_cell * nasl_get_tmp_dir(lex_ctxt *lexic)
tree_cell * nasl_file_stat(lex_ctxt *lexic)
Stat file.
tree_cell * nasl_fwrite(lex_ctxt *lexic)
Write file.
static char * pread_streams(int fdout, int fderr)
tree_cell * nasl_find_in_path(lex_ctxt *lexic)
tree_cell * nasl_file_write(lex_ctxt *lexic)
Write file.
tree_cell * nasl_file_read(lex_ctxt *lexic)
Read file.
tree_cell * nasl_file_open(lex_ctxt *lexic)
Open file.
tree_cell * nasl_file_close(lex_ctxt *lexic)
Close file.
tree_cell * nasl_fread(lex_ctxt *lexic)
Read file.
void nasl_perror(lex_ctxt *lexic, char *msg,...)
struct struct_lex_ctxt lex_ctxt
int get_var_size_by_name(lex_ctxt *, const char *)
char * get_str_var_by_name(lex_ctxt *, const char *)
char * get_str_var_by_num(lex_ctxt *, int)
long int get_int_var_by_num(lex_ctxt *, int, int)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
tree_cell * alloc_typed_cell(int typ)
void deref_cell(tree_cell *c)
const char * var2str(anon_nasl_var *v)
struct st_a_nasl_var anon_nasl_var
struct st_nasl_array nasl_array
Header file for module plugutils.
union TC::@332262321161220155002104006201360276211317150140 x
union st_a_nasl_var::@154137074032032170165360023270032033276061363156 v
struct st_n_nasl_var ** hash_elt
struct st_a_nasl_var ** num_elt