26 #include "../misc/plugutils.h" 39 #include <sys/param.h> 46 #define MAXPATHLEN 4096 56 str = g_string_new (
"");
62 int ret, ret_out = 0, ret_err = 0;
63 int maxfd = fdout > fderr ? fdout : fderr;
69 ret = select (maxfd + 1, &fds, NULL, NULL, NULL);
76 bzero (buf,
sizeof (buf));
77 if (FD_ISSET (fdout, &fds))
79 ret_out = read (fdout, buf,
sizeof (buf));
81 g_string_append (str, buf);
83 if (FD_ISSET (fderr, &fds))
85 ret_err = read (fderr, buf,
sizeof (buf));
87 g_string_append (str, buf);
89 if (ret_out <= 0 && ret_err <= 0)
93 return g_string_free (str, FALSE);
103 int i, j, n, cd, fdout = 0, fderr = 0;
104 char **args = NULL, *cmd, *str;
106 GError *error = NULL;
110 nasl_perror (lexic,
"nasl_pread is not reentrant!\n");
116 if (cmd == NULL || a == NULL || (v = a->x.ref_val) == NULL)
119 nasl_perror (lexic,
"pread() usage: cmd:..., argv:...\n");
128 nasl_perror (lexic,
"pread: argv element must be an array (0x%x)\n",
140 memset (newdir,
'\0',
sizeof (newdir));
142 strncpy (newdir, cmd,
sizeof (newdir) - 1);
145 p = g_find_program_in_path (cmd);
147 strncpy (newdir, p,
sizeof (newdir) - 1);
150 nasl_perror (lexic,
"pread: '%s' not found in $PATH\n", cmd);
155 p = strrchr (newdir,
'/');
156 if (p && p != newdir)
158 if (getcwd (cwd,
sizeof (cwd)) == NULL)
160 nasl_perror (lexic,
"pread(): getcwd: %s\n", strerror (errno));
164 if (chdir (newdir) < 0)
166 nasl_perror (lexic,
"pread: could not chdir to %s\n", newdir);
169 if (cmd[0] !=
'/' && strlen (newdir) + strlen (cmd) + 1 <
sizeof (newdir))
171 strcat (newdir,
"/");
172 strcat (newdir, cmd);
177 nasl_perror (lexic,
"pread: named elements in 'cmd' are ignored!\n");
179 args = g_malloc0 (
sizeof (
char *) * (n + 2));
180 for (j = 0, i = 0; i < n; i++)
184 args[j++] = g_strdup (str);
188 if (g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL,
189 NULL, &
pid, NULL, &fdout, &fderr, &error)
194 g_warning (
"%s: %s", __FUNCTION__, error->message);
195 g_error_free (error);
205 retc->
size = strlen (str);
207 else if (errno && errno != EINTR)
208 nasl_perror (lexic,
"nasl_pread: fread(): %s\n", strerror (errno));
213 nasl_perror (lexic,
"pread(): chdir(%s): %s\n", cwd, strerror (errno));
216 for (i = 0; i < n; i++)
220 g_spawn_close_pid (
pid);
235 nasl_perror (lexic,
"find_in_path() usage: cmd\n");
240 result = g_find_program_in_path (cmd);
257 char *fname, *fcontent;
259 GError *ferror = NULL;
264 nasl_perror (lexic,
"fread: need one argument (file name)\n");
268 if (!g_file_get_contents (fname, &fcontent, &flen, &ferror))
270 nasl_perror (lexic,
"fread: %s", ferror ? ferror->message :
"Error");
272 g_error_free (ferror);
297 nasl_perror (lexic,
"unlink: need one argument (file name)\n");
301 if (unlink (fname) < 0)
303 nasl_perror (lexic,
"unlink(%s): %s\n", fname, strerror (errno));
318 char *fcontent, *fname;
320 GError *ferror = NULL;
324 if (!fcontent || !fname)
326 nasl_perror (lexic,
"fwrite: need two arguments 'data' and 'file'\n");
331 if (!g_file_set_contents (fname, fcontent, flen, &ferror))
333 nasl_perror (lexic,
"fwrite: %s", ferror ? ferror->message :
"Error");
335 g_error_free (ferror);
349 snprintf (path,
sizeof (path),
"%s/", g_get_tmp_dir ());
350 if (access (path, R_OK | W_OK | X_OK) < 0)
354 "get_tmp_dir(): %s not available - check your OpenVAS installation\n",
384 nasl_perror (lexic,
"file_stat: need one argument (file name)\n");
388 if (stat (fname, &st) < 0)
392 retc->
x.
i_val = (int) st.st_size;
404 struct stat lstat_info, fstat_info;
406 int imode = O_RDONLY;
411 nasl_perror (lexic,
"file_open: need file name argument\n");
418 nasl_perror (lexic,
"file_open: need file mode argument\n");
422 if (strcmp (mode,
"r") == 0)
424 else if (strcmp (mode,
"w") == 0)
425 imode = O_WRONLY | O_CREAT;
426 else if (strcmp (mode,
"w+") == 0)
427 imode = O_WRONLY | O_TRUNC | O_CREAT;
428 else if (strcmp (mode,
"a") == 0)
429 imode = O_WRONLY | O_APPEND | O_CREAT;
430 else if (strcmp (mode,
"a+") == 0)
431 imode = O_RDWR | O_APPEND | O_CREAT;
433 if (lstat (fname, &lstat_info) == -1)
437 nasl_perror (lexic,
"file_open: %s: %s\n", fname, strerror (errno));
440 fd = open (fname, imode, 0600);
443 nasl_perror (lexic,
"file_open: %s: %s\n", fname, strerror (errno));
449 fd = open (fname, imode, 0600);
452 nasl_perror (lexic,
"file_open: %s: possible symlink attack!?! %s\n",
453 fname, strerror (errno));
456 if (fstat (fd, &fstat_info) == -1)
459 nasl_perror (lexic,
"fread: %s: possible symlink attack!?! %s\n",
460 fname, strerror (errno));
465 if (lstat_info.st_mode != fstat_info.st_mode
466 || lstat_info.st_ino != fstat_info.st_ino
467 || lstat_info.st_dev != fstat_info.st_dev)
470 nasl_perror (lexic,
"fread: %s: possible symlink attack!?!\n",
494 nasl_perror (lexic,
"file_close: need file pointer argument\n");
500 nasl_perror (lexic,
"file_close: %s\n", strerror (errno));
524 nasl_perror (lexic,
"file_read: need file pointer argument\n");
530 buf = g_malloc0 (flength + 1);
532 for (n = 0; n < flength;)
536 e = read (fd, buf + n, flength - n);
537 if (e < 0 && errno == EINTR)
565 if (content == NULL || fd < 0)
567 nasl_perror (lexic,
"file_write: need two arguments 'fp' and 'data'\n");
572 for (n = 0; n < len;)
576 e = write (fd, content + n, len - n);
577 if (e < 0 && errno == EINTR)
581 nasl_perror (lexic,
"file_write: write() failed - %s\n",
608 nasl_perror (lexic,
"file_seek: need one arguments 'fp'\n");
612 if (lseek (fd, foffset, SEEK_SET) < 0)
614 nasl_perror (lexic,
"fseek: %s\n", strerror (errno));
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.
static char * pread_streams(int fdout, int fderr)
void deref_cell(tree_cell *c)
char * get_str_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.
struct st_a_nasl_var ** num_elt
tree_cell * nasl_pread(lex_ctxt *lexic)
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)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
tree_cell * nasl_find_in_path(lex_ctxt *lexic)
tree_cell * nasl_unlink(lex_ctxt *lexic)
Unlink file.
union st_a_nasl_var::@4 v
struct st_n_nasl_var ** hash_elt
const char * var2str(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 *)