OpenVAS Scanner  5.1.3
utils.c
Go to the documentation of this file.
1 /* OpenVAS
2 * $Id$
3 * Description: A bunch of miscellaneous functions, mostly file conversions.
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 
28 #include <stdlib.h> /* for atoi() */
29 #include <string.h> /* for strchr() */
30 #include <sys/wait.h> /* for waitpid() */
31 #include <errno.h> /* for errno() */
32 #include <sys/ioctl.h> /* for ioctl() */
33 #include <sys/stat.h> /* for stat() */
34 
35 #include <openvas/misc/network.h> /* for stream_zero */
36 #include <openvas/misc/prefs.h> /* for prefs_get() */
37 
38 #include "log.h"
39 #include "comm.h"
40 #include "ntp.h"
41 #include "utils.h"
42 #include "pluginscheduler.h"
43 
44 extern int global_max_hosts;
45 extern int global_max_checks;
46 
47 
53 int
54 common (l1, l2)
55  struct arglist *l1, *l2;
56 {
57  struct arglist *l2_start = l2;
58  if (!l1 || !l2)
59  {
60  return 0;
61  }
62  while (l1->next != NULL)
63  {
64  l2 = l2_start;
65  while (l2->next != NULL)
66  {
67  if (strcmp (l1->name, l2->name) == 0)
68  return 1;
69  l2 = l2->next;
70  }
71  l1 = l1->next;
72  }
73  return 0;
74 }
75 
80 struct arglist *
82  char *list;
83 {
84  struct arglist *ret;
85  char *t;
86 
87  if (!list)
88  return NULL;
89 
90  ret = g_malloc0 (sizeof (struct arglist));
91 
92  while ((t = strchr (list, ',')) != NULL)
93  {
94  t[0] = 0;
95  while (list[0] == ' ')
96  list++;
97  if (list[0] != '\0')
98  {
99  arg_add_value (ret, list, ARG_INT, (void *) 1);
100  }
101  list = t + 1;
102  }
103 
104  while (list[0] == ' ')
105  list++;
106  if (list[0] != '\0')
107  {
108  arg_add_value (ret, list, ARG_INT, (void *) 1);
109  }
110  return ret;
111 }
112 
113 
114 
115 
119 int
121 {
122  int max_hosts;
123  if (prefs_get ("max_hosts"))
124  {
125  max_hosts = atoi (prefs_get ("max_hosts"));
126  if (max_hosts <= 0)
127  {
128  log_write ("Error ! max_hosts = %d -- check %s", max_hosts,
129  (char *) prefs_get ("config_file"));
130  max_hosts = global_max_hosts;
131  }
132  else if (max_hosts > global_max_hosts)
133  {
134  log_write ("Client tried to raise the maximum hosts number - %d."
135  " Using %d. Change 'max_hosts' in openvassd.conf if you"
136  " believe this is incorrect", max_hosts, global_max_hosts);
137  max_hosts = global_max_hosts;
138  }
139  }
140  else
141  max_hosts = global_max_hosts;
142  return (max_hosts);
143 }
144 
149 int
151 {
152  int max_checks;
153  if (prefs_get ("max_checks"))
154  {
155  max_checks = atoi (prefs_get ("max_checks"));
156  if (max_checks <= 0)
157  {
158  log_write ("Error ! max_hosts = %d -- check %s", max_checks,
159  (char *) prefs_get ("config_file"));
160  max_checks = global_max_checks;
161  }
162  else if (max_checks > global_max_checks)
163  {
164  log_write ("Client tried to raise the maximum checks number - %d."
165  " Using %d. Change 'max_checks' in openvassd.conf if you"
166  " believe this is incorrect", max_checks, global_max_checks);
167  max_checks = global_max_checks;
168  }
169  }
170  else
171  max_checks = global_max_checks;
172  return (max_checks);
173 }
174 
175 
179 int
180 process_alive (pid_t pid)
181 {
182  int i, ret;
183  if (pid == 0)
184  return 0;
185 
186  for (i = 0, ret = 1; (i < 10) && (ret > 0); i++)
187  ret = waitpid (pid, NULL, WNOHANG);
188 
189  return kill (pid, 0) == 0;
190 }
191 
192 int
194  int soc;
195 {
196  int data = 0;
197  ioctl (soc, FIONREAD, &data);
198  return data;
199 }
200 
201 void
203 {
204  int e, n = 0;
205  do
206  {
207  errno = 0;
208  e = waitpid (-1, NULL, WNOHANG);
209  n++;
210  }
211  while ((e > 0 || errno == EINTR) && n < 20);
212 }
213 
214 /*
215  * @brief Checks if a provided preference is scanner-only and can't be
216  * read/written by the client.
217  *
218  * @return 1 if pref is scanner-only, 0 otherwise.
219  */
220 int
221 is_scanner_only_pref (const char *pref)
222 {
223  if (pref == NULL)
224  return 0;
225  if (!strcmp (pref, "logfile") || !strcmp (pref, "config_file")
226  || !strcmp (pref, "plugins_folder")
227  || !strcmp (pref, "kb_location")
228  || !strcmp (pref, "dumpfile")
229  || !strcmp (pref, "negot_timeout")
230  || !strcmp (pref, "force_pubkey_auth")
231  || !strcmp (pref, "log_whole_attack")
232  || !strcmp (pref, "be_nice")
233  || !strcmp (pref, "log_plugins_name_at_load")
234  || !strcmp (pref, "nasl_no_signature_check")
235  /* Preferences starting with sys_ are scanner-side only. */
236  || !strncmp (pref, "sys_", 4))
237  return 1;
238  return 0;
239 }
240 
244 static void
245 auth_send (int soc, char *data)
246 {
247  unsigned int sent = 0;
248  gsize length;
249 
250  if (soc < 0)
251  return;
252 
253  /* Convert to UTF-8 before sending to Manager. */
254  data = g_convert (data, -1, "UTF-8", "ISO_8859-1", NULL, &length, NULL);
255  while (sent < length)
256  {
257  int n = nsend (soc, data + sent, length - sent, 0);
258  if (n < 0)
259  {
260  if ((errno != ENOMEM) && (errno != ENOBUFS))
261  {
262  g_free (data);
263  return;
264  }
265  }
266  else
267  sent += n;
268  }
269  g_free (data);
270 }
271 
275 void
276 send_printf (int soc, char *data, ...)
277 {
278  va_list param;
279  char *buffer;
280 
281  va_start (param, data);
282  buffer = g_strdup_vprintf (data, param);
283  va_end (param);
284 
285  auth_send (soc, buffer);
286  g_free (buffer);
287 }
int process_alive(pid_t pid)
Definition: utils.c:180
void log_write(const char *str,...)
Write into the logfile / syslog.
Definition: log.c:140
int global_max_hosts
Definition: openvassd.c:86
void send_printf(int soc, char *data,...)
Writes data to a socket.
Definition: utils.c:276
int is_scanner_only_pref(const char *pref)
Definition: utils.c:221
int get_max_hosts_number(void)
Definition: utils.c:120
int global_max_checks
Definition: openvassd.c:87
int data_left(int soc)
Definition: utils.c:193
int common(struct arglist *l1, struct arglist *l2)
Returns 1 if the two arglists have a name in common.
Definition: utils.c:54
struct arglist * list2arglist(char *list)
Definition: utils.c:81
void wait_for_children1(void)
Definition: utils.c:202
int get_max_checks_number(void)
Definition: utils.c:150