OpenVAS Scanner  5.1.3
nasl_plugins.c
Go to the documentation of this file.
1 /* OpenVAS
2 * $Id$
3 * Description: Launches NASL plugins.
4 *
5 * Authors: - Renaud Deraison <deraison@nessus.org> (Original pre-fork develoment)
6 * - Tim Brown <mailto:timb@openvas.org> (Initial fork)
7 * - Laban Mwangi <mailto:labanm@openvas.org> (Renaming work)
8 * - Tarik El-Yassem <mailto:tarik@openvas.org> (Headers section)
9 *
10 * Copyright:
11 * Portions Copyright (C) 2006 Software in the Public Interest, Inc.
12 * Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2,
16 * as published by the Free Software Foundation
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27 
32 #include <errno.h>
33 #include <unistd.h> /* for close() */
34 #include <string.h> /* for strlen() */
35 #include <sys/stat.h>
36 
37 #include <glib.h>
38 
39 #include <sys/types.h>
40 #include <utime.h>
41 
42 #include <openvas/base/drop_privileges.h> /* for drop_privileges */
43 #include <openvas/base/nvticache.h> /* for nvticache_add */
44 #include <openvas/nasl/nasl.h>
45 #include <openvas/misc/network.h> /* for internal_send */
46 #include <openvas/misc/nvt_categories.h> /* for ACT_SCANNER */
47 #include <openvas/misc/plugutils.h> /* for plug_set_launch */
48 #include <openvas/misc/internal_com.h> /* for INTERNAL_COMM_CTRL_FINISHED */
49 #include <openvas/misc/openvas_proctitle.h>
50 #include <openvas/misc/prefs.h> /* for prefs_get_bool */
51 
52 #include "pluginload.h"
53 #include "pluginscheduler.h"
54 #include "pluginlaunch.h"
55 #include "processes.h"
56 #include "log.h"
57 
72 int
73 nasl_plugin_add (char *folder, char *filename)
74 {
75  char fullname[PATH_MAX + 1];
76  int nasl_mode;
77  nasl_mode = NASL_EXEC_DESCR;
78 
79  snprintf (fullname, sizeof (fullname), "%s/%s", folder, filename);
80 
81  if (prefs_get_bool ("nasl_no_signature_check"))
82  {
83  nasl_mode |= NASL_ALWAYS_SIGNED;
84  }
85 
86  if (!nvticache_check (filename))
87  {
88  nvti_t *new_nvti;
89  struct arglist *plugin_args;
90 
91  plugin_args = g_malloc0 (sizeof (struct arglist));
92  arg_add_value (plugin_args, "key", ARG_PTR, nvticache_get_kb ());
93  new_nvti = nvti_new ();
94  arg_add_value (plugin_args, "NVTI", ARG_PTR, new_nvti);
95 
96  if (exec_nasl_script (plugin_args, fullname, NULL, nasl_mode) < 0)
97  {
98  log_write ("%s: Could not be loaded", fullname);
99  arg_free_all (plugin_args);
100  return -1;
101  }
102  arg_free_all (plugin_args);
103 
104  // Check mtime of plugin before caching it
105  // Set to now if mtime is in the future
106  struct stat plug_stat;
107  time_t now = time (NULL) - 1;
108  stat (fullname, &plug_stat);
109  if (plug_stat.st_mtime > now)
110  {
111  struct utimbuf fixed_timestamp;
112  fixed_timestamp.actime = now;
113  fixed_timestamp.modtime = now;
114  if (utime (fullname, &fixed_timestamp) == 0)
115  log_write ("The timestamp for %s was from the future. This has been fixed.", fullname);
116  else
117  log_write ("The timestamp for %s is from the future and could not be fixed.", fullname);
118  }
119 
120  if (nvti_oid (new_nvti))
121  nvticache_add (new_nvti, filename);
122  else
123  // Most likely an exit was hit before the description could be parsed.
124  log_write ("\r%s could not be added to the cache and is likely to stay"
125  " invisible to the client.", filename);
126  nvti_free (new_nvti);
127  }
128  return 0;
129 }
130 
132  struct arglist *args;
133  char *name;
134  const char *oid;
135  int soc;
136 };
137 
138 static void
139 nasl_thread (struct nasl_thread_args *);
140 
144 int
145 nasl_plugin_launch (struct arglist *globals, struct host_info *hostinfo,
146  kb_t kb, char *name, const char *oid, int soc)
147 {
148  int module;
149  struct nasl_thread_args nargs;
150  struct arglist *plugin;
151 
152  plugin = g_malloc0 (sizeof (struct arglist));
153  arg_add_value (plugin, "HOSTNAME", ARG_PTR, hostinfo);
154  arg_add_value (plugin, "globals", ARG_ARGLIST, globals);
155  arg_add_value (plugin, "key", ARG_PTR, kb);
156 
157  nargs.args = plugin;
158  nargs.name = name;
159  nargs.oid = oid;
160  nargs.soc = soc;
161 
162  module = create_process ((process_func_t) nasl_thread, &nargs);
163  arg_free (plugin);
164  return module;
165 }
166 
167 static void
168 nasl_thread (struct nasl_thread_args *nargs)
169 {
170  struct arglist *args = nargs->args;
171  struct arglist *globals = arg_get_value (args, "globals");
172  struct host_info *hostinfo = arg_get_value (args, "HOSTNAME");
173  char *name = nargs->name;
174  int nasl_mode = 0;
175  kb_t kb;
176  GError *error = NULL;
177 
178  nvticache_reset ();
179  if (prefs_get_bool ("be_nice"))
180  {
181  int nice_retval;
182  errno = 0;
183  nice_retval = nice (-5);
184  if (nice_retval == -1 && errno != 0)
185  {
186  log_write ("Unable to renice process: %d", errno);
187  }
188  }
189 
191  kb = arg_get_value (args, "key");
192  kb_lnk_reset (kb);
193  arg_set_value (globals, "global_socket", GSIZE_TO_POINTER (nargs->soc));
194  proctitle_set ("openvassd: testing %s (%s)", hostinfo->name, name);
195 
196  if (prefs_get_bool ("nasl_no_signature_check"))
197  nasl_mode |= NASL_ALWAYS_SIGNED;
198 
199  if (prefs_get_bool ("drop_privileges"))
200  {
201  int drop_priv_res = drop_privileges (NULL, &error);
202  if (drop_priv_res != OPENVAS_DROP_PRIVILEGES_OK)
203  {
204  if (drop_priv_res != OPENVAS_DROP_PRIVILEGES_FAIL_NOT_ROOT)
205  log_write ("Failed to drop privileges for %s", name);
206  g_error_free (error);
207  }
208  }
209 
210  exec_nasl_script (args, name, nargs->oid, nasl_mode);
211  internal_send (nargs->soc, NULL,
212  INTERNAL_COMM_MSG_TYPE_CTRL | INTERNAL_COMM_CTRL_FINISHED);
213 }
void log_write(const char *str,...)
Write into the logfile / syslog.
Definition: log.c:140
struct arglist * args
Definition: nasl_plugins.c:132
int nasl_plugin_add(char *folder, char *filename)
The nasl - plugin class. Loads or launches nasl- plugins.
Definition: nasl_plugins.c:73
int nasl_plugin_launch(struct arglist *globals, struct host_info *hostinfo, kb_t kb, char *name, const char *oid, int soc)
Launch a NASL plugin.
Definition: nasl_plugins.c:145
const char * oid
Definition: nasl_plugins.c:134
void pluginlaunch_child_cleanup(void)
Cleanup file descriptors used by the processes array. To be called by the child process running the p...
Definition: pluginlaunch.c:486
void(* process_func_t)(void *)
Definition: processes.h:31
pid_t create_process(process_func_t function, void *argument)
Create a new process (fork).
Definition: processes.c:77