Greenbone Security Assistant  7.0.3~git
gsad_omp.c
Go to the documentation of this file.
1 /* Greenbone Security Assistant
2  * $Id$
3  * Description: OMP communication module.
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  * Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
8  * Michael Wiegand <michael.wiegand@greenbone.net>
9  *
10  * Copyright:
11  * Copyright (C) 2009, 2014 Greenbone Networks GmbH
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
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 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <glib.h>
41 #include <netinet/in.h>
42 #include <netdb.h>
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <sys/time.h>
46 #include <arpa/inet.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <assert.h>
50 #include <time.h>
51 #include <sys/time.h>
52 
53 #include <microhttpd.h>
54 
55 #include "gsad_base.h"
56 #include "gsad_omp.h"
57 #include "xslt_i18n.h"
58 
59 #include <openvas/misc/openvas_server.h>
60 #include <openvas/base/openvas_file.h>
61 #include <openvas/base/cvss.h>
62 #include <openvas/omp/omp.h>
63 #include <openvas/omp/xml.h>
64 
65 /*
66  * XSLT includes
67  */
68 #include <libxml2/libxml/xmlmemory.h>
69 #include <libxml2/libxml/HTMLtree.h>
70 #include <libxml2/libxml/xmlIO.h>
71 #include <libxml2/libxml/xinclude.h>
72 #include <libxslt/xslt.h>
73 #include <libxslt/xsltInternals.h>
74 #include <libxslt/transform.h>
75 #include <libxslt/xsltutils.h>
76 
77 #undef G_LOG_DOMAIN
78 
81 #define G_LOG_DOMAIN "gsad omp"
82 
86 #define OPENVASMD_ADDRESS "127.0.0.1"
87 
92 
96 gchar *manager_address = NULL;
97 
101 int manager_port = 9390;
102 
103 
104 /* Headers. */
105 
106 static int
107 omp (openvas_connection_t *, credentials_t *, gchar **, entity_t *,
108  cmd_response_data_t*, const char *);
109 
110 static int
111 ompf (openvas_connection_t *, credentials_t *, gchar **, entity_t *,
112  cmd_response_data_t*, const char *, ...);
113 
114 static char *edit_role (openvas_connection_t *, credentials_t *, params_t *,
115  const char *, cmd_response_data_t*);
116 
117 static char *edit_task (openvas_connection_t *, credentials_t *, params_t *,
118  const char *, cmd_response_data_t*);
119 
120 static char *get_alert (openvas_connection_t *, credentials_t *, params_t *,
121  const char *, cmd_response_data_t*);
122 
123 static char *get_alerts (openvas_connection_t *, credentials_t *, params_t *,
124  const char *, cmd_response_data_t*);
125 
126 static char *get_agent (openvas_connection_t *, credentials_t *, params_t *,
127  const char *, cmd_response_data_t*);
128 
129 static char *get_agents (openvas_connection_t *, credentials_t *, params_t *,
130  const char *, cmd_response_data_t*);
131 
132 static char *get_asset (openvas_connection_t *, credentials_t *, params_t *,
133  const char *, cmd_response_data_t*);
134 
135 static char *get_assets (openvas_connection_t *, credentials_t *, params_t *,
136  const char *, cmd_response_data_t*);
137 
138 static char *get_assets_chart (openvas_connection_t *, credentials_t *,
139  params_t *, const char *, cmd_response_data_t*);
140 
141 
142 static char *get_task (openvas_connection_t *, credentials_t *, params_t *,
143  const char *, cmd_response_data_t*);
144 
145 static char *get_tasks (openvas_connection_t *, credentials_t *, params_t *,
146  const char *, cmd_response_data_t*);
147 
148 static char *get_tasks_chart (openvas_connection_t *, credentials_t *,
149  params_t *, const char *, cmd_response_data_t*);
150 
151 static char *get_trash (openvas_connection_t *, credentials_t *, params_t *,
152  const char *, cmd_response_data_t*);
153 
154 static char *get_config_family (openvas_connection_t *, credentials_t *,
155  params_t *, int, cmd_response_data_t*);
156 
157 static char *get_config (openvas_connection_t *, credentials_t *, params_t *,
158  const char *, int, cmd_response_data_t*);
159 
160 static char *get_configs (openvas_connection_t *, credentials_t *, params_t *,
161  const char *, cmd_response_data_t*);
162 
163 static char *get_filter (openvas_connection_t *, credentials_t *, params_t *,
164  const char *, cmd_response_data_t*);
165 
166 static char *get_filters (openvas_connection_t *, credentials_t *, params_t *,
167  const char *, cmd_response_data_t*);
168 
169 static char *get_group (openvas_connection_t *, credentials_t *, params_t *,
170  const char *, cmd_response_data_t*);
171 
172 static char *get_groups (openvas_connection_t *, credentials_t *, params_t *,
173  const char *, cmd_response_data_t*);
174 
175 static char *get_credential (openvas_connection_t *, credentials_t *,
176  params_t *, const char *, cmd_response_data_t*);
177 
178 static char *get_credentials (openvas_connection_t *, credentials_t *,
179  params_t *, const char *, cmd_response_data_t*);
180 
181 static char *get_notes (openvas_connection_t *, credentials_t *, params_t *,
182  const char *, cmd_response_data_t*);
183 
184 static char *get_note (openvas_connection_t *, credentials_t *, params_t *,
185  const char *, cmd_response_data_t*);
186 
187 static char *get_nvts (openvas_connection_t *, credentials_t *credentials,
188  params_t *, const char *, const char *,
190 
191 static char *get_overrides (openvas_connection_t *, credentials_t *, params_t *,
192  const char *, cmd_response_data_t*);
193 
194 static char *get_override (openvas_connection_t *, credentials_t *, params_t *,
195  const char *, cmd_response_data_t*);
196 
197 static char *get_permission (openvas_connection_t *, credentials_t *,
198  params_t *, const char *, cmd_response_data_t*);
199 
200 static char *get_permissions (openvas_connection_t *, credentials_t *,
201  params_t *, const char *, cmd_response_data_t*);
202 
203 static char *get_port_list (openvas_connection_t *, credentials_t *, params_t *,
204  const char *, cmd_response_data_t*);
205 
206 static char *get_port_lists (openvas_connection_t *, credentials_t *,
207  params_t *, const char *, cmd_response_data_t*);
208 
209 static char *edit_port_list (openvas_connection_t *, credentials_t *,
210  params_t *, const char *, cmd_response_data_t*);
211 
212 static char *get_tag (openvas_connection_t *, credentials_t *, params_t *,
213  const char *, cmd_response_data_t*);
214 
215 static char *get_tags (openvas_connection_t *, credentials_t *, params_t *,
216  const char *, cmd_response_data_t*);
217 
218 static char *get_target (openvas_connection_t *, credentials_t *, params_t *,
219  const char *, cmd_response_data_t*);
220 
221 static char *get_targets (openvas_connection_t *, credentials_t *, params_t *,
222  const char *, cmd_response_data_t*);
223 
224 static char *get_report (openvas_connection_t *, credentials_t *, params_t *,
225  const char *, gsize *, gchar **, char **, const char *,
226  int *, cmd_response_data_t*);
227 
228 static char *get_report_format (openvas_connection_t *, credentials_t *,
229  params_t *, const char *, cmd_response_data_t*);
230 
231 static char *get_report_formats (openvas_connection_t *, credentials_t *,
232  params_t *, const char *,
234 
235 static char *get_report_section (openvas_connection_t *, credentials_t *,
236  params_t *, const char *,
238 
239 static char *get_reports (openvas_connection_t *, credentials_t *, params_t *,
240  const char *, cmd_response_data_t*);
241 
242 static char *get_result_page (openvas_connection_t *, credentials_t *,
243  params_t *, const char *, cmd_response_data_t*);
244 
245 static char *get_results (openvas_connection_t *, credentials_t *, params_t *,
246  const char *, cmd_response_data_t*);
247 
248 static char *get_role (openvas_connection_t *, credentials_t *, params_t *,
249  const char *, cmd_response_data_t*);
250 
251 static char *get_roles (openvas_connection_t *, credentials_t *, params_t *,
252  const char *, cmd_response_data_t*);
253 
254 static char *get_scanner (openvas_connection_t *, credentials_t *, params_t *,
255  const char *, cmd_response_data_t*);
256 
257 static char *get_scanners (openvas_connection_t *, credentials_t *, params_t *,
258  const char *, cmd_response_data_t*);
259 
260 static char *get_schedule (openvas_connection_t *, credentials_t *, params_t *,
261  const char *, cmd_response_data_t*);
262 
263 static char *get_schedules (openvas_connection_t *, credentials_t *, params_t *,
264  const char *, cmd_response_data_t*);
265 
266 static char *get_user (openvas_connection_t *, credentials_t *, params_t *,
267  const char *, cmd_response_data_t*);
268 
269 static char *get_users (openvas_connection_t *, credentials_t *, params_t *,
270  const char *, cmd_response_data_t*);
271 
272 static char *wizard (openvas_connection_t *, credentials_t *, params_t *,
273  const char *, cmd_response_data_t*);
274 
275 static char *wizard_get (openvas_connection_t *, credentials_t *, params_t *,
276  const char *, cmd_response_data_t*);
277 
278 int token_user_remove (const char *);
279 
280 static int omp_success (entity_t entity);
281 
282 static gchar *next_page_url (credentials_t *, params_t *, const char *,
283  const char *, const char *, const char *,
284  const char *);
285 
286 static gchar *action_result_page (openvas_connection_t *, credentials_t *,
288  const char*, const char*, const char*,
289  const char*, const char*);
290 
291 static gchar* response_from_entity (openvas_connection_t *, credentials_t*,
292  params_t *, entity_t, int, const char*,
293  const char *, const char*, const char*,
294  const char *, cmd_response_data_t *);
295 
296 /* Helpers. */
297 
304 int
305 command_enabled (credentials_t *credentials, const gchar *name)
306 {
307  /* TODO Hack. Fails if command named in summary of another command. */
308  return strstr (credentials->capabilities, name) ? 1 : 0;
309 }
310 
318 void
319 omp_init (const gchar *manager_address_unix, const gchar *manager_address_tls,
320  int port_manager)
321 {
322  if (manager_address_unix)
323  {
324  manager_address = g_strdup (manager_address_unix);
325  manager_use_tls = 0;
326  }
327  else if (manager_address_tls)
328  {
329  manager_address = g_strdup (manager_address_tls);
330  manager_use_tls = 1;
331  }
332  else
333  {
334  manager_address = g_build_filename (OPENVAS_RUN_DIR,
335  "openvasmd.sock",
336  NULL);
337  manager_use_tls = 0;
338  }
339  manager_port = port_manager;
340 }
341 
347 void
349 {
350  data->http_status_code = MHD_HTTP_OK;
351  data->redirect = NULL;
352 }
353 
359 void
361 {
362  memset (data, 0, sizeof (cmd_response_data_t));
363 }
364 
374 static gboolean
375 print_chart_pref (gchar *id, gchar *value, GString* buffer)
376 {
377  g_string_append_printf (buffer,
378  "<chart_preference id=\"%s\">"
379  "<value>%s</value>"
380  "</chart_preference>",
381  id,
382  value);
383  return 0;
384 }
385 
389 typedef struct
390 {
391  gchar *value;
392  GList *keys;
394 
395 void
397 {
398  find->keys = NULL;
399  find->value = value;
400 }
401 
402 void
404 {
405  g_list_free (find->keys);
406 }
407 
418 static gboolean
419 find_by_value (gchar *key, gchar *value, find_by_value_t *data)
420 {
421  if (strcmp (value, data->value) == 0)
422  {
423  g_debug ("%s found key %s for value %s\n",
424  __FUNCTION__, key, value);
425  data->keys = g_list_append (data->keys, key);
426  }
427  return FALSE;
428 }
429 
439 static int
440 filter_exists (openvas_connection_t *connection, const char *filt_id)
441 {
442  entity_t entity;
443 
444  if (filt_id == NULL
445  || strcmp (filt_id, "0")
446  || strcmp (filt_id, "-2"))
447  return 1;
448 
449  /* check if filter still exists */
450  if (openvas_connection_sendf (connection, "<get_filters filter_id='%s'/>",
451  filt_id))
452  {
453  return -2;
454  }
455 
456  if (read_entity_c (connection, &entity))
457  {
458  return -3;
459  }
460 
461  return omp_success (entity);
462 }
463 
474 static char *
475 xsl_transform_omp (openvas_connection_t *connection,
476  credentials_t * credentials, params_t *params, gchar * xml,
477  cmd_response_data_t *response_data)
478 {
479  time_t now;
480  gchar *res, *name;
481  GString *string;
482  char *html;
483  char ctime_now[200];
484  params_iterator_t iter;
485  param_t *param;
486  const char *refresh_interval, *xml_flag;
487  struct timeval tv;
488 
489  assert (credentials);
490 
491  now = time (NULL);
492  ctime_r_strip_newline (&now, ctime_now);
493 
494  string = g_string_new ("");
495 
496  gettimeofday (&tv, NULL);
497  res = g_markup_printf_escaped ("<envelope>"
498  "<version>%s</version>"
499  "<vendor_version>%s</vendor_version>"
500  "<token>%s</token>"
501  "<caller>%s</caller>"
502  "<current_page>%s</current_page>"
503  "<time>%s</time>"
504  "<timezone>%s</timezone>"
505  "<login>%s</login>"
506  "<role>%s</role>"
507  "<severity>%s</severity>"
508  "<i18n>%s</i18n>"
509  "<charts>%d</charts>"
510  "<guest>%d</guest>"
511  "<client_address>%s</client_address>"
512  "<backend_operation>%.2f</backend_operation>",
513  GSAD_VERSION,
515  credentials->token,
516  credentials->caller ? credentials->caller : "",
517  credentials->current_page
518  ? credentials->current_page
519  : (credentials->caller
520  ? credentials->caller
521  : ""),
522  ctime_now,
523  credentials->timezone
524  ? credentials->timezone : "",
525  credentials->username,
526  credentials->role,
527  credentials->severity,
528  credentials->language,
529  credentials->charts,
530  credentials->guest,
531  credentials->client_address,
532  (double) ((tv.tv_sec
533  - credentials->cmd_start.tv_sec)
534  * 1000000L
535  + tv.tv_usec
536  - credentials->cmd_start.tv_usec)
537  / 1000000.0);
538  g_string_append (string, res);
539  g_free (res);
540 
541  g_string_append (string, "<chart_preferences>");
542  g_tree_foreach (credentials->chart_prefs,
543  (GTraverseFunc)print_chart_pref,
544  string);
545  g_string_append (string, "</chart_preferences>");
546 
547  if (credentials->pw_warning)
548  {
549  gchar *warning_elem;
550  warning_elem = g_markup_printf_escaped ("<password_warning>"
551  "%s"
552  "</password_warning>",
553  credentials->pw_warning);
554  g_string_append (string, warning_elem);
555  g_free (warning_elem);
556  }
557 
558  refresh_interval = params_value (credentials->params, "refresh_interval");
559  if ((refresh_interval == NULL) || (strcmp (refresh_interval, "") == 0))
560  g_string_append_printf (string,
561  "<autorefresh interval=\"%s\"/>",
562  credentials->autorefresh);
563  else
564  {
565  int ret;
566  gchar *interval_64, *response;
567  entity_t entity;
568 
569  interval_64 = (refresh_interval
570  ? g_base64_encode ((guchar*) refresh_interval,
571  strlen (refresh_interval))
572  : g_strdup (""));
573  ret = ompf (connection, credentials, &response, &entity, response_data,
574  "<modify_setting"
575  " setting_id=\"578a1c14-e2dc-45ef-a591-89d31391d007\">"
576  "<value>%s</value>"
577  "</modify_setting>",
578  interval_64);
579  g_free (interval_64);
580  switch (ret)
581  {
582  case 0:
583  case -1:
584  break;
585  case 1:
586  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
587  return gsad_message (credentials,
588  "Internal error", __FUNCTION__, __LINE__,
589  "An internal error occurred while modifying the"
590  " autorefresh setting for the settings. "
591  "Diagnostics: Failure to send command to"
592  " manager daemon.",
593  "/omp?cmd=get_my_settings", response_data);
594  case 2:
595  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
596  return gsad_message (credentials,
597  "Internal error", __FUNCTION__, __LINE__,
598  "An internal error occurred while modifying the"
599  " autorefresh setting for the settings. "
600  "Diagnostics: Failure to receive response from"
601  " manager daemon.",
602  "/omp?cmd=get_my_settings", response_data);
603  default:
604  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
605  return gsad_message (credentials,
606  "Internal error", __FUNCTION__, __LINE__,
607  "An internal error occurred while modifying the"
608  " autorefresh setting for the settings. "
609  "Diagnostics: Internal Error.",
610  "/omp?cmd=get_my_settings", response_data);
611  }
612 
613  free_entity (entity);
614  g_free (credentials->autorefresh);
615  credentials->autorefresh = g_strdup (refresh_interval);
616  user_set_autorefresh (credentials->token, refresh_interval);
617 
618  g_string_append_printf (string,
619  "<autorefresh interval=\"%s\"/>",
620  credentials->autorefresh);
621  }
622 
623  g_string_append (string, "<params>");
624  params_iterator_init (&iter, credentials->params);
625  while (params_iterator_next (&iter, &name, &param))
626  {
627  if (name && strcmp (name, ""))
628  {
629  if ((name[strlen (name) - 1] == ':') && param->values)
630  {
631  gchar *child_name;
632  params_iterator_t children_iter;
633  param_t *child_param;
634 
635  params_iterator_init (&children_iter, param->values);
636  while (params_iterator_next (&children_iter, &child_name,
637  &child_param))
638  if (child_param->value
639  && child_param->valid
640  && child_param->valid_utf8)
641  xml_string_append (string,
642  "<_param>"
643  "<name>%s%s</name><value>%s</value>"
644  "</_param>",
645  name, child_name, child_param->value);
646  }
647 
648  if (param->value && param->valid && param->valid_utf8
649  && strcmp (name, "xml_file") && strcmp (name, "installer"))
650  xml_string_append (string, "<%s>%s</%s>", name, param->value, name);
651  }
652  else
653  g_warning ("%s: Param without name found", __FUNCTION__);
654  }
655  g_string_append (string, "</params>");
656 
657  g_string_append_printf (string,
658  "<capabilities>%s</capabilities>"
659  "%s"
660  "</envelope>",
661  credentials->capabilities,
662  xml);
663  g_free (xml);
664 
665  xml_flag = params_value (credentials->params, "xml");
666  if (xml_flag && strcmp (xml_flag, "0"))
667  return g_string_free (string, FALSE);
668 
669  html = xsl_transform (string->str, response_data);
670  g_string_free (string, TRUE);
671  if (html == NULL)
672  {
673  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
674  res = g_strdup_printf ("<gsad_response>"
675  "<title>Internal Error</title>"
676  "<message>"
677  "An internal server error has occurred during XSL"
678  " transformation."
679  "</message>"
680  "<backurl>/omp?cmd=get_tasks</backurl>"
681  "</gsad_response>");
682  html = xsl_transform (res, response_data);
683  if (html == NULL)
684  html = g_strdup ("<html>"
685  "<body>"
686  "An internal server error has occurred during XSL"
687  " transformation."
688  "</body>"
689  "</html>");
690  g_free (res);
691  }
692  return html;
693 }
694 
703 static int
704 member (params_t *params, const char *string)
705 {
706  params_iterator_t iter;
707  param_t *param;
708  char *name;
709 
710  params_iterator_init (&iter, params);
711  while (params_iterator_next (&iter, &name, &param))
712  if (strcmp (name, string) == 0) return 1;
713  return 0;
714 }
715 
724 int
725 member1 (params_t *params, const char *string)
726 {
727  params_iterator_t iter;
728  param_t *param;
729  char *name;
730 
731  params_iterator_init (&iter, params);
732  while (params_iterator_next (&iter, &name, &param))
733  if (param->value_size
734  && param->value[0] == '1'
735  && strcmp (name, string) == 0)
736  return 1;
737  return 0;
738 }
739 
753 static char *
754 check_modify_config (credentials_t *credentials,
755  openvas_connection_t *connection, params_t *params,
756  const char* next, const char* fail_next, int* success,
757  cmd_response_data_t *response_data)
758 {
759  entity_t entity;
760  gchar *response;
761  const char *no_redirect, *status_text;
762 
763  no_redirect = params_value (params, "no_redirect");
764 
765  if (success)
766  *success = 0;
767 
770  /* Read the response. */
771 
772  if (read_entity_c (connection, &entity))
773  {
774  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
775  return gsad_message (credentials,
776  "Internal error", __FUNCTION__, __LINE__,
777  "An internal error occurred while saving a config. "
778  "It is unclear whether the entire config has been saved. "
779  "Diagnostics: Failure to read command to manager daemon.",
780  "/omp?cmd=get_configs", response_data);
781  }
782 
783  /* Check the response. */
784 
785  status_text = entity_attribute (entity, "status_text");
786  if (status_text == NULL)
787  {
788  free_entity (entity);
789  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
790  return gsad_message (credentials,
791  "Internal error", __FUNCTION__, __LINE__,
792  "An internal error occurred while saving a config. "
793  "It is unclear whether the entire config has been saved. "
794  "Diagnostics: Failure to parse status_text from response.",
795  "/omp?cmd=get_configs", response_data);
796  }
797 
798  if (strcmp (status_text, "Config is in use") == 0)
799  {
800  const char* message = "The config is now in use by a task,"
801  " so only name and comment can be modified.";
802  gchar *next_url = next_page_url (credentials, params,
803  NULL, fail_next,
804  "Save Config",
805  entity_attribute (entity, "status"),
806  message);
807 
808  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
809  response
810  = action_result_page (connection, credentials, params, response_data,
811  "Save Config",
812  entity_attribute (entity, "status"),
813  message, NULL, next_url);
814 
815  g_free (next_url);
816  free_entity (entity);
817  return response;
818  }
819  else if (strcmp (status_text, "MODIFY_CONFIG name must be unique") == 0)
820  {
821  const char* message = "A config with the given name exists already.";
822  gchar *next_url = next_page_url (credentials, params,
823  NULL, fail_next,
824  "Save Config",
825  entity_attribute (entity, "status"),
826  message);
827 
828  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
829  response
830  = action_result_page (connection, credentials, params, response_data,
831  "Save Config",
832  entity_attribute (entity, "status"),
833  message, NULL, next_url);
834 
835  g_free (next_url);
836  free_entity (entity);
837  return response;
838  }
839  else if (success && omp_success (entity))
840  {
841  *success = 1;
842  }
843 
844  response
845  = response_from_entity (connection, credentials, params, entity,
846  (no_redirect && strcmp (no_redirect, "0")),
847  NULL, next,
848  NULL, fail_next,
849  "Save Config", response_data);
850  free_entity (entity);
851 
852  return response;
853 }
854 
862 static int
863 omp_success (entity_t entity)
864 {
865  const char *status;
866 
867  if (entity == NULL)
868  return 0;
869 
870  status = entity_attribute (entity, "status");
871  if ((status == NULL)
872  || (strlen (status) == 0))
873  return -1;
874 
875  return status[0] == '2';
876 }
877 
884 void
886  cmd_response_data_t *response_data)
887 {
888  if (entity == NULL)
889  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
890  else if (strcmp (entity_attribute (entity, "status_text"),
891  "Permission denied")
892  == 0)
893  response_data->http_status_code = MHD_HTTP_FORBIDDEN;
894  else if (strcmp (entity_attribute (entity, "status"), "404") == 0)
895  response_data->http_status_code = MHD_HTTP_NOT_FOUND;
896  else
897  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
898 }
899 
913 static int
914 omp (openvas_connection_t *connection, credentials_t *credentials,
915  gchar **response, entity_t *entity_return,
916  cmd_response_data_t *response_data, const char *command)
917 {
918  int ret;
919  entity_t entity;
920 
921  if (entity_return)
922  *entity_return = NULL;
923 
924  ret = openvas_connection_sendf (connection, "%s", command);
925  if (ret == -1)
926  {
927  return 1;
928  }
929 
930  entity = NULL;
931  if (read_entity_and_text_c (connection, &entity, response))
932  {
933  return 2;
934  }
935  if (entity_return)
936  *entity_return = entity;
937  else
938  free_entity (entity);
939  return 0;
940 }
941 
956 static int
957 simple_ompf (openvas_connection_t *connection, const gchar *message_operation,
958  credentials_t *credentials, gchar **response,
959  cmd_response_data_t *response_data, const char *format, ...)
960 {
961  int ret;
962  gchar *command;
963  va_list args;
964  entity_t entity;
965 
966  va_start (args, format);
967  command = g_markup_vprintf_escaped (format, args);
968  va_end (args);
969 
970  ret = omp (connection, credentials, response, &entity, response_data,
971  command);
972  g_free (command);
973  switch (ret)
974  {
975  case 0:
976  break;
977  case -1:
978  /* 'omp' set response. */
979  return 4;
980  case 1:
981  if (response_data)
982  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
983  if (response)
984  {
985  gchar *message;
986  message = g_strdup_printf
987  ("An internal error occurred while %s. "
988  " The operation was not started."
989  " Diagnostics: Failure to send command to manager"
990  " daemon.",
991  message_operation
992  ? message_operation
993  : "performing an operation");
994  *response = gsad_message (credentials, "Internal error",
995  __FUNCTION__, __LINE__,
996  message, "/omp?cmd=get_tasks",
997  response_data);
998  g_free (message);
999  }
1000  return 1;
1001  case 2:
1002  if (response_data)
1003  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1004  if (response)
1005  {
1006  gchar *message;
1007  message = g_strdup_printf
1008  ("An internal error occurred while %s."
1009  " It is unclear whether the operation succeeded."
1010  " Diagnostics: Failure to receive response from manager"
1011  " daemon.",
1012  message_operation
1013  ? message_operation
1014  : "performing an operation");
1015  *response = gsad_message (credentials, "Internal error",
1016  __FUNCTION__, __LINE__,
1017  message, "/omp?cmd=get_tasks",
1018  response_data);
1019  g_free (message);
1020  }
1021  return 2;
1022  default:
1023  if (response_data)
1024  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1025  if (response)
1026  {
1027  gchar *message;
1028  message = g_strdup_printf
1029  ("An internal error occurred while %s."
1030  " It is unclear whether the operation succeeded."
1031  " Diagnostics: Internal Error.",
1032  message_operation
1033  ? message_operation
1034  : "performing an operation");
1035  *response = gsad_message (credentials, "Internal error",
1036  __FUNCTION__, __LINE__,
1037  message, "/omp?cmd=get_tasks",
1038  response_data);
1039  g_free (message);
1040  }
1041  return -1;
1042  }
1043 
1044  switch (omp_success (entity))
1045  {
1046  case 0:
1047  set_http_status_from_entity (entity, response_data);
1048  ret = 3;
1049  break;
1050  case 1:
1051  ret = 0;
1052  break;
1053  default:
1054  ret = -1;
1055  break;
1056  }
1057  free_entity (entity);
1058  return ret;
1059 }
1060 
1075 static int
1076 ompf (openvas_connection_t* connection, credentials_t *credentials,
1077  gchar **response, entity_t *entity_return,
1078  cmd_response_data_t *response_data, const char *format, ...)
1079 {
1080  int ret;
1081  gchar *command;
1082  va_list args;
1083 
1084  va_start (args, format);
1085  command = g_markup_vprintf_escaped (format, args);
1086  va_end (args);
1087 
1088  ret = omp (connection, credentials, response, entity_return, response_data,
1089  command);
1090  g_free (command);
1091  return ret;
1092 }
1093 
1104 static int
1105 setting_get_value (openvas_connection_t *connection, const char *setting_id,
1106  gchar **value, cmd_response_data_t *response_data)
1107 {
1108  int ret;
1109  entity_t entity;
1110  const char *status;
1111  gchar *response;
1112 
1113  *value = NULL;
1114 
1115  ret = openvas_connection_sendf (connection,
1116  "<get_settings setting_id=\"%s\"/>",
1117  setting_id);
1118  if (ret)
1119  return 1;
1120 
1121  entity = NULL;
1122  if (read_entity_and_text_c (connection, &entity, &response))
1123  return 2;
1124 
1125  status = entity_attribute (entity, "status");
1126  if (status == NULL
1127  || strlen (status) == 0)
1128  {
1129  g_free (response);
1130  free_entity (entity);
1131  return -1;
1132  }
1133 
1134  if (status[0] == '2')
1135  {
1136  entity_t setting;
1137  setting = entity_child (entity, "setting");
1138  if (setting == NULL)
1139  {
1140  free_entity (entity);
1141  g_free (response);
1142  return 0;
1143  }
1144  setting = entity_child (setting, "value");
1145  if (setting == NULL)
1146  {
1147  free_entity (entity);
1148  g_free (response);
1149  return -1;
1150  }
1151  *value = g_strdup (entity_text (setting));
1152  g_free (response);
1153  free_entity (entity);
1154  }
1155  else
1156  {
1157  if (response_data)
1158  set_http_status_from_entity (entity, response_data);
1159  free_entity (entity);
1160  g_free (response);
1161  return -1;
1162  }
1163 
1164  return 0;
1165 }
1166 
1174 #define CHECK_PARAM(name, op_name, ret_func) \
1175  if (name == NULL) \
1176  { \
1177  gchar *msg, *ret_html; \
1178  msg = g_strdup_printf (GSAD_MESSAGE_INVALID, \
1179  "Given " G_STRINGIFY (name) " was invalid", \
1180  op_name); \
1181  ret_html = ret_func (connection, credentials, params, msg, \
1182  response_data); \
1183  g_free (msg); \
1184  response_data->http_status_code = MHD_HTTP_BAD_REQUEST; \
1185  return ret_html; \
1186  }
1187 
1195 #define CHECK_PARAM_INVALID(name, op_name, next_cmd) \
1196  if (name == NULL) \
1197  { \
1198  return message_invalid (connection, credentials, params, response_data, \
1199  "Given " G_STRINGIFY (name) " was invalid", \
1200  G_STRINGIFY (MHD_HTTP_BAD_REQUEST), \
1201  op_name, next_cmd); \
1202  }
1203 
1212 static char *
1213 page_url (credentials_t *credentials, const gchar *cmd)
1214 {
1215  GString *url;
1216 
1217  assert (cmd);
1218 
1219  url = g_string_new ("");
1220 
1221  if (credentials->caller && strlen (credentials->caller))
1222  {
1223  gchar **split_question, *page, *params;
1224 
1225  split_question = g_strsplit (credentials->caller, "?", 2);
1226 
1227  page = *split_question;
1228 
1229  if (page)
1230  {
1231  g_string_append_printf (url, "%s?cmd=%s", page, cmd);
1232  params = split_question[1];
1233  }
1234  else
1235  {
1236  g_string_append_printf (url, "cmd=%s", cmd);
1237  params = credentials->caller;
1238  }
1239  if (params)
1240  {
1241  gchar **split_amp, **point;
1242 
1243  point = split_amp = g_strsplit (params, "&", 0);
1244  while (*point)
1245  {
1246  gchar *param;
1247 
1248  param = *point;
1249 
1250  g_strstrip (param);
1251 
1252  if (strstr (param, "cmd=") == param)
1253  {
1254  point++;
1255  continue;
1256  }
1257 
1258  g_string_append_printf (url, "&%s", param);
1259 
1260  point++;
1261  }
1262  g_strfreev (split_amp);
1263  }
1264  g_strfreev (split_question);
1265  }
1266  else
1267  g_string_append_printf (url, "?cmd=%s", cmd);
1268 
1269  return g_string_free (url, FALSE);
1270 }
1271 
1279 static gchar*
1280 capitalize (const char* input)
1281 {
1282  gchar *output;
1283  if (input == NULL)
1284  return NULL;
1285  else
1286  {
1287  int first_letter = 1;
1288  int pos = 0;
1289  output = g_strdup (input);
1290 
1291  while (output[pos])
1292  {
1293  if (g_ascii_isalpha (output[pos]) && first_letter)
1294  {
1295  output[pos] = g_ascii_toupper (output[pos]);
1296  first_letter = 0;
1297  }
1298  else if (output[pos] == '_')
1299  {
1300  output[pos] = ' ';
1301  first_letter = 1;
1302  }
1303  pos++;
1304  }
1305  return output;
1306  }
1307 }
1308 
1309 
1310 /* Generic page handlers. */
1311 
1325 static gchar *
1326 next_page_url (credentials_t *credentials, params_t *params,
1327  const char* override_next, const char *default_next,
1328  const char* prev_action, const char* action_status,
1329  const char* action_message)
1330 {
1331  GString *url;
1332  const char *next_cmd, *xml_param, *next_xml_param;
1333  params_iterator_t iter;
1334  gchar *param_name;
1335  param_t *current_param;
1336  int error;
1337  if (action_status && strcmp (action_status, "") && action_status[0] != '2')
1338  error = 1;
1339  else
1340  error = 0;
1341 
1342  url = g_string_new ("/omp?cmd=");
1343 
1344  if (override_next)
1345  next_cmd = override_next;
1346  else if (error && params_valid (params, "next_error"))
1347  next_cmd = params_value (params, "next_error");
1348  else if (!error && params_valid (params, "next"))
1349  next_cmd = params_value (params, "next");
1350  else if (default_next)
1351  next_cmd = default_next;
1352  else
1353  next_cmd = "get_tasks";
1354 
1355  xml_param = params_value (params, "xml");
1356  next_xml_param = params_value (params, "next_xml");
1357 
1358  g_string_append (url, next_cmd);
1359 
1360  params_iterator_init (&iter, params);
1361  while (params_iterator_next (&iter, &param_name, &current_param))
1362  {
1363  if (strcmp (param_name, "asset_type") == 0
1364  || strcmp (param_name, "filter") == 0
1365  || strcmp (param_name, "filt_id") == 0
1366  || (strstr (param_name, "_id")
1367  == param_name + strlen (param_name) - strlen ("_id"))
1368  || (strcmp (param_name, "name") == 0
1369  && strcasecmp (prev_action, "Run Wizard") == 0)
1370  || (strcmp (param_name, "get_name") == 0
1371  && strcasecmp (next_cmd, "wizard_get") == 0))
1372  {
1373  g_string_append_printf (url, "&%s=%s",
1374  param_name,
1375  current_param->value
1376  ? current_param->value
1377  : "");
1378  }
1379  }
1380 
1381  if (next_xml_param)
1382  {
1383  g_string_append_printf (url, "&xml=%s", next_xml_param);
1384  }
1385  else if (xml_param)
1386  {
1387  g_string_append_printf (url, "&xml=%s", xml_param);
1388  }
1389 
1390  if (action_status)
1391  {
1392  gchar *escaped = g_uri_escape_string (action_status, NULL, FALSE);
1393  g_string_append_printf (url, "&action_status=%s", escaped);
1394  g_free (escaped);
1395  }
1396 
1397  if (action_message)
1398  {
1399  gchar *escaped = g_uri_escape_string (action_message, NULL, FALSE);
1400  g_string_append_printf (url, "&action_message=%s", escaped);
1401  g_free (escaped);
1402  }
1403 
1404  if (prev_action)
1405  {
1406  gchar *escaped = g_uri_escape_string (prev_action, NULL, FALSE);
1407  g_string_append_printf (url, "&prev_action=%s", escaped);
1408  g_free (escaped);
1409  }
1410 
1411 
1412  g_string_append_printf (url, "&token=%s", credentials->token);
1413 
1414  return g_string_free (url, FALSE);
1415 }
1416 
1432 static gchar *
1433 action_result_page (openvas_connection_t *connection,
1434  credentials_t *credentials, params_t *params,
1435  cmd_response_data_t *response_data,
1436  const char* action, const char* status,
1437  const char* message, const char* details,
1438  const char* next_url)
1439 {
1440  gchar *xml;
1441  xml = g_markup_printf_escaped ("<action_result>"
1442  "<action>%s</action>"
1443  "<status>%s</status>"
1444  "<message>%s</message>"
1445  "<details>%s</details>"
1446  "<next>%s</next>"
1447  "</action_result>",
1448  action ? action : "",
1449  status ? status : "",
1450  message ? message : "",
1451  details ? details : "",
1452  next_url ? next_url : "");
1453  return xsl_transform_omp (connection, credentials, params, xml,
1454  response_data);
1455 }
1456 
1471 gchar *
1472 message_invalid (openvas_connection_t *connection, credentials_t *credentials,
1473  params_t *params, cmd_response_data_t *response_data,
1474  const char *message, const char *status,
1475  const char *op_name, const char *next_cmd)
1476 {
1477  gchar *ret;
1478  gchar *next_url;
1479 
1480  next_url = next_page_url (credentials, params, next_cmd, NULL, op_name,
1481  status, message);
1482  ret = action_result_page (connection, credentials, params, response_data,
1483  op_name, G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
1484  message, NULL,
1485  next_url);
1486  g_free (next_url);
1487  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
1488  return ret;
1489 }
1490 
1496 static gchar*
1497 response_from_entity (openvas_connection_t *connection,
1498  credentials_t* credentials, params_t *params,
1499  entity_t entity, int no_redirect,
1500  const char* override_next, const char *default_next,
1501  const char* override_fail_next,
1502  const char* default_fail_next,
1503  const char* action, cmd_response_data_t *response_data)
1504 {
1505  gchar *res, *next_url;
1506  entity_t status_details_entity;
1507  const char *status_details;
1508  int success;
1509  success = omp_success (entity);
1510 
1511  if (success)
1512  {
1513  next_url = next_page_url (credentials, params,
1514  override_next, default_next,
1515  action,
1516  entity_attribute (entity, "status"),
1517  entity_attribute (entity, "status_text"));
1518  }
1519  else
1520  {
1521  set_http_status_from_entity (entity, response_data);
1522  next_url = next_page_url (credentials, params,
1523  override_fail_next, default_fail_next,
1524  action,
1525  entity_attribute (entity, "status"),
1526  entity_attribute (entity, "status_text"));
1527  }
1528 
1529  status_details_entity = entity_child (entity, "status_details");
1530  if (status_details_entity)
1531  {
1532  status_details = status_details_entity->text;
1533  }
1534  else
1535  {
1536  status_details = NULL;
1537  }
1538 
1539  if (no_redirect || success == 0)
1540  {
1541  res = action_result_page (connection, credentials, params, response_data,
1542  action, entity_attribute (entity, "status"),
1543  entity_attribute (entity, "status_text"),
1544  status_details, next_url);
1545  g_free (next_url);
1546  }
1547  else
1548  {
1549  res = NULL;
1550  response_data->redirect = next_url;
1551  }
1552  return res;
1553 }
1554 
1567 static char *
1568 generate_page (openvas_connection_t *connection, credentials_t *credentials,
1569  params_t *params, gchar *response, const gchar *next,
1570  cmd_response_data_t* response_data)
1571 {
1572  credentials->current_page = page_url (credentials, next);
1573  if (g_utf8_validate (credentials->current_page, -1, NULL) == FALSE)
1574  {
1575  g_free (credentials->current_page);
1576  g_warning ("%s - current_page is not valid UTF-8", __FUNCTION__);
1577  credentials->current_page = NULL;
1578  }
1579 
1580  if (strcmp (next, "edit_role") == 0)
1581  return edit_role (connection, credentials, params, response, response_data);
1582 
1583  if (strcmp (next, "edit_task") == 0)
1584  return edit_task (connection, credentials, params, response, response_data);
1585 
1586  if (strcmp (next, "get_alerts") == 0)
1587  return get_alerts (connection, credentials, params, response,
1588  response_data);
1589 
1590  if (strcmp (next, "get_alert") == 0)
1591  return get_alert (connection, credentials, params, response, response_data);
1592 
1593  if (strcmp (next, "edit_port_list") == 0)
1594  return edit_port_list (connection, credentials, params, response,
1595  response_data);
1596 
1597  if (strcmp (next, "get_agents") == 0)
1598  return get_agents (connection, credentials, params, response,
1599  response_data);
1600 
1601  if (strcmp (next, "get_agent") == 0)
1602  return get_agent (connection, credentials, params, response, response_data);
1603 
1604  if (strcmp (next, "get_asset") == 0)
1605  return get_asset (connection, credentials, params, response, response_data);
1606 
1607  if (strcmp (next, "get_assets") == 0)
1608  return get_assets (connection, credentials, params, response,
1609  response_data);
1610 
1611  if (strcmp (next, "get_assets_chart") == 0)
1612  return get_assets_chart (connection, credentials, params, response,
1613  response_data);
1614 
1615  if (strcmp (next, "get_config") == 0)
1616  return get_config (connection, credentials, params, response, 0,
1617  response_data);
1618 
1619  if (strcmp (next, "get_configs") == 0)
1620  return get_configs (connection, credentials, params, response,
1621  response_data);
1622 
1623  if (strcmp (next, "get_filter") == 0)
1624  return get_filter (connection, credentials, params, response,
1625  response_data);
1626 
1627  if (strcmp (next, "get_filters") == 0)
1628  return get_filters (connection, credentials, params, response,
1629  response_data);
1630 
1631  if (strcmp (next, "get_group") == 0)
1632  return get_group (connection, credentials, params, response, response_data);
1633 
1634  if (strcmp (next, "get_groups") == 0)
1635  return get_groups (connection, credentials, params, response,
1636  response_data);
1637 
1638  if (strcmp (next, "get_credential") == 0)
1639  return get_credential (connection, credentials, params, response,
1640  response_data);
1641 
1642  if (strcmp (next, "get_credentials") == 0)
1643  return get_credentials (connection, credentials, params, response,
1644  response_data);
1645 
1646  if (strcmp (next, "get_note") == 0)
1647  return get_note (connection, credentials, params, response, response_data);
1648 
1649  if (strcmp (next, "get_notes") == 0)
1650  return get_notes (connection, credentials, params, response, response_data);
1651 
1652  if (strcmp (next, "get_nvts") == 0)
1653  return get_nvts (connection, credentials, params, NULL, response,
1654  response_data);
1655 
1656  if (strcmp (next, "get_override") == 0)
1657  return get_override (connection, credentials, params, response,
1658  response_data);
1659 
1660  if (strcmp (next, "get_overrides") == 0)
1661  return get_overrides (connection, credentials, params, response,
1662  response_data);
1663 
1664  if (strcmp (next, "get_permission") == 0)
1665  return get_permission (connection, credentials, params, response,
1666  response_data);
1667 
1668  if (strcmp (next, "get_permissions") == 0)
1669  return get_permissions (connection, credentials, params, response,
1670  response_data);
1671 
1672  if (strcmp (next, "get_port_list") == 0)
1673  return get_port_list (connection, credentials, params, response,
1674  response_data);
1675 
1676  if (strcmp (next, "get_port_lists") == 0)
1677  return get_port_lists (connection, credentials, params, response,
1678  response_data);
1679 
1680  if (strcmp (next, "get_tag") == 0)
1681  return get_tag (connection, credentials, params, response, response_data);
1682 
1683  if (strcmp (next, "get_tags") == 0)
1684  return get_tags (connection, credentials, params, response, response_data);
1685 
1686  if (strcmp (next, "get_target") == 0)
1687  return get_target (connection, credentials, params, response,
1688  response_data);
1689 
1690  if (strcmp (next, "get_targets") == 0)
1691  return get_targets (connection, credentials, params, response,
1692  response_data);
1693 
1694  if (strcmp (next, "get_task") == 0)
1695  return get_task (connection, credentials, params, response, response_data);
1696 
1697  if (strcmp (next, "get_tasks") == 0)
1698  return get_tasks (connection, credentials, params, response,
1699  response_data);
1700 
1701  if (strcmp (next, "get_tasks_chart") == 0)
1702  return get_tasks_chart (connection, credentials, params, response,
1703  response_data);
1704 
1705  if (strcmp (next, "get_report") == 0)
1706  {
1707  char *result;
1708  int error = 0;
1709 
1710  result = get_report (connection, credentials, params, NULL, NULL, NULL,
1711  NULL, response, &error, response_data);
1712 
1713  return error ? result : xsl_transform_omp (connection, credentials,
1714  params, result,
1715  response_data);
1716  }
1717 
1718  if (strcmp (next, "get_report_format") == 0)
1719  return get_report_format (connection, credentials, params, response,
1720  response_data);
1721 
1722  if (strcmp (next, "get_report_formats") == 0)
1723  return get_report_formats (connection, credentials, params, response,
1724  response_data);
1725 
1726  if (strcmp (next, "get_report_section") == 0)
1727  return get_report_section (connection, credentials, params, response,
1728  response_data);
1729 
1730  if (strcmp (next, "get_reports") == 0)
1731  return get_reports (connection, credentials, params, response,
1732  response_data);
1733 
1734  if (strcmp (next, "get_results") == 0)
1735  return get_results (connection, credentials, params, response,
1736  response_data);
1737 
1738  if (strcmp (next, "get_result") == 0)
1739  return get_result_page (connection, credentials, params, response,
1740  response_data);
1741 
1742  if (strcmp (next, "get_role") == 0)
1743  return get_role (connection, credentials, params, response, response_data);
1744 
1745  if (strcmp (next, "get_roles") == 0)
1746  return get_roles (connection, credentials, params, response, response_data);
1747 
1748  if (strcmp (next, "get_scanner") == 0)
1749  return get_scanner (connection, credentials, params, response,
1750  response_data);
1751 
1752  if (strcmp (next, "get_scanners") == 0)
1753  return get_scanners (connection, credentials, params, response,
1754  response_data);
1755 
1756  if (strcmp (next, "get_schedule") == 0)
1757  return get_schedule (connection, credentials, params, response,
1758  response_data);
1759 
1760  if (strcmp (next, "get_schedules") == 0)
1761  return get_schedules (connection, credentials, params, response,
1762  response_data);
1763 
1764  if (strcmp (next, "get_user") == 0)
1765  return get_user (connection, credentials, params, response, response_data);
1766 
1767  if (strcmp (next, "get_users") == 0)
1768  return get_users (connection, credentials, params, response, response_data);
1769 
1770  if (strcmp (next, "get_info") == 0)
1771  return get_info (connection, credentials, params, response, response_data);
1772 
1773  if (strcmp (next, "wizard") == 0)
1774  return wizard (connection, credentials, params, response, response_data);
1775 
1776  if (strcmp (next, "wizard_get") == 0)
1777  return wizard_get (connection, credentials, params, response,
1778  response_data);
1779 
1780  return NULL;
1781 }
1782 
1794 static char *
1795 next_page (openvas_connection_t *connection, credentials_t *credentials,
1796  params_t *params, gchar *response,
1797  cmd_response_data_t* response_data)
1798 {
1799  const char *next;
1800 
1801  next = params_value (params, "next");
1802  if (next == NULL)
1803  return NULL;
1804 
1805  return generate_page (connection, credentials, params, response, next,
1806  response_data);
1807 }
1808 
1822 char *
1823 get_one (openvas_connection_t *connection, const char *type,
1824  credentials_t * credentials, params_t *params,
1825  const char *extra_xml, const char *extra_attribs,
1826  cmd_response_data_t* response_data)
1827 {
1828  GString *xml;
1829  int ret;
1830  gchar *end, *id_name;
1831  const char *id, *sort_field, *sort_order, *filter, *first, *max;
1832 
1833  id_name = g_strdup_printf ("%s_id", type);
1834  id = params_value (params, id_name);
1835  g_free (id_name);
1836  sort_field = params_value (params, "sort_field");
1837  sort_order = params_value (params, "sort_order");
1838 
1839  if (id == NULL)
1840  {
1841  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1842  return gsad_message (credentials,
1843  "Internal error", __FUNCTION__, __LINE__,
1844  "An internal error occurred while getting a resource. "
1845  "Diagnostics: missing ID.",
1846  "/omp?cmd=get_tasks", response_data);
1847  }
1848 
1849  xml = g_string_new ("");
1850  g_string_append_printf (xml, "<get_%s>", type);
1851 
1852  if (strcmp (type, "role") == 0
1853  && command_enabled (credentials, "GET_PERMISSIONS")
1854  && params_value (params, "role_id"))
1855  {
1856  gchar *response;
1857  entity_t entity;
1858 
1859  response = NULL;
1860  entity = NULL;
1861  switch (ompf (connection, credentials, &response, &entity, response_data,
1862  "<get_permissions"
1863  " filter=\"rows=-1 subject_type=role and subject_uuid=%s\"/>",
1864  params_value (params, "role_id")))
1865  {
1866  case 0:
1867  case -1:
1868  break;
1869  case 1:
1870  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1871  return gsad_message (credentials,
1872  "Internal error", __FUNCTION__, __LINE__,
1873  "An internal error occurred getting permissions. "
1874  "Diagnostics: Failure to send command to manager daemon.",
1875  "/omp?cmd=get_roles", response_data);
1876  case 2:
1877  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1878  return gsad_message (credentials,
1879  "Internal error", __FUNCTION__, __LINE__,
1880  "An internal error occurred getting permissions. "
1881  "Diagnostics: Failure to receive response from manager daemon.",
1882  "/omp?cmd=get_roles", response_data);
1883  default:
1884  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1885  return gsad_message (credentials,
1886  "Internal error", __FUNCTION__, __LINE__,
1887  "An internal error occurred getting permissins. "
1888  "Diagnostics: Internal Error.",
1889  "/omp?cmd=get_roles", response_data);
1890  }
1891 
1892  g_string_append (xml, response);
1893 
1894  if (!omp_success (entity))
1895  set_http_status_from_entity (entity, response_data);
1896 
1897  free_entity (entity);
1898  g_free (response);
1899  }
1900 
1901  /* Pass through params for get_resources. */
1902  filter = params_value (params, "filter");
1903  first = params_value (params, "first");
1904  max = params_value (params, "max");
1905  end = g_markup_printf_escaped ("<filters><term>%s</term></filters>"
1906  "<%ss start=\"%s\" max=\"%s\"/>",
1907  filter ? filter : "",
1908  type,
1909  first ? first : "",
1910  max ? max : "");
1911  g_string_append (xml, end);
1912  g_free (end);
1913 
1914  if (extra_xml)
1915  g_string_append (xml, extra_xml);
1916 
1917  /* Get the resource. */
1918 
1919  if (openvas_connection_sendf (connection,
1920  "<get_%ss"
1921  " %s_id=\"%s\""
1922  " sort_field=\"%s\""
1923  " sort_order=\"%s\""
1924  " details=\"1\""
1925  " %s/>",
1926  type,
1927  type,
1928  id,
1929  sort_field ? sort_field : "name",
1930  sort_order ? sort_order : "ascending",
1931  extra_attribs ? extra_attribs : "")
1932  == -1)
1933  {
1934  g_string_free (xml, TRUE);
1935  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1936  return gsad_message (credentials,
1937  "Internal error", __FUNCTION__, __LINE__,
1938  "An internal error occurred while getting resources list. "
1939  "The current list of resources is not available. "
1940  "Diagnostics: Failure to send command to manager daemon.",
1941  "/omp?cmd=get_resources", response_data);
1942  }
1943 
1944  if (read_string_c (connection, &xml))
1945  {
1946  g_string_free (xml, TRUE);
1947  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1948  return gsad_message (credentials,
1949  "Internal error", __FUNCTION__, __LINE__,
1950  "An internal error occurred while getting resources list. "
1951  "The current list of resources is not available. "
1952  "Diagnostics: Failure to receive response from manager daemon.",
1953  "/omp?cmd=get_resources", response_data);
1954  }
1955 
1956  /* Get tag names */
1957 
1958  if (openvas_connection_sendf (connection,
1959  "<get_tags"
1960  " filter=\"resource_type=%s"
1961  " first=1"
1962  " rows=-1\""
1963  " names_only=\"1\""
1964  "/>",
1965  type)
1966  == -1)
1967  {
1968  g_string_free (xml, TRUE);
1969  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1970  return gsad_message (credentials,
1971  "Internal error", __FUNCTION__, __LINE__,
1972  "An internal error occurred while getting tag names list. "
1973  "The current list of resources is not available. "
1974  "Diagnostics: Failure to send command to manager daemon.",
1975  "/omp?cmd=get_resources", response_data);
1976  }
1977 
1978  if (read_string_c (connection, &xml))
1979  {
1980  g_string_free (xml, TRUE);
1981  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1982  return gsad_message (credentials,
1983  "Internal error", __FUNCTION__, __LINE__,
1984  "An internal error occurred while getting tag names list. "
1985  "The current list of resources is not available. "
1986  "Diagnostics: Failure to receive response from manager daemon.",
1987  "/omp?cmd=get_resources", response_data);
1988  }
1989 
1990  /* Get permissions */
1991 
1992  g_string_append (xml, "<permissions>");
1993 
1994  if ((strcmp (type, "user") == 0)
1995  || (strcmp (type, "group") == 0)
1996  || (strcmp (type, "role") == 0))
1997  ret = openvas_connection_sendf (connection,
1998  "<get_permissions"
1999  " filter=\"subject_uuid=%s"
2000  " and not resource_uuid=&quot;&quot;"
2001  " or resource_uuid=%s"
2002  " first=1 rows=-1\"/>",
2003  id,
2004  id);
2005  else
2006  ret = openvas_connection_sendf (connection,
2007  "<get_permissions"
2008  " filter=\"resource_uuid=%s"
2009  " first=1 rows=-1\"/>",
2010  id);
2011  if (ret == -1)
2012  {
2013  g_string_free (xml, TRUE);
2014  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2015  return gsad_message (credentials,
2016  "Internal error", __FUNCTION__, __LINE__,
2017  "An internal error occurred while getting permissions list. "
2018  "The current list of resources is not available. "
2019  "Diagnostics: Failure to send command to manager daemon.",
2020  "/omp?cmd=get_resources", response_data);
2021  }
2022 
2023  if (read_string_c (connection, &xml))
2024  {
2025  g_string_free (xml, TRUE);
2026  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2027  return gsad_message (credentials,
2028  "Internal error", __FUNCTION__, __LINE__,
2029  "An internal error occurred while getting permissions list. "
2030  "The current list of resources is not available. "
2031  "Diagnostics: Failure to receive response from manager daemon.",
2032  "/omp?cmd=get_resources", response_data);
2033  }
2034 
2035  g_string_append (xml, "</permissions>");
2036 
2037  /* Cleanup, and return transformed XML. */
2038 
2039  g_string_append_printf (xml, "</get_%s>", type);
2040  return xsl_transform_omp (connection, credentials, params,
2041  g_string_free (xml, FALSE), response_data);
2042 }
2043 
2057 static char *
2058 get_many (openvas_connection_t *connection, const char *type,
2059  credentials_t * credentials, params_t *params,
2060  const char *extra_xml, const char *extra_attribs,
2061  cmd_response_data_t* response_data)
2062 {
2063  GString *xml;
2064  GString *type_many; /* The plural form of type */
2065  gchar *filter_type, *request, *built_filter;
2066  int no_filter_history;
2067  const char *build_filter, *given_filt_id, *filt_id, *filter, *filter_extra;
2068  const char *first, *max, *sort_field, *sort_order, *owner, *permission;
2069  const char *replace_task_id;
2070  const char *overrides, *autofp, *autofp_value, *min_qod;
2071  const char *level_high, *level_medium, *level_low, *level_log;
2072  const char *level_false_positive;
2073 
2074  no_filter_history = params_value(params, "no_filter_history")
2075  ? atoi (params_value(params, "no_filter_history"))
2076  : 0;
2077  build_filter = params_value(params, "build_filter");
2078  given_filt_id = params_value (params, "filt_id");
2079  filter = params_value (params, "filter");
2080  filter_extra = params_value (params, "filter_extra");
2081  first = params_value (params, "first");
2082  max = params_value (params, "max");
2083  replace_task_id = params_value (params, "replace_task_id");
2084  sort_field = params_value (params, "sort_field");
2085  sort_order = params_value (params, "sort_order");
2086  owner = params_value (params, "owner");
2087  permission = params_value (params, "permission");
2088  overrides = params_value (params, "overrides");
2089  autofp = params_value (params, "autofp");
2090  autofp_value = params_value (params, "autofp_value");
2091  min_qod = params_value (params, "min_qod");
2092  level_high = params_value (params, "level_high");
2093  level_medium = params_value (params, "level_medium");
2094  level_low = params_value (params, "level_low");
2095  level_log = params_value (params, "level_log");
2096  level_false_positive = params_value (params, "level_false_positive");
2097 
2098  if (strcasecmp (type, "info") == 0)
2099  filter_type = g_strdup (params_value (params, "info_type"));
2100  else
2101  filter_type = g_strdup (type);
2102 
2103  if (given_filt_id)
2104  {
2105  if (no_filter_history == 0
2106  && strcmp (given_filt_id, "0") && strcmp (given_filt_id, "-2"))
2107  g_tree_replace (credentials->last_filt_ids, filter_type,
2108  g_strdup (given_filt_id));
2109  filt_id = given_filt_id;
2110  }
2111  else
2112  {
2113  if (no_filter_history == 0
2114  && (filter == NULL || strcmp (filter, "") == 0))
2115  filt_id = g_tree_lookup (credentials->last_filt_ids, filter_type);
2116  else
2117  filt_id = NULL;
2118  g_free (filter_type);
2119  }
2120 
2121  /* check if filter still exists */
2122  switch (filter_exists (connection, filt_id))
2123  {
2124  case 1:
2125  break;
2126  case 0:
2127  g_debug ("%s filter doesn't exist anymore %s!\n", __FUNCTION__,
2128  filt_id);
2129  filt_id = NULL;
2130  break;
2131  case -1:
2132  g_debug ("%s filter response didn't contain a status!\n", __FUNCTION__);
2133  filt_id = NULL;
2134  break;
2135  case -2:
2136  g_debug ("%s could not send filter request!\n", __FUNCTION__);
2137  filt_id = NULL;
2138  break;
2139  case -3:
2140  g_debug ("%s could not read entity from filter response!\n",
2141  __FUNCTION__);
2142  filt_id = NULL;
2143  break;
2144  default:
2145  filt_id = NULL;
2146  }
2147 
2148  xml = g_string_new ("");
2149  type_many = g_string_new (type);
2150 
2151  /* Workaround the fact that info is a non countable noun */
2152  if (strcmp (type, "info") != 0)
2153  g_string_append (type_many, "s");
2154 
2155  g_string_append_printf (xml, "<get_%s>", type_many->str);
2156 
2157  if (extra_xml)
2158  g_string_append (xml, extra_xml);
2159 
2160  built_filter = NULL;
2161  if (filt_id == NULL
2162  || (strcmp (filt_id, "") == 0)
2163  || (strcmp (filt_id, "--") == 0))
2164  {
2165  if ((build_filter && (strcmp (build_filter, "1") == 0))
2166  || ((filter == NULL || strcmp (filter, "") == 0)
2167  && (filter_extra == NULL || strcmp (filter_extra, "") == 0)))
2168  {
2169  if (build_filter && (strcmp (build_filter, "1") == 0))
2170  {
2171  gchar *task;
2172  const char *search_phrase, *task_id;
2173 
2174  if (strcmp (type, "report") == 0
2175  || strcmp (type, "task") == 0)
2176  {
2177  task = g_strdup_printf ("apply_overrides=%i min_qod=%s ",
2178  overrides
2179  && strcmp (overrides, "0"),
2180  min_qod ? min_qod : "");
2181  }
2182  else if (strcmp (type, "result") == 0)
2183  {
2184  gchar *levels
2185  = g_strdup_printf ("%s%s%s%s%s",
2186  level_high ? "h" : "",
2187  level_medium ? "m" : "",
2188  level_low ? "l" : "",
2189  level_log ? "g" : "",
2190  level_false_positive ? "f" : "");
2191  task = g_strdup_printf ("apply_overrides=%i min_qod=%s"
2192  " autofp=%s levels=%s ",
2193  (overrides
2194  && strcmp (overrides, "0")),
2195  min_qod ? min_qod : "",
2196  (autofp && autofp_value)
2197  ? autofp_value : "0",
2198  levels);
2199  g_free (levels);
2200  }
2201  else
2202  task = NULL;
2203 
2204  search_phrase = params_value (params, "search_phrase");
2205  task_id = params_value (params, "task_id");
2206  built_filter = g_strdup_printf
2207  ("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
2208  task ? task : "",
2209  task_id ? "task_id=" : "",
2210  task_id ? task_id : "",
2211  task_id ? " " : "",
2212  first ? "first=" : "",
2213  first ? first : "",
2214  first ? " " : "",
2215  max ? "rows=" : "",
2216  max ? max : "",
2217  max ? " " : "",
2218  sort_field
2219  ? ((sort_order && strcmp (sort_order,
2220  "ascending"))
2221  ? "sort-reverse="
2222  : "sort=")
2223  : "",
2224  sort_field ? sort_field : "",
2225  sort_field ? " " : "",
2226  permission ? "permission=" : "",
2227  permission ? permission : "",
2228  permission ? " " : "",
2229  owner ? "owner=" : "",
2230  owner ? owner : "",
2231  owner ? " " : "",
2232  (filter && search_phrase) ? " " : "",
2233  filter ? filter : "",
2234  search_phrase ? " " : "",
2235  search_phrase
2236  ? search_phrase
2237  : "");
2238  filt_id = "-2";
2239  g_free (task);
2240  }
2241  else if (strcmp (type, "info") == 0
2242  && params_value (params, "info_type"))
2243  {
2244  if (strcmp (params_value (params, "info_type"), "cve") == 0)
2245  filter = "sort-reverse=published rows=-2";
2246  else
2247  filter = "sort-reverse=created rows=-2";
2248  }
2249  else if (strcmp (type, "user") == 0)
2250  filter = "sort=roles rows=-2";
2251  else if (strcmp (type, "report") == 0)
2252  {
2253  const char *task_id;
2254  task_id = params_value (params, "task_id");
2255  if (task_id)
2256  built_filter = g_strdup_printf ("task_id=%s apply_overrides=1"
2257  " rows=-2 sort-reverse=date",
2258  task_id);
2259  else
2260  filter = "apply_overrides=1 rows=-2 sort-reverse=date";
2261  }
2262  else if (strcmp (type, "result") == 0)
2263  {
2264  built_filter
2265  = g_strdup_printf("apply_overrides=%d autofp=%s rows=-2"
2266  " sort-reverse=created",
2267  (overrides == NULL
2268  || strcmp (overrides, "0")),
2269  (autofp && strcmp (autofp, "0"))
2270  ? autofp_value
2271  : "0");
2272  }
2273  else if (strcmp (type, "task"))
2274  filter = "rows=-2";
2275  else
2276  filter = "apply_overrides=1 rows=-2";
2277  if (filt_id && strcmp (filt_id, ""))
2278  /* Request to use "filter" instead. */
2279  filt_id = "0";
2280  else
2281  filt_id = "-2";
2282  }
2283  else if ((strcmp (filter, "sort=nvt") == 0)
2284  && ((strcmp (type, "note") == 0)
2285  || (strcmp (type, "override") == 0)))
2286  filt_id = "-2";
2287  else if ((strcmp (filter, "apply_overrides=1") == 0)
2288  && (strcmp (type, "task") == 0))
2289  filt_id = "-2";
2290  }
2291  else if (replace_task_id)
2292  {
2293  const char *task_id;
2294  task_id = params_value (params, "task_id");
2295  if (task_id)
2296  built_filter = g_strdup_printf ("task_id=%s %s",
2297  task_id,
2298  filter ? filter : "");
2299  }
2300 
2301  /* Get the list. */
2302 
2303  request = g_markup_printf_escaped (" %sfilt_id=\"%s\""
2304  " %sfilter=\"%s%s%s%s\""
2305  " filter_replace=\"%s\""
2306  " first=\"%s\""
2307  " max=\"%s\""
2308  " sort_field=\"%s\""
2309  " sort_order=\"%s\"",
2310  strcmp (type, "report") ? "" : "report_",
2311  filt_id ? filt_id : "0",
2312  strcmp (type, "report") ? "" : "report_",
2313  built_filter
2314  ? built_filter
2315  : (filter ? filter : ""),
2316  filter_extra ? " " : "",
2317  filter_extra ? filter_extra : "",
2318  filter_extra ? " " : "",
2319  replace_task_id ? "task_id" : "",
2320  first ? first : "1",
2321  max ? max : "-2",
2322  sort_field ? sort_field : "name",
2323  sort_order ? sort_order : "ascending");
2324 
2325  g_free (built_filter);
2326  if (openvas_connection_sendf (connection,
2327  "<get_%s%s%s %s/>",
2328  type_many->str,
2329  strcmp (type, "report") ? "" : " details=\"0\"",
2330  request,
2331  extra_attribs ? extra_attribs : "")
2332  == -1)
2333  {
2334  g_free(request);
2335  g_string_free (xml, TRUE);
2336  g_string_free (type_many, TRUE);
2337  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2338  return gsad_message (credentials,
2339  "Internal error", __FUNCTION__, __LINE__,
2340  "An internal error occurred while getting a resource list. "
2341  "The current list of resources is not available. "
2342  "Diagnostics: Failure to send command to manager daemon.",
2343  "/omp?cmd=get_tasks", response_data);
2344  }
2345  g_free(request);
2346  if (read_string_c (connection, &xml))
2347  {
2348  g_string_free (xml, TRUE);
2349  g_string_free (type_many, TRUE);
2350  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2351  return gsad_message (credentials,
2352  "Internal error", __FUNCTION__, __LINE__,
2353  "An internal error occurred while getting resources list. "
2354  "The current list of resources is not available. "
2355  "Diagnostics: Failure to receive response from manager daemon.",
2356  "/omp?cmd=get_tasks", response_data);
2357  }
2358 
2359  // TODO: Test response
2360 
2361  if (command_enabled (credentials, "GET_FILTERS"))
2362  {
2363  /* Get the filters. */
2364 
2365  g_string_append (xml, "<filters>");
2366 
2367  if (openvas_connection_sendf_xml
2368  (connection,
2369  "<get_filters"
2370  " filter=\"rows=-1 type=%s or type=\"/>",
2371  type)
2372  == -1)
2373  {
2374  g_string_free (xml, TRUE);
2375  g_string_free (type_many, TRUE);
2376  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2377  return gsad_message (credentials,
2378  "Internal error", __FUNCTION__, __LINE__,
2379  "An internal error occurred while getting the filter list. "
2380  "The current list of filters is not available. "
2381  "Diagnostics: Failure to send command to manager daemon.",
2382  "/omp?cmd=get_tasks", response_data);
2383  }
2384 
2385  if (read_string_c (connection, &xml))
2386  {
2387  g_string_free (xml, TRUE);
2388  g_string_free (type_many, TRUE);
2389  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2390  return gsad_message (credentials,
2391  "Internal error", __FUNCTION__, __LINE__,
2392  "An internal error occurred while getting the filter list. "
2393  "The current list of filters is not available. "
2394  "Diagnostics: Failure to receive response from manager daemon.",
2395  "/omp?cmd=get_tasks", response_data);
2396  }
2397 
2398  g_string_append (xml, "</filters>");
2399  }
2400 
2401  if (command_enabled (credentials, "GET_SETTINGS"))
2402  {
2403  /* Get the Wizard Rows setting. */
2404 
2405  if (openvas_connection_sendf_xml
2406  (connection,
2407  "<get_settings"
2408  " setting_id=\"20f3034c-e709-11e1-87e7-406186ea4fc5\"/>",
2409  type)
2410  == -1)
2411  {
2412  g_string_free (xml, TRUE);
2413  g_string_free (type_many, TRUE);
2414  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2415  return gsad_message (credentials,
2416  "Internal error", __FUNCTION__, __LINE__,
2417  "An internal error occurred while getting the filter list. "
2418  "The current list of filters is not available. "
2419  "Diagnostics: Failure to send command to manager daemon.",
2420  "/omp?cmd=get_tasks", response_data);
2421  }
2422 
2423  if (read_string_c (connection, &xml))
2424  {
2425  g_string_free (xml, TRUE);
2426  g_string_free (type_many, TRUE);
2427  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2428  return gsad_message (credentials,
2429  "Internal error", __FUNCTION__, __LINE__,
2430  "An internal error occurred while getting the filter list. "
2431  "The current list of filters is not available. "
2432  "Diagnostics: Failure to receive response from manager daemon.",
2433  "/omp?cmd=get_tasks", response_data);
2434  }
2435  }
2436 
2437  if (command_enabled (credentials, "GET_TAGS")
2438  && params_value (params, "details"))
2439  {
2440  /* Get tag names */
2441 
2442  if (openvas_connection_sendf (connection,
2443  "<get_tags"
2444  " filter=\"resource_type=%s"
2445  " first=1"
2446  " rows=-1\""
2447  " names_only=\"1\""
2448  "/>",
2449  strcmp (type, "info")
2450  ? type
2451  : params_value (params, "info_type"))
2452  == -1)
2453  {
2454  g_string_free (xml, TRUE);
2455  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2456  return gsad_message (credentials,
2457  "Internal error", __FUNCTION__, __LINE__,
2458  "An internal error occurred while getting tag names list. "
2459  "The current list of resources is not available. "
2460  "Diagnostics: Failure to send command to manager daemon.",
2461  "/omp?cmd=get_resources", response_data);
2462  }
2463 
2464  if (read_string_c (connection, &xml))
2465  {
2466  g_string_free (xml, TRUE);
2467  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2468  return gsad_message (credentials,
2469  "Internal error", __FUNCTION__, __LINE__,
2470  "An internal error occurred while getting tag names list. "
2471  "The current list of resources is not available. "
2472  "Diagnostics: Failure to receive response from manager daemon.",
2473  "/omp?cmd=get_resources", response_data);
2474  }
2475  }
2476 
2477  /* Cleanup, and return transformed XML. */
2478  g_string_append_printf (xml, "</get_%s>", type_many->str);
2479  g_string_free (type_many, TRUE);
2480  return xsl_transform_omp (connection, credentials, params,
2481  g_string_free (xml, FALSE), response_data);
2482 }
2483 
2497 char *
2498 edit_resource (openvas_connection_t *connection, const char *type,
2499  credentials_t *credentials, params_t *params,
2500  const char *extra_get_attribs, const char *extra_xml,
2501  cmd_response_data_t* response_data)
2502 {
2503  GString *xml;
2504  gchar *id_name;
2505  const char *resource_id;
2506 
2507  id_name = g_strdup_printf ("%s_id", type);
2508  resource_id = params_value (params, id_name);
2509  g_free (id_name);
2510 
2511  if (resource_id == NULL)
2512  {
2513  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
2514  return gsad_message (credentials,
2515  "Internal error", __FUNCTION__, __LINE__,
2516  "An internal error occurred while editing a resource. "
2517  "The resource remains as it was. "
2518  "Diagnostics: Required ID parameter was NULL.",
2519  "/omp?cmd=get_tasks", response_data);
2520  }
2521 
2522  if (openvas_connection_sendf (connection,
2523  /* TODO: Remove redundant COMMANDS. */
2524  "<commands>"
2525  "<get_%ss"
2526  " %s"
2527  " %s_id=\"%s\""
2528  " details=\"1\"/>"
2529  "</commands>",
2530  type,
2531  extra_get_attribs ? extra_get_attribs : "",
2532  type,
2533  resource_id)
2534  == -1)
2535  {
2536  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2537  return gsad_message (credentials,
2538  "Internal error", __FUNCTION__, __LINE__,
2539  "An internal error occurred while getting a resource. "
2540  "Diagnostics: Failure to send command to manager daemon.",
2541  "/omp?cmd=get_tasks", response_data);
2542  }
2543 
2544  xml = g_string_new ("");
2545 
2546  g_string_append_printf (xml, "<edit_%s>", type);
2547 
2548  if (extra_xml)
2549  g_string_append (xml, extra_xml);
2550 
2551  if (read_string_c (connection, &xml))
2552  {
2553  g_string_free (xml, TRUE);
2554  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2555  return gsad_message (credentials,
2556  "Internal error", __FUNCTION__, __LINE__,
2557  "An internal error occurred while getting a resource. "
2558  "Diagnostics: Failure to receive response from manager daemon.",
2559  "/omp?cmd=get_tasks", response_data);
2560  }
2561 
2562  /* Cleanup, and return transformed XML. */
2563 
2564  g_string_append_printf (xml, "</edit_%s>", type);
2565  return xsl_transform_omp (connection, credentials, params,
2566  g_string_free (xml, FALSE), response_data);
2567 }
2568 
2580 gchar *
2581 format_file_name (gchar* fname_format, credentials_t* credentials,
2582  const char* type, const char* uuid,
2583  entity_t resource_entity)
2584 {
2585  gchar *creation_time, *modification_time, *name, *format_name;
2586  gchar *ret;
2587 
2588  if (resource_entity)
2589  {
2590  entity_t creation_time_entity, modification_time_entity;
2591  entity_t task_entity, format_entity, format_name_entity, name_entity;
2592 
2593  creation_time_entity = entity_child (resource_entity,
2594  "creation_time");
2595 
2596  if (creation_time_entity)
2597  creation_time = entity_text (creation_time_entity);
2598  else
2599  creation_time = NULL;
2600 
2601  modification_time_entity = entity_child (resource_entity,
2602  "modification_time");
2603 
2604  if (modification_time_entity)
2605  modification_time = entity_text (modification_time_entity);
2606  else
2607  modification_time = NULL;
2608 
2609  if (strcasecmp (type, "report") == 0)
2610  {
2611  task_entity = entity_child (resource_entity, "task");
2612  if (task_entity)
2613  name_entity = entity_child (task_entity, "name");
2614  else
2615  name_entity = NULL;
2616 
2617  format_entity = entity_child (resource_entity, "report_format");
2618  if (format_entity)
2619  {
2620  format_name_entity = entity_child (format_entity, "name");
2621  }
2622  else
2623  format_name_entity = NULL;
2624 
2625  if (format_name_entity && strlen (entity_text (format_name_entity)))
2626  format_name = entity_text (format_name_entity);
2627  else
2628  format_name = NULL;
2629  }
2630  else
2631  {
2632  name_entity = entity_child (resource_entity, "name");
2633  format_name = NULL;
2634  }
2635 
2636  if (name_entity)
2637  name = entity_text (name_entity);
2638  else
2639  name = NULL;
2640  }
2641  else
2642  {
2643  creation_time = NULL;
2644  modification_time = NULL;
2645  name = NULL;
2646  format_name = NULL;
2647  }
2648 
2649  ret = openvas_export_file_name (fname_format, credentials->username,
2650  type, uuid, creation_time, modification_time,
2651  name, format_name);
2652  return ret;
2653 }
2654 
2655 
2670 char *
2671 export_resource (openvas_connection_t *connection, const char *type,
2672  credentials_t * credentials, params_t *params,
2673  enum content_type * content_type,
2674  char **content_disposition, gsize *content_length,
2675  cmd_response_data_t* response_data)
2676 {
2677  GString *xml;
2678  entity_t entity;
2679  entity_t resource_entity;
2680  char *content = NULL;
2681  gchar *id_name;
2682  gchar *fname_format, *file_name;
2683  int ret;
2684  const char *resource_id, *subtype;
2685 
2686  *content_length = 0;
2687 
2688  xml = g_string_new ("");
2689 
2690  id_name = g_strdup_printf ("%s_id", type);
2691  resource_id = params_value (params, id_name);
2692  g_free (id_name);
2693 
2694  if (resource_id == NULL)
2695  {
2696  g_string_append (xml, GSAD_MESSAGE_INVALID_PARAM ("Export Resource"));
2697  return xsl_transform_omp (connection, credentials, params,
2698  g_string_free (xml, FALSE), response_data);
2699  }
2700 
2701  subtype = params_value (params, "subtype");
2702 
2703  if (openvas_connection_sendf (connection,
2704  "<get_%ss"
2705  " %s_id=\"%s\""
2706  "%s%s%s"
2707  " export=\"1\""
2708  " details=\"1\"/>",
2709  type,
2710  type,
2711  resource_id,
2712  subtype ? " type=\"" : "",
2713  subtype ? subtype : "",
2714  subtype ? "\"" : "")
2715  == -1)
2716  {
2717  g_string_free (xml, TRUE);
2718  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2719  return gsad_message (credentials,
2720  "Internal error", __FUNCTION__, __LINE__,
2721  "An internal error occurred while getting a resource. "
2722  "The resource could not be delivered. "
2723  "Diagnostics: Failure to send command to manager daemon.",
2724  "/omp?cmd=get_tasks", response_data);
2725  }
2726 
2727  entity = NULL;
2728  if (read_entity_and_text_c (connection, &entity, &content))
2729  {
2730  g_string_free (xml, TRUE);
2731  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2732  return gsad_message (credentials,
2733  "Internal error", __FUNCTION__, __LINE__,
2734  "An internal error occurred while getting a resource. "
2735  "The resource could not be delivered. "
2736  "Diagnostics: Failure to receive response from manager daemon.",
2737  "/omp?cmd=get_tasks", response_data);
2738  }
2739 
2740  if (!omp_success (entity))
2741  set_http_status_from_entity (entity, response_data);
2742 
2743  resource_entity = entity_child (entity, type);
2744 
2745  if (resource_entity == NULL)
2746  {
2747  g_free (content);
2748  free_entity (entity);
2749  g_string_free (xml, TRUE);
2750  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2751  return gsad_message (credentials,
2752  "Internal error", __FUNCTION__, __LINE__,
2753  "An internal error occurred while getting a resource. "
2754  "The resource could not be delivered. "
2755  "Diagnostics: Failure to receive resource from manager daemon.",
2756  "/omp?cmd=get_tasks", response_data);
2757  }
2758 
2759  ret = setting_get_value (connection,
2760  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2",
2761  &fname_format,
2762  response_data);
2763  if (ret)
2764  {
2765  g_free (content);
2766  free_entity (entity);
2767  g_string_free (xml, TRUE);
2768  switch (ret)
2769  {
2770  case 1:
2771  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2772  return gsad_message (credentials,
2773  "Internal error", __FUNCTION__, __LINE__,
2774  "An internal error occurred while getting a setting. "
2775  "The setting could not be delivered. "
2776  "Diagnostics: Failure to send command to manager daemon.",
2777  "/omp?cmd=get_tasks", response_data);
2778  case 2:
2779  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2780  return gsad_message (credentials,
2781  "Internal error", __FUNCTION__, __LINE__,
2782  "An internal error occurred while getting a setting. "
2783  "The setting could not be delivered. "
2784  "Diagnostics: Failure to receive response from manager daemon.",
2785  "/omp?cmd=get_tasks", response_data);
2786  default:
2787  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2788  return gsad_message (credentials,
2789  "Internal error", __FUNCTION__, __LINE__,
2790  "An internal error occurred while getting a setting. "
2791  "The setting could not be delivered. "
2792  "Diagnostics: Internal error.",
2793  "/omp?cmd=get_tasks", response_data);
2794  }
2795  }
2796 
2797  if (fname_format == NULL)
2798  {
2799  g_warning ("%s : File name format setting not found.", __FUNCTION__);
2800  fname_format = "%T-%U";
2801  }
2802 
2803  file_name = format_file_name (fname_format, credentials, type, resource_id,
2804  resource_entity);
2805  if (file_name == NULL)
2806  file_name = g_strdup_printf ("%s-%s", type, resource_id);
2807 
2808  *content_type = GSAD_CONTENT_TYPE_APP_XML;
2809  *content_disposition = g_strdup_printf ("attachment; filename=\"%s.xml\"",
2810  file_name);
2811  *content_length = strlen (content);
2812  free_entity (entity);
2813  g_free (file_name);
2814  g_string_free (xml, TRUE);
2815  return content;
2816 }
2817 
2831 static char *
2832 export_many (openvas_connection_t *connection,const char *type,
2833  credentials_t * credentials, params_t *params,
2834  enum content_type * content_type, char **content_disposition,
2835  gsize *content_length, cmd_response_data_t* response_data)
2836 {
2837  entity_t entity;
2838  char *content = NULL;
2839  const char *filter;
2840  gchar *filter_escaped;
2841  gchar *type_many;
2842  gchar *fname_format, *file_name;
2843  int ret;
2844 
2845  *content_length = 0;
2846 
2847  filter = params_value (params, "filter");
2848 
2849  filter_escaped = g_markup_escape_text (filter, -1);
2850 
2851  if (strcmp (type, "info") == 0)
2852  {
2853  if (openvas_connection_sendf (connection,
2854  "<get_info"
2855  " type=\"%s\""
2856  " export=\"1\""
2857  " details=\"1\""
2858  " filter=\"%s\"/>",
2859  params_value (params, "info_type"),
2860  filter_escaped ? filter_escaped : "")
2861  == -1)
2862  {
2863  g_free (filter_escaped);
2864  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2865  return gsad_message (credentials,
2866  "Internal error", __FUNCTION__, __LINE__,
2867  "An internal error occurred while getting a list. "
2868  "The list could not be delivered. "
2869  "Diagnostics: Failure to send command to manager daemon.",
2870  "/omp?cmd=get_tasks", response_data);
2871  }
2872  }
2873  else if (strcmp (type, "asset") == 0)
2874  {
2875  if (openvas_connection_sendf (connection,
2876  "<get_assets"
2877  " type=\"%s\""
2878  " export=\"1\""
2879  " details=\"1\""
2880  " filter=\"%s\"/>",
2881  params_value (params, "asset_type"),
2882  filter_escaped ? filter_escaped : "")
2883  == -1)
2884  {
2885  g_free (filter_escaped);
2886  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2887  return gsad_message (credentials,
2888  "Internal error", __FUNCTION__, __LINE__,
2889  "An internal error occurred while getting a list. "
2890  "The list could not be delivered. "
2891  "Diagnostics: Failure to send command to manager daemon.",
2892  "/omp?cmd=get_tasks", response_data);
2893  }
2894  }
2895  else
2896  {
2897  if (openvas_connection_sendf (connection,
2898  "<get_%ss"
2899  " export=\"1\""
2900  " details=\"1\""
2901  " filter=\"%s\"/>",
2902  type,
2903  filter_escaped ? filter_escaped : "")
2904  == -1)
2905  {
2906  g_free (filter_escaped);
2907  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2908  return gsad_message (credentials,
2909  "Internal error", __FUNCTION__, __LINE__,
2910  "An internal error occurred while getting a list. "
2911  "The list could not be delivered. "
2912  "Diagnostics: Failure to send command to manager daemon.",
2913  "/omp?cmd=get_tasks", response_data);
2914  }
2915  }
2916  g_free (filter_escaped);
2917 
2918  entity = NULL;
2919  if (read_entity_and_text_c (connection, &entity, &content))
2920  {
2921  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2922  return gsad_message (credentials,
2923  "Internal error", __FUNCTION__, __LINE__,
2924  "An internal error occurred while getting a list. "
2925  "The list could not be delivered. "
2926  "Diagnostics: Failure to receive response from manager daemon.",
2927  "/omp?cmd=get_tasks", response_data);
2928  }
2929 
2930  if (!omp_success (entity))
2931  set_http_status_from_entity (entity, response_data);
2932 
2933  ret = setting_get_value (connection,
2934  "0872a6ed-4f85-48c5-ac3f-a5ef5e006745",
2935  &fname_format,
2936  response_data);
2937  if (ret)
2938  {
2939  g_free (content);
2940  free_entity (entity);
2941  switch (ret)
2942  {
2943  case 1:
2944  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2945  return gsad_message (credentials,
2946  "Internal error", __FUNCTION__, __LINE__,
2947  "An internal error occurred while getting a setting. "
2948  "The setting could not be delivered. "
2949  "Diagnostics: Failure to send command to manager daemon.",
2950  "/omp?cmd=get_tasks", response_data);
2951  case 2:
2952  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2953  return gsad_message (credentials,
2954  "Internal error", __FUNCTION__, __LINE__,
2955  "An internal error occurred while getting a setting. "
2956  "The setting could not be delivered. "
2957  "Diagnostics: Failure to receive response from manager daemon.",
2958  "/omp?cmd=get_tasks", response_data);
2959  default:
2960  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2961  return gsad_message (credentials,
2962  "Internal error", __FUNCTION__, __LINE__,
2963  "An internal error occurred while getting a setting. "
2964  "The setting could not be delivered. "
2965  "Diagnostics: Internal error.",
2966  "/omp?cmd=get_tasks", response_data);
2967  }
2968  }
2969 
2970  if (fname_format == NULL)
2971  {
2972  g_warning ("%s : File name format setting not found.", __FUNCTION__);
2973  fname_format = "%T-%D";
2974  }
2975 
2976  if (strcmp (type, "info") == 0)
2977  type_many = g_strdup (type);
2978  else
2979  type_many = g_strdup_printf ("%ss", type);
2980 
2981  file_name = format_file_name (fname_format, credentials, type_many, "list",
2982  NULL);
2983  if (file_name == NULL)
2984  file_name = g_strdup_printf ("%s-%s", type_many, "list");
2985 
2986  g_free (type_many);
2987 
2989  *content_disposition = g_strdup_printf ("attachment; filename=\"%s.xml\"",
2990  file_name);
2991  *content_length = strlen (content);
2992  free_entity (entity);
2993  g_free (file_name);
2994  return content;
2995 }
2996 
3010 char *
3011 delete_resource (openvas_connection_t *connection, const char *type,
3012  credentials_t * credentials, params_t *params, int ultimate,
3013  const char *get, cmd_response_data_t* response_data)
3014 {
3015  gchar *html, *response, *id_name, *resource_id, *extra_attribs;
3016  const char *no_redirect, *next_id;
3017  entity_t entity;
3018  gchar *cap_type, *default_next, *prev_action;
3019 
3020  no_redirect = params_value (params, "no_redirect");
3021  id_name = g_strdup_printf ("%s_id", type);
3022  if (params_value (params, id_name))
3023  resource_id = g_strdup (params_value (params, id_name));
3024  else
3025  {
3026  g_free (id_name);
3027  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
3028  return gsad_message (credentials,
3029  "Internal error", __FUNCTION__, __LINE__,
3030  "An internal error occurred while deleting a resource. "
3031  "The resource was not deleted. "
3032  "Diagnostics: Required parameter resource_id was NULL.",
3033  "/omp?cmd=get_tasks", response_data);
3034  }
3035 
3036  /* This is a hack for assets, because asset_id is the param name used for
3037  * both the asset being deleted and the asset on the next page. */
3038  next_id = params_value (params, "next_id");
3039  if (next_id)
3040  {
3041  param_t *param;
3042  param = params_get (params, id_name);
3043  g_free (param->value);
3044  param->value = g_strdup (next_id);
3045  param->value_size = strlen (param->value);
3046  }
3047 
3048  g_free (id_name);
3049 
3050  /* Extra attributes */
3051  extra_attribs = NULL;
3052 
3053  /* Inheritor of user's resource */
3054  if (strcmp (type, "user") == 0)
3055  {
3056  const char* inheritor_id;
3057  inheritor_id = params_value (params, "inheritor_id");
3058  if (inheritor_id)
3059  extra_attribs = g_strdup_printf ("inheritor_id=\"%s\"", inheritor_id);
3060  }
3061 
3062  /* Delete the resource and get all resources. */
3063 
3064  if (openvas_connection_sendf (connection,
3065  "<delete_%s %s_id=\"%s\" ultimate=\"%i\"%s%s/>",
3066  type,
3067  type,
3068  resource_id,
3069  !!ultimate,
3070  extra_attribs ? " " : "",
3071  extra_attribs ? extra_attribs : "")
3072  == -1)
3073  {
3074  g_free (resource_id);
3075  g_free (extra_attribs);
3076  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3077  return gsad_message (credentials,
3078  "Internal error", __FUNCTION__, __LINE__,
3079  "An internal error occurred while deleting a resource. "
3080  "The resource is not deleted. "
3081  "Diagnostics: Failure to send command to manager daemon.",
3082  "/omp?cmd=get_tasks", response_data);
3083  }
3084 
3085  g_free (resource_id);
3086  g_free (extra_attribs);
3087 
3088  entity = NULL;
3089  if (read_entity_and_text_c (connection, &entity, &response))
3090  {
3091  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3092  return gsad_message (credentials,
3093  "Internal error", __FUNCTION__, __LINE__,
3094  "An internal error occurred while deleting a resource. "
3095  "It is unclear whether the resource has been deleted or not. "
3096  "Diagnostics: Failure to read response from manager daemon.",
3097  "/omp?cmd=get_tasks", response_data);
3098  }
3099 
3100  if (!omp_success (entity))
3101  set_http_status_from_entity (entity, response_data);
3102 
3103  cap_type = capitalize (type);
3104  default_next = g_strdup_printf ("get_%ss", type);
3105  prev_action = g_strdup_printf ("Delete %s", cap_type);
3106 
3107  html = response_from_entity (connection, credentials, params, entity,
3108  (no_redirect && strcmp (no_redirect, "0")),
3109  NULL, default_next,
3110  NULL, default_next,
3111  prev_action, response_data);
3112 
3113  g_free (response);
3114  free_entity (entity);
3115  g_free (cap_type);
3116  g_free (default_next);
3117  g_free (prev_action);
3118 
3119  return html;
3120 }
3121 
3134 char *
3135 resource_action (openvas_connection_t *connection, credentials_t *credentials,
3136  params_t *params, const char *type, const char *action,
3137  cmd_response_data_t* response_data)
3138 {
3139  gchar *html, *response, *param_name;
3140  const char *no_redirect, *resource_id;
3141  gchar *cap_action, *cap_type, *get_cmd, *prev_action;
3142 
3143  int ret;
3144  entity_t entity;
3145 
3146  assert (type);
3147 
3148  param_name = g_strdup_printf ("%s_id", type);
3149  no_redirect = params_value (params, "no_redirect");
3150  resource_id = params_value (params, param_name);
3151 
3152  if (resource_id == NULL)
3153  {
3154  gchar *message;
3155  message = g_strdup_printf
3156  ("An internal error occurred while performing an action. "
3157  "The resource remains the same. "
3158  "Diagnostics: Required parameter %s was NULL.",
3159  param_name);
3160  g_free (param_name);
3161  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
3162  html = gsad_message (credentials,
3163  "Internal error", __FUNCTION__, __LINE__,
3164  message,
3165  "/omp?cmd=get_tasks", response_data);
3166  g_free (message);
3167  return html;
3168  }
3169  g_free (param_name);
3170 
3171  response = NULL;
3172  entity = NULL;
3173  ret = ompf (connection, credentials, &response, &entity, response_data,
3174  "<%s_%s %s_id=\"%s\"/>",
3175  action,
3176  type,
3177  type,
3178  resource_id);
3179  switch (ret)
3180  {
3181  case 0:
3182  case -1:
3183  break;
3184  case 1:
3185  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3186  return gsad_message (credentials,
3187  "Internal error", __FUNCTION__, __LINE__,
3188  "An internal error occurred while performing an action. "
3189  "The resource remains the same. "
3190  "Diagnostics: Failure to send command to manager daemon.",
3191  "/omp?cmd=get_tasks", response_data);
3192  case 2:
3193  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3194  return gsad_message (credentials,
3195  "Internal error", __FUNCTION__, __LINE__,
3196  "An internal error occurred while performing an action. "
3197  "It is unclear whether the resource has been affected. "
3198  "Diagnostics: Failure to receive response from manager daemon.",
3199  "/omp?cmd=get_tasks", response_data);
3200  default:
3201  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3202  return gsad_message (credentials,
3203  "Internal error", __FUNCTION__, __LINE__,
3204  "An internal error occurred while performing an action. "
3205  "It is unclear whether the resource has been affected. "
3206  "Diagnostics: Internal Error.",
3207  "/omp?cmd=get_tasks", response_data);
3208  }
3209 
3210  if (!omp_success (entity))
3211  set_http_status_from_entity (entity, response_data);
3212 
3213  cap_action = capitalize (action);
3214  cap_type = capitalize (type);
3215  get_cmd = g_strdup_printf ("get_%ss", type);
3216  prev_action = g_strdup_printf ("%s %s", cap_action, cap_type);
3217  html = response_from_entity (connection, credentials, params, entity,
3218  (no_redirect && strcmp (no_redirect, "0")),
3219  NULL, get_cmd,
3220  NULL, get_cmd,
3221  prev_action, response_data);
3222 
3223  g_free (response);
3224  free_entity (entity);
3225  g_free (cap_action);
3226  g_free (cap_type);
3227  g_free (get_cmd);
3228  g_free (prev_action);
3229 
3230  return html;
3231 }
3232 
3233 
3234 /* Page handlers. */
3235 
3284 static char *
3285 setting_get_value_error (credentials_t *credentials,
3286  openvas_connection_t *connection,
3287  const gchar *setting_id,
3288  gchar **value,
3289  cmd_response_data_t* response_data)
3290 {
3291  switch (setting_get_value (connection, setting_id, value, response_data))
3292  {
3293  case 0:
3294  return NULL;
3295  case 1:
3296  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3297  return gsad_message (credentials,
3298  "Internal error", __FUNCTION__, __LINE__,
3299  "An internal error occurred while getting a setting. "
3300  "The setting could not be delivered. "
3301  "Diagnostics: Failure to send command to manager daemon.",
3302  "/omp?cmd=get_tasks", response_data);
3303  case 2:
3304  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3305  return gsad_message (credentials,
3306  "Internal error", __FUNCTION__, __LINE__,
3307  "An internal error occurred while getting a setting. "
3308  "The setting could not be delivered. "
3309  "Diagnostics: Failure to receive response from manager daemon.",
3310  "/omp?cmd=get_tasks", response_data);
3311  default:
3312  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3313  return gsad_message (credentials,
3314  "Internal error", __FUNCTION__, __LINE__,
3315  "An internal error occurred while getting a setting. "
3316  "The setting could not be delivered. "
3317  "Diagnostics: Internal error.",
3318  "/omp?cmd=get_tasks", response_data);
3319  }
3320 }
3321 
3330 #define PARAM_OR_SETTING(value, param, setting_id, cleanup) \
3331  if (params_valid (params, param)) \
3332  value = g_strdup (params_value (params, param)); \
3333  else \
3334  { \
3335  char *message; \
3336  message = setting_get_value_error (credentials, connection, setting_id, \
3337  &value, response_data); \
3338  if (message) \
3339  { \
3340  cleanup; \
3341  return message; \
3342  } \
3343  }
3344 
3357 static char *
3358 new_task (openvas_connection_t *connection, credentials_t * credentials,
3359  const char *message, params_t *params, const char *extra_xml,
3360  cmd_response_data_t* response_data)
3361 {
3362  GString *xml;
3363  int apply_overrides;
3364  const char *alerts, *overrides;
3365  int ret;
3366  gchar *alert, *schedule, *target;
3367  gchar *openvas_config, *osp_config, *openvas_scanner, *osp_scanner;
3368 
3369  alerts = params_value (params, "alerts");
3370  overrides = params_value (params, "overrides");
3371 
3372  apply_overrides = overrides ? strcmp (overrides, "0") : 0;
3373 
3374  ret = setting_get_value (connection,
3375  "f9f5a546-8018-48d0-bef5-5ad4926ea899",
3376  &alert,
3377  response_data);
3378  if (ret)
3379  {
3380  switch (ret)
3381  {
3382  case 1:
3383  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3384  return gsad_message (credentials,
3385  "Internal error", __FUNCTION__, __LINE__,
3386  "An internal error occurred while getting a setting. "
3387  "The setting could not be delivered. "
3388  "Diagnostics: Failure to send command to manager daemon.",
3389  "/omp?cmd=get_tasks", response_data);
3390  case 2:
3391  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3392  return gsad_message (credentials,
3393  "Internal error", __FUNCTION__, __LINE__,
3394  "An internal error occurred while getting a setting. "
3395  "The setting could not be delivered. "
3396  "Diagnostics: Failure to receive response from manager daemon.",
3397  "/omp?cmd=get_tasks", response_data);
3398  default:
3399  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3400  return gsad_message (credentials,
3401  "Internal error", __FUNCTION__, __LINE__,
3402  "An internal error occurred while getting a setting. "
3403  "The setting could not be delivered. "
3404  "Diagnostics: Internal error.",
3405  "/omp?cmd=get_tasks", response_data);
3406  }
3407  }
3408 
3409  PARAM_OR_SETTING (schedule, "schedule_id",
3410  "778eedad-5550-4de0-abb6-1320d13b5e18",
3411  g_free (alert));
3412 
3413  PARAM_OR_SETTING (target, "target_id",
3414  "23409203-940a-4b4a-b70c-447475f18323",
3415  g_free (alert);
3416  g_free (schedule));
3417 
3418  PARAM_OR_SETTING (openvas_config, "config_id",
3419  "fe7ea321-e3e3-4cc6-9952-da836aae83ce",
3420  g_free (alert);
3421  g_free (schedule);
3422  g_free (target));
3423 
3424  PARAM_OR_SETTING (osp_config, "osp_config_id",
3425  "fb19ac4b-614c-424c-b046-0bc32bf1be73",
3426  g_free (alert);
3427  g_free (schedule);
3428  g_free (target);
3429  g_free (openvas_config));
3430 
3431  PARAM_OR_SETTING (openvas_scanner, "scanner_id",
3432  "f7d0f6ed-6f9e-45dc-8bd9-05cced84e80d",
3433  g_free (alert);
3434  g_free (schedule);
3435  g_free (target);
3436  g_free (openvas_config);
3437  g_free (osp_config));
3438 
3439  PARAM_OR_SETTING (osp_scanner, "osp_scanner_id",
3440  "b20697c9-be0a-4cd4-8b4d-5fe7841ebb03",
3441  g_free (alert);
3442  g_free (schedule);
3443  g_free (target);
3444  g_free (openvas_config);
3445  g_free (osp_config);
3446  g_free (openvas_scanner));
3447 
3448  xml = g_string_new ("<new_task>");
3449 
3450  g_string_append_printf (xml,
3451  "<alert_id>%s</alert_id>"
3452  "<config_id>%s</config_id>"
3453  "<osp_config_id>%s</osp_config_id>"
3454  "<scanner_id>%s</scanner_id>"
3455  "<osp_scanner_id>%s</osp_scanner_id>"
3456  "<schedule_id>%s</schedule_id>"
3457  "<target_id>%s</target_id>",
3458  alert ? alert : "",
3459  (openvas_config && strlen (openvas_config))
3460  ? openvas_config
3461  : "daba56c8-73ec-11df-a475-002264764cea",
3462  osp_config ? osp_config : "",
3463  openvas_scanner ? openvas_scanner : "",
3464  osp_scanner ? osp_scanner : "",
3465  schedule ? schedule : "",
3466  (target && strlen (target))
3467  ? target
3468  : "b493b7a8-7489-11df-a3ec-002264764cea");
3469 
3470  g_free (schedule);
3471  g_free (target);
3472  g_free (openvas_config);
3473  g_free (osp_config);
3474  g_free (openvas_scanner);
3475  g_free (osp_scanner);
3476 
3477  /* Get list of targets. */
3478  if (openvas_connection_sendf (connection,
3479  "<get_targets"
3480  " filter=\"rows=-1 sort=name\"/>")
3481  == -1)
3482  {
3483  g_string_free (xml, TRUE);
3484  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3485  return gsad_message (credentials,
3486  "Internal error", __FUNCTION__, __LINE__,
3487  "An internal error occurred while getting targets list. "
3488  "The current list of targets is not available. "
3489  "Diagnostics: Failure to send command to manager daemon.",
3490  "/omp?cmd=get_tasks", response_data);
3491  }
3492 
3493  if (read_string_c (connection, &xml))
3494  {
3495  g_string_free (xml, TRUE);
3496  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3497  return gsad_message (credentials,
3498  "Internal error", __FUNCTION__, __LINE__,
3499  "An internal error occurred while getting targets list. "
3500  "The current list of targets is not available. "
3501  "Diagnostics: Failure to receive response from manager daemon.",
3502  "/omp?cmd=get_tasks", response_data);
3503  }
3504 
3505  /* Get configs to select in new task UI. */
3506  if (openvas_connection_sendf (connection,
3507  "<get_configs"
3508  " filter=\"rows=-1 sort=name\"/>")
3509  == -1)
3510  {
3511  g_string_free (xml, TRUE);
3512  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3513  return gsad_message (credentials,
3514  "Internal error", __FUNCTION__, __LINE__,
3515  "An internal error occurred while getting config list. "
3516  "The current list of configs is not available. "
3517  "Diagnostics: Failure to send command to manager daemon.",
3518  "/omp?cmd=get_tasks", response_data);
3519  }
3520 
3521  if (read_string_c (connection, &xml))
3522  {
3523  g_string_free (xml, TRUE);
3524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3525  return gsad_message (credentials,
3526  "Internal error", __FUNCTION__, __LINE__,
3527  "An internal error occurred while getting config list. "
3528  "The current list of configs is not available. "
3529  "Diagnostics: Failure to receive response from manager daemon.",
3530  "/omp?cmd=get_tasks", response_data);
3531  }
3532 
3533  if (command_enabled (credentials, "GET_ALERTS"))
3534  {
3535  /* Get alerts to select in new task UI. */
3536  if (openvas_connection_sendf (connection,
3537  "<get_alerts"
3538  " filter=\"rows=-1 sort=name\"/>")
3539  == -1)
3540  {
3541  g_string_free (xml, TRUE);
3542  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3543  return gsad_message (credentials,
3544  "Internal error", __FUNCTION__, __LINE__,
3545  "An internal error occurred while getting alert list. "
3546  "The current list of alerts is not available. "
3547  "Diagnostics: Failure to send command to manager daemon.",
3548  "/omp?cmd=get_tasks", response_data);
3549  }
3550 
3551  if (read_string_c (connection, &xml))
3552  {
3553  g_string_free (xml, TRUE);
3554  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3555  return gsad_message (credentials,
3556  "Internal error", __FUNCTION__, __LINE__,
3557  "An internal error occurred while getting alert list. "
3558  "The current list of alerts is not available. "
3559  "Diagnostics: Failure to receive response from manager daemon.",
3560  "/omp?cmd=get_tasks", response_data);
3561  }
3562  }
3563 
3564  if (command_enabled (credentials, "GET_SCHEDULES"))
3565  {
3566  /* Get schedules to select in new task UI. */
3567  if (openvas_connection_sendf
3568  (connection,
3569  "<get_schedules"
3570  " filter=\"rows=-1 sort=name\"/>")
3571  == -1)
3572  {
3573  g_string_free (xml, TRUE);
3574  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3575  return gsad_message (credentials,
3576  "Internal error", __FUNCTION__, __LINE__,
3577  "An internal error occurred while getting the schedule list. "
3578  "The current list of schedules is not available. "
3579  "Diagnostics: Failure to send command to manager daemon.",
3580  "/omp?cmd=get_tasks", response_data);
3581  }
3582 
3583  if (read_string_c (connection, &xml))
3584  {
3585  g_string_free (xml, TRUE);
3586  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3587  return gsad_message (credentials,
3588  "Internal error", __FUNCTION__, __LINE__,
3589  "An internal error occurred while getting the schedule list. "
3590  "The current list of schedules is not available. "
3591  "Diagnostics: Failure to receive response from manager daemon.",
3592  "/omp?cmd=get_tasks", response_data);
3593  }
3594  }
3595 
3596  if (command_enabled (credentials, "GET_SCANNERS"))
3597  {
3598  /* Get scanners to select in new task UI. */
3599  if (openvas_connection_sendf (connection,
3600  "<get_scanners"
3601  " filter=\"rows=-1\"/>")
3602  == -1)
3603  {
3604  g_string_free (xml, TRUE);
3605  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3606  return gsad_message (credentials,
3607  "Internal error", __FUNCTION__, __LINE__,
3608  "An internal error occurred while getting the"
3609  " scanner list. "
3610  "The current list of scanners is not available. "
3611  "Diagnostics: Failure to send command to manager"
3612  " daemon.",
3613  "/omp?cmd=get_tasks", response_data);
3614  }
3615 
3616  if (read_string_c (connection, &xml))
3617  {
3618  g_string_free (xml, TRUE);
3619  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3620  return gsad_message (credentials,
3621  "Internal error", __FUNCTION__, __LINE__,
3622  "An internal error occurred while getting"
3623  " the scanner list."
3624  "The current list of scanners is not available. "
3625  "Diagnostics: Failure to receive response from"
3626  " manager daemon.",
3627  "/omp?cmd=get_tasks", response_data);
3628  }
3629  }
3630 
3631  if (command_enabled (credentials, "GET_GROUPS"))
3632  {
3633  /* Get groups for Observer Groups. */
3634 
3635  if (openvas_connection_sendf (connection,
3636  "<get_groups/>")
3637  == -1)
3638  {
3639  g_string_free (xml, TRUE);
3640  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3641  return gsad_message (credentials,
3642  "Internal error", __FUNCTION__, __LINE__,
3643  "An internal error occurred while getting group list. "
3644  "The current list of groups is not available. "
3645  "Diagnostics: Failure to send command to manager daemon.",
3646  "/omp?cmd=get_tasks", response_data);
3647  }
3648 
3649  if (read_string_c (connection, &xml))
3650  {
3651  g_string_free (xml, TRUE);
3652  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3653  return gsad_message (credentials,
3654  "Internal error", __FUNCTION__, __LINE__,
3655  "An internal error occurred while getting group list. "
3656  "The current list of groups is not available. "
3657  "Diagnostics: Failure to receive response from manager daemon.",
3658  "/omp?cmd=get_tasks", response_data);
3659  }
3660  }
3661 
3662  if (command_enabled (credentials, "GET_TAGS"))
3663  {
3664  /* Get tag names. */
3665 
3666  if (openvas_connection_sendf (connection,
3667  "<get_tags names_only=\"1\""
3668  " filter=\"resource_type=task rows=-1\"/>")
3669  == -1)
3670  {
3671  g_string_free (xml, TRUE);
3672  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3673  return gsad_message (credentials,
3674  "Internal error", __FUNCTION__, __LINE__,
3675  "An internal error occurred while getting tag list. "
3676  "The current list of tags is not available. "
3677  "Diagnostics: Failure to send command to manager daemon.",
3678  "/omp?cmd=get_tasks", response_data);
3679  }
3680 
3681  if (read_string_c (connection, &xml))
3682  {
3683  g_string_free (xml, TRUE);
3684  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3685  return gsad_message (credentials,
3686  "Internal error", __FUNCTION__, __LINE__,
3687  "An internal error occurred while getting tag list. "
3688  "The current list of tags is not available. "
3689  "Diagnostics: Failure to receive response from manager daemon.",
3690  "/omp?cmd=get_tasks", response_data);
3691  }
3692  }
3693 
3694  if (extra_xml)
3695  g_string_append (xml, extra_xml);
3696  if (message)
3697  g_string_append_printf (xml, GSAD_MESSAGE_INVALID, message, "Create Task");
3698  g_string_append_printf (xml,
3699  "<user>%s</user>"
3700  "<apply_overrides>%i</apply_overrides>"
3701  "<alerts>%s</alerts>"
3702  "</new_task>",
3703  credentials->username,
3704  apply_overrides,
3705  alerts ? alerts : "1");
3706 
3707  return xsl_transform_omp (connection, credentials, params,
3708  g_string_free (xml, FALSE), response_data);
3709 }
3710 
3721 char *
3722 new_task_omp (openvas_connection_t *connection, credentials_t * credentials,
3723  params_t *params, cmd_response_data_t* response_data)
3724 {
3725  return new_task (connection, credentials, NULL, params, NULL, response_data);
3726 }
3727 
3740 static char *
3741 new_container_task (openvas_connection_t *connection,
3742  credentials_t * credentials, const char *message,
3743  params_t *params, const char *extra_xml,
3744  cmd_response_data_t* response_data)
3745 {
3746  GString *xml;
3747 
3748  xml = g_string_new ("<new_container_task>");
3749 
3750  if (extra_xml)
3751  g_string_append (xml, extra_xml);
3752 
3753  if (message)
3754  g_string_append_printf (xml, GSAD_MESSAGE_INVALID, message, "Create Task");
3755 
3756  g_string_append_printf (xml, "</new_container_task>");
3757 
3758  return xsl_transform_omp (connection, credentials, params,
3759  g_string_free (xml, FALSE), response_data);
3760 }
3761 
3772 char *
3773 new_container_task_omp (openvas_connection_t *connection,
3774  credentials_t * credentials, params_t *params,
3775  cmd_response_data_t* response_data)
3776 {
3777  return new_container_task (connection, credentials, NULL, params, NULL,
3778  response_data);
3779 }
3780 
3792 static char *
3793 upload_report (openvas_connection_t *connection, credentials_t *credentials,
3794  params_t *params, const char *extra_xml,
3795  cmd_response_data_t* response_data)
3796 {
3797  GString *xml;
3798 
3799  xml = g_string_new ("<upload_report>");
3800  if (extra_xml)
3801  g_string_append (xml, extra_xml);
3802 
3803  if (command_enabled (credentials, "GET_TASKS"))
3804  {
3805  gchar *response;
3806 
3807  if (simple_ompf (connection, "getting Tasks", credentials, &response,
3808  response_data,
3809  "<get_tasks"
3810  /* All container tasks. */
3811  " filter=\"target= rows=-1 owner=any permission=any\"/>"))
3812  {
3813  g_string_free (xml, TRUE);
3814  return response;
3815  }
3816 
3817  g_string_append (xml, response);
3818  }
3819 
3820  g_string_append (xml, "</upload_report>");
3821 
3822  return xsl_transform_omp (connection, credentials, params,
3823  g_string_free (xml, FALSE), response_data);
3824 }
3825 
3836 char *
3837 upload_report_omp (openvas_connection_t *connection, credentials_t *credentials,
3838  params_t *params, cmd_response_data_t* response_data)
3839 {
3840  return upload_report (connection, credentials, params, NULL, response_data);
3841 }
3842 
3853 char *
3854 create_report_omp (openvas_connection_t *connection,
3855  credentials_t * credentials, params_t *params,
3856  cmd_response_data_t* response_data)
3857 {
3858  entity_t entity;
3859  int ret;
3860  gchar *command, *html, *response;
3861  const char *no_redirect, *cmd, *task_id, *name, *comment, *xml_file;
3862  const char *in_assets;
3863 
3864  no_redirect = params_value (params, "no_redirect");
3865  task_id = params_value (params, "task_id");
3866  xml_file = params_value (params, "xml_file");
3867  name = params_value (params, "name");
3868  comment = params_value (params, "comment");
3869  in_assets = params_value (params, "in_assets");
3870 
3871  if (task_id == NULL)
3872  {
3873  CHECK_PARAM_INVALID (name, "Create Report", "new_container_task");
3874  CHECK_PARAM_INVALID (comment, "Create Report", "new_container_task");
3875  }
3876  CHECK_PARAM_INVALID (xml_file, "Create Report", "new_container_task");
3877 
3878  if (params_given (params, "in_assets"))
3879  CHECK_PARAM_INVALID (xml_file, "Create Report", "new_container_task");
3880 
3881  if (strlen (xml_file) == 0)
3882  {
3883  if (task_id)
3884  return message_invalid (connection, credentials, params, response_data,
3885  "Report required",
3886  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
3887  "Create Report", "new_container_task");
3888 
3889  /* Create only the container task. */
3890 
3891  command = g_markup_printf_escaped ("<create_task>"
3892  "<target id=\"0\"/>"
3893  "<name>%s</name>"
3894  "<comment>%s</comment>"
3895  "</create_task>",
3896  name,
3897  comment);
3898  }
3899  else
3900  {
3901  gchar **xml_file_array, *xml_file_escaped;
3902 
3903  xml_file_array = g_strsplit (xml_file, "%", -1);
3904  if (xml_file_array != NULL && xml_file_array[0] != NULL)
3905  xml_file_escaped = g_strjoinv ("%%", xml_file_array);
3906  else
3907  xml_file_escaped = g_strdup (xml_file);
3908  g_strfreev (xml_file_array);
3909 
3910  if (task_id)
3911  command = g_strdup_printf ("<create_report>"
3912  "<in_assets>%s</in_assets>"
3913  "<task id=\"%s\"/>"
3914  "%s"
3915  "</create_report>",
3916  in_assets ? in_assets : "0",
3917  task_id ? task_id : "0",
3918  xml_file_escaped ? xml_file_escaped : "");
3919  else
3920  {
3921  gchar *name_escaped, *comment_escaped;
3922  name_escaped = name ? g_markup_escape_text (name, -1)
3923  : NULL;
3924  comment_escaped = comment ? g_markup_escape_text (comment, -1)
3925  : NULL;
3926  command = g_strdup_printf ("<create_report>"
3927  "<in_assets>%s</in_assets>"
3928  "<task>"
3929  "<name>%s</name>"
3930  "<comment>%s</comment>"
3931  "</task>"
3932  "%s"
3933  "</create_report>",
3934  in_assets ? in_assets : "",
3935  name_escaped,
3936  comment_escaped,
3937  xml_file_escaped);
3938  g_free (name_escaped);
3939  g_free (comment_escaped);
3940  }
3941  g_free (xml_file_escaped);
3942  }
3943 
3944  ret = omp (connection, credentials,
3945  &response,
3946  &entity,
3947  response_data,
3948  command);
3949  g_free (command);
3950 
3951  switch (ret)
3952  {
3953  case 0:
3954  break;
3955  case -1:
3956  /* 'omp' set response. */
3957  return response;
3958  case 1:
3959  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3960  return gsad_message (credentials,
3961  "Internal error", __FUNCTION__, __LINE__,
3962  "An internal error occurred while creating a new report. "
3963  "No new report was created. "
3964  "Diagnostics: Failure to send command to manager daemon.",
3965  "/omp?cmd=get_tasks", response_data);
3966  case 2:
3967  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3968  return gsad_message (credentials,
3969  "Internal error", __FUNCTION__, __LINE__,
3970  "An internal error occurred while creating a new report. "
3971  "It is unclear whether the report has been created or not. "
3972  "Diagnostics: Failure to receive response from manager daemon.",
3973  "/omp?cmd=get_tasks", response_data);
3974  default:
3975  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3976  return gsad_message (credentials,
3977  "Internal error", __FUNCTION__, __LINE__,
3978  "An internal error occurred while creating a new report. "
3979  "It is unclear whether the report has been created or not. "
3980  "Diagnostics: Internal Error.",
3981  "/omp?cmd=get_tasks", response_data);
3982  }
3983 
3984  cmd = params_value (params, "cmd");
3985  html = response_from_entity (connection, credentials, params, entity,
3986  (no_redirect && strcmp (no_redirect, "0")),
3987  NULL, "get_tasks",
3988  NULL,
3989  (cmd && strcmp (cmd, "import_report"))
3990  ? "new_container_task" : "upload_report",
3991  "Import Report", response_data);
3992  free_entity (entity);
3993  g_free (response);
3994  return html;
3995 }
3996 
4007 char *
4008 import_report_omp (openvas_connection_t *connection,
4009  credentials_t * credentials, params_t *params,
4010  cmd_response_data_t* response_data)
4011 {
4012  return create_report_omp (connection, credentials, params, response_data);
4013 }
4014 
4015 #define CHECK(name) \
4016 CHECK_PARAM_INVALID (name, "Create Task", "new_task")
4017 
4028 char *
4029 create_container_task_omp (openvas_connection_t *connection,
4030  credentials_t * credentials, params_t *params,
4031  cmd_response_data_t* response_data)
4032 {
4033  entity_t entity;
4034  int ret;
4035  gchar *command, *html, *response;
4036  const char *no_redirect, *name, *comment;
4037 
4038  no_redirect = params_value (params, "no_redirect");
4039  name = params_value (params, "name");
4040  comment = params_value (params, "comment");
4041  CHECK_PARAM_INVALID (name, "Create Container Task", "new_container_task");
4042  CHECK_PARAM_INVALID (comment, "Create Container Task", "new_container_task");
4043 
4044  command = g_markup_printf_escaped ("<create_task>"
4045  "<target id=\"0\"/>"
4046  "<name>%s</name>"
4047  "<comment>%s</comment>"
4048  "</create_task>",
4049  name,
4050  comment);
4051  ret = omp (connection, credentials,
4052  &response,
4053  &entity,
4054  response_data,
4055  command);
4056  g_free (command);
4057 
4058  switch (ret)
4059  {
4060  case 0:
4061  break;
4062  case -1:
4063  /* 'omp' set response. */
4064  return response;
4065  case 1:
4066  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4067  return gsad_message (credentials,
4068  "Internal error", __FUNCTION__, __LINE__,
4069  "An internal error occurred while creating a container task. "
4070  "No task was created. "
4071  "Diagnostics: Failure to send command to manager daemon.",
4072  "/omp?cmd=get_tasks", response_data);
4073  case 2:
4074  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4075  return gsad_message (credentials,
4076  "Internal error", __FUNCTION__, __LINE__,
4077  "An internal error occurred while creating a container task. "
4078  "It is unclear whether the task has been created or not. "
4079  "Diagnostics: Failure to receive response from manager daemon.",
4080  "/omp?cmd=get_tasks", response_data);
4081  default:
4082  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4083  return gsad_message (credentials,
4084  "Internal error", __FUNCTION__, __LINE__,
4085  "An internal error occurred while creating a container task. "
4086  "It is unclear whether the task has been created or not. "
4087  "Diagnostics: Internal Error.",
4088  "/omp?cmd=get_tasks", response_data);
4089  }
4090 
4091  if (entity_attribute (entity, "id"))
4092  params_add (params, "task_id", entity_attribute (entity, "id"));
4093  html = response_from_entity (connection, credentials, params, entity,
4094  (no_redirect && strcmp (no_redirect, "0")),
4095  NULL, "get_tasks",
4096  NULL, "new_container_task",
4097  "Create Container Task", response_data);
4098  free_entity (entity);
4099  g_free (response);
4100  return html;
4101 }
4102 
4113 char *
4114 create_task_omp (openvas_connection_t *connection, credentials_t * credentials,
4115  params_t *params, cmd_response_data_t* response_data)
4116 {
4117  entity_t entity;
4118  int ret;
4119  gchar *schedule_element, *command;
4120  gchar *response, *html;
4121  const char *no_redirect;
4122  const char *name, *comment, *config_id, *target_id, *scanner_type;
4123  const char *scanner_id, *schedule_id, *schedule_periods;
4124  const char *max_checks, *max_hosts;
4125  const char *in_assets, *hosts_ordering, *alterable, *source_iface;
4126  const char *add_tag, *tag_name, *tag_value, *auto_delete, *auto_delete_data;
4127  const char *apply_overrides, *min_qod;
4128  gchar *name_escaped, *comment_escaped;
4129  params_t *alerts;
4130  GString *alert_element;
4131 
4132  no_redirect = params_value (params, "no_redirect");
4133  name = params_value (params, "name");
4134  comment = params_value (params, "comment");
4135  target_id = params_value (params, "target_id");
4136  scanner_type = params_value (params, "scanner_type");
4137  hosts_ordering = params_value (params, "hosts_ordering");
4138  schedule_id = params_value (params, "schedule_id");
4139  schedule_periods = params_value (params, "schedule_periods");
4140  scanner_id = params_value (params, "scanner_id");
4141  config_id = params_value (params, "config_id");
4142  in_assets = params_value (params, "in_assets");
4143  apply_overrides = params_value (params, "apply_overrides");
4144  min_qod = params_value (params, "min_qod");
4145  max_checks = params_value (params, "max_checks");
4146  source_iface = params_value (params, "source_iface");
4147  auto_delete = params_value (params, "auto_delete");
4148  auto_delete_data = params_value (params, "auto_delete_data");
4149  max_hosts = params_value (params, "max_hosts");
4150  alterable = params_value (params, "alterable");
4151  add_tag = params_value (params, "add_tag");
4152  tag_name = params_value (params, "tag_name");
4153  tag_value = params_value (params, "tag_value");
4154  CHECK (scanner_type);
4155  if (!strcmp (scanner_type, "1"))
4156  {
4157  hosts_ordering = "";
4158  max_checks = "";
4159  source_iface = "";
4160  max_hosts = "";
4161  }
4162  else if (!strcmp (scanner_type, "3"))
4163  {
4164  config_id = "";
4165  hosts_ordering = "";
4166  max_checks = "";
4167  source_iface = "";
4168  max_hosts = "";
4169  }
4170 
4171  CHECK (name);
4172  CHECK (comment);
4173  CHECK (config_id);
4174  CHECK (target_id);
4175  CHECK (hosts_ordering);
4176  CHECK (scanner_id);
4177  CHECK (schedule_id);
4178  if (params_given (params, "schedule_periods"))
4179  {
4180  CHECK (schedule_periods);
4181  }
4182  else
4183  schedule_periods = "0";
4184  CHECK (in_assets);
4185  if (!strcmp (in_assets, "1"))
4186  {
4187  CHECK (apply_overrides);
4188  CHECK (min_qod);
4189  }
4190  else
4191  {
4192  if (!params_given (params, "apply_overrides")
4193  || !params_valid (params, "apply_overrides"))
4194  apply_overrides = "";
4195 
4196  if (!params_given (params, "min_qod")
4197  || !params_valid (params, "min_qod"))
4198  min_qod = "";
4199  }
4200  CHECK (max_checks);
4201  CHECK (source_iface);
4202  CHECK (auto_delete);
4203  CHECK (auto_delete_data);
4204  CHECK (max_hosts);
4205  CHECK (alterable);
4206  if (add_tag)
4207  {
4208  CHECK (add_tag);
4209  CHECK (tag_name);
4210  CHECK (tag_value);
4211  }
4212 
4213  if (schedule_id == NULL || strcmp (schedule_id, "0") == 0)
4214  schedule_element = g_strdup ("");
4215  else
4216  schedule_element = g_strdup_printf ("<schedule id=\"%s\"/>", schedule_id);
4217 
4218  alert_element = g_string_new ("");
4219  if (params_given (params, "alert_id_optional:"))
4220  alerts = params_values (params, "alert_id_optional:");
4221  else
4222  alerts = params_values (params, "alert_ids:");
4223 
4224  if (alerts)
4225  {
4226  params_iterator_t iter;
4227  char *name;
4228  param_t *param;
4229 
4230  params_iterator_init (&iter, alerts);
4231  while (params_iterator_next (&iter, &name, &param))
4232  if (param->value && strcmp (param->value, "0"))
4233  g_string_append_printf (alert_element,
4234  "<alert id=\"%s\"/>",
4235  param->value ? param->value : "");
4236  }
4237 
4238  name_escaped = name ? g_markup_escape_text (name, -1) : NULL;
4239  comment_escaped = comment ? g_markup_escape_text (comment, -1) : NULL;
4240 
4241  command = g_strdup_printf ("<create_task>"
4242  "<config id=\"%s\"/>"
4243  "<schedule_periods>%s</schedule_periods>"
4244  "%s%s"
4245  "<target id=\"%s\"/>"
4246  "<scanner id=\"%s\"/>"
4247  "<hosts_ordering>%s</hosts_ordering>"
4248  "<name>%s</name>"
4249  "<comment>%s</comment>"
4250  "<preferences>"
4251  "<preference>"
4252  "<scanner_name>max_checks</scanner_name>"
4253  "<value>%s</value>"
4254  "</preference>"
4255  "<preference>"
4256  "<scanner_name>max_hosts</scanner_name>"
4257  "<value>%s</value>"
4258  "</preference>"
4259  "<preference>"
4260  "<scanner_name>in_assets</scanner_name>"
4261  "<value>%s</value>"
4262  "</preference>"
4263  "<preference>"
4264  "<scanner_name>"
4265  "assets_apply_overrides"
4266  "</scanner_name>"
4267  "<value>%s</value>"
4268  "</preference>"
4269  "<preference>"
4270  "<scanner_name>assets_min_qod</scanner_name>"
4271  "<value>%s</value>"
4272  "</preference>"
4273  "<preference>"
4274  "<scanner_name>source_iface</scanner_name>"
4275  "<value>%s</value>"
4276  "</preference>"
4277  "<preference>"
4278  "<scanner_name>auto_delete</scanner_name>"
4279  "<value>%s</value>"
4280  "</preference>"
4281  "<preference>"
4282  "<scanner_name>auto_delete_data</scanner_name>"
4283  "<value>%s</value>"
4284  "</preference>"
4285  "</preferences>"
4286  "<alterable>%i</alterable>"
4287  "</create_task>",
4288  config_id,
4289  schedule_periods,
4290  schedule_element,
4291  alert_element->str,
4292  target_id,
4293  scanner_id,
4294  hosts_ordering,
4295  name_escaped,
4296  comment_escaped,
4297  max_checks,
4298  max_hosts,
4299  strcmp (in_assets, "0") ? "yes" : "no",
4300  strcmp (apply_overrides, "0") ? "yes" : "no",
4301  min_qod,
4302  source_iface,
4303  auto_delete,
4304  auto_delete_data,
4305  alterable ? strcmp (alterable, "0") : 0);
4306 
4307  g_free (name_escaped);
4308  g_free (comment_escaped);
4309 
4310  ret = omp (connection, credentials,
4311  &response,
4312  &entity,
4313  response_data,
4314  command);
4315  g_free (command);
4316 
4317  g_free (schedule_element);
4318  g_string_free (alert_element, TRUE);
4319 
4320  switch (ret)
4321  {
4322  case 0:
4323  break;
4324  case -1:
4325  /* 'omp' set response. */
4326  return response;
4327  case 1:
4328  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4329  return gsad_message (credentials,
4330  "Internal error", __FUNCTION__, __LINE__,
4331  "An internal error occurred while creating a new task. "
4332  "No new task was created. "
4333  "Diagnostics: Failure to send command to manager daemon.",
4334  "/omp?cmd=get_tasks", response_data);
4335  case 2:
4336  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4337  return gsad_message (credentials,
4338  "Internal error", __FUNCTION__, __LINE__,
4339  "An internal error occurred while creating a new task. "
4340  "It is unclear whether the task has been created or not. "
4341  "Diagnostics: Failure to receive response from manager daemon.",
4342  "/omp?cmd=get_tasks", response_data);
4343  default:
4344  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4345  return gsad_message (credentials,
4346  "Internal error", __FUNCTION__, __LINE__,
4347  "An internal error occurred while creating a new task. "
4348  "It is unclear whether the task has been created or not. "
4349  "Diagnostics: Internal Error.",
4350  "/omp?cmd=get_tasks", response_data);
4351  }
4352 
4353  if (omp_success (entity))
4354  {
4355  if (add_tag && strcmp (add_tag, "0"))
4356  {
4357  const char *new_task_id = entity_attribute (entity, "id");
4358  gchar *tag_command, *tag_response;
4359  entity_t tag_entity;
4360 
4361  if (tag_value && strcmp (tag_value, ""))
4362  tag_command
4363  = g_markup_printf_escaped ("<create_tag>"
4364  "<name>%s</name>"
4365  "<resource id=\"%s\">"
4366  "<type>task</type>"
4367  "</resource>"
4368  "<value>%s</value>"
4369  "</create_tag>",
4370  tag_name,
4371  new_task_id,
4372  tag_value);
4373  else
4374  tag_command
4375  = g_markup_printf_escaped ("<create_tag>"
4376  "<name>%s</name>"
4377  "<resource id=\"%s\">"
4378  "<type>task</type>"
4379  "</resource>"
4380  "</create_tag>",
4381  tag_name,
4382  new_task_id);
4383 
4384  ret = omp (connection, credentials,
4385  &tag_response,
4386  &tag_entity,
4387  response_data,
4388  tag_command);
4389 
4390  switch (ret)
4391  {
4392  case 0:
4393  case -1:
4394  break;
4395  case 1:
4396  free_entity (entity);
4397  g_free (response);
4398  response_data->http_status_code
4399  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4400  return gsad_message (credentials,
4401  "Internal error", __FUNCTION__, __LINE__,
4402  "An internal error occurred while creating a new tag. "
4403  "No new tag was created. "
4404  "Diagnostics: Failure to send command to manager daemon.",
4405  "/omp?cmd=get_tasks", response_data);
4406  case 2:
4407  free_entity (entity);
4408  g_free (response);
4409  response_data->http_status_code
4410  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4411  return gsad_message (credentials,
4412  "Internal error", __FUNCTION__, __LINE__,
4413  "An internal error occurred while creating a new tag. "
4414  "It is unclear whether the tag has been created or not. "
4415  "Diagnostics: Failure to receive response from manager daemon.",
4416  "/omp?cmd=get_tasks", response_data);
4417  default:
4418  free_entity (entity);
4419  g_free (response);
4420  response_data->http_status_code
4421  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4422  return gsad_message (credentials,
4423  "Internal error", __FUNCTION__, __LINE__,
4424  "An internal error occurred while creating a new task. "
4425  "It is unclear whether the tag has been created or not. "
4426  "Diagnostics: Internal Error.",
4427  "/omp?cmd=get_tasks", response_data);
4428  }
4429 
4430  if (entity_attribute (entity, "id"))
4431  params_add (params, "task_id", entity_attribute (entity, "id"));
4432  html
4433  = response_from_entity (connection, credentials, params, tag_entity,
4434  (no_redirect && strcmp (no_redirect, "0")),
4435  NULL, "get_tasks",
4436  NULL, "new_tasks",
4437  "Create Task and Tag", response_data);
4438  free_entity (tag_entity);
4439  g_free (tag_response);
4440  }
4441  else
4442  {
4443  if (entity_attribute (entity, "id"))
4444  params_add (params, "task_id", entity_attribute (entity, "id"));
4445  html
4446  = response_from_entity (connection, credentials, params, entity,
4447  (no_redirect && strcmp (no_redirect, "0")),
4448  NULL, "get_tasks",
4449  NULL, "new_task",
4450  "Create Task", response_data);
4451  }
4452  }
4453  else
4454  {
4455  html = response_from_entity (connection, credentials, params, entity,
4456  (no_redirect && strcmp (no_redirect, "0")),
4457  NULL, "get_tasks",
4458  NULL, "new_task",
4459  "Create Task", response_data);
4460  }
4461  free_entity (entity);
4462  g_free (response);
4463  return html;
4464 }
4465 
4466 
4477 char *
4478 delete_task_omp (openvas_connection_t *connection, credentials_t * credentials,
4479  params_t *params, cmd_response_data_t* response_data)
4480 {
4481  return delete_resource (connection, "task", credentials, params, 0, NULL, response_data);
4482 }
4483 
4495 char *
4496 edit_task (openvas_connection_t *connection, credentials_t * credentials,
4497  params_t *params, const char *extra_xml,
4498  cmd_response_data_t* response_data)
4499 {
4500  GString *xml;
4501  const char *task_id, *next, *refresh_interval, *sort_field, *sort_order;
4502  const char *overrides, *alerts;
4503  int apply_overrides;
4504 
4505  task_id = params_value (params, "task_id");
4506  next = params_value (params, "next");
4507  refresh_interval = params_value (params, "refresh_interval");
4508  sort_field = params_value (params, "sort_field");
4509  sort_order = params_value (params, "sort_order");
4510  overrides = params_value (params, "overrides");
4511  alerts = params_value (params, "alerts");
4512 
4513  apply_overrides = overrides ? strcmp (overrides, "0") : 1;
4514 
4515  if (task_id == NULL)
4516  {
4517  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
4518  return gsad_message (credentials,
4519  "Internal error", __FUNCTION__, __LINE__,
4520  "An internal error occurred while editing a task. "
4521  "The task remains as it was. "
4522  "Diagnostics: Required parameter was NULL.",
4523  "/omp?cmd=get_tasks", response_data);
4524  }
4525 
4526  if (next == NULL)
4527  next = "get_task";
4528 
4529  if (openvas_connection_sendf (connection,
4530  "<commands>"
4531  "<get_tasks task_id=\"%s\" details=\"1\" />"
4532  "<get_targets"
4533  " filter=\"rows=-1 sort=name\"/>"
4534  "<get_configs"
4535  " filter=\"rows=-1 sort=name\"/>"
4536  "%s"
4537  "%s"
4538  "%s"
4539  "%s"
4540  "</commands>",
4541  task_id,
4542  command_enabled (credentials, "GET_ALERTS")
4543  ? "<get_alerts"
4544  " filter=\"rows=-1 sort=name\"/>"
4545  : "",
4546  command_enabled (credentials, "GET_SCHEDULES")
4547  ? "<get_schedules"
4548  " filter=\"rows=-1 sort=name\"/>"
4549  : "",
4550  command_enabled (credentials, "GET_SCANNERS")
4551  ? "<get_scanners"
4552  " filter=\"rows=-1\"/>"
4553  : "",
4554  command_enabled (credentials, "GET_GROUPS")
4555  ? "<get_groups/>"
4556  : "")
4557  == -1)
4558  {
4559  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4560  return gsad_message (credentials,
4561  "Internal error", __FUNCTION__, __LINE__,
4562  "An internal error occurred while getting task info. "
4563  "Diagnostics: Failure to send command to manager daemon.",
4564  "/omp?cmd=get_tasks", response_data);
4565  }
4566 
4567  xml = g_string_new ("");
4568 
4569  if (extra_xml)
4570  g_string_append (xml, extra_xml);
4571 
4572  g_string_append_printf (xml,
4573  "<edit_task>"
4574  "<task id=\"%s\"/>"
4575  "<user>%s</user>"
4576  "<alerts>%s</alerts>"
4577  /* Page that follows. */
4578  "<next>%s</next>"
4579  /* Passthroughs. */
4580  "<refresh_interval>%s</refresh_interval>"
4581  "<sort_field>%s</sort_field>"
4582  "<sort_order>%s</sort_order>"
4583  "<apply_overrides>%i</apply_overrides>",
4584  task_id,
4585  credentials->username,
4586  alerts ? alerts : "1",
4587  next,
4588  refresh_interval ? refresh_interval : "",
4589  sort_field,
4590  sort_order,
4591  apply_overrides);
4592 
4593  if (read_string_c (connection, &xml))
4594  {
4595  g_string_free (xml, TRUE);
4596  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4597  return gsad_message (credentials,
4598  "Internal error", __FUNCTION__, __LINE__,
4599  "An internal error occurred while getting task info. "
4600  "Diagnostics: Failure to receive response from manager daemon.",
4601  "/omp?cmd=get_tasks", response_data);
4602  }
4603 
4604  /* Cleanup, and return transformed XML. */
4605 
4606  g_string_append (xml, "</edit_task>");
4607  return xsl_transform_omp (connection, credentials, params,
4608  g_string_free (xml, FALSE), response_data);
4609 }
4610 
4621 char *
4622 edit_task_omp (openvas_connection_t *connection, credentials_t * credentials,
4623  params_t *params, cmd_response_data_t* response_data)
4624 {
4625  return edit_task (connection, credentials, params, NULL, response_data);
4626 }
4627 
4638 char *
4639 save_task_omp (openvas_connection_t *connection, credentials_t * credentials,
4640  params_t *params, cmd_response_data_t* response_data)
4641 {
4642  gchar *html, *response, *format;
4643  const char *no_redirect;
4644  const char *comment, *name, *schedule_id, *in_assets;
4645  const char *scanner_id, *task_id, *max_checks, *max_hosts;
4646  const char *config_id, *target_id, *hosts_ordering, *alterable, *source_iface;
4647  const char *scanner_type, *schedule_periods, *auto_delete, *auto_delete_data;
4648  const char *apply_overrides, *min_qod;
4649  int ret;
4650  params_t *alerts;
4651  GString *alert_element;
4652  entity_t entity;
4653 
4654  no_redirect = params_value (params, "no_redirect");
4655  comment = params_value (params, "comment");
4656  name = params_value (params, "name");
4657  task_id = params_value (params, "task_id");
4658  in_assets = params_value (params, "in_assets");
4659  apply_overrides = params_value (params, "apply_overrides");
4660  min_qod = params_value (params, "min_qod");
4661  target_id = params_value (params, "target_id");
4662  scanner_type = params_value (params, "scanner_type");
4663  hosts_ordering = params_value (params, "hosts_ordering");
4664  config_id = params_value (params, "config_id");
4665  schedule_id = params_value (params, "schedule_id");
4666  schedule_periods = params_value (params, "schedule_periods");
4667  scanner_id = params_value (params, "scanner_id");
4668  max_checks = params_value (params, "max_checks");
4669  source_iface = params_value (params, "source_iface");
4670  auto_delete = params_value (params, "auto_delete");
4671  auto_delete_data = params_value (params, "auto_delete_data");
4672  max_hosts = params_value (params, "max_hosts");
4673  alterable = params_value (params, "alterable");
4674  CHECK_PARAM_INVALID (scanner_type, "Save Task", "edit_task");
4675  if (!strcmp (scanner_type, "1"))
4676  {
4677  hosts_ordering = "";
4678  max_checks = "";
4679  source_iface = "";
4680  max_hosts = "";
4681  }
4682  else if (!strcmp (scanner_type, "3"))
4683  {
4684  config_id = "0";
4685  hosts_ordering = "";
4686  max_checks = "";
4687  source_iface = "";
4688  max_hosts = "";
4689  }
4690 
4691  CHECK_PARAM_INVALID (name, "Save Task", "edit_task");
4692  CHECK_PARAM_INVALID (comment, "Save Task", "edit_task");
4693  CHECK_PARAM_INVALID (target_id, "Save Task", "edit_task");
4694  CHECK_PARAM_INVALID (hosts_ordering, "Save Task", "edit_task");
4695  CHECK_PARAM_INVALID (config_id, "Save Task", "edit_task");
4696  CHECK_PARAM_INVALID (schedule_id, "Save Task", "edit_task");
4697  if (params_given (params, "schedule_periods"))
4698  {
4699  CHECK (schedule_periods);
4700  }
4701  else
4702  schedule_periods = "0";
4703  CHECK_PARAM_INVALID (scanner_id, "Save Task", "edit_task");
4704  CHECK_PARAM_INVALID (task_id, "Save Task", "edit_task");
4705  CHECK_PARAM_INVALID (max_checks, "Save Task", "edit_task");
4706  CHECK_PARAM_INVALID (source_iface, "Save Task", "edit_task");
4707  CHECK_PARAM_INVALID (auto_delete, "Save Task", "edit_task");
4708  CHECK_PARAM_INVALID (auto_delete_data, "Save Task", "edit_task");
4709  CHECK_PARAM_INVALID (max_hosts, "Save Task", "edit_task");
4710  CHECK_PARAM_INVALID (in_assets, "Save Task", "edit_task");
4711 
4712  if (!strcmp (in_assets, "1"))
4713  {
4714  CHECK_PARAM_INVALID (apply_overrides, "Save Task", "edit_task");
4715  CHECK_PARAM_INVALID (min_qod, "Save Task", "edit_task");
4716  }
4717  else
4718  {
4719  if (!params_given (params, "apply_overrides")
4720  || !params_valid (params, "apply_overrides"))
4721  apply_overrides = "";
4722 
4723  if (!params_given (params, "min_qod")
4724  || !params_valid (params, "min_qod"))
4725  min_qod = "";
4726  }
4727 
4728  alert_element = g_string_new ("");
4729  if (params_given (params, "alert_id_optional:"))
4730  alerts = params_values (params, "alert_id_optional:");
4731  else
4732  alerts = params_values (params, "alert_ids:");
4733 
4734  if (alerts)
4735  {
4736  params_iterator_t iter;
4737  char *name;
4738  param_t *param;
4739 
4740  params_iterator_init (&iter, alerts);
4741  while (params_iterator_next (&iter, &name, &param))
4742  {
4743  if (param->value && strcmp (param->value, "0"))
4744  g_string_append_printf (alert_element,
4745  "<alert id=\"%s\"/>",
4746  param->value ? param->value : "");
4747  }
4748  }
4749 
4750  // Remove Alerts from Task if none are given.
4751  if (strcmp (alert_element->str, "") == 0)
4752  g_string_append_printf (alert_element, "<alert id=\"0\"/>");
4753 
4754  format = g_strdup_printf ("<modify_task task_id=\"%%s\">"
4755  "<name>%%s</name>"
4756  "<comment>%%s</comment>"
4757  "<hosts_ordering>%s</hosts_ordering>"
4758  "%s"
4759  "<target id=\"%%s\"/>"
4760  "<config id=\"%%s\"/>"
4761  "<schedule id=\"%%s\"/>"
4762  "<schedule_periods>%%s</schedule_periods>"
4763  "<scanner id=\"%%s\"/>"
4764  "<preferences>"
4765  "<preference>"
4766  "<scanner_name>max_checks</scanner_name>"
4767  "<value>%%s</value>"
4768  "</preference>"
4769  "<preference>"
4770  "<scanner_name>max_hosts</scanner_name>"
4771  "<value>%%s</value>"
4772  "</preference>"
4773  "<preference>"
4774  "<scanner_name>in_assets</scanner_name>"
4775  "<value>%%s</value>"
4776  "</preference>"
4777  "<preference>"
4778  "<scanner_name>assets_apply_overrides</scanner_name>"
4779  "<value>%%s</value>"
4780  "</preference>"
4781  "<preference>"
4782  "<scanner_name>assets_min_qod</scanner_name>"
4783  "<value>%%s</value>"
4784  "</preference>"
4785  "<preference>"
4786  "<scanner_name>source_iface</scanner_name>"
4787  "<value>%%s</value>"
4788  "</preference>"
4789  "<preference>"
4790  "<scanner_name>auto_delete</scanner_name>"
4791  "<value>%%s</value>"
4792  "</preference>"
4793  "<preference>"
4794  "<scanner_name>auto_delete_data</scanner_name>"
4795  "<value>%%s</value>"
4796  "</preference>"
4797  "</preferences>"
4798  "%s%i%s"
4799  "</modify_task>",
4800  hosts_ordering,
4801  alert_element->str,
4802  alterable ? "<alterable>" : "",
4803  alterable ? strcmp (alterable, "0") : 0,
4804  alterable ? "</alterable>" : "");
4805  response = NULL;
4806  entity = NULL;
4807  ret = ompf (connection,
4808  credentials,
4809  &response,
4810  &entity,
4811  response_data,
4812  format,
4813  task_id,
4814  name,
4815  comment,
4816  target_id,
4817  config_id,
4818  schedule_id,
4819  schedule_periods,
4820  scanner_id,
4821  max_checks,
4822  max_hosts,
4823  strcmp (in_assets, "0") ? "yes" : "no",
4824  strcmp (apply_overrides, "0") ? "yes" : "no",
4825  min_qod,
4826  source_iface,
4827  auto_delete,
4828  auto_delete_data);
4829  g_free (format);
4830 
4831  g_string_free (alert_element, TRUE);
4832 
4833  switch (ret)
4834  {
4835  case 0:
4836  case -1:
4837  break;
4838  case 1:
4839  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4840  return gsad_message (credentials,
4841  "Internal error", __FUNCTION__, __LINE__,
4842  "An internal error occurred while saving a task. "
4843  "The task was not saved. "
4844  "Diagnostics: Failure to send command to manager daemon.",
4845  "/omp?cmd=get_tasks", response_data);
4846  case 2:
4847  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4848  return gsad_message (credentials,
4849  "Internal error", __FUNCTION__, __LINE__,
4850  "An internal error occurred while saving a task. "
4851  "It is unclear whether the task has been saved or not. "
4852  "Diagnostics: Failure to receive response from manager daemon.",
4853  "/omp?cmd=get_tasks", response_data);
4854  default:
4855  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4856  return gsad_message (credentials,
4857  "Internal error", __FUNCTION__, __LINE__,
4858  "An internal error occurred while saving a task. "
4859  "It is unclear whether the task has been saved or not. "
4860  "Diagnostics: Internal Error.",
4861  "/omp?cmd=get_tasks", response_data);
4862  }
4863 
4864  html = response_from_entity (connection, credentials, params, entity,
4865  (no_redirect && strcmp (no_redirect, "0")),
4866  NULL, "get_tasks",
4867  NULL, "edit_task",
4868  "Save Task", response_data);
4869  free_entity (entity);
4870  g_free (response);
4871  return html;
4872 }
4873 
4874 #undef CHECK
4875 
4886 char *
4887 save_container_task_omp (openvas_connection_t *connection,
4888  credentials_t * credentials, params_t *params,
4889  cmd_response_data_t* response_data)
4890 {
4891  gchar *format, *response, *html;
4892  const char *no_redirect, *comment, *name, *task_id;
4893  const char *in_assets, *auto_delete, *auto_delete_data;
4894  int ret;
4895  entity_t entity;
4896 
4897  no_redirect = params_value (params, "no_redirect");
4898  comment = params_value (params, "comment");
4899  in_assets = params_value (params, "in_assets");
4900  name = params_value (params, "name");
4901  task_id = params_value (params, "task_id");
4902  auto_delete = params_value (params, "auto_delete");
4903  auto_delete_data = params_value (params, "auto_delete_data");
4904  CHECK_PARAM_INVALID (name, "Save Task", "edit_task")
4905  CHECK_PARAM_INVALID (comment, "Save Task", "edit_task")
4906  CHECK_PARAM_INVALID (task_id, "Save Task", "edit_task")
4907  CHECK_PARAM_INVALID (in_assets, "Save Task", "edit_task")
4908  CHECK_PARAM_INVALID (auto_delete, "Save Task", "edit_task");
4909  CHECK_PARAM_INVALID (auto_delete_data, "Save Task", "edit_task");
4910 
4911  format = g_strdup_printf ("<modify_task task_id=\"%%s\">"
4912  "<name>%%s</name>"
4913  "<comment>%%s</comment>"
4914  "<preferences>"
4915  "<preference>"
4916  "<scanner_name>in_assets</scanner_name>"
4917  "<value>%%s</value>"
4918  "</preference>"
4919  "<preference>"
4920  "<scanner_name>auto_delete</scanner_name>"
4921  "<value>%%s</value>"
4922  "</preference>"
4923  "<preference>"
4924  "<scanner_name>auto_delete_data</scanner_name>"
4925  "<value>%%s</value>"
4926  "</preference>"
4927  "</preferences>"
4928  "</modify_task>");
4929 
4930  response = NULL;
4931  entity = NULL;
4932  ret = ompf (connection, credentials, &response, &entity, response_data,
4933  format, task_id, name, comment,
4934  strcmp (in_assets, "0") ? "yes" : "no",
4935  auto_delete,
4936  auto_delete_data);
4937  g_free (format);
4938  switch (ret)
4939  {
4940  case 0:
4941  case -1:
4942  break;
4943  case 1:
4944  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4945  return gsad_message (credentials,
4946  "Internal error", __FUNCTION__, __LINE__,
4947  "An internal error occurred while saving a task. "
4948  "No new task was created. "
4949  "Diagnostics: Failure to send command to manager daemon.",
4950  "/omp?cmd=get_tasks", response_data);
4951  case 2:
4952  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4953  return gsad_message (credentials,
4954  "Internal error", __FUNCTION__, __LINE__,
4955  "An internal error occurred while saving a task. "
4956  "It is unclear whether the task has been created or not. "
4957  "Diagnostics: Failure to receive response from manager daemon.",
4958  "/omp?cmd=get_tasks", response_data);
4959  default:
4960  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4961  return gsad_message (credentials,
4962  "Internal error", __FUNCTION__, __LINE__,
4963  "An internal error occurred while saving a task. "
4964  "It is unclear whether the task has been created or not. "
4965  "Diagnostics: Internal Error.",
4966  "/omp?cmd=get_tasks", response_data);
4967  }
4968 
4969  html = response_from_entity (connection, credentials, params, entity,
4970  (no_redirect && strcmp (no_redirect, "0")),
4971  NULL, "get_tasks",
4972  NULL, "edit_task",
4973  "Save Container Task", response_data);
4974  free_entity (entity);
4975  g_free (response);
4976  return html;
4977 }
4978 
4992 char *
4993 export_task_omp (openvas_connection_t *connection, credentials_t * credentials,
4994  params_t *params, enum content_type * content_type,
4995  char **content_disposition, gsize *content_length,
4996  cmd_response_data_t* response_data)
4997 {
4998  return export_resource (connection, "task", credentials, params, content_type,
4999  content_disposition, content_length, response_data);
5000 }
5001 
5016 char *
5017 export_tasks_omp (openvas_connection_t *connection, credentials_t * credentials,
5018  params_t *params, enum content_type * content_type,
5019  char **content_disposition, gsize *content_length,
5020  cmd_response_data_t* response_data)
5021 {
5022  return export_many (connection, "task", credentials, params, content_type,
5023  content_disposition, content_length, response_data);
5024 }
5025 
5036 char *
5037 stop_task_omp (openvas_connection_t *connection, credentials_t * credentials,
5038  params_t *params, cmd_response_data_t* response_data)
5039 {
5040  return resource_action (connection, credentials, params, "task", "stop",
5041  response_data);
5042 }
5043 
5054 char *
5055 resume_task_omp (openvas_connection_t *connection, credentials_t * credentials,
5056  params_t *params, cmd_response_data_t* response_data)
5057 {
5058  return resource_action (connection, credentials, params, "task", "resume",
5059  response_data);
5060 }
5061 
5072 char *
5073 start_task_omp (openvas_connection_t *connection, credentials_t * credentials,
5074  params_t *params, cmd_response_data_t* response_data)
5075 {
5076  return resource_action (connection, credentials, params, "task", "start",
5077  response_data);
5078 }
5079 
5090 char *
5091 move_task_omp (openvas_connection_t *connection, credentials_t * credentials,
5092  params_t *params, cmd_response_data_t* response_data)
5093 {
5094  gchar *command, *response, *html;
5095  const char *no_redirect, *task_id, *slave_id;
5096  int ret;
5097  entity_t entity;
5098 
5099  no_redirect = params_value (params, "no_redirect");
5100  slave_id = params_value (params, "slave_id");
5101  task_id = params_value (params, "task_id");
5102 
5103  command = g_strdup_printf ("<move_task task_id=\"%s\" slave_id=\"%s\"/>",
5104  task_id ? task_id : "",
5105  slave_id ? slave_id : "");
5106 
5107  response = NULL;
5108  entity = NULL;
5109  ret = omp (connection, credentials, &response, &entity, response_data,
5110  command);
5111  g_free (command);
5112  switch (ret)
5113  {
5114  case 0:
5115  break;
5116  case -1:
5117  /* 'omp' set response. */
5118  return response;
5119  case 1:
5120  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5121  return gsad_message (credentials,
5122  "Internal error", __FUNCTION__, __LINE__,
5123  "An internal error occurred while moving a task. "
5124  "The task was not moved. "
5125  "Diagnostics: Failure to send command to manager daemon.",
5126  "/omp?cmd=get_tasks", response_data);
5127  case 2:
5128  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5129  return gsad_message (credentials,
5130  "Internal error", __FUNCTION__, __LINE__,
5131  "An internal error occurred while moving a task. "
5132  "It is unclear whether the task has been moved or not. "
5133  "Diagnostics: Failure to receive response from manager daemon.",
5134  "/omp?cmd=get_tasks", response_data);
5135  default:
5136  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5137  return gsad_message (credentials,
5138  "Internal error", __FUNCTION__, __LINE__,
5139  "An internal error occurred while moving a task. "
5140  "It is unclear whether the task has been moved or not. "
5141  "Diagnostics: Internal Error.",
5142  "/omp?cmd=get_tasks", response_data);
5143  }
5144 
5145  html = response_from_entity (connection, credentials, params, entity,
5146  (no_redirect && strcmp (no_redirect, "0")),
5147  NULL, "get_tasks",
5148  NULL, "get_tasks",
5149  "Move Task", response_data);
5150 
5151  free_entity (entity);
5152  g_free (response);
5153  return html;
5154 }
5155 
5168 static char*
5169 get_nvts (openvas_connection_t *connection, credentials_t *credentials,
5170  params_t *params, const char *commands, const char *extra_xml,
5171  cmd_response_data_t* response_data)
5172 {
5173  GString *xml = NULL;
5174  const char *oid;
5175 
5176  oid = params_value (params, "oid");
5177  if (oid == NULL)
5178  {
5179  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5180  return gsad_message (credentials,
5181  "Internal error", __FUNCTION__, __LINE__,
5182  "An internal error occurred while getting an NVT. "
5183  "Diagnostics: Required parameter was NULL.",
5184  "/omp?cmd=get_tasks", response_data);
5185  }
5186 
5187  if (openvas_connection_sendf (connection,
5188  "<commands>"
5189  "%s"
5190  "<get_nvts"
5191  " nvt_oid=\"%s\""
5192  " details=\"1\""
5193  " preferences=\"1\"/>"
5194  "<get_notes"
5195  " nvt_oid=\"%s\""
5196  " sort_field=\"notes.text\"/>"
5197  "<get_overrides"
5198  " nvt_oid=\"%s\""
5199  " sort_field=\"overrides.text\"/>"
5200  "</commands>",
5201  commands ? commands : "",
5202  oid,
5203  oid,
5204  oid)
5205  == -1)
5206  {
5207  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5208  return gsad_message (credentials,
5209  "Internal error", __FUNCTION__, __LINE__,
5210  "An internal error occurred while getting nvt details. "
5211  "Diagnostics: Failure to send command to manager daemon.",
5212  "/omp?cmd=get_tasks", response_data);
5213  }
5214 
5215  xml = g_string_new ("<get_nvts>");
5216  if (read_string_c (connection, &xml))
5217  {
5218  g_string_free (xml, TRUE);
5219  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5220  return gsad_message (credentials,
5221  "Internal error", __FUNCTION__, __LINE__,
5222  "An internal error occurred while getting nvt details. "
5223  "Diagnostics: Failure to receive response from manager daemon.",
5224  "/omp?cmd=get_tasks", response_data);
5225  }
5226 
5227  /* Append extra_xml */
5228  if (extra_xml)
5229  g_string_append (xml, extra_xml);
5230 
5231  /* Get tag names */
5232 
5233  if (openvas_connection_sendf (connection,
5234  "<get_tags"
5235  " filter=\"resource_type=nvt"
5236  " first=1"
5237  " rows=-1\""
5238  " names_only=\"1\""
5239  "/>")
5240  == -1)
5241  {
5242  g_string_free (xml, TRUE);
5243  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5244  return gsad_message (credentials,
5245  "Internal error", __FUNCTION__, __LINE__,
5246  "An internal error occurred while getting tag names list. "
5247  "The current list of resources is not available. "
5248  "Diagnostics: Failure to send command to manager daemon.",
5249  "/omp?cmd=get_resources", response_data);
5250  }
5251 
5252  if (read_string_c (connection, &xml))
5253  {
5254  g_string_free (xml, TRUE);
5255  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5256  return gsad_message (credentials,
5257  "Internal error", __FUNCTION__, __LINE__,
5258  "An internal error occurred while getting tag names list. "
5259  "The current list of resources is not available. "
5260  "Diagnostics: Failure to receive response from manager daemon.",
5261  "/omp?cmd=get_resources", response_data);
5262  }
5263 
5264  g_string_append (xml, "</get_nvts>");
5265 
5266  return xsl_transform_omp (connection, credentials, params,
5267  g_string_free (xml, FALSE), response_data);
5268 }
5269 
5281 char *
5282 get_info (openvas_connection_t *connection, credentials_t *credentials,
5283  params_t *params, const char *extra_xml,
5284  cmd_response_data_t* response_data)
5285 {
5286  char *ret;
5287  GString *extra_attribs, *extra_response;
5288  const char *info_type;
5289 
5290  info_type = params_value (params, "info_type");
5291  if (info_type == NULL)
5292  {
5293  param_t *param;
5294  param = params_add (params, "info_type", "nvt");
5295  param->valid = 1;
5296  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
5297  info_type = params_value (params, "info_type");
5298  }
5299 
5300  if (strcmp (info_type, "nvt")
5301  && strcmp (info_type, "cve")
5302  && strcmp (info_type, "cpe")
5303  && strcmp (info_type, "ovaldef")
5304  && strcmp (info_type, "cert_bund_adv")
5305  && strcmp (info_type, "dfn_cert_adv")
5306  && strcmp (info_type, "allinfo")
5307  && strcmp (info_type, "NVT")
5308  && strcmp (info_type, "CVE")
5309  && strcmp (info_type, "CPE")
5310  && strcmp (info_type, "OVALDEF")
5311  && strcmp (info_type, "CERT_BUND_ADV")
5312  && strcmp (info_type, "DFN_CERT_ADV")
5313  && strcmp (info_type, "ALLINFO"))
5314  {
5315  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5316  return gsad_message (credentials,
5317  "Internal error", __FUNCTION__, __LINE__,
5318  "An internal error occurred while getting SecInfo. "
5319  "Diagnostics: Invalid info_type parameter value",
5320  "/omp?cmd=get_info", response_data);
5321  }
5322 
5323  if (params_value (params, "info_name")
5324  && params_value (params, "info_id"))
5325  {
5326  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5327  return gsad_message (credentials,
5328  "Internal error", __FUNCTION__, __LINE__,
5329  "An internal error occurred while getting SecInfo. "
5330  "Diagnostics: Both ID and Name set.",
5331  "/omp?cmd=get_info", response_data);
5332  }
5333  extra_response = g_string_new (extra_xml ? extra_xml : "");
5334 
5335  if (command_enabled (credentials, "GET_NOTES")
5336  && (strcasecmp (info_type, "NVT") == 0)
5337  && params_value (params, "info_id"))
5338  {
5339  gchar *response;
5340 
5341  if (simple_ompf (connection, "getting SecInfo", credentials, &response,
5342  response_data,
5343  "<get_notes"
5344  " nvt_oid=\"%s\""
5345  " sort_field=\"notes.text\"/>",
5346  params_value (params, "info_id")))
5347  {
5348  g_string_free (extra_response, TRUE);
5349  return response;
5350  }
5351 
5352  g_string_append (extra_response, response);
5353  }
5354 
5355  if (command_enabled (credentials, "GET_OVERRIDES")
5356  && (strcasecmp (info_type, "NVT") == 0)
5357  && params_value (params, "info_id"))
5358  {
5359  gchar *response;
5360 
5361  if (simple_ompf (connection, "getting SecInfo", credentials, &response,
5362  response_data,
5363  "<get_overrides"
5364  " nvt_oid=\"%s\""
5365  " sort_field=\"overrides.text\"/>",
5366  params_value (params, "info_id")))
5367  {
5368  g_string_free (extra_response, TRUE);
5369  return response;
5370  }
5371 
5372  g_string_append (extra_response, response);
5373  }
5374 
5375  extra_attribs = g_string_new("");
5376  g_string_append_printf (extra_attribs, "type=\"%s\"",
5377  params_value (params, "info_type"));
5378  if (params_value (params, "info_name"))
5379  g_string_append_printf (extra_attribs,
5380  " name=\"%s\"",
5381  params_value (params, "info_name"));
5382  else if (params_value (params, "info_id"))
5383  g_string_append_printf (extra_attribs,
5384  " info_id=\"%s\"",
5385  params_value (params, "info_id"));
5386  if (params_value (params, "details"))
5387  g_string_append_printf (extra_attribs,
5388  " details=\"%s\"",
5389  params_value (params, "details"));
5390  ret = get_many (connection, "info", credentials, params, extra_response->str,
5391  extra_attribs->str, response_data);
5392 
5393  g_string_free (extra_response, TRUE);
5394  g_string_free (extra_attribs, TRUE);
5395 
5396  return ret;
5397 }
5398 
5409 char *
5410 get_info_omp (openvas_connection_t *connection, credentials_t * credentials,
5411  params_t *params,
5412  cmd_response_data_t* response_data)
5413 {
5414  return get_info (connection, credentials, params, NULL, response_data);
5415 }
5416 
5427 char*
5428 get_nvts_omp (openvas_connection_t *connection, credentials_t *credentials,
5429  params_t *params, cmd_response_data_t* response_data)
5430 {
5431  return get_nvts (connection, credentials, params, NULL, NULL, response_data);
5432 }
5433 
5440 static void
5441 params_toggle_overrides (params_t *params, const char *overrides)
5442 {
5443  param_t *filt_id, *build_filter;
5444  const char *new_filt_id;
5445 
5446  build_filter = params_get (params, "build_filter");
5447 
5448  if (build_filter)
5449  new_filt_id = "";
5450  else
5451  new_filt_id = "0";
5452 
5453  filt_id = params_get (params, "filt_id");
5454  if (filt_id)
5455  {
5456  filt_id->value = g_strdup (new_filt_id);
5457  filt_id->value_size = strlen (filt_id->value);
5458  filt_id->valid = 1;
5459  filt_id->valid_utf8 = 1;
5460  }
5461  else
5462  params_add (params, "filt_id", new_filt_id);
5463 
5464  if (build_filter == NULL)
5465  {
5466  param_t *filter;
5467  filter = params_get (params, "filter");
5468  if (filter && filter->value)
5469  {
5470  gchar *old;
5471  old = filter->value;
5472  filter->value = g_strdup_printf ("apply_overrides=%s %s",
5473  overrides,
5474  old);
5475  g_free (old);
5476  }
5477  else if (strcmp (overrides, "0"))
5478  params_add (params, "filter",
5479  "apply_overrides=1 rows=-2");
5480  else
5481  params_add (params, "filter",
5482  "apply_overrides=0 rows=-2");
5483  }
5484 }
5485 
5497 static char *
5498 get_tasks (openvas_connection_t *connection, credentials_t *credentials,
5499  params_t *params, const char *extra_xml,
5500  cmd_response_data_t* response_data)
5501 {
5502  const char *overrides, *schedules_only, *ignore_pagination;
5503  gchar *extra_attribs, *ret;
5504 
5505  overrides = params_value (params, "overrides");
5506  schedules_only = params_value (params, "schedules_only");
5507  ignore_pagination = params_value (params, "ignore_pagination");
5508 
5509  if (overrides)
5510  /* User toggled overrides. Set the overrides value in the filter. */
5511  params_toggle_overrides (params, overrides);
5512 
5513  extra_attribs = g_strdup_printf ("%s%s%s"
5514  "%s%s%s",
5515  schedules_only ? "schedules_only=\"" : "",
5516  schedules_only ? schedules_only : "",
5517  schedules_only ? "\" " : "",
5518  ignore_pagination
5519  ? "ignore_pagination=\""
5520  : "",
5521  ignore_pagination ? ignore_pagination : "",
5522  ignore_pagination ? "\" " : "");
5523 
5524  ret = get_many (connection, "task", credentials, params, extra_xml,
5525  extra_attribs, response_data);
5526  g_free (extra_attribs);
5527  return ret;
5528 }
5529 
5540 char *
5541 get_tasks_omp (openvas_connection_t *connection, credentials_t * credentials,
5542  params_t *params, cmd_response_data_t* response_data)
5543 {
5544  return get_tasks (connection, credentials, params, NULL, response_data);
5545 }
5546 
5558 static char *
5559 get_tasks_chart (openvas_connection_t *connection, credentials_t *credentials,
5560  params_t *params, const char *extra_xml,
5561  cmd_response_data_t* response_data)
5562 {
5563  return xsl_transform_omp (connection, credentials, params,
5564  g_strdup ("<get_tasks_chart/>"), response_data);
5565 }
5566 
5577 char *
5578 get_tasks_chart_omp (openvas_connection_t *connection,
5579  credentials_t * credentials, params_t *params,
5580  cmd_response_data_t* response_data)
5581 {
5582  return get_tasks_chart (connection, credentials, params, NULL, response_data);
5583 }
5584 
5585 
5597 static char *
5598 get_task (openvas_connection_t *connection, credentials_t *credentials,
5599  params_t *params, const char *extra_xml,
5600  cmd_response_data_t* response_data)
5601 {
5602  GString *xml = NULL;
5603  GString *commands_xml = NULL;
5604  entity_t commands_entity = NULL;
5605  entity_t task_entity = NULL;
5606  int notes, get_overrides, apply_overrides;
5607  int get_target, get_alerts;
5608  const char *overrides, *task_id;
5609 
5610  task_id = params_value (params, "task_id");
5611  if (task_id == NULL)
5612  return get_tasks (connection, credentials, params, extra_xml,
5613  response_data);
5614 
5615  overrides = params_value (params, "overrides");
5616  apply_overrides = overrides ? strcmp (overrides, "0") : 1;
5617 
5618  notes = command_enabled (credentials, "GET_NOTES");
5619  get_overrides = command_enabled (credentials, "GET_OVERRIDES");
5620  if (openvas_connection_sendf
5621  (connection,
5622  "<commands>"
5623  "<get_tasks"
5624  " task_id=\"%s\""
5625  " filter=\"apply_overrides=%i\""
5626  " details=\"0\"/>"
5627  "%s%s%s"
5628  "%s%s%s"
5629  "</commands>",
5630  task_id,
5631  apply_overrides,
5632  notes
5633  ? "<get_notes"
5634  " sort_field=\"notes_nvt_name, notes.text\""
5635  " task_id=\""
5636  : "",
5637  notes ? task_id : "",
5638  notes ? "\"/>" : "",
5639  get_overrides
5640  ? "<get_overrides"
5641  " sort_field=\"overrides_nvt_name, overrides.text\""
5642  " task_id=\""
5643  : "",
5644  get_overrides ? task_id : "",
5645  get_overrides ? "\"/>" : "")
5646  == -1)
5647  {
5648  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5649  return gsad_message (credentials,
5650  "Internal error", __FUNCTION__, __LINE__,
5651  "An internal error occurred while getting the status. "
5652  "No update on the requested task can be retrieved. "
5653  "Diagnostics: Failure to send command to manager daemon.",
5654  "/omp?cmd=get_tasks", response_data);
5655  }
5656 
5657  commands_xml = g_string_new ("");
5658  xml = g_string_new ("<get_task>");
5659 
5660  if (extra_xml)
5661  g_string_append (xml, extra_xml);
5662 
5663  g_string_append_printf (xml,
5664  "<apply_overrides>%i</apply_overrides>"
5665  "<delta>%s</delta>",
5666  apply_overrides,
5667  params_value (params, "delta_report_id")
5668  ? params_value (params, "delta_report_id")
5669  : "");
5670  if (read_string_c (connection, &commands_xml))
5671  {
5672  g_string_free (commands_xml, TRUE);
5673  g_string_free (xml, TRUE);
5674  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5675  return gsad_message (credentials,
5676  "Internal error", __FUNCTION__, __LINE__,
5677  "An internal error occurred while getting the status. "
5678  "No update of the status can be retrieved. "
5679  "Diagnostics: Failure to receive response from manager daemon.",
5680  "/omp?cmd=get_tasks", response_data);
5681  }
5682  g_string_append (xml, commands_xml->str);
5683 
5684  if (parse_entity (commands_xml->str, &commands_entity))
5685  {
5686  g_string_free (commands_xml, TRUE);
5687  g_string_free (xml, TRUE);
5688  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5689  return gsad_message (credentials,
5690  "Internal error", __FUNCTION__, __LINE__,
5691  "An internal error occurred while getting the status. "
5692  "No update of the status can be retrieved. "
5693  "Diagnostics: Failure to parse response from manager daemon.",
5694  "/omp?cmd=get_tasks", response_data);
5695  }
5696 
5697  get_target = command_enabled (credentials, "GET_TARGETS");
5698  get_alerts = command_enabled (credentials, "GET_ALERTS");
5699  task_entity = entity_child (commands_entity, "get_tasks_response");
5700  if (task_entity == NULL)
5701  {
5702  g_warning ("%s: No get_tasks_response found in manager response.",
5703  __FUNCTION__);
5704  }
5705  else
5706  {
5707  task_entity = entity_child (task_entity, "task");
5708  if (task_entity == NULL)
5709  g_message ("%s: No task found in manager response.", __FUNCTION__);
5710  else if (get_target || get_alerts)
5711  {
5712  entities_t child_entities;
5713  entity_t child_entity;
5714  child_entities = task_entity->entities;
5715 
5716  while ((child_entity = first_entity (child_entities)))
5717  {
5718  if (get_alerts &&
5719  strcmp (entity_name (child_entity), "alert") == 0)
5720  {
5721  const char *resource_id
5722  = entity_attribute (child_entity, "id");
5723 
5724  if (resource_id != NULL && strcmp (resource_id, ""))
5725  {
5726  if (openvas_connection_sendf (connection,
5727  "<get_alerts"
5728  " alert_id=\"%s\"/>",
5729  resource_id))
5730  {
5731  g_string_free (xml, TRUE);
5732  g_string_free (commands_xml, TRUE);
5733  free_entity (commands_entity);
5734  response_data->http_status_code
5735  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5736  return gsad_message (credentials,
5737  "Internal error",
5738  __FUNCTION__, __LINE__,
5739  "An internal error occurred while getting an alert of a task. "
5740  "Diagnostics: Failure to send command to manager daemon.",
5741  "/omp?cmd=get_tasks",
5742  response_data);
5743  }
5744  if (read_string_c (connection, &xml))
5745  {
5746  g_string_free (commands_xml, TRUE);
5747  g_string_free (xml, TRUE);
5748  free_entity (commands_entity);
5749  response_data->http_status_code
5750  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5751  return gsad_message (credentials,
5752  "Internal error",
5753  __FUNCTION__, __LINE__,
5754  "An internal error occurred while getting an alert of a task. "
5755  "Diagnostics: Failure to receive response from manager daemon.",
5756  "/omp?cmd=get_tasks",
5757  response_data);
5758  }
5759  }
5760  }
5761 
5762  if (get_target &&
5763  strcmp (entity_name (child_entity), "target") == 0)
5764  {
5765  const char *resource_id
5766  = entity_attribute (child_entity, "id");
5767 
5768  if (resource_id != NULL && strcmp (resource_id, ""))
5769  {
5770  if (openvas_connection_sendf (connection,
5771  "<get_targets"
5772  " target_id=\"%s\"/>",
5773  resource_id))
5774  {
5775  g_string_free (xml, TRUE);
5776  g_string_free (commands_xml, TRUE);
5777  free_entity (commands_entity);
5778  response_data->http_status_code
5779  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5780  return gsad_message (credentials,
5781  "Internal error",
5782  __FUNCTION__, __LINE__,
5783  "An internal error occurred while getting the target of a task. "
5784  "Diagnostics: Failure to send command to manager daemon.",
5785  "/omp?cmd=get_tasks",
5786  response_data);
5787  }
5788  if (read_string_c (connection, &xml))
5789  {
5790  g_string_free (commands_xml, TRUE);
5791  g_string_free (xml, TRUE);
5792  free_entity (commands_entity);
5793  response_data->http_status_code
5794  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5795  return gsad_message (credentials,
5796  "Internal error",
5797  __FUNCTION__, __LINE__,
5798  "An internal error occurred while getting the target of a task. "
5799  "Diagnostics: Failure to receive response from manager daemon.",
5800  "/omp?cmd=get_tasks",
5801  response_data);
5802  }
5803  }
5804  }
5805 
5806  child_entities = next_entities (child_entities);
5807  }
5808  }
5809  }
5810 
5811  g_string_free (commands_xml, TRUE);
5812  free_entity (commands_entity);
5813 
5814  /* Get slave scanners. */
5815 
5816  if (command_enabled (credentials, "GET_SCANNERS"))
5817  {
5818  if (openvas_connection_sendf (connection,
5819  "<get_scanners"
5820  " filter=\"first=1 rows=-1 type=4\"/>")
5821  == -1)
5822  {
5823  g_string_free (xml, TRUE);
5824  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5825  return gsad_message (credentials,
5826  "Internal error", __FUNCTION__, __LINE__,
5827  "An internal error occurred while getting slaves list. "
5828  "The current list of resources is not available. "
5829  "Diagnostics: Failure to send command to manager daemon.",
5830  "/omp?cmd=get_tasks", response_data);
5831  }
5832 
5833  if (read_string_c (connection, &xml))
5834  {
5835  g_string_free (xml, TRUE);
5836  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5837  return gsad_message (credentials,
5838  "Internal error", __FUNCTION__, __LINE__,
5839  "An internal error occurred while getting slaves list. "
5840  "The current list of resources is not available. "
5841  "Diagnostics: Failure to receive response from manager daemon.",
5842  "/omp?cmd=get_tasks", response_data);
5843  }
5844  }
5845 
5846  /* Get tag names */
5847 
5848  if (openvas_connection_sendf (connection,
5849  "<get_tags"
5850  " filter=\"resource_type=task"
5851  " first=1"
5852  " rows=-1\""
5853  " names_only=\"1\""
5854  "/>")
5855  == -1)
5856  {
5857  g_string_free (xml, TRUE);
5858  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5859  return gsad_message (credentials,
5860  "Internal error", __FUNCTION__, __LINE__,
5861  "An internal error occurred while getting tag names list. "
5862  "The current list of resources is not available. "
5863  "Diagnostics: Failure to send command to manager daemon.",
5864  "/omp?cmd=get_resources", response_data);
5865  }
5866 
5867  if (read_string_c (connection, &xml))
5868  {
5869  g_string_free (xml, TRUE);
5870  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5871  return gsad_message (credentials,
5872  "Internal error", __FUNCTION__, __LINE__,
5873  "An internal error occurred while getting tag names list. "
5874  "The current list of resources is not available. "
5875  "Diagnostics: Failure to receive response from manager daemon.",
5876  "/omp?cmd=get_resources", response_data);
5877  }
5878 
5879  /* Get permissions */
5880 
5881  g_string_append (xml, "<permissions>");
5882 
5883  if (openvas_connection_sendf (connection,
5884  "<get_permissions"
5885  " filter=\"name:^.*(task)s?$"
5886  " and resource_uuid=%s"
5887  " first=1 rows=-1\"/>",
5888  task_id)
5889  == -1)
5890  {
5891  g_string_free (xml, TRUE);
5892  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5893  return gsad_message (credentials,
5894  "Internal error", __FUNCTION__, __LINE__,
5895  "An internal error occurred while getting permissions list. "
5896  "The current list of resources is not available. "
5897  "Diagnostics: Failure to send command to manager daemon.",
5898  "/omp?cmd=get_resources", response_data);
5899  }
5900 
5901  if (read_string_c (connection, &xml))
5902  {
5903  g_string_free (xml, TRUE);
5904  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5905  return gsad_message (credentials,
5906  "Internal error", __FUNCTION__, __LINE__,
5907  "An internal error occurred while getting permissions list. "
5908  "The current list of resources is not available. "
5909  "Diagnostics: Failure to receive response from manager daemon.",
5910  "/omp?cmd=get_resources", response_data);
5911  }
5912 
5913  g_string_append (xml, "</permissions>");
5914 
5915  g_string_append (xml, "</get_task>");
5916 
5917  return xsl_transform_omp (connection, credentials, params,
5918  g_string_free (xml, FALSE), response_data);
5919 }
5920 
5931 char *
5932 get_task_omp (openvas_connection_t *connection, credentials_t * credentials,
5933  params_t *params, cmd_response_data_t* response_data)
5934 {
5935  return get_task (connection, credentials, params, NULL, response_data);
5936 }
5937 
5949 static char *
5950 new_credential (openvas_connection_t *connection, credentials_t *credentials,
5951  params_t *params, const char *extra_xml,
5952  cmd_response_data_t* response_data)
5953 {
5954  GString *xml;
5955  xml = g_string_new ("<new_credential>");
5956  if (extra_xml)
5957  g_string_append (xml, extra_xml);
5958  g_string_append (xml, "</new_credential>");
5959  return xsl_transform_omp (connection, credentials, params,
5960  g_string_free (xml, FALSE), response_data);
5961 }
5962 
5973 char *
5974 create_credential_omp (openvas_connection_t *connection,
5975  credentials_t * credentials, params_t *params,
5976  cmd_response_data_t* response_data)
5977 {
5978  int ret;
5979  gchar *html, *response;
5980  const char *no_redirect;
5981  const char *name, *comment, *login, *type, *password, *passphrase;
5982  const char *private_key, *certificate, *community, *privacy_password;
5983  const char *auth_algorithm, *privacy_algorithm, *allow_insecure;
5984  int autogenerate;
5985  entity_t entity;
5986 
5987  no_redirect = params_value (params, "no_redirect");
5988  name = params_value (params, "name");
5989  comment = params_value (params, "comment");
5990  login = params_value (params, "credential_login");
5991  type = params_value (params, "base");
5992  password = params_value (params, "lsc_password");
5993  passphrase = params_value (params, "passphrase");
5994  private_key = params_value (params, "private_key");
5995  certificate = params_value (params, "certificate");
5996  community = params_value (params, "community");
5997  privacy_password = params_value (params, "privacy_password");
5998  auth_algorithm = params_value (params, "auth_algorithm");
5999  privacy_algorithm = params_value (params, "privacy_algorithm");
6000  allow_insecure = params_value (params, "allow_insecure");
6001 
6002  if (params_value (params, "autogenerate"))
6003  autogenerate = strcmp (params_value (params, "autogenerate"), "0");
6004  else if (params_given (params, "autogenerate"))
6005  return message_invalid (connection, credentials, params, response_data,
6006  "Given autogenerate was invalid",
6007  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
6008  "Create Credential", "new_credential");
6009  else
6010  autogenerate = 0;
6011 
6012  CHECK_PARAM_INVALID (name, "Create Credential", "new_credential");
6013  CHECK_PARAM_INVALID (comment, "Create Credential", "new_credential");
6014  CHECK_PARAM_INVALID (type, "Create Credential", "new_credential");
6015  CHECK_PARAM_INVALID (allow_insecure, "Create Credential", "new_credential");
6016 
6017  if (autogenerate)
6018  {
6019  if (type && (strcmp (type, "cc") == 0))
6020  {
6021  // Auto-generate types without username
6022  ret = ompf (connection,
6023  credentials,
6024  &response,
6025  &entity,
6026  response_data,
6027  "<create_credential>"
6028  "<name>%s</name>"
6029  "<comment>%s</comment>"
6030  "<type>%s</type>"
6031  "<allow_insecure>%s</allow_insecure>"
6032  "</create_credential>",
6033  name,
6034  comment ? comment : "",
6035  type,
6036  allow_insecure);
6037  }
6038  else
6039  {
6040  // Auto-generate types with username
6041  CHECK_PARAM_INVALID (login, "Create Credential", "new_credential");
6042 
6043  ret = ompf (connection, credentials,
6044  &response,
6045  &entity,
6046  response_data,
6047  "<create_credential>"
6048  "<name>%s</name>"
6049  "<comment>%s</comment>"
6050  "<type>%s</type>"
6051  "<login>%s</login>"
6052  "<allow_insecure>%s</allow_insecure>"
6053  "</create_credential>",
6054  name,
6055  comment ? comment : "",
6056  type,
6057  login,
6058  allow_insecure);
6059  }
6060  }
6061  else
6062  {
6063  if (type && (strcmp (type, "up") == 0))
6064  {
6065  CHECK_PARAM_INVALID (login,
6066  "Create Credential", "new_credential");
6067  CHECK_PARAM_INVALID (password,
6068  "Create Credential", "new_credential");
6069 
6070  ret = ompf (connection, credentials,
6071  &response,
6072  &entity,
6073  response_data,
6074  "<create_credential>"
6075  "<name>%s</name>"
6076  "<comment>%s</comment>"
6077  "<type>%s</type>"
6078  "<login>%s</login>"
6079  "<password>%s</password>"
6080  "<allow_insecure>%s</allow_insecure>"
6081  "</create_credential>",
6082  name,
6083  comment ? comment : "",
6084  type,
6085  login ? login : "",
6086  password ? password : "",
6087  allow_insecure);
6088  }
6089  else if (type && (strcmp (type, "usk") == 0))
6090  {
6091  CHECK_PARAM_INVALID (login,
6092  "Create Credential", "new_credential");
6093  CHECK_PARAM_INVALID (passphrase,
6094  "Create Credential", "new_credential");
6095  CHECK_PARAM_INVALID (private_key,
6096  "Create Credential", "new_credential");
6097 
6098  ret = ompf (connection, credentials,
6099  &response,
6100  &entity,
6101  response_data,
6102  "<create_credential>"
6103  "<name>%s</name>"
6104  "<comment>%s</comment>"
6105  "<type>%s</type>"
6106  "<login>%s</login>"
6107  "<key>"
6108  "<private>%s</private>"
6109  "<phrase>%s</phrase>"
6110  "</key>"
6111  "<allow_insecure>%s</allow_insecure>"
6112  "</create_credential>",
6113  name,
6114  comment ? comment : "",
6115  type,
6116  login ? login : "",
6117  private_key ? private_key : "",
6118  passphrase ? passphrase : "",
6119  allow_insecure);
6120  }
6121  else if (type && (strcmp (type, "cc") == 0))
6122  {
6123  CHECK_PARAM_INVALID (certificate,
6124  "Create Credential", "new_credential");
6125  CHECK_PARAM_INVALID (private_key,
6126  "Create Credential", "new_credential");
6127 
6128  ret = ompf (connection, credentials,
6129  &response,
6130  &entity,
6131  response_data,
6132  "<create_credential>"
6133  "<name>%s</name>"
6134  "<comment>%s</comment>"
6135  "<type>%s</type>"
6136  "<certificate>%s</certificate>"
6137  "<key>"
6138  "<private>%s</private>"
6139  "</key>"
6140  "<allow_insecure>%s</allow_insecure>"
6141  "</create_credential>",
6142  name,
6143  comment ? comment : "",
6144  type,
6145  certificate ? certificate : "",
6146  private_key ? private_key : "",
6147  allow_insecure);
6148 
6149  }
6150  else if (type && (strcmp (type, "snmp") == 0))
6151  {
6152  CHECK_PARAM_INVALID (community,
6153  "Create Credential", "new_credential");
6154  CHECK_PARAM_INVALID (login,
6155  "Create Credential", "new_credential");
6156  CHECK_PARAM_INVALID (password,
6157  "Create Credential", "new_credential");
6158  CHECK_PARAM_INVALID (privacy_password,
6159  "Create Credential", "new_credential");
6160  CHECK_PARAM_INVALID (auth_algorithm,
6161  "Create Credential", "new_credential");
6162  CHECK_PARAM_INVALID (privacy_algorithm,
6163  "Create Credential", "new_credential");
6164 
6165  if (privacy_password && strcmp (privacy_password, ""))
6166  ret = ompf (connection, credentials,
6167  &response,
6168  &entity,
6169  response_data,
6170  "<create_credential>"
6171  "<name>%s</name>"
6172  "<comment>%s</comment>"
6173  "<type>%s</type>"
6174  "<community>%s</community>"
6175  "<login>%s</login>"
6176  "<password>%s</password>"
6177  "<privacy>"
6178  "<password>%s</password>"
6179  "<algorithm>%s</algorithm>"
6180  "</privacy>"
6181  "<auth_algorithm>%s</auth_algorithm>"
6182  "<allow_insecure>%s</allow_insecure>"
6183  "</create_credential>",
6184  name,
6185  comment ? comment : "",
6186  type,
6187  community ? community : "",
6188  login ? login : "",
6189  password ? password : "",
6190  privacy_password ? privacy_password : "",
6191  privacy_algorithm ? privacy_algorithm : "",
6192  auth_algorithm ? auth_algorithm : "",
6193  allow_insecure);
6194  else
6195  ret = ompf (connection, credentials,
6196  &response,
6197  &entity,
6198  response_data,
6199  "<create_credential>"
6200  "<name>%s</name>"
6201  "<comment>%s</comment>"
6202  "<type>%s</type>"
6203  "<community>%s</community>"
6204  "<login>%s</login>"
6205  "<password>%s</password>"
6206  "<auth_algorithm>%s</auth_algorithm>"
6207  "<allow_insecure>%s</allow_insecure>"
6208  "</create_credential>",
6209  name,
6210  comment ? comment : "",
6211  type,
6212  community ? community : "",
6213  login ? login : "",
6214  password ? password : "",
6215  auth_algorithm ? auth_algorithm : "",
6216  allow_insecure);
6217  }
6218  else
6219  {
6220  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6221  return gsad_message (credentials,
6222  "Internal error", __FUNCTION__, __LINE__,
6223  "An internal error occurred while creating a new credential. "
6224  "The credential could not be created. "
6225  "Diagnostics: Unrecognized credential type.",
6226  "/omp?cmd=get_credentials", response_data);
6227  }
6228  }
6229 
6230  /* Create the credential. */
6231  switch (ret)
6232  {
6233  case 0:
6234  case -1:
6235  break;
6236  case 1:
6237  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6238  return gsad_message (credentials,
6239  "Internal error", __FUNCTION__, __LINE__,
6240  "An internal error occurred while creating a new credential. "
6241  "It is unclear whether the credential has been created or not. "
6242  "Diagnostics: Failure to send command to manager daemon.",
6243  "/omp?cmd=get_credentials", response_data);
6244  case 2:
6245  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6246  return gsad_message (credentials,
6247  "Internal error", __FUNCTION__, __LINE__,
6248  "An internal error occurred while creating a new credential. "
6249  "It is unclear whether the credential has been created or not. "
6250  "Diagnostics: Failure to receive response from manager daemon.",
6251  "/omp?cmd=get_credentials", response_data);
6252  default:
6253  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6254  return gsad_message (credentials,
6255  "Internal error", __FUNCTION__, __LINE__,
6256  "An internal error occurred while creating a new credential. "
6257  "It is unclear whether the credential has been created or not. "
6258  "Diagnostics: Internal Error.",
6259  "/omp?cmd=get_credentials", response_data);
6260  }
6261 
6262  if (entity_attribute (entity, "id"))
6263  params_add (params, "credential_id", entity_attribute (entity, "id"));
6264  html = response_from_entity (connection, credentials, params, entity,
6265  (no_redirect && strcmp (no_redirect, "0")),
6266  NULL, "get_credentials",
6267  NULL, "new_credential",
6268  "Create Credential",
6269  response_data);
6270  free_entity (entity);
6271  g_free (response);
6272  return html;
6273 }
6274 
6286 static char *
6287 get_credential (openvas_connection_t *connection, credentials_t * credentials,
6288  params_t *params, const char *extra_xml,
6289  cmd_response_data_t* response_data)
6290 {
6291  return get_one (connection, "credential", credentials, params, extra_xml,
6292  "targets=\"1\" scanners=\"1\"", response_data);
6293 }
6294 
6305 char *
6306 get_credential_omp (openvas_connection_t *connection,
6307  credentials_t * credentials, params_t *params,
6308  cmd_response_data_t* response_data)
6309 {
6310  return get_credential (connection, credentials, params, NULL, response_data);
6311 }
6312 
6327 int
6328 download_credential_omp (openvas_connection_t *connection,
6329  credentials_t * credentials,
6330  params_t *params,
6331  gsize *result_len,
6332  char ** html,
6333  char ** login,
6334  cmd_response_data_t* response_data)
6335 {
6336  entity_t entity;
6337  const char *credential_id, *format;
6338 
6339  assert (html);
6340 
6341  if (result_len) *result_len = 0;
6342 
6343  /* Send the request. */
6344 
6345  credential_id = params_value (params, "credential_id");
6346  format = params_value (params, "package_format");
6347 
6348  if ((credential_id == NULL) || (format == NULL))
6349  {
6350  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
6351  *html = gsad_message (credentials,
6352  "Internal error", __FUNCTION__, __LINE__,
6353  "An internal error occurred while getting a credential. "
6354  "Diagnostics: Required parameter was NULL.",
6355  "/omp?cmd=get_credentials", response_data);
6356  return 1;
6357  }
6358 
6359  if (openvas_connection_sendf (connection,
6360  "<get_credentials"
6361  " credential_id=\"%s\""
6362  " format=\"%s\"/>",
6363  credential_id,
6364  format)
6365  == -1)
6366  {
6367  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6368  *html = gsad_message (credentials,
6369  "Internal error", __FUNCTION__, __LINE__,
6370  "An internal error occurred while getting a credential. "
6371  "Diagnostics: Failure to send command to manager daemon.",
6372  "/omp?cmd=get_credentials", response_data);
6373  return 1;
6374  }
6375 
6376  /* Read and handle the response. */
6377 
6378  if (strcmp (format, "rpm") == 0
6379  || strcmp (format, "deb") == 0
6380  || strcmp (format, "exe") == 0)
6381  {
6382  gchar *package_decoded = NULL;
6383  entity_t package_entity = NULL, credential_entity;
6384 
6385  /* A base64 encoded package. */
6386 
6387  entity = NULL;
6388  if (read_entity_c (connection, &entity))
6389  {
6390  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6391  *html = gsad_message (credentials,
6392  "Internal error", __FUNCTION__, __LINE__,
6393  "An internal error occurred while getting a credential. "
6394  "The credential is not available. "
6395  "Diagnostics: Failure to receive response from manager daemon.",
6396  "/omp?cmd=get_credentials", response_data);
6397  return 1;
6398  }
6399 
6400  credential_entity = entity_child (entity, "credential");
6401  if (credential_entity)
6402  package_entity = entity_child (credential_entity, "package");
6403  if (package_entity != NULL)
6404  {
6405  gsize len;
6406  char *package_encoded = entity_text (package_entity);
6407  if (strlen (package_encoded))
6408  {
6409  package_decoded = (gchar *) g_base64_decode (package_encoded,
6410  &len);
6411  if (package_decoded == NULL)
6412  {
6413  package_decoded = (gchar *) g_strdup ("");
6414  len = 0;
6415  }
6416  }
6417  else
6418  {
6419  package_decoded = (gchar *) g_strdup ("");
6420  len = 0;
6421  }
6422  if (result_len) *result_len = len;
6423  *html = package_decoded;
6424  if (login)
6425  {
6426  entity_t login_entity;
6427  login_entity = entity_child (credential_entity, "login");
6428  if (login_entity)
6429  *login = g_strdup (entity_text (login_entity));
6430  else
6431  *login = NULL;
6432  }
6433  free_entity (entity);
6434  return 0;
6435  }
6436  else
6437  {
6438  free_entity (entity);
6439  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6440  *html = gsad_message (credentials,
6441  "Internal error", __FUNCTION__, __LINE__,
6442  "An internal error occurred while getting a credential. "
6443  "The credential could not be delivered. "
6444  "Diagnostics: Failure to receive credential from manager daemon.",
6445  "/omp?cmd=get_credentials", response_data);
6446  return 1;
6447  }
6448  }
6449  else
6450  {
6451  entity_t credential_entity, key_entity = NULL;
6452 
6453  /* A key or certificate. */
6454 
6455  entity = NULL;
6456  if (read_entity_c (connection, &entity))
6457  {
6458  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6459  *html = gsad_message (credentials,
6460  "Internal error", __FUNCTION__, __LINE__,
6461  "An internal error occurred while getting a credential. "
6462  "The credential could not be delivered. "
6463  "Diagnostics: Failure to receive credential from manager daemon.",
6464  "/omp?cmd=get_credentials", response_data);
6465  return 1;
6466  }
6467 
6468  credential_entity = entity_child (entity, "credential");
6469  if (credential_entity)
6470  {
6471  if (strcmp (format, "pem") == 0)
6472  key_entity = entity_child (credential_entity, "certificate");
6473  else
6474  key_entity = entity_child (credential_entity, "public_key");
6475  }
6476  if (key_entity != NULL)
6477  {
6478  *html = g_strdup (entity_text (key_entity));
6479  if (login)
6480  {
6481  entity_t login_entity = entity_child (credential_entity, "login");
6482  if (login_entity)
6483  *login = g_strdup (entity_text (login_entity));
6484  else
6485  *login = NULL;
6486  }
6487  free_entity (entity);
6488  return 0;
6489  }
6490  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6491  *html = gsad_message (credentials,
6492  "Internal error", __FUNCTION__, __LINE__,
6493  "An internal error occurred while getting a credential. "
6494  "The credential could not be delivered. "
6495  "Diagnostics: Failure to parse credential from manager daemon.",
6496  "/omp?cmd=get_credentials", response_data);
6497  free_entity (entity);
6498  return 1;
6499  }
6500 }
6501 
6516 char *
6517 export_credential_omp (openvas_connection_t *connection,
6518  credentials_t * credentials, params_t *params,
6519  enum content_type * content_type,
6520  char **content_disposition, gsize *content_length,
6521  cmd_response_data_t* response_data)
6522 {
6523  return export_resource (connection, "credential", credentials, params,
6524  content_type, content_disposition, content_length,
6525  response_data);
6526 }
6527 
6542 char *
6543 export_credentials_omp (openvas_connection_t *connection,
6544  credentials_t * credentials, params_t *params,
6545  enum content_type * content_type,
6546  char **content_disposition, gsize *content_length,
6547  cmd_response_data_t* response_data)
6548 {
6549  return export_many (connection, "credential", credentials, params,
6550  content_type, content_disposition, content_length,
6551  response_data);
6552 }
6553 
6566 static char *
6567 get_credentials (openvas_connection_t *connection, credentials_t * credentials,
6568  params_t *params, const char *extra_xml,
6569  cmd_response_data_t* response_data)
6570 {
6571  return get_many (connection, "credential", credentials, params, extra_xml,
6572  NULL, response_data);
6573 }
6574 
6585 char *
6586 get_credentials_omp (openvas_connection_t *connection,
6587  credentials_t * credentials, params_t *params,
6588  cmd_response_data_t* response_data)
6589 {
6590  return get_credentials (connection, credentials, params, NULL, response_data);
6591 }
6592 
6603 char *
6604 delete_credential_omp (openvas_connection_t *connection,
6605  credentials_t * credentials, params_t *params,
6606  cmd_response_data_t* response_data)
6607 {
6608  return delete_resource (connection, "credential", credentials, params, 0,
6609  "get_credentials", response_data);
6610 }
6611 
6622 char *
6623 new_credential_omp (openvas_connection_t *connection,
6624  credentials_t *credentials, params_t *params,
6625  cmd_response_data_t* response_data)
6626 {
6627  return new_credential (connection, credentials, params, NULL, response_data);
6628 }
6629 
6641 static char *
6642 edit_credential (openvas_connection_t *connection, credentials_t * credentials,
6643  params_t *params, const char *extra_xml,
6644  cmd_response_data_t* response_data)
6645 {
6646  return edit_resource (connection, "credential", credentials, params, NULL,
6647  extra_xml, response_data);
6648 }
6649 
6660 char *
6661 edit_credential_omp (openvas_connection_t *connection,
6662  credentials_t * credentials, params_t *params,
6663  cmd_response_data_t* response_data)
6664 {
6665  return edit_credential (connection, credentials, params, NULL, response_data);
6666 }
6667 
6678 char *
6679 save_credential_omp (openvas_connection_t *connection,
6680  credentials_t * credentials, params_t *params,
6681  cmd_response_data_t* response_data)
6682 {
6683  int ret, change_password, change_passphrase;
6684  int change_community, change_privacy_password;
6685  gchar *html, *response;
6686  const char *no_redirect, *credential_id;
6687  const char *name, *comment, *login, *password, *passphrase;
6688  const char *private_key, *certificate, *community, *privacy_password;
6689  const char *auth_algorithm, *privacy_algorithm, *allow_insecure;
6690  GString *command;
6691  entity_t entity;
6692 
6693  no_redirect = params_value (params, "no_redirect");
6694  credential_id = params_value (params, "credential_id");
6695  name = params_value (params, "name");
6696  comment = params_value (params, "comment");
6697  login = params_value (params, "credential_login");
6698  password = params_value (params, "password");
6699  passphrase = params_value (params, "passphrase");
6700  private_key = params_value (params, "private_key");
6701  certificate = params_value (params, "certificate");
6702  community = params_value (params, "community");
6703  privacy_password = params_value (params, "privacy_password");
6704  auth_algorithm = params_value (params, "auth_algorithm");
6705  privacy_algorithm = params_value (params, "privacy_algorithm");
6706  allow_insecure = params_value (params, "allow_insecure");
6707 
6708  CHECK_PARAM_INVALID (credential_id, "Save Credential", "edit_credential");
6709  CHECK_PARAM_INVALID (name, "Save Credential", "edit_credential");
6710  CHECK_PARAM_INVALID (comment, "Save Credential", "edit_credential");
6711  CHECK_PARAM_INVALID (allow_insecure, "Save Credential", "edit_credential");
6712  if (params_given (params, "certificate"))
6713  CHECK_PARAM_INVALID (certificate, "Save Credential", "edit_credential");
6714  if (params_given (params, "private_key"))
6715  CHECK_PARAM_INVALID (private_key, "Save Credential", "edit_credential");
6716  if (params_given (params, "login"))
6717  CHECK_PARAM_INVALID (login, "Save Credential", "edit_credential");
6718  if (params_given (params, "auth_algorithm"))
6719  CHECK_PARAM_INVALID (auth_algorithm, "Save Credential", "edit_credential");
6720  if (params_given (params, "privacy_algorithm"))
6721  CHECK_PARAM_INVALID (privacy_algorithm,
6722  "Save Credential", "edit_credential");
6723 
6724  change_community = (params_value (params, "change_community") ? 1 : 0);
6725  if (change_community)
6726  CHECK_PARAM_INVALID (community, "Save Credential", "edit_credential");
6727  change_passphrase = (params_value (params, "change_passphrase") ? 1 : 0);
6728  if (change_passphrase)
6729  CHECK_PARAM_INVALID (passphrase, "Save Credential", "edit_credential");
6730  change_password = (params_value (params, "change_password") ? 1 : 0);
6731  if (change_password)
6732  CHECK_PARAM_INVALID (password, "Save Credential", "edit_credential");
6733  change_privacy_password
6734  = (params_value (params, "change_privacy_password") ? 1 : 0);
6735  if (change_privacy_password)
6736  CHECK_PARAM_INVALID (privacy_password,
6737  "Save Credential", "edit_credential");
6738 
6739  /* Prepare command */
6740  command = g_string_new ("");
6741 
6742  xml_string_append (command,
6743  "<modify_credential credential_id=\"%s\">"
6744  "<name>%s</name>"
6745  "<comment>%s</comment>"
6746  "<allow_insecure>%s</allow_insecure>",
6747  credential_id,
6748  name,
6749  comment,
6750  allow_insecure);
6751 
6752  if (auth_algorithm)
6753  xml_string_append (command,
6754  "<auth_algorithm>%s</auth_algorithm>",
6755  auth_algorithm);
6756 
6757  if (certificate && strcmp (certificate, ""))
6758  xml_string_append (command,
6759  "<certificate>%s</certificate>",
6760  certificate);
6761 
6762  if (change_community)
6763  xml_string_append (command,
6764  "<community>%s</community>",
6765  community);
6766 
6767  if (login && strcmp (login, ""))
6768  xml_string_append (command,
6769  "<login>%s</login>",
6770  login);
6771 
6772  if (change_password)
6773  xml_string_append (command,
6774  "<password>%s</password>",
6775  password);
6776 
6777  if (privacy_algorithm || change_privacy_password)
6778  {
6779  xml_string_append (command,
6780  "<privacy>");
6781  if (privacy_algorithm && strcmp (privacy_algorithm, ""))
6782  {
6783  xml_string_append (command,
6784  "<algorithm>%s</algorithm>",
6785  privacy_algorithm);
6786  if (change_privacy_password)
6787  xml_string_append (command,
6788  "<password>%s</password>",
6789  privacy_password);
6790  }
6791  else if (privacy_algorithm)
6792  {
6793  xml_string_append (command,
6794  "<algorithm></algorithm>");
6795  xml_string_append (command,
6796  "<password></password>");
6797  }
6798 
6799  xml_string_append (command,
6800  "</privacy>");
6801  }
6802 
6803  if ((private_key && strcmp (private_key, "")) || change_passphrase)
6804  {
6805  xml_string_append (command,
6806  "<key>");
6807  if (change_passphrase)
6808  xml_string_append (command,
6809  "<phrase>%s</phrase>",
6810  passphrase);
6811  if (private_key)
6812  xml_string_append (command,
6813  "<private>%s</private>",
6814  private_key);
6815  xml_string_append (command,
6816  "</key>");
6817  }
6818 
6819  xml_string_append (command,
6820  "</modify_credential>");
6821 
6822  /* Modify the credential. */
6823  response = NULL;
6824  entity = NULL;
6825  ret = omp (connection, credentials,
6826  &response,
6827  &entity,
6828  response_data,
6829  command->str);
6830  g_string_free (command, TRUE);
6831 
6832  switch (ret)
6833  {
6834  case 0:
6835  break;
6836  case -1:
6837  /* 'omp' set response. */
6838  return response;
6839  case 1:
6840  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6841  return gsad_message (credentials,
6842  "Internal error", __FUNCTION__, __LINE__,
6843  "An internal error occurred while saving a Credential. "
6844  "The Credential was not saved. "
6845  "Diagnostics: Failure to send command to manager daemon.",
6846  "/omp?cmd=get_credentials", response_data);
6847  case 2:
6848  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6849  return gsad_message (credentials,
6850  "Internal error", __FUNCTION__, __LINE__,
6851  "An internal error occurred while saving a Credential. "
6852  "It is unclear whether the Credential has been saved or not. "
6853  "Diagnostics: Failure to receive response from manager daemon.",
6854  "/omp?cmd=get_credentials", response_data);
6855  default:
6856  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6857  return gsad_message (credentials,
6858  "Internal error", __FUNCTION__, __LINE__,
6859  "An internal error occurred while saving a Credential. "
6860  "It is unclear whether the Credential has been saved or not. "
6861  "Diagnostics: Internal Error.",
6862  "/omp?cmd=get_credentials", response_data);
6863  }
6864 
6865  html = response_from_entity (connection, credentials, params, entity,
6866  (no_redirect && strcmp (no_redirect, "0")),
6867  NULL, "get_credentials",
6868  NULL, "edit_credential",
6869  "Save Credential", response_data);
6870  free_entity (entity);
6871  g_free (response);
6872  return html;
6873 }
6874 
6886 static char *
6887 new_agent (openvas_connection_t *connection, credentials_t *credentials,
6888  params_t *params, const char *extra_xml,
6889  cmd_response_data_t* response_data)
6890 {
6891  GString *xml;
6892  xml = g_string_new ("<new_agent>");
6893  if (extra_xml)
6894  g_string_append (xml, extra_xml);
6895  g_string_append (xml, "</new_agent>");
6896  return xsl_transform_omp (connection, credentials, params,
6897  g_string_free (xml, FALSE), response_data);
6898 }
6899 
6910 char *
6911 new_agent_omp (openvas_connection_t *connection, credentials_t *credentials,
6912  params_t *params, cmd_response_data_t* response_data)
6913 {
6914  return new_agent (connection, credentials, params, NULL, response_data);
6915 }
6916 
6927 char *
6928 create_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
6929  params_t *params, cmd_response_data_t* response_data)
6930 {
6931  entity_t entity;
6932  gchar *response, *html;
6933  const char *no_redirect;
6934  const char *name, *comment, *installer, *installer_filename, *installer_sig;
6935  const char *howto_install, *howto_use;
6936  int installer_size, installer_sig_size, howto_install_size, howto_use_size;
6937  int ret;
6938  gchar *name_escaped, *comment_escaped;
6939  gchar *installer_64, *installer_sig_64, *howto_install_64, *howto_use_64;
6940  gchar *command;
6941 
6942  no_redirect = params_value (params, "no_redirect");
6943  name = params_value (params, "name");
6944  comment = params_value (params, "comment");
6945  installer = params_value (params, "installer");
6946  installer_filename = params_filename (params, "installer");
6947  installer_size = params_value_size (params, "installer");
6948  installer_sig = params_value (params, "installer_sig");
6949  installer_sig_size = params_value_size (params, "installer_sig");
6950  howto_install = params_value (params, "howto_install");
6951  howto_install_size = params_value_size (params, "howto_install");
6952  howto_use = params_value (params, "howto_use");
6953  howto_use_size = params_value_size (params, "howto_use");
6954 
6955  CHECK_PARAM_INVALID (name, "Create Agent", "new_agent");
6956  CHECK_PARAM_INVALID (comment, "Create Agent", "new_agent");
6957 
6958  /* Create the agent. */
6959 
6960  installer_64 = (installer_size > 0)
6961  ? g_base64_encode ((guchar *) installer,
6962  installer_size)
6963  : g_strdup ("");
6964 
6965  installer_sig_64 = (installer_sig_size > 0)
6966  ? g_base64_encode ((guchar *) installer_sig,
6967  installer_sig_size)
6968  : g_strdup ("");
6969 
6970  howto_install_64 = (howto_install_size > 0)
6971  ? g_base64_encode ((guchar *) howto_install,
6972  howto_install_size)
6973  : g_strdup ("");
6974 
6975  howto_use_64 = (howto_use_size > 0)
6976  ? g_base64_encode ((guchar *) howto_use,
6977  howto_use_size)
6978  : g_strdup ("");
6979 
6980  name_escaped = name ? g_markup_escape_text (name, -1) : NULL;
6981  comment_escaped = comment ? g_markup_escape_text (comment, -1) : NULL;
6982 
6983  command = g_strdup_printf ("<create_agent>"
6984  "<name>%s</name>"
6985  "%s%s%s"
6986  "<installer>"
6987  "%s"
6988  "<signature>%s</signature>"
6989  "<filename>%s</filename>"
6990  "</installer>"
6991  "<howto_install>%s</howto_install>"
6992  "<howto_use>%s</howto_use>"
6993  "</create_agent>",
6994  name_escaped,
6995  comment_escaped ? "<comment>" : "",
6996  comment_escaped ? comment_escaped : "",
6997  comment_escaped ? "</comment>" : "",
6998  installer_64,
6999  installer_sig_64,
7000  installer_filename ? installer_filename : "",
7001  howto_install_64,
7002  howto_use_64);
7003 
7004  ret = omp (connection, credentials,
7005  &response,
7006  &entity,
7007  response_data,
7008  command);
7009  g_free (command);
7010 
7011  g_free (installer_64);
7012  g_free (howto_install_64);
7013  g_free (howto_use_64);
7014  g_free (name_escaped);
7015  g_free (comment_escaped);
7016 
7017  switch (ret)
7018  {
7019  case 0:
7020  break;
7021  case -1:
7022  /* 'omp' set response. */
7023  return response;
7024  case 1:
7025  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7026  return gsad_message (credentials,
7027  "Internal error", __FUNCTION__, __LINE__,
7028  "An internal error occurred while creating a new agent. "
7029  "No new agent was created. "
7030  "Diagnostics: Failure to send command to manager daemon.",
7031  "/omp?cmd=get_agents", response_data);
7032  case 2:
7033  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7034  return gsad_message (credentials,
7035  "Internal error", __FUNCTION__, __LINE__,
7036  "An internal error occurred while creating a new agent. "
7037  "It is unclear whether the agent has been created or not. "
7038  "Diagnostics: Failure to receive response from manager daemon.",
7039  "/omp?cmd=get_agents", response_data);
7040  default:
7041  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7042  return gsad_message (credentials,
7043  "Internal error", __FUNCTION__, __LINE__,
7044  "An internal error occurred while creating a new agent. "
7045  "It is unclear whether the agent has been created or not. "
7046  "Diagnostics: Internal Error.",
7047  "/omp?cmd=get_agents", response_data);
7048  }
7049 
7050  if (entity_attribute (entity, "id"))
7051  params_add (params, "agent_id", entity_attribute (entity, "id"));
7052  html = response_from_entity (connection, credentials, params, entity,
7053  (no_redirect && strcmp (no_redirect, "0")),
7054  NULL, "get_agents",
7055  NULL, "new_agent",
7056  "Create Agent", response_data);
7057  free_entity (entity);
7058  g_free (response);
7059  return html;
7060 }
7061 
7072 char *
7073 delete_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7074  params_t *params, cmd_response_data_t* response_data)
7075 {
7076  return delete_resource (connection, "agent", credentials, params, 0,
7077  "get_agents", response_data);
7078 }
7079 
7094 int
7095 download_agent_omp (openvas_connection_t *connection,
7096  credentials_t * credentials,
7097  params_t *params,
7098  gsize *result_len,
7099  char ** html,
7100  char ** filename,
7101  cmd_response_data_t* response_data)
7102 {
7103  entity_t entity;
7104  const char *agent_id, *format;
7105 
7106  agent_id = params_value (params, "agent_id");
7107  format = params_value (params, "agent_format");
7108 
7109  if ((agent_id == NULL) || (format == NULL))
7110  {
7111  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
7112  *html = gsad_message (credentials,
7113  "Internal error", __FUNCTION__, __LINE__,
7114  "An internal error occurred while downloading "
7115  "an agent. "
7116  "Diagnostics: Required parameter was NULL.",
7117  "/omp?cmd=get_agents", response_data);
7118  return 1;
7119  }
7120 
7121  *result_len = 0;
7122 
7123  /* Send the request. */
7124 
7125  if (openvas_connection_sendf (connection,
7126  "<get_agents agent_id=\"%s\" format=\"%s\"/>",
7127  agent_id,
7128  format)
7129  == -1)
7130  {
7131  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7132  *html = gsad_message (credentials,
7133  "Internal error", __FUNCTION__, __LINE__,
7134  "An internal error occurred while getting agent list. "
7135  "The current list of agents is not available. "
7136  "Diagnostics: Failure to send command to manager daemon.",
7137  "/omp?cmd=get_agents", response_data);
7138  return 1;
7139  }
7140 
7141  /* Read and handle the response. */
7142 
7143  if (strcmp (format, "installer") == 0
7144  || strcmp (format, "howto_install") == 0
7145  || strcmp (format, "howto_use") == 0)
7146  {
7147  gchar *package_decoded = NULL;
7148  entity_t package_entity = NULL, agent_entity;
7149 
7150  /* A base64 encoded package. */
7151 
7152  entity = NULL;
7153  if (read_entity_c (connection, &entity))
7154  {
7155  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7156  *html = gsad_message (credentials,
7157  "Internal error", __FUNCTION__, __LINE__,
7158  "An internal error occurred while getting a agent. "
7159  "The agent is not available. "
7160  "Diagnostics: Failure to receive response from manager daemon.",
7161  "/omp?cmd=get_agents", response_data);
7162  return 1;
7163  }
7164 
7165  agent_entity = entity_child (entity, "agent");
7166  if (agent_entity)
7167  package_entity = entity_child (agent_entity, "package");
7168  if (package_entity != NULL)
7169  {
7170  char *package_encoded = entity_text (package_entity);
7171  if (strlen (package_encoded))
7172  {
7173  package_decoded = (gchar *) g_base64_decode (package_encoded,
7174  result_len);
7175  if (package_decoded == NULL)
7176  {
7177  package_decoded = (gchar *) g_strdup ("");
7178  *result_len = 0;
7179  }
7180  }
7181  else
7182  {
7183  package_decoded = (gchar *) g_strdup ("");
7184  *result_len = 0;
7185  }
7186  *html = package_decoded;
7187  if (filename)
7188  {
7189  entity_t filename_entity;
7190  filename_entity = entity_child (package_entity,
7191  "filename");
7192  if (filename_entity)
7193  *filename = g_strdup (entity_text (filename_entity));
7194  else
7195  *filename = NULL;
7196  if (!(*filename && strlen (*filename)))
7197  {
7198  g_free(*filename);
7199  *filename = g_strdup_printf ("agent-%s-%s", agent_id, format);
7200  }
7201  }
7202  free_entity (entity);
7203  return 0;
7204  }
7205  else
7206  {
7207  free_entity (entity);
7208  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7209  *html = gsad_message (credentials,
7210  "Internal error", __FUNCTION__, __LINE__,
7211  "An internal error occurred while getting a agent. "
7212  "The agent could not be delivered. "
7213  "Diagnostics: Failure to receive agent from manager daemon.",
7214  "/omp?cmd=get_tasks", response_data);
7215  return 1;
7216  }
7217  }
7218  else
7219  {
7220  /* An error. */
7221 
7222  entity = NULL;
7223  if (read_entity_c (connection, &entity))
7224  {
7225  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7226  *html = gsad_message (credentials,
7227  "Internal error", __FUNCTION__, __LINE__,
7228  "An internal error occurred while getting a agent. "
7229  "The agent could not be delivered. "
7230  "Diagnostics: Failure to receive agent from manager daemon.",
7231  "/omp?cmd=get_tasks", response_data);
7232  return 1;
7233  }
7234 
7235  free_entity (entity);
7236  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7237  *html = gsad_message (credentials,
7238  "Internal error", __FUNCTION__, __LINE__,
7239  "An internal error occurred while getting a agent. "
7240  "The agent could not be delivered. "
7241  "Diagnostics: Failure to parse agent from manager daemon.",
7242  "/omp?cmd=get_tasks", response_data);
7243  return 1;
7244  }
7245 }
7246 
7258 char *
7259 edit_agent (openvas_connection_t *connection, credentials_t * credentials,
7260  params_t *params, const char *extra_xml,
7261  cmd_response_data_t* response_data)
7262 {
7263  return edit_resource (connection, "agent", credentials, params, NULL,
7264  extra_xml, response_data);
7265 }
7266 
7277 char *
7278 edit_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7279  params_t *params, cmd_response_data_t* response_data)
7280 {
7281  return edit_agent (connection, credentials, params, NULL, response_data);
7282 }
7283 
7294 char *
7295 save_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7296  params_t *params, cmd_response_data_t* response_data)
7297 {
7298  int ret;
7299  gchar *html, *response;
7300  const char *no_redirect, *agent_id, *name, *comment;
7301  entity_t entity;
7302 
7303  no_redirect = params_value (params, "no_redirect");
7304  agent_id = params_value (params, "agent_id");
7305  name = params_value (params, "name");
7306  comment = params_value (params, "comment");
7307 
7308  CHECK_PARAM_INVALID (agent_id, "Save Agent", "edit_agent");
7309  CHECK_PARAM_INVALID (name, "Save Agent", "edit_agent");
7310  CHECK_PARAM_INVALID (comment, "Save Agent", "edit_agent");
7311 
7312  /* Modify the agent. */
7313 
7314  response = NULL;
7315  entity = NULL;
7316  ret = ompf (connection, credentials,
7317  &response,
7318  &entity,
7319  response_data,
7320  "<modify_agent agent_id=\"%s\">"
7321  "<name>%s</name>"
7322  "<comment>%s</comment>"
7323  "</modify_agent>",
7324  agent_id,
7325  name,
7326  comment);
7327 
7328  switch (ret)
7329  {
7330  case 0:
7331  case -1:
7332  break;
7333  case 1:
7334  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7335  return gsad_message (credentials,
7336  "Internal error", __FUNCTION__, __LINE__,
7337  "An internal error occurred while saving a agent. "
7338  "The agent was not saved. "
7339  "Diagnostics: Failure to send command to manager daemon.",
7340  "/omp?cmd=get_agents", response_data);
7341  case 2:
7342  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7343  return gsad_message (credentials,
7344  "Internal error", __FUNCTION__, __LINE__,
7345  "An internal error occurred while saving a agent. "
7346  "It is unclear whether the agent has been saved or not. "
7347  "Diagnostics: Failure to receive response from manager daemon.",
7348  "/omp?cmd=get_agents", response_data);
7349  default:
7350  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7351  return gsad_message (credentials,
7352  "Internal error", __FUNCTION__, __LINE__,
7353  "An internal error occurred while saving a agent. "
7354  "It is unclear whether the agent has been saved or not. "
7355  "Diagnostics: Internal Error.",
7356  "/omp?cmd=get_agents", response_data);
7357  }
7358 
7359  html = response_from_entity (connection, credentials, params, entity,
7360  (no_redirect && strcmp (no_redirect, "0")),
7361  NULL, "get_agents",
7362  NULL, "edit_agent",
7363  "Save Agent", response_data);
7364  free_entity (entity);
7365  g_free (response);
7366  return html;
7367 }
7368 
7380 char *
7381 get_agent (openvas_connection_t *connection, credentials_t * credentials,
7382  params_t *params, const char *extra_xml,
7383  cmd_response_data_t* response_data)
7384 {
7385  return get_one (connection, "agent", credentials, params, extra_xml, NULL,
7386  response_data);
7387 }
7388 
7399 char *
7400 get_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7401  params_t *params, cmd_response_data_t* response_data)
7402 {
7403  return get_agent (connection, credentials, params, NULL, response_data);
7404 }
7405 
7417 char *
7418 get_agents (openvas_connection_t *connection, credentials_t * credentials,
7419  params_t *params, const char *extra_xml,
7420  cmd_response_data_t* response_data)
7421 {
7422  return get_many (connection, "agent", credentials, params, extra_xml, NULL,
7423  response_data);
7424 }
7425 
7436 char *
7437 get_agents_omp (openvas_connection_t *connection, credentials_t * credentials,
7438  params_t *params, cmd_response_data_t* response_data)
7439 {
7440  return get_agents (connection, credentials, params, NULL, response_data);
7441 }
7442 
7453 char *
7454 verify_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7455  params_t *params, cmd_response_data_t* response_data)
7456 {
7457  gchar *html, *response;
7458  const char *agent_id;
7459  int ret;
7460  entity_t entity;
7461 
7462  agent_id = params_value (params, "agent_id");
7463  if (agent_id == NULL)
7464  {
7465  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
7466  return gsad_message (credentials,
7467  "Internal error", __FUNCTION__, __LINE__,
7468  "An internal error occurred while verifying an agent. "
7469  "Diagnostics: Required parameter was NULL.",
7470  "/omp?cmd=get_agents", response_data);
7471  }
7472  response = NULL;
7473  entity = NULL;
7474  ret = ompf (connection, credentials,
7475  &response,
7476  &entity,
7477  response_data,
7478  "<verify_agent agent_id=\"%s\" />",
7479  agent_id);
7480 
7481  switch (ret)
7482  {
7483  case 0:
7484  case -1:
7485  break;
7486  case 1:
7487  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7488  return gsad_message (credentials,
7489  "Internal error", __FUNCTION__, __LINE__,
7490  "An internal error occurred while verifying a agent. "
7491  "The agent was not verified. "
7492  "Diagnostics: Failure to send command to manager daemon.",
7493  "/omp?cmd=get_agents", response_data);
7494  case 2:
7495  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7496  return gsad_message (credentials,
7497  "Internal error", __FUNCTION__, __LINE__,
7498  "An internal error occurred while verifying a agent. "
7499  "It is unclear whether the agent was verified or not. "
7500  "Diagnostics: Failure to send command to manager daemon.",
7501  "/omp?cmd=get_agents", response_data);
7502  default:
7503  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7504  return gsad_message (credentials,
7505  "Internal error", __FUNCTION__, __LINE__,
7506  "An internal error occurred while verifying a agent. "
7507  "It is unclear whether the agent was verified or not. "
7508  "Diagnostics: Failure to send command to manager daemon.",
7509  "/omp?cmd=get_agents", response_data);
7510  }
7511 
7512  if (omp_success (entity))
7513  {
7514  html = next_page (connection, credentials, params, response,
7515  response_data);
7516  if (html == NULL)
7517  {
7518  free_entity (entity);
7519  g_free (response);
7520  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7521  return gsad_message (credentials,
7522  "Internal error", __FUNCTION__, __LINE__,
7523  "An internal error occurred while verifying a agent. "
7524  "It is unclear whether the agent was verified or not. "
7525  "Diagnostics: Failure to receive response from manager daemon.",
7526  "/omp?cmd=get_agents", response_data);
7527  }
7528  }
7529  else
7530  {
7531  set_http_status_from_entity (entity, response_data);
7532  html = get_agents (connection, credentials, params, response,
7533  response_data);
7534  }
7535  free_entity (entity);
7536  g_free (response);
7537  return html;
7538 }
7539 
7553 char *
7554 export_agent_omp (openvas_connection_t *connection, credentials_t * credentials,
7555  params_t *params, enum content_type * content_type,
7556  char **content_disposition, gsize *content_length,
7557  cmd_response_data_t* response_data)
7558 {
7559  return export_resource (connection, "agent", credentials, params,
7560  content_type, content_disposition, content_length,
7561  response_data);
7562 }
7563 
7578 char *
7579 export_agents_omp (openvas_connection_t *connection,
7580  credentials_t * credentials, params_t *params,
7581  enum content_type * content_type, char **content_disposition,
7582  gsize *content_length, cmd_response_data_t* response_data)
7583 {
7584  return export_many (connection, "agent", credentials, params, content_type,
7585  content_disposition, content_length, response_data);
7586 }
7587 
7598 char *
7599 get_aggregate_omp (openvas_connection_t *connection,
7600  credentials_t * credentials, params_t *params,
7601  cmd_response_data_t* response_data)
7602 {
7603  params_t *data_columns, *text_columns;
7604  params_t *sort_fields, *sort_stats, *sort_orders;
7605  params_iterator_t data_columns_iterator, text_columns_iterator;
7606  params_iterator_t sort_fields_iterator, sort_stats_iterator;
7607  params_iterator_t sort_orders_iterator;
7608  char *param_name;
7609  param_t *param;
7610 
7611  const char *data_column, *group_column, *subgroup_column, *type;
7612  const char *filter, *filt_id, *xml_param;
7613  const char *first_group, *max_groups;
7614  const char *mode;
7615  gchar *filter_escaped, *command_escaped, *response;
7616  entity_t entity;
7617  GString *xml, *command;
7618  int ret;
7619 
7620  data_columns = params_values (params, "data_columns:");
7621  data_column = params_value (params, "data_column");
7622  text_columns = params_values (params, "text_columns:");
7623  group_column = params_value (params, "group_column");
7624  subgroup_column = params_value (params, "subgroup_column");
7625  type = params_value (params, "aggregate_type");
7626  filter = params_value (params, "filter");
7627  filt_id = params_value (params, "filt_id");
7628  sort_fields = params_values (params, "sort_fields:");
7629  sort_stats = params_values (params, "sort_stats:");
7630  sort_orders = params_values (params, "sort_orders:");
7631  first_group = params_value (params, "first_group");
7632  max_groups = params_value (params, "max_groups");
7633  mode = params_value (params, "aggregate_mode");
7634  if (filter && strcmp (filter, ""))
7635  filter_escaped = g_markup_escape_text (filter, -1);
7636  else
7637  {
7638  if (filt_id == NULL
7639  || strcmp (filt_id, "") == 0
7640  || strcmp (filt_id, "0") == 0)
7641  filter_escaped = g_strdup ("rows=-2");
7642  else
7643  filter_escaped = NULL;
7644  }
7645  xml_param = params_value (params, "xml");
7646 
7647  if (xml_param == NULL || atoi (xml_param) == 0)
7648  {
7649  return xsl_transform_omp (connection, credentials, params,
7650  g_strdup ("<get_aggregate/>"), response_data);
7651  }
7652  xml = g_string_new ("<get_aggregate>");
7653 
7654  command = g_string_new ("<get_aggregates");
7655  g_string_append_printf (command, " type=\"%s\"", type);
7656  if (data_column)
7657  g_string_append_printf (command, " data_column=\"%s\"", data_column);
7658  if (group_column)
7659  g_string_append_printf (command, " group_column=\"%s\"", group_column);
7660  if (subgroup_column)
7661  g_string_append_printf (command,
7662  " subgroup_column=\"%s\"", subgroup_column);
7663  if (filter_escaped && strcmp (filter_escaped, ""))
7664  g_string_append_printf (command, " filter=\"%s\"", filter_escaped);
7665  if (filt_id && strcmp (filt_id, ""))
7666  g_string_append_printf (command, " filt_id=\"%s\"", filt_id);
7667  if (first_group && strcmp (first_group, ""))
7668  g_string_append_printf (command, " first_group=\"%s\"", first_group);
7669  if (max_groups && strcmp (max_groups, ""))
7670  g_string_append_printf (command, " max_groups=\"%s\"", max_groups);
7671  if (mode && strcmp (mode, ""))
7672  g_string_append_printf (command, " mode=\"%s\"", mode);
7673  g_string_append (command, ">");
7674 
7675  if (sort_fields && sort_stats && sort_orders)
7676  {
7677  param_t *field_param, *stat_param, *order_param;
7678  gchar *field_i, *stat_i, *order_i;
7679 
7680  params_iterator_init (&sort_fields_iterator, sort_fields);
7681  params_iterator_init (&sort_stats_iterator, sort_stats);
7682  params_iterator_init (&sort_orders_iterator, sort_orders);
7683 
7684  while (params_iterator_next (&sort_fields_iterator,
7685  &field_i, &field_param)
7686  && params_iterator_next (&sort_stats_iterator,
7687  &stat_i, &stat_param)
7688  && params_iterator_next (&sort_orders_iterator,
7689  &order_i, &order_param))
7690  {
7691  if (field_param->valid && stat_param->valid && order_param->valid)
7692  {
7693  xml_string_append (command,
7694  "<sort field=\"%s\""
7695  " stat=\"%s\""
7696  " order=\"%s\"/>",
7697  field_param->value ? field_param->value : "",
7698  stat_param->value ? stat_param->value : "",
7699  order_param->value ? order_param->value : "");
7700  }
7701  }
7702  }
7703 
7704  if (data_columns)
7705  {
7706  params_iterator_init (&data_columns_iterator, data_columns);
7707  while (params_iterator_next (&data_columns_iterator, &param_name, &param))
7708  {
7709  if (param->valid)
7710  {
7711  xml_string_append (command,
7712  "<data_column>%s</data_column>",
7713  param->value);
7714  }
7715  }
7716  }
7717 
7718  if (text_columns)
7719  {
7720  params_iterator_init (&text_columns_iterator, text_columns);
7721  while (params_iterator_next (&text_columns_iterator, &param_name, &param))
7722  {
7723  if (param->valid)
7724  {
7725  xml_string_append (command,
7726  "<text_column>%s</text_column>",
7727  param->value);
7728  }
7729  }
7730  }
7731 
7732  g_string_append (command, "</get_aggregates>");
7733 
7734  g_free (filter_escaped);
7735 
7736  command_escaped = g_markup_escape_text (command->str, -1);
7737  g_string_append (xml, command_escaped);
7738  g_free (command_escaped);
7739 
7740  ret = omp (connection, credentials, &response, &entity, response_data,
7741  command->str);
7742  g_string_free (command, TRUE);
7743  switch (ret)
7744  {
7745  case 0:
7746  break;
7747  case -1:
7748  /* 'omp' set response. */
7749  return response;
7750  case 1:
7751  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7752  return gsad_message (credentials,
7753  "Internal error", __FUNCTION__, __LINE__,
7754  "An internal error occurred while getting aggregates. "
7755  "Diagnostics: Failure to send command to manager daemon.",
7756  "/omp?cmd=get_tasks", response_data);
7757  case 2:
7758  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7759  return gsad_message (credentials,
7760  "Internal error", __FUNCTION__, __LINE__,
7761  "An internal error occurred while getting aggregates. "
7762  "Diagnostics: Failure to receive response from manager daemon.",
7763  "/omp?cmd=get_tasks", response_data);
7764  default:
7765  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7766  return gsad_message (credentials,
7767  "Internal error", __FUNCTION__, __LINE__,
7768  "An internal error occurred while getting aggregates. "
7769  "Diagnostics: Internal Error.",
7770  "/omp?cmd=get_tasks", response_data);
7771  }
7772 
7773  g_string_append (xml, response);
7774 
7775  g_string_append (xml, "</get_aggregate>");
7776 
7777  return xsl_transform_omp (connection, credentials, params,
7778  g_string_free (xml, FALSE), response_data);
7779 }
7780 
7792 static char *
7793 new_alert (openvas_connection_t *connection, credentials_t *credentials,
7794  params_t *params, const char *extra_xml,
7795  cmd_response_data_t* response_data)
7796 {
7797  GString *xml;
7798  int ret;
7799  entity_t entity;
7800  gchar *response;
7801 
7802  xml = g_string_new ("<new_alert>");
7803  if (extra_xml)
7804  g_string_append (xml, extra_xml);
7805 
7806  /* Get Report Formats. */
7807 
7808  response = NULL;
7809  entity = NULL;
7810  ret = omp (connection, credentials, &response, &entity, response_data,
7811  "<get_report_formats filter=\"rows=-1\"/>");
7812  switch (ret)
7813  {
7814  case 0:
7815  case -1:
7816  break;
7817  case 1:
7818  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7819  return gsad_message (credentials,
7820  "Internal error", __FUNCTION__, __LINE__,
7821  "An internal error occurred while getting Report "
7822  "Formats for new alert. "
7823  "Diagnostics: Failure to send command to manager daemon.",
7824  "/omp?cmd=get_alerts", response_data);
7825  case 2:
7826  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7827  return gsad_message (credentials,
7828  "Internal error", __FUNCTION__, __LINE__,
7829  "An internal error occurred while getting Report "
7830  "Formats for new alert. "
7831  "Diagnostics: Failure to receive response from manager daemon.",
7832  "/omp?cmd=get_alerts", response_data);
7833  default:
7834  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7835  return gsad_message (credentials,
7836  "Internal error", __FUNCTION__, __LINE__,
7837  "An internal error occurred while getting Report "
7838  "Formats for new alert. It is unclear whether"
7839  " the alert has been saved or not. "
7840  "Diagnostics: Internal Error.",
7841  "/omp?cmd=get_alerts", response_data);
7842  }
7843  g_string_append (xml, response);
7844  g_free (response);
7845  free_entity (entity);
7846 
7847  /* Get Report Filters. */
7848 
7849  ret = omp (connection, credentials, &response, &entity, response_data,
7850  "<get_filters filter=\"rows=-1\"/>");
7851 
7852  switch (ret)
7853  {
7854  case 0:
7855  case -1:
7856  break;
7857  case 1:
7858  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7859  return gsad_message (credentials,
7860  "Internal error", __FUNCTION__, __LINE__,
7861  "An internal error occurred while getting Report "
7862  "Filters for new alert. "
7863  "The task was not saved. "
7864  "Diagnostics: Failure to send command to manager daemon.",
7865  "/omp?cmd=get_alerts", response_data);
7866  case 2:
7867  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7868  return gsad_message (credentials,
7869  "Internal error", __FUNCTION__, __LINE__,
7870  "An internal error occurred while getting Report "
7871  "Filters for new alert. "
7872  "Diagnostics: Failure to receive response from manager daemon.",
7873  "/omp?cmd=get_alerts", response_data);
7874  default:
7875  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7876  return gsad_message (credentials,
7877  "Internal error", __FUNCTION__, __LINE__,
7878  "An internal error occurred while getting Report "
7879  "Filters for new alert. "
7880  "Diagnostics: Internal Error.",
7881  "/omp?cmd=get_alerts", response_data);
7882  }
7883  g_string_append (xml, response);
7884  g_free (response);
7885  free_entity (entity);
7886 
7887  /* Get Tasks. */
7888 
7889  ret = omp (connection, credentials, &response, &entity, response_data,
7890  "<get_tasks"
7891  " schedules_only=\"1\""
7892  " filter=\"owner=any permission=start_task rows=-1\"/>");
7893 
7894  switch (ret)
7895  {
7896  case 0:
7897  case -1:
7898  break;
7899  case 1:
7900  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7901  return gsad_message (credentials,
7902  "Internal error", __FUNCTION__, __LINE__,
7903  "An internal error occurred while getting Tasks"
7904  " for new alert. "
7905  "The task was not saved. "
7906  "Diagnostics: Failure to send command to manager daemon.",
7907  "/omp?cmd=get_alerts",
7908  response_data);
7909  case 2:
7910  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7911  return gsad_message (credentials,
7912  "Internal error", __FUNCTION__, __LINE__,
7913  "An internal error occurred while getting Tasks"
7914  " for new alert. "
7915  "Diagnostics: Failure to receive response from manager daemon.",
7916  "/omp?cmd=get_alerts",
7917  response_data);
7918  default:
7919  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7920  return gsad_message (credentials,
7921  "Internal error", __FUNCTION__, __LINE__,
7922  "An internal error occurred while getting Tasks"
7923  " for new alert. "
7924  "Diagnostics: Internal Error.",
7925  "/omp?cmd=get_alerts",
7926  response_data);
7927  }
7928  g_string_append (xml, response);
7929  g_free (response);
7930  free_entity (entity);
7931 
7932  /* Get Credentials. */
7933 
7934  ret = omp (connection, credentials, &response, &entity, response_data,
7935  "<get_credentials"
7936  " filter=\"type=up owner=any permission=any rows=-1\"/>");
7937 
7938  switch (ret)
7939  {
7940  case 0:
7941  case -1:
7942  break;
7943  case 1:
7944  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7945  return gsad_message (credentials,
7946  "Internal error", __FUNCTION__, __LINE__,
7947  "An internal error occurred while getting"
7948  " Credentials for new alert. "
7949  "The task was not saved. "
7950  "Diagnostics: Failure to send command to manager daemon.",
7951  "/omp?cmd=get_alerts",
7952  response_data);
7953  case 2:
7954  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7955  return gsad_message (credentials,
7956  "Internal error", __FUNCTION__, __LINE__,
7957  "An internal error occurred while getting"
7958  " Credentials for new alert. "
7959  "Diagnostics: Failure to receive response from manager daemon.",
7960  "/omp?cmd=get_alerts",
7961  response_data);
7962  default:
7963  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7964  return gsad_message (credentials,
7965  "Internal error", __FUNCTION__, __LINE__,
7966  "An internal error occurred while getting"
7967  " Credentials for new alert. "
7968  "Diagnostics: Internal Error.",
7969  "/omp?cmd=get_alerts",
7970  response_data);
7971  }
7972 
7973  g_string_append (xml, response);
7974  g_free (response);
7975  free_entity (entity);
7976 
7977  g_string_append (xml, "</new_alert>");
7978  return xsl_transform_omp (connection, credentials, params,
7979  g_string_free (xml, FALSE), response_data);
7980 }
7981 
7992 char *
7993 new_alert_omp (openvas_connection_t *connection, credentials_t *credentials,
7994  params_t *params, cmd_response_data_t* response_data)
7995 {
7996  return new_alert (connection, credentials, params, NULL, response_data);
7997 }
7998 
7999 char *
8000 get_alerts (openvas_connection_t*, credentials_t *, params_t *, const char *,
8002 
8012 static void
8013 append_alert_event_data (GString *xml, params_t *data, const char *event)
8014 {
8015  if (data)
8016  {
8017  params_iterator_t iter;
8018  char *name;
8019  param_t *param;
8020 
8021  params_iterator_init (&iter, data);
8022  while (params_iterator_next (&iter, &name, &param))
8023  if ((strcmp (event, "Task run status changed") == 0
8024  && strcmp (name, "status") == 0)
8025  || ((strcmp (event, "New SecInfo arrived") == 0
8026  || strcmp (event, "Updated SecInfo arrived") == 0)
8027  && strcmp (name, "secinfo_type") == 0))
8028  xml_string_append (xml,
8029  "<data><name>%s</name>%s</data>",
8030  name,
8031  param->value ? param->value : "");
8032  }
8033 }
8034 
8042 static void
8043 append_alert_condition_data (GString *xml, params_t *data,
8044  const char * condition)
8045 {
8046  if (data)
8047  {
8048  params_iterator_t iter;
8049  char *name;
8050  param_t *param;
8051 
8052  params_iterator_init (&iter, data);
8053  while (params_iterator_next (&iter, &name, &param))
8054  {
8055  if (strcmp (condition, "Filter count at least") == 0)
8056  {
8057  if (strcmp (name, "at_least_count") == 0)
8058  xml_string_append (xml,
8059  "<data><name>count</name>%s</data>",
8060  param->value ? param->value : "");
8061  if (strcmp (name, "at_least_filter_id") == 0)
8062  xml_string_append (xml,
8063  "<data><name>filter_id</name>%s</data>",
8064  param->value ? param->value : "");
8065  }
8066  else if (strcmp (condition, "Filter count changed") == 0)
8067  {
8068  if (strcmp (name, "count") == 0
8069  || strcmp (name, "filter_id") == 0)
8070  xml_string_append (xml,
8071  "<data><name>%s</name>%s</data>",
8072  name,
8073  param->value ? param->value : "");
8074  else if (strcmp (name, "filter_direction") == 0)
8075  xml_string_append (xml,
8076  "<data><name>direction</name>%s</data>",
8077  param->value ? param->value : "");
8078  }
8079  else if ((strcmp (condition, "Severity at least") == 0
8080  && strcmp (name, "severity") == 0)
8081  || (strcmp (condition, "Severity changed") == 0
8082  && strcmp (name, "direction") == 0))
8083  xml_string_append (xml,
8084  "<data><name>%s</name>%s</data>",
8085  name,
8086  param->value ? param->value : "");
8087  }
8088  }
8089 }
8090 
8098 static void
8099 append_alert_method_data (GString *xml, params_t *data, const char *method)
8100 {
8101  params_iterator_t iter;
8102  char *name;
8103  param_t *param;
8104  int notice;
8105 
8106  if (data == NULL)
8107  return;
8108 
8109  params_iterator_init (&iter, data);
8110  /* Used to check email notice type before sending report formats values */
8111  notice = 1;
8112  while (params_iterator_next (&iter, &name, &param))
8113  if (strcmp (name, "notice") == 0)
8114  {
8115  notice = atoi (param->value);
8116  break;
8117  }
8118 
8119  if (strcmp (method, "Sourcefire Connector"))
8120  {
8121  params_iterator_init (&iter, data);
8122 
8123  while (params_iterator_next (&iter, &name, &param))
8124  if ((strcmp (method, "HTTP Get") == 0
8125  && strcmp (name, "URL") == 0)
8126  || (strcmp (method, "Send") == 0
8127  && (strcmp (name, "send_host") == 0
8128  || strcmp (name, "send_port") == 0
8129  || strcmp (name, "send_report_format") == 0))
8130  || (strcmp (method, "SCP") == 0
8131  && (strcmp (name, "scp_credential") == 0
8132  || strcmp (name, "scp_host") == 0
8133  || strcmp (name, "scp_known_hosts") == 0
8134  || strcmp (name, "scp_path") == 0
8135  || strcmp (name, "scp_report_format") == 0))
8136  || (strcmp (method, "SMB") == 0
8137  && (strcmp (name, "smb_credential") == 0
8138  || strcmp (name, "smb_file_path") == 0
8139  || strcmp (name, "smb_report_format") == 0
8140  || strcmp (name, "smb_share_path") == 0))
8141  || (strcmp (method, "SNMP") == 0
8142  && (strcmp (name, "snmp_community") == 0
8143  || strcmp (name, "snmp_agent") == 0
8144  || strcmp (name, "snmp_message") == 0))
8145  || (strcmp (method, "TippingPoint SMS") == 0
8146  && (strcmp (name, "tp_sms_credential") == 0
8147  || strcmp (name, "tp_sms_hostname") == 0
8148  || strcmp (name, "tp_sms_tls_certificate") == 0
8149  || strcmp (name, "tp_sms_tls_workaround") == 0))
8150  || (strcmp (method, "verinice Connector") == 0
8151  && (strcmp (name, "verinice_server_credential") == 0
8152  || strcmp (name, "verinice_server_url") == 0
8153  || strcmp (name, "verinice_server_report_format") == 0))
8154  || (strcmp (method, "Email") == 0
8155  && (strcmp (name, "to_address") == 0
8156  || strcmp (name, "from_address") == 0
8157  || strcmp (name, "subject") == 0
8158  || strcmp (name, "notice") == 0
8159  || (strcmp (name, "notice_report_format") == 0
8160  && notice == 0)
8161  || (strcmp (name, "notice_attach_format") == 0
8162  && notice == 2)))
8163  || (strcmp (method, "Syslog") == 0
8164  && strcmp (name, "submethod") == 0)
8165  || (strcmp (method, "Start Task") == 0
8166  && strcmp (name, "start_task_task") == 0)
8167  || strcmp (name, "details_url") == 0)
8168  xml_string_append (xml,
8169  "<data><name>%s</name>%s</data>",
8170  name,
8171  param->value ? param->value : "");
8172  else if (strcmp (method, "Email") == 0
8173  && notice == 0
8174  && strcmp (name, "message") == 0)
8175  xml_string_append (xml,
8176  "<data><name>message</name>%s</data>",
8177  param->value ? param->value : "");
8178  else if (strcmp (method, "Email") == 0
8179  && notice == 2
8180  && strcmp (name, "message_attach") == 0)
8181  xml_string_append (xml,
8182  "<data><name>message</name>%s</data>",
8183  param->value ? param->value : "");
8184 
8185  return;
8186  }
8187 
8188  params_iterator_init (&iter, data);
8189  while (params_iterator_next (&iter, &name, &param))
8190  if (strcmp (name, "pkcs12"))
8191  {
8192  if (strcmp (name, "defense_center_ip") == 0
8193  || strcmp (name, "defense_center_port") == 0)
8194  xml_string_append (xml,
8195  "<data><name>%s</name>%s</data>",
8196  name,
8197  param->value ? param->value : "");
8198  }
8199  else
8200  {
8201  gchar *base64;
8202 
8203  /* Special case the pkcs12 file, which is binary. */
8204 
8205  base64 = (param->value && param->value_size)
8206  ? g_base64_encode ((guchar*) param->value,
8207  param->value_size)
8208  : g_strdup ("");
8209  xml_string_append (xml,
8210  "<data><name>%s</name>%s</data>",
8211  name,
8212  base64);
8213  g_free (base64);
8214  }
8215 }
8216 
8227 char *
8228 create_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
8229  params_t *params, cmd_response_data_t* response_data)
8230 {
8231  int ret;
8232  gchar *html, *response;
8233  const char *no_redirect;
8234  const char *name, *comment, *condition, *event, *method, *filter_id;
8235  params_t *method_data, *event_data, *condition_data;
8236  entity_t entity;
8237  GString *xml;
8238 
8239  no_redirect = params_value (params, "no_redirect");
8240  name = params_value (params, "name");
8241  comment = params_value (params, "comment");
8242  if (params_given (params, "condition"))
8243  condition = params_value (params, "condition");
8244  else
8245  condition = "Always";
8246  event = params_value (params, "event");
8247  method = params_value (params, "method");
8248  filter_id = params_value (params, "filter_id");
8249 
8250  CHECK_PARAM_INVALID (name, "Create Alert", "new_alert");
8251  CHECK_PARAM_INVALID (comment, "Create Alert", "new_alert");
8252  CHECK_PARAM_INVALID (condition, "Create Alert", "new_alert");
8253  CHECK_PARAM_INVALID (event, "Create Alert", "new_alert");
8254  CHECK_PARAM_INVALID (method, "Create Alert", "new_alert");
8255  if (strcmp (event, "Task run status changed") == 0)
8256  {
8257  CHECK_PARAM_INVALID (filter_id, "Save Alert", "new_alert");
8258  }
8259 
8260  /* Create the alert. */
8261 
8262  method_data = params_values (params, "method_data:");
8263  event_data = params_values (params, "event_data:");
8264  condition_data = params_values (params, "condition_data:");
8265 
8266  xml = g_string_new ("");
8267 
8268  if ((strcmp (event, "New SecInfo arrived") == 0) && event_data)
8269  {
8270  params_iterator_t iter;
8271  char *name;
8272  param_t *param;
8273 
8274  params_iterator_init (&iter, event_data);
8275  while (params_iterator_next (&iter, &name, &param))
8276  if ((strcmp (name, "feed_event") == 0)
8277  && param->value
8278  && (strcmp (param->value, "updated") == 0))
8279  {
8280  event = "Updated SecInfo arrived";
8281  break;
8282  }
8283  }
8284 
8285  xml_string_append (xml,
8286  "<create_alert>"
8287  "<name>%s</name>"
8288  "<filter id=\"%s\"/>"
8289  "<comment>%s</comment>"
8290  "<event>%s",
8291  name,
8292  filter_id,
8293  comment ? comment : "",
8294  event);
8295 
8296  append_alert_event_data (xml, event_data, event);
8297 
8298  xml_string_append (xml,
8299  "</event>"
8300  "<method>%s",
8301  method);
8302 
8303  append_alert_method_data (xml, method_data, method);
8304 
8305  xml_string_append (xml,
8306  "</method>"
8307  "<condition>%s",
8308  condition);
8309 
8310  append_alert_condition_data (xml, condition_data, condition);
8311 
8312  xml_string_append (xml,
8313  "</condition>"
8314  "</create_alert>");
8315 
8316  ret = omp (connection, credentials,
8317  &response,
8318  &entity,
8319  response_data,
8320  xml->str);
8321  g_string_free (xml, TRUE);
8322  switch (ret)
8323  {
8324  case 0:
8325  case -1:
8326  break;
8327  case 1:
8328  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8329  return gsad_message (credentials,
8330  "Internal error", __FUNCTION__, __LINE__,
8331  "An internal error occurred while creating a new alert. "
8332  "No new alert was created. "
8333  "Diagnostics: Failure to send command to manager daemon.",
8334  "/omp?cmd=get_alerts", response_data);
8335  case 2:
8336  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8337  return gsad_message (credentials,
8338  "Internal error", __FUNCTION__, __LINE__,
8339  "An internal error occurred while creating a new alert. "
8340  "It is unclear whether the alert has been created or not. "
8341  "Diagnostics: Failure to receive response from manager daemon.",
8342  "/omp?cmd=get_alerts", response_data);
8343  default:
8344  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8345  return gsad_message (credentials,
8346  "Internal error", __FUNCTION__, __LINE__,
8347  "An internal error occurred while creating a new alert. "
8348  "It is unclear whether the alert has been created or not. "
8349  "Diagnostics: Internal Error.",
8350  "/omp?cmd=get_alerts", response_data);
8351  }
8352 
8353  if (entity_attribute (entity, "id"))
8354  params_add (params, "alert_id", entity_attribute (entity, "id"));
8355  html = response_from_entity (connection, credentials, params, entity,
8356  (no_redirect && strcmp (no_redirect, "0")),
8357  NULL, "get_alerts",
8358  NULL, "new_alert",
8359  "Create Alert", response_data);
8360  free_entity (entity);
8361  g_free (response);
8362  return html;
8363 }
8364 
8375 char *
8376 delete_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
8377  params_t *params, cmd_response_data_t* response_data)
8378 {
8379  return delete_resource (connection, "alert", credentials, params, 0,
8380  "get_alerts", response_data);
8381 }
8382 
8394 char *
8395 get_alert (openvas_connection_t *connection, credentials_t * credentials,
8396  params_t *params, const char *extra_xml,
8397  cmd_response_data_t* response_data)
8398 {
8399  gchar *html;
8400  GString *extra;
8401 
8402  extra = g_string_new ("");
8403  if (extra_xml)
8404  g_string_append (extra, extra_xml);
8405  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
8406  {
8407  gchar *response;
8408  entity_t entity;
8409 
8410  response = NULL;
8411  entity = NULL;
8412  switch (omp (connection, credentials, &response, &entity, response_data,
8413  "<get_report_formats"
8414  " filter=\"rows=-1\"/>"))
8415  {
8416  case 0:
8417  case -1:
8418  break;
8419  case 1:
8420  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8421  return gsad_message (credentials,
8422  "Internal error", __FUNCTION__, __LINE__,
8423  "An internal error occurred while getting Report "
8424  "Formats for the alert. "
8425  "Diagnostics: Failure to send command to manager daemon.",
8426  "/omp?cmd=get_alerts", response_data);
8427  case 2:
8428  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8429  return gsad_message (credentials,
8430  "Internal error", __FUNCTION__, __LINE__,
8431  "An internal error occurred while getting Report "
8432  "Formats for the alert. "
8433  "Diagnostics: Failure to receive response from manager daemon.",
8434  "/omp?cmd=get_alerts", response_data);
8435  default:
8436  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8437  return gsad_message (credentials,
8438  "Internal error", __FUNCTION__, __LINE__,
8439  "An internal error occurred while getting Report "
8440  "Formats for the alert. "
8441  "It is unclear whether the task has been saved or not. "
8442  "Diagnostics: Internal Error.",
8443  "/omp?cmd=get_alerts", response_data);
8444  }
8445 
8446  g_string_append (extra, response);
8447 
8448  free_entity (entity);
8449  g_free (response);
8450  }
8451 
8452  if (command_enabled (credentials, "GET_TASKS"))
8453  {
8454  gchar *response;
8455  entity_t entity;
8456 
8457  response = NULL;
8458  entity = NULL;
8459  switch (omp (connection, credentials, &response, &entity, response_data,
8460  "<get_tasks"
8461  " schedules_only=\"1\""
8462  " filter=\"owner=any permission=start_task rows=-1\"/>"))
8463  {
8464  case 0:
8465  case -1:
8466  break;
8467  case 1:
8468  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8469  return gsad_message (credentials,
8470  "Internal error", __FUNCTION__, __LINE__,
8471  "An internal error occurred while getting Tasks "
8472  "for the alert. "
8473  "Diagnostics: Failure to send command to manager daemon.",
8474  "/omp?cmd=get_alerts", response_data);
8475  case 2:
8476  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8477  return gsad_message (credentials,
8478  "Internal error", __FUNCTION__, __LINE__,
8479  "An internal error occurred while getting Tasks "
8480  "for the alert. "
8481  "Diagnostics: Failure to receive response from manager daemon.",
8482  "/omp?cmd=get_alerts", response_data);
8483  default:
8484  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8485  return gsad_message (credentials,
8486  "Internal error", __FUNCTION__, __LINE__,
8487  "An internal error occurred while getting Tasks "
8488  "for the alert. "
8489  "Diagnostics: Internal Error.",
8490  "/omp?cmd=get_alerts", response_data);
8491  }
8492 
8493  g_string_append (extra, response);
8494 
8495  free_entity (entity);
8496  g_free (response);
8497  }
8498 
8499  if (command_enabled (credentials, "GET_FILTERS"))
8500  {
8501  gchar *response;
8502  entity_t entity;
8503 
8504  /* Get result filters for condition link. */
8505 
8506  response = NULL;
8507  entity = NULL;
8508  switch (omp (connection, credentials, &response, &entity, response_data,
8509  "<get_filters"
8510  " filter=\"type=result rows=-1\"/>"))
8511  {
8512  case 0:
8513  case -1:
8514  break;
8515  case 1:
8516  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8517  return gsad_message (credentials,
8518  "Internal error", __FUNCTION__, __LINE__,
8519  "An internal error occurred while getting filters "
8520  "for the alert. "
8521  "Diagnostics: Failure to send command to manager daemon.",
8522  "/omp?cmd=get_alerts", response_data);
8523  case 2:
8524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8525  return gsad_message (credentials,
8526  "Internal error", __FUNCTION__, __LINE__,
8527  "An internal error occurred while getting filters "
8528  "for the alert. "
8529  "Diagnostics: Failure to receive response from manager daemon.",
8530  "/omp?cmd=get_alerts", response_data);
8531  default:
8532  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8533  return gsad_message (credentials,
8534  "Internal error", __FUNCTION__, __LINE__,
8535  "An internal error occurred while getting filters "
8536  "for the alert. "
8537  "Diagnostics: Internal Error.",
8538  "/omp?cmd=get_alerts", response_data);
8539  }
8540 
8541  g_string_append (extra, response);
8542 
8543  free_entity (entity);
8544  g_free (response);
8545  }
8546 
8547  html = get_one (connection, "alert", credentials, params, extra->str,
8548  "tasks=\"1\"", response_data);
8549  g_string_free (extra, TRUE);
8550  return html;
8551 }
8552 
8563 char *
8564 get_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
8565  params_t *params, cmd_response_data_t* response_data)
8566 {
8567  return get_alert (connection, credentials, params, NULL, response_data);
8568 }
8569 
8581 char *
8582 get_alerts (openvas_connection_t *connection, credentials_t * credentials,
8583  params_t *params, const char *extra_xml,
8584  cmd_response_data_t* response_data)
8585 {
8586  gchar *html;
8587  GString *extra;
8588 
8589  extra = g_string_new ("");
8590  if (command_enabled (credentials, "GET_TASKS"))
8591  {
8592  gchar *response;
8593  entity_t entity;
8594 
8595  response = NULL;
8596  entity = NULL;
8597  switch (omp (connection, credentials, &response, &entity, response_data,
8598  "<get_tasks"
8599  " schedules_only=\"1\""
8600  " filter=\"owner=any permission=start_task rows=-1\"/>"))
8601  {
8602  case 0:
8603  case -1:
8604  break;
8605  case 1:
8606  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8607  return gsad_message (credentials,
8608  "Internal error", __FUNCTION__, __LINE__,
8609  "An internal error occurred getting the tasks. "
8610  "Diagnostics: Failure to send command to manager daemon.",
8611  "/omp?cmd=get_tasks",
8612  response_data);
8613  case 2:
8614  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8615  return gsad_message (credentials,
8616  "Internal error", __FUNCTION__, __LINE__,
8617  "An internal error occurred getting the tasks. "
8618  "Diagnostics: Failure to receive response from manager daemon.",
8619  "/omp?cmd=get_tasks",
8620  response_data);
8621  default:
8622  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8623  return gsad_message (credentials,
8624  "Internal error", __FUNCTION__, __LINE__,
8625  "An internal error occurred getting the reports. "
8626  "Diagnostics: Internal Error.",
8627  "/omp?cmd=get_tasks",
8628  response_data);
8629  }
8630 
8631  g_string_append (extra, response);
8632 
8633  free_entity (entity);
8634  g_free (response);
8635  }
8636 
8637 
8638  if (command_enabled (credentials, "GET_FILTERS"))
8639  {
8640  gchar *response;
8641  entity_t entity;
8642 
8643  /* Get result filters for condition link. */
8644 
8645  response = NULL;
8646  entity = NULL;
8647  switch (omp (connection, credentials, &response, &entity, response_data,
8648  "<get_filters"
8649  " filter=\"type=result rows=-1\"/>"))
8650  {
8651  case 0:
8652  case -1:
8653  break;
8654  case 1:
8655  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8656  return gsad_message (credentials,
8657  "Internal error", __FUNCTION__, __LINE__,
8658  "An internal error occurred while getting filters "
8659  "for the alerts. "
8660  "Diagnostics: Failure to send command to manager daemon.",
8661  "/omp?cmd=get_tasks", response_data);
8662  case 2:
8663  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8664  return gsad_message (credentials,
8665  "Internal error", __FUNCTION__, __LINE__,
8666  "An internal error occurred while getting filters "
8667  "for the alerts. "
8668  "Diagnostics: Failure to receive response from manager daemon.",
8669  "/omp?cmd=get_tasks", response_data);
8670  default:
8671  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8672  return gsad_message (credentials,
8673  "Internal error", __FUNCTION__, __LINE__,
8674  "An internal error occurred while getting filters "
8675  "for the alerts. "
8676  "Diagnostics: Internal Error.",
8677  "/omp?cmd=get_tasks", response_data);
8678  }
8679 
8680  g_string_append (extra, response);
8681 
8682  free_entity (entity);
8683  g_free (response);
8684  }
8685 
8686  if (extra_xml)
8687  g_string_append (extra, extra_xml);
8688  html = get_many (connection, "alert", credentials, params, extra->str, NULL,
8689  response_data);
8690  g_string_free (extra, TRUE);
8691  return html;
8692 }
8693 
8704 char *
8705 get_alerts_omp (openvas_connection_t *connection, credentials_t * credentials,
8706  params_t *params, cmd_response_data_t* response_data)
8707 {
8708  return get_alerts (connection, credentials, params, NULL, response_data);
8709 }
8710 
8722 char *
8723 edit_alert (openvas_connection_t *connection, credentials_t * credentials,
8724  params_t *params, const char *extra_xml,
8725  cmd_response_data_t* response_data)
8726 {
8727  GString *xml;
8728  gchar *edit;
8729  const char *alert_id, *next, *filter;
8730 
8731  alert_id = params_value (params, "alert_id");
8732  next = params_value (params, "next");
8733  filter = params_value (params, "filter");
8734 
8735  if (alert_id == NULL)
8736  {
8737  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
8738  return gsad_message (credentials,
8739  "Internal error", __FUNCTION__, __LINE__,
8740  "An internal error occurred while editing an alert. "
8741  "The alert remains as it was. "
8742  "Diagnostics: Required parameter alert_id was NULL.",
8743  "/omp?cmd=get_alerts", response_data);
8744  }
8745 
8746  if (next == NULL)
8747  next = "get_alerts";
8748 
8749  if (openvas_connection_sendf (connection,
8750  "<get_alerts"
8751  " alert_id=\"%s\""
8752  " details=\"1\"/>",
8753  alert_id)
8754  == -1)
8755  {
8756  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8757  return gsad_message (credentials,
8758  "Internal error", __FUNCTION__, __LINE__,
8759  "An internal error occurred while getting alert info. "
8760  "Diagnostics: Failure to send command to manager daemon.",
8761  "/omp?cmd=get_alerts", response_data);
8762  }
8763 
8764  xml = g_string_new ("<edit_alert>");
8765 
8766  if (extra_xml)
8767  g_string_append (xml, extra_xml);
8768 
8769  edit = g_markup_printf_escaped ("<alert id=\"%s\"/>"
8770  /* Page that follows. */
8771  "<next>%s</next>"
8772  /* Passthroughs. */
8773  "<filters><term>%s</term></filters>",
8774  alert_id,
8775  next,
8776  filter);
8777  g_string_append (xml, edit);
8778  g_free (edit);
8779 
8780  if (read_string_c (connection, &xml))
8781  {
8782  g_string_free (xml, TRUE);
8783  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8784  return gsad_message (credentials,
8785  "Internal error", __FUNCTION__, __LINE__,
8786  "An internal error occurred while getting alert info. "
8787  "Diagnostics: Failure to receive response from manager daemon.",
8788  "/omp?cmd=get_alerts", response_data);
8789  }
8790 
8791  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
8792  {
8793  /* Get the report formats. */
8794 
8795  if (openvas_connection_sendf (connection,
8796  "<get_report_formats"
8797  " filter=\"rows=-1\"/>")
8798  == -1)
8799  {
8800  g_string_free (xml, TRUE);
8801  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8802  return gsad_message (credentials,
8803  "Internal error", __FUNCTION__, __LINE__,
8804  "An internal error occurred while getting report formats. "
8805  "The current list of report formats is not available. "
8806  "Diagnostics: Failure to send command to manager daemon.",
8807  "/omp?cmd=get_alerts", response_data);
8808  }
8809 
8810  if (read_string_c (connection, &xml))
8811  {
8812  g_string_free (xml, TRUE);
8813  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8814  return gsad_message (credentials,
8815  "Internal error", __FUNCTION__, __LINE__,
8816  "An internal error occurred while getting report formats. "
8817  "The current list of report formats is not available. "
8818  "Diagnostics: Failure to receive response from manager daemon.",
8819  "/omp?cmd=get_alerts", response_data);
8820  }
8821  }
8822 
8823  if (command_enabled (credentials, "GET_FILTERS"))
8824  {
8825  /* Get filters. */
8826 
8827  if (openvas_connection_sendf (connection,
8828  "<get_filters filter=\"rows=-1\"/>")
8829  == -1)
8830  {
8831  g_string_free (xml, TRUE);
8832  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8833  return gsad_message (credentials,
8834  "Internal error", __FUNCTION__, __LINE__,
8835  "An internal error occurred while getting the list "
8836  "of filters. "
8837  "The current list of filters is not available. "
8838  "Diagnostics: Failure to send command to manager daemon.",
8839  "/omp?cmd=get_alerts", response_data);
8840  }
8841 
8842  if (read_string_c (connection, &xml))
8843  {
8844  g_string_free (xml, TRUE);
8845  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8846  return gsad_message (credentials,
8847  "Internal error", __FUNCTION__, __LINE__,
8848  "An internal error occurred while getting the list "
8849  "of filters. "
8850  "The current list of filters is not available. "
8851  "Diagnostics: Failure to receive response from manager daemon.",
8852  "/omp?cmd=get_filters", response_data);
8853  }
8854  }
8855 
8856  if (command_enabled (credentials, "GET_TASKS"))
8857  {
8858  /* Get tasks. */
8859 
8860  if (openvas_connection_sendf (connection,
8861  "<get_tasks"
8862  " schedules_only=\"1\""
8863  " filter=\"owner=any permission=start_task"
8864  " rows=-1\"/>")
8865  == -1)
8866  {
8867  g_string_free (xml, TRUE);
8868  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8869  return gsad_message (credentials,
8870  "Internal error", __FUNCTION__, __LINE__,
8871  "An internal error occurred while getting the list "
8872  "of tasks. "
8873  "The current list of tasks is not available. "
8874  "Diagnostics: Failure to send command to manager daemon.",
8875  "/omp?cmd=get_alerts", response_data);
8876  }
8877 
8878  if (read_string_c (connection, &xml))
8879  {
8880  g_string_free (xml, TRUE);
8881  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8882  return gsad_message (credentials,
8883  "Internal error", __FUNCTION__, __LINE__,
8884  "An internal error occurred while getting the list "
8885  "of tasks. "
8886  "The current list of tasks is not available. "
8887  "Diagnostics: Failure to receive response from manager daemon.",
8888  "/omp?cmd=get_tasks", response_data);
8889  }
8890  }
8891 
8892  /* Get Credentials. */
8893 
8894  if (command_enabled (credentials, "GET_CREDENTIALS"))
8895  {
8896  if (openvas_connection_sendf (connection,
8897  "<get_credentials"
8898  " filter=\"type=up owner=any permission=any"
8899  " rows=-1\"/>")
8900  == -1)
8901  {
8902  g_string_free (xml, TRUE);
8903  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8904  return gsad_message (credentials,
8905  "Internal error", __FUNCTION__, __LINE__,
8906  "An internal error occurred while getting the list "
8907  "of credentials. "
8908  "The current list of tasks is not available. "
8909  "Diagnostics: Failure to send command to manager daemon.",
8910  "/omp?cmd=get_alerts", response_data);
8911  }
8912 
8913  if (read_string_c (connection, &xml))
8914  {
8915  g_string_free (xml, TRUE);
8916  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8917  return gsad_message (credentials,
8918  "Internal error", __FUNCTION__, __LINE__,
8919  "An internal error occurred while getting the list "
8920  "of credentials. "
8921  "The current list of tasks is not available. "
8922  "Diagnostics: Failure to receive response from manager daemon.",
8923  "/omp?cmd=get_tasks", response_data);
8924  }
8925  }
8926 
8927  /* Cleanup, and return transformed XML. */
8928 
8929  g_string_append (xml, "</edit_alert>");
8930  return xsl_transform_omp (connection, credentials, params,
8931  g_string_free (xml, FALSE), response_data);
8932 }
8933 
8944 char *
8945 edit_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
8946  params_t *params, cmd_response_data_t* response_data)
8947 {
8948  return edit_alert (connection, credentials, params, NULL, response_data);
8949 }
8950 
8961 char *
8962 save_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
8963  params_t *params, cmd_response_data_t* response_data)
8964 {
8965  GString *xml;
8966  int ret;
8967  gchar *html, *response;
8968  const char *no_redirect, *name, *comment, *alert_id;
8969  const char *event, *condition, *method;
8970  const char *filter_id;
8971  params_t *event_data, *condition_data, *method_data;
8972  entity_t entity;
8973 
8974  no_redirect = params_value (params, "no_redirect");
8975  name = params_value (params, "name");
8976  comment = params_value (params, "comment");
8977  if (params_given (params, "condition"))
8978  condition = params_value (params, "condition");
8979  else
8980  condition = "Always";
8981  event = params_value (params, "event");
8982  method = params_value (params, "method");
8983  alert_id = params_value (params, "alert_id");
8984  filter_id = params_value (params, "filter_id");
8985 
8986  CHECK_PARAM_INVALID (name, "Save Alert", "edit_alert");
8987  CHECK_PARAM_INVALID (comment, "Save Alert", "edit_alert");
8988  CHECK_PARAM_INVALID (alert_id, "Save Alert", "edit_alert");
8989  CHECK_PARAM_INVALID (condition, "Save Alert", "edit_alert");
8990  CHECK_PARAM_INVALID (event, "Save Alert", "edit_alert");
8991  CHECK_PARAM_INVALID (method, "Save Alert", "edit_alert");
8992  if (strcmp (event, "Task run status changed") == 0)
8993  {
8994  CHECK_PARAM_INVALID (filter_id, "Save Alert", "edit_alert");
8995  }
8996 
8997  xml = g_string_new ("");
8998 
8999  /* Modify the alert. */
9000 
9001  event_data = params_values (params, "event_data:");
9002  condition_data = params_values (params, "condition_data:");
9003  method_data = params_values (params, "method_data:");
9004 
9005  if ((strcmp (event, "New SecInfo arrived") == 0) && event_data)
9006  {
9007  params_iterator_t iter;
9008  char *name;
9009  param_t *param;
9010 
9011  params_iterator_init (&iter, event_data);
9012  while (params_iterator_next (&iter, &name, &param))
9013  if ((strcmp (name, "feed_event") == 0)
9014  && param->value
9015  && (strcmp (param->value, "updated") == 0))
9016  {
9017  event = "Updated SecInfo arrived";
9018  break;
9019  }
9020  }
9021 
9022  xml_string_append (xml,
9023  "<modify_alert alert_id=\"%s\">"
9024  "<name>%s</name>"
9025  "<filter id=\"%s\"/>"
9026  "<comment>%s</comment>"
9027  "<event>%s",
9028  alert_id,
9029  name,
9030  filter_id,
9031  comment ? comment : "",
9032  event);
9033 
9034  append_alert_event_data (xml, event_data, event);
9035 
9036  xml_string_append (xml,
9037  "</event>"
9038  "<method>%s",
9039  method);
9040 
9041  append_alert_method_data (xml, method_data, method);
9042 
9043  xml_string_append (xml,
9044  "</method>"
9045  "<condition>%s",
9046  condition);
9047 
9048  append_alert_condition_data (xml, condition_data, condition);
9049 
9050  xml_string_append (xml,
9051  "</condition>"
9052  "</modify_alert>");
9053 
9054  ret = omp (connection, credentials,
9055  &response,
9056  &entity,
9057  response_data,
9058  xml->str);
9059  g_string_free (xml, TRUE);
9060  switch (ret)
9061  {
9062  case 0:
9063  case -1:
9064  break;
9065  case 1:
9066  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9067  return gsad_message (credentials,
9068  "Internal error", __FUNCTION__, __LINE__,
9069  "An internal error occurred while saving a new alert. "
9070  "No new alert was created. "
9071  "Diagnostics: Failure to send command to manager daemon.",
9072  "/omp?cmd=get_alerts", response_data);
9073  case 2:
9074  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9075  return gsad_message (credentials,
9076  "Internal error", __FUNCTION__, __LINE__,
9077  "An internal error occurred while saving a new alert. "
9078  "It is unclear whether the alert has been created or not. "
9079  "Diagnostics: Failure to receive response from manager daemon.",
9080  "/omp?cmd=get_alerts", response_data);
9081  default:
9082  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9083  return gsad_message (credentials,
9084  "Internal error", __FUNCTION__, __LINE__,
9085  "An internal error occurred while saving a new alert. "
9086  "It is unclear whether the alert has been created or not. "
9087  "Diagnostics: Internal Error.",
9088  "/omp?cmd=get_alerts", response_data);
9089  }
9090 
9091  html = response_from_entity (connection, credentials, params, entity,
9092  (no_redirect && strcmp (no_redirect, "0")),
9093  NULL, "get_alerts",
9094  NULL, "edit_alert",
9095  "Save Alert", response_data);
9096  free_entity (entity);
9097  g_free (response);
9098  return html;
9099 }
9100 
9111 char *
9112 test_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
9113  params_t *params, cmd_response_data_t* response_data)
9114 {
9115  gchar *html, *response;
9116  const char *no_redirect, *alert_id;
9117  entity_t entity;
9118 
9119  no_redirect = params_value (params, "no_redirect");
9120  alert_id = params_value (params, "alert_id");
9121 
9122  if (alert_id == NULL)
9123  {
9124  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
9125  return gsad_message (credentials,
9126  "Internal error", __FUNCTION__, __LINE__,
9127  "An internal error occurred while testing an alert. "
9128  "Diagnostics: Required parameter was NULL.",
9129  "/omp?cmd=get_alerts", response_data);
9130  }
9131 
9132  /* Test the alert. */
9133 
9134  if (openvas_connection_sendf (connection,
9135  "<test_alert alert_id=\"%s\"/>",
9136  alert_id)
9137  == -1)
9138  {
9139  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9140  return gsad_message (credentials,
9141  "Internal error", __FUNCTION__, __LINE__,
9142  "An internal error occurred while testing an alert. "
9143  "Diagnostics: Failure to send command to manager daemon.",
9144  "/omp?cmd=get_alerts", response_data);
9145  }
9146 
9147  entity = NULL;
9148  if (read_entity_and_text_c (connection, &entity, &response))
9149  {
9150  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9151  return gsad_message (credentials,
9152  "Internal error", __FUNCTION__, __LINE__,
9153  "An internal error occurred while testing an alert. "
9154  "Diagnostics: Failure to receive response from manager daemon.",
9155  "/omp?cmd=get_alerts", response_data);
9156  }
9157 
9158  /* Cleanup, and return transformed XML. */
9159 
9160  html = response_from_entity (connection, credentials, params, entity,
9161  (no_redirect && strcmp (no_redirect, "0")),
9162  NULL, "get_alerts",
9163  NULL, "get_alerts",
9164  "Test Alert", response_data);
9165 
9166  free_entity (entity);
9167  g_free (response);
9168  return html;
9169 }
9170 
9184 char *
9185 export_alert_omp (openvas_connection_t *connection, credentials_t * credentials,
9186  params_t *params, enum content_type * content_type,
9187  char **content_disposition, gsize *content_length,
9188  cmd_response_data_t* response_data)
9189 {
9190  return export_resource (connection, "alert", credentials, params,
9191  content_type, content_disposition, content_length,
9192  response_data);
9193 }
9194 
9209 char *
9210 export_alerts_omp (openvas_connection_t *connection,
9211  credentials_t * credentials, params_t *params,
9212  enum content_type * content_type, char **content_disposition,
9213  gsize *content_length, cmd_response_data_t* response_data)
9214 {
9215  return export_many (connection, "alert", credentials, params, content_type,
9216  content_disposition, content_length, response_data);
9217 }
9218 
9230 static char *
9231 new_target (openvas_connection_t *connection, credentials_t *credentials,
9232  params_t *params, const char *extra_xml,
9233  cmd_response_data_t* response_data)
9234 {
9235  GString *xml;
9236  gchar *port_list;
9237  gchar *ssh_credential, *smb_credential, *esxi_credential, *snmp_credential;
9238  gchar *end;
9239  const char *filter, *first, *max;
9240 
9241  filter = params_value (params, "filter");
9242  if (filter == NULL)
9243  filter = "";
9244 
9245  first = params_value (params, "first");
9246  if (first == NULL)
9247  first = "";
9248 
9249  max = params_value (params, "max");
9250  if (max == NULL)
9251  max = "";
9252 
9253  PARAM_OR_SETTING (port_list, "port_list_id",
9254  "d74a9ee8-7d35-4879-9485-ab23f1bd45bc",);
9255 
9256  PARAM_OR_SETTING (ssh_credential, "ssh_credential_id",
9257  "6fc56b72-c1cf-451c-a4c4-3a9dc784c3bd",
9258  g_free (port_list));
9259 
9260  PARAM_OR_SETTING (smb_credential, "smb_credential_id",
9261  "a25c0cfe-f977-417b-b1da-47da370c03e8",
9262  g_free (port_list);
9263  g_free (ssh_credential));
9264 
9265  PARAM_OR_SETTING (esxi_credential, "esxi_credential_id",
9266  "83545bcf-0c49-4b4c-abbf-63baf82cc2a7",
9267  g_free (port_list);
9268  g_free (ssh_credential);
9269  g_free (smb_credential));
9270 
9271  PARAM_OR_SETTING (snmp_credential, "snmp_credential_id",
9272  "024550b8-868e-4b3c-98bf-99bb732f6a0d",
9273  g_free (port_list);
9274  g_free (ssh_credential);
9275  g_free (smb_credential);
9276  g_free (esxi_credential));
9277 
9278  xml = g_string_new ("<new_target>");
9279 
9280  g_string_append_printf (xml,
9281  "<port_list_id>%s</port_list_id>"
9282  "<ssh_credential_id>%s</ssh_credential_id>"
9283  "<smb_credential_id>%s</smb_credential_id>"
9284  "<esxi_credential_id>%s</esxi_credential_id>"
9285  "<snmp_credential_id>%s</snmp_credential_id>",
9286  (port_list && strlen (port_list))
9287  ? port_list
9288  : "33d0cd82-57c6-11e1-8ed1-406186ea4fc5",
9289  ssh_credential ? ssh_credential : "",
9290  smb_credential ? smb_credential : "",
9291  esxi_credential ? esxi_credential : "",
9292  snmp_credential ? snmp_credential : "");
9293 
9294  g_free (port_list);
9295  g_free (ssh_credential);
9296  g_free (smb_credential);
9297  g_free (esxi_credential);
9298 
9299  if (extra_xml)
9300  g_string_append (xml, extra_xml);
9301 
9302  if (command_enabled (credentials, "GET_CREDENTIALS"))
9303  {
9304  /* Get the credentials. */
9305 
9306  if (openvas_connection_sendf (connection,
9307  "<get_credentials"
9308  " filter=\"rows=-1 sort=name\"/>")
9309  == -1)
9310  {
9311  g_string_free (xml, TRUE);
9312  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9313  return gsad_message (credentials,
9314  "Internal error", __FUNCTION__, __LINE__,
9315  "An internal error occurred while getting targets list. "
9316  "The current list of targets is not available. "
9317  "Diagnostics: Failure to send command to manager daemon.",
9318  "/omp?cmd=get_targets", response_data);
9319  }
9320 
9321  if (read_string_c (connection, &xml))
9322  {
9323  g_string_free (xml, TRUE);
9324  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9325  return gsad_message (credentials,
9326  "Internal error", __FUNCTION__, __LINE__,
9327  "An internal error occurred while getting targets list. "
9328  "The current list of targets is not available. "
9329  "Diagnostics: Failure to receive response from manager daemon.",
9330  "/omp?cmd=get_targets", response_data);
9331  }
9332  }
9333 
9334  if (command_enabled (credentials, "GET_PORT_LISTS"))
9335  {
9336  /* Get the port lists. */
9337 
9338  if (openvas_connection_sendf (connection,
9339  "<get_port_lists"
9340  " filter=\"rows=-1 sort=name\"/>")
9341  == -1)
9342  {
9343  g_string_free (xml, TRUE);
9344  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9345  return gsad_message (credentials,
9346  "Internal error", __FUNCTION__, __LINE__,
9347  "An internal error occurred while getting targets list. "
9348  "The current list of targets is not available. "
9349  "Diagnostics: Failure to send command to manager daemon.",
9350  "/omp?cmd=get_tasks", response_data);
9351  }
9352 
9353  if (read_string_c (connection, &xml))
9354  {
9355  g_string_free (xml, TRUE);
9356  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9357  return gsad_message (credentials,
9358  "Internal error", __FUNCTION__, __LINE__,
9359  "An internal error occurred while getting targets list. "
9360  "The current list of targets is not available. "
9361  "Diagnostics: Failure to receive response from manager daemon.",
9362  "/omp?cmd=get_tasks", response_data);
9363  }
9364  }
9365 
9366  end = g_markup_printf_escaped ("<filters><term>%s</term></filters>"
9367  "<targets start=\"%s\" max=\"%s\"/>"
9368  "</new_target>",
9369  filter,
9370  first,
9371  max);
9372  g_string_append (xml, end);
9373  g_free (end);
9374 
9375  return xsl_transform_omp (connection, credentials, params,
9376  g_string_free (xml, FALSE), response_data);
9377 }
9378 
9389 char *
9390 new_target_omp (openvas_connection_t *connection, credentials_t *credentials,
9391  params_t *params, cmd_response_data_t* response_data)
9392 {
9393  return new_target (connection, credentials, params, NULL, response_data);
9394 }
9395 
9406 char *
9407 create_target_omp (openvas_connection_t *connection,
9408  credentials_t * credentials, params_t *params,
9409  cmd_response_data_t* response_data)
9410 {
9411  int ret;
9412  gchar *html, *response, *command;
9413  const char *no_redirect, *name, *hosts, *exclude_hosts, *comment;
9414  const char *target_ssh_credential, *port, *target_smb_credential;
9415  const char *target_esxi_credential, *target_snmp_credential, *target_source;
9416  const char *port_list_id, *reverse_lookup_only, *reverse_lookup_unify;
9417  const char *alive_tests, *hosts_filter, *file;
9418  gchar *ssh_credentials_element, *smb_credentials_element;
9419  gchar *esxi_credentials_element, *snmp_credentials_element;
9420  gchar *asset_hosts_element;
9421  gchar* comment_element = NULL;
9422  entity_t entity;
9423  GString *xml;
9424 
9425  no_redirect = params_value (params, "no_redirect");
9426  name = params_value (params, "name");
9427  hosts = params_value (params, "hosts");
9428  exclude_hosts = params_value (params, "exclude_hosts");
9429  reverse_lookup_only = params_value (params, "reverse_lookup_only");
9430  reverse_lookup_unify = params_value (params, "reverse_lookup_unify");
9431  target_source = params_value (params, "target_source");
9432  comment = params_value (params, "comment");
9433  port_list_id = params_value (params, "port_list_id");
9434  target_ssh_credential = params_value (params, "ssh_credential_id");
9435  port = params_value (params, "port");
9436  target_smb_credential = params_value (params, "smb_credential_id");
9437  target_esxi_credential = params_value (params, "esxi_credential_id");
9438  target_snmp_credential = params_value (params, "snmp_credential_id");
9439  alive_tests = params_value (params, "alive_tests");
9440  hosts_filter = params_value (params, "hosts_filter");
9441  file = params_value (params, "file");
9442 
9443  CHECK_PARAM_INVALID (name, "Create Target", "new_target");
9444  CHECK_PARAM_INVALID (target_source, "Create Target", "new_target")
9445  if (hosts == NULL && strcmp (target_source, "manual") == 0)
9446  return message_invalid (connection, credentials, params, response_data,
9447  "Missing manual list of hosts",
9448  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
9449  "Create Target", "new_target");
9450  if (strcmp (target_source, "file") == 0 && file == NULL)
9451  return message_invalid (connection, credentials, params, response_data,
9452  "Missing hosts file",
9453  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
9454  "Create Target", "new_target");
9455 
9456  /* require hosts_filter if target_source is "asset_hosts" */
9457  if (strcmp (target_source, "asset_hosts") == 0)
9458  CHECK_PARAM_INVALID (hosts_filter, "Create Target", "new_target");
9459 
9460  CHECK_PARAM_INVALID (comment, "Create Target", "new_target");
9461  CHECK_PARAM_INVALID (port_list_id, "Create Target", "new_target");
9462  CHECK_PARAM_INVALID (target_ssh_credential, "Create Target", "new_target");
9463  if (strcmp (target_ssh_credential, "--"))
9464  CHECK_PARAM_INVALID (port, "Create Target", "new_target");
9465  CHECK_PARAM_INVALID (target_smb_credential, "Create Target", "new_target");
9466  CHECK_PARAM_INVALID (target_esxi_credential, "Create Target", "new_target");
9467  CHECK_PARAM_INVALID (target_snmp_credential, "Create Target", "new_target");
9468  CHECK_PARAM_INVALID (alive_tests, "Create Target", "new_target");
9469 
9470  if (comment != NULL)
9471  comment_element = g_strdup_printf ("<comment>%s</comment>", comment);
9472  else
9473  comment_element = g_strdup ("");
9474 
9475  if (strcmp (target_ssh_credential, "0") == 0)
9476  ssh_credentials_element = g_strdup ("");
9477  else
9478  ssh_credentials_element =
9479  g_strdup_printf ("<ssh_credential id=\"%s\">"
9480  "<port>%s</port>"
9481  "</ssh_credential>",
9482  target_ssh_credential,
9483  port);
9484 
9485  if (strcmp (target_smb_credential, "0") == 0)
9486  smb_credentials_element = g_strdup ("");
9487  else
9488  smb_credentials_element =
9489  g_strdup_printf ("<smb_credential id=\"%s\"/>",
9490  target_smb_credential);
9491 
9492  if (strcmp (target_esxi_credential, "0") == 0)
9493  esxi_credentials_element = g_strdup ("");
9494  else
9495  esxi_credentials_element =
9496  g_strdup_printf ("<esxi_credential id=\"%s\"/>",
9497  target_esxi_credential);
9498 
9499  if (strcmp (target_snmp_credential, "0") == 0)
9500  snmp_credentials_element = g_strdup ("");
9501  else
9502  snmp_credentials_element =
9503  g_strdup_printf ("<snmp_credential id=\"%s\"/>",
9504  target_snmp_credential);
9505 
9506  if (strcmp (target_source, "asset_hosts") == 0)
9507  asset_hosts_element = g_strdup_printf ("<asset_hosts filter=\"%s\"/>",
9508  hosts_filter);
9509  else
9510  asset_hosts_element = g_strdup ("");
9511 
9512  /* Create the target. */
9513 
9514  xml = g_string_new ("");
9515 
9516  xml_string_append (xml,
9517  "<name>%s</name>"
9518  "<hosts>%s</hosts>"
9519  "<exclude_hosts>%s</exclude_hosts>"
9520  "<reverse_lookup_only>%s</reverse_lookup_only>"
9521  "<reverse_lookup_unify>%s</reverse_lookup_unify>"
9522  "<port_list id=\"%s\"/>"
9523  "<alive_tests>%s</alive_tests>",
9524  name,
9525  strcmp (target_source, "file") == 0 ? file : hosts,
9526  exclude_hosts ? exclude_hosts : "",
9527  reverse_lookup_only ? reverse_lookup_only : "0",
9528  reverse_lookup_unify ? reverse_lookup_unify : "0",
9529  port_list_id,
9530  alive_tests);
9531 
9532  command = g_strdup_printf ("<create_target>"
9533  "%s%s%s%s%s%s%s"
9534  "</create_target>",
9535  xml->str,
9536  comment_element,
9537  ssh_credentials_element,
9538  smb_credentials_element,
9539  esxi_credentials_element,
9540  snmp_credentials_element,
9541  asset_hosts_element);
9542 
9543  g_string_free (xml, TRUE);
9544  g_free (comment_element);
9545  g_free (ssh_credentials_element);
9546  g_free (smb_credentials_element);
9547  g_free (esxi_credentials_element);
9548 
9549  ret = omp (connection, credentials,
9550  &response,
9551  &entity,
9552  response_data,
9553  command);
9554  g_free (command);
9555  switch (ret)
9556  {
9557  case 0:
9558  break;
9559  case -1:
9560  /* 'omp' set response. */
9561  return response;
9562  case 1:
9563  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9564  return gsad_message (credentials,
9565  "Internal error", __FUNCTION__, __LINE__,
9566  "An internal error occurred while creating a new target. "
9567  "No new target was created. "
9568  "Diagnostics: Failure to send command to manager daemon.",
9569  "/omp?cmd=get_targets", response_data);
9570  case 2:
9571  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9572  return gsad_message (credentials,
9573  "Internal error", __FUNCTION__, __LINE__,
9574  "An internal error occurred while creating a new target. "
9575  "It is unclear whether the target has been created or not. "
9576  "Diagnostics: Failure to receive response from manager daemon.",
9577  "/omp?cmd=get_targets", response_data);
9578  default:
9579  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9580  return gsad_message (credentials,
9581  "Internal error", __FUNCTION__, __LINE__,
9582  "An internal error occurred while creating a new target. "
9583  "It is unclear whether the target has been created or not. "
9584  "Diagnostics: Internal Error.",
9585  "/omp?cmd=get_targets", response_data);
9586  }
9587 
9588  if (entity_attribute (entity, "id"))
9589  params_add (params, "target_id", entity_attribute (entity, "id"));
9590  html = response_from_entity (connection, credentials, params, entity,
9591  (no_redirect && strcmp (no_redirect, "0")),
9592  NULL, "get_targets",
9593  NULL, "new_target",
9594  "Create Target", response_data);
9595  free_entity (entity);
9596  g_free (response);
9597  return html;
9598 }
9599 
9605 #define CHECK(name) \
9606  if (name == NULL) \
9607  { \
9608  response_data->http_status_code = MHD_HTTP_BAD_REQUEST; \
9609  return gsad_message (credentials, \
9610  "Internal error", __FUNCTION__, __LINE__, \
9611  "An internal error occurred while cloning a resource. " \
9612  "The resource was not cloned. " \
9613  "Diagnostics: Required parameter '" G_STRINGIFY (name) \
9614  "' was NULL.", \
9615  "/omp?cmd=get_tasks", response_data); \
9616  }
9617 
9628 char *
9629 clone_omp (openvas_connection_t *connection, credentials_t *credentials,
9630  params_t *params, cmd_response_data_t* response_data)
9631 {
9632  gchar *html, *response;
9633  const char *id, *type, *alterable, *no_redirect, *next_id;
9634  gchar *next_id_name, *cap_type, *prev_action;
9635  entity_t entity;
9636 
9637  id = params_value (params, "id");
9638  type = params_value (params, "resource_type");
9639  alterable = params_value (params, "alterable");
9640  no_redirect = params_value (params, "no_redirect");
9641 
9642  CHECK (id);
9643  CHECK (type);
9644 
9645  /* Clone the resource. */
9646 
9647  if (alterable && strcmp (alterable, "0"))
9648  {
9649  if (openvas_connection_sendf (connection,
9650  "<create_%s>"
9651  "<copy>%s</copy>"
9652  "<alterable>1</alterable>"
9653  "</create_%s>",
9654  type,
9655  id,
9656  type)
9657  == -1)
9658  {
9659  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9660  return gsad_message (credentials,
9661  "Internal error", __FUNCTION__, __LINE__,
9662  "An internal error occurred while cloning a resource. "
9663  "The resource was not cloned. "
9664  "Diagnostics: Failure to send command to manager daemon.",
9665  "/omp?cmd=get_tasks", response_data);
9666  }
9667  }
9668  else if (openvas_connection_sendf (connection,
9669  "<create_%s>"
9670  "<copy>%s</copy>"
9671  "</create_%s>",
9672  type,
9673  id,
9674  type)
9675  == -1)
9676  {
9677  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9678  return gsad_message (credentials,
9679  "Internal error", __FUNCTION__, __LINE__,
9680  "An internal error occurred while cloning a resource. "
9681  "The resource was not cloned. "
9682  "Diagnostics: Failure to send command to manager daemon.",
9683  "/omp?cmd=get_tasks", response_data);
9684  }
9685 
9686  entity = NULL;
9687  if (read_entity_and_text_c (connection, &entity, &response))
9688  {
9689  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9690  return gsad_message (credentials,
9691  "Internal error", __FUNCTION__, __LINE__,
9692  "An internal error occurred while cloning a resource. "
9693  "It is unclear whether the resource has been cloned or not. "
9694  "Diagnostics: Failure to read response from manager daemon.",
9695  "/omp?cmd=get_tasks", response_data);
9696  }
9697 
9698  /* Cleanup, and return next page. */
9699 
9700  if (omp_success (entity) == 0 || params_given (params, "next") == 0)
9701  {
9702  gchar *next;
9703  next = g_strdup_printf ("get_%ss", type);
9704  params_add (params, "next", next);
9705  g_free (next);
9706  }
9707 
9708  if (omp_success (entity))
9709  {
9710  next_id = entity_attribute (entity, "id");
9711  if (next_id == NULL)
9712  {
9713  free_entity (entity);
9714  g_free (response);
9715  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9716  return gsad_message (credentials,
9717  "Internal error", __FUNCTION__, __LINE__,
9718  "An internal error occurred while cloning a resource. "
9719  "The resource remains the same. "
9720  "Diagnostics: Error getting new resource.",
9721  "/omp?cmd=get_tasks", response_data);
9722  }
9723  next_id_name = g_strdup_printf ("%s_id", type);
9724  params_add (params, next_id_name, next_id);
9725  g_free (next_id_name);
9726  }
9727  else
9728  {
9729  set_http_status_from_entity (entity, response_data);
9730  next_id_name = NULL;
9731  next_id = NULL;
9732  }
9733 
9734  cap_type = capitalize (type);
9735  prev_action = g_strdup_printf ("Clone %s", cap_type);
9736  html = response_from_entity (connection, credentials, params, entity,
9737  (no_redirect && strcmp (no_redirect, "0")),
9738  NULL, NULL,
9739  NULL, NULL,
9740  prev_action, response_data);
9741 
9742  free_entity (entity);
9743  g_free (cap_type);
9744  g_free (prev_action);
9745  g_free (response);
9746 
9747  return html;
9748 }
9749 
9750 #undef CHECK
9751 
9762 char *
9763 delete_target_omp (openvas_connection_t *connection,
9764  credentials_t * credentials, params_t *params,
9765  cmd_response_data_t* response_data)
9766 {
9767  return delete_resource (connection, "target", credentials, params, 0,
9768  "get_targets", response_data);
9769 }
9770 
9781 char *
9782 delete_trash_agent_omp (openvas_connection_t *connection,
9783  credentials_t * credentials, params_t *params,
9784  cmd_response_data_t* response_data)
9785 {
9786  return delete_resource (connection, "agent", credentials, params, 1,
9787  "get_trash", response_data);
9788 }
9789 
9800 char *
9801 delete_trash_config_omp (openvas_connection_t *connection,
9802  credentials_t * credentials, params_t *params,
9803  cmd_response_data_t* response_data)
9804 {
9805  return delete_resource (connection, "config", credentials, params, 1,
9806  "get_trash", response_data);
9807 }
9808 
9819 char *
9820 delete_trash_alert_omp (openvas_connection_t *connection,
9821  credentials_t * credentials, params_t *params,
9822  cmd_response_data_t* response_data)
9823 {
9824  return delete_resource (connection, "alert", credentials, params, 1,
9825  "get_trash", response_data);
9826 }
9827 
9838 char *
9839 delete_trash_credential_omp (openvas_connection_t *connection,
9840  credentials_t * credentials, params_t *params,
9841  cmd_response_data_t* response_data)
9842 {
9843  return delete_resource (connection, "credential", credentials, params, 1,
9844  "get_trash", response_data);
9845 }
9846 
9857 char *
9858 delete_trash_report_format_omp (openvas_connection_t *connection,
9859  credentials_t * credentials, params_t *params,
9860  cmd_response_data_t* response_data)
9861 {
9862  return delete_resource (connection, "report_format", credentials, params, 1,
9863  "get_trash", response_data);
9864 }
9865 
9876 char *
9877 delete_trash_schedule_omp (openvas_connection_t *connection,
9878  credentials_t * credentials, params_t *params,
9879  cmd_response_data_t* response_data)
9880 {
9881  return delete_resource (connection, "schedule", credentials, params, 1,
9882  "get_trash", response_data);
9883 }
9884 
9895 char *
9896 delete_trash_target_omp (openvas_connection_t *connection,
9897  credentials_t * credentials, params_t *params,
9898  cmd_response_data_t* response_data)
9899 {
9900  return delete_resource (connection, "target", credentials, params, 1,
9901  "get_trash", response_data);
9902 }
9903 
9914 char *
9915 delete_trash_task_omp (openvas_connection_t *connection,
9916  credentials_t * credentials, params_t *params,
9917  cmd_response_data_t* response_data)
9918 {
9919  return delete_resource (connection, "task", credentials, params, 1,
9920  "get_trash", response_data);
9921 }
9922 
9933 char *
9934 restore_omp (openvas_connection_t *connection, credentials_t * credentials,
9935  params_t *params, cmd_response_data_t* response_data)
9936 {
9937  GString *xml;
9938  gchar *ret;
9939  entity_t entity;
9940  const char *target_id, *no_redirect;
9941 
9942  target_id = params_value (params, "target_id");
9943  no_redirect = params_value (params, "no_redirect");
9944 
9945  if (target_id == NULL)
9946  {
9947  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
9948  return gsad_message (credentials,
9949  "Internal error", __FUNCTION__, __LINE__,
9950  "An internal error occurred while restoring a resource. "
9951  "The resource was not restored. "
9952  "Diagnostics: Required parameter was NULL.",
9953  "/omp?cmd=get_tasks", response_data);
9954  }
9955 
9956  xml = g_string_new ("");
9957 
9958  /* Restore the resource. */
9959 
9960  if (openvas_connection_sendf (connection,
9961  "<restore"
9962  " id=\"%s\"/>",
9963  target_id)
9964  == -1)
9965  {
9966  g_string_free (xml, TRUE);
9967  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9968  return gsad_message (credentials,
9969  "Internal error", __FUNCTION__, __LINE__,
9970  "An internal error occurred while restoring a resource. "
9971  "The resource was not deleted. "
9972  "Diagnostics: Failure to send command to manager daemon.",
9973  "/omp?cmd=get_trash", response_data);
9974  }
9975 
9976  if (read_entity_and_string_c (connection, &entity, &xml))
9977  {
9978  g_string_free (xml, TRUE);
9979  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9980  return gsad_message (credentials,
9981  "Internal error", __FUNCTION__, __LINE__,
9982  "An internal error occurred while restoring a resource. "
9983  "It is unclear whether the resource has been restored or not. "
9984  "Diagnostics: Failure to read response from manager daemon.",
9985  "/omp?cmd=get_trash", response_data);
9986  }
9987 
9988  /* Cleanup, and return trash page. */
9989 
9990  ret = response_from_entity (connection, credentials, params, entity,
9991  (no_redirect && strcmp (no_redirect, "0")),
9992  NULL, "get_trash",
9993  NULL, "get_trash",
9994  "Restore", response_data);
9995  free_entity (entity);
9996  g_string_free (xml, FALSE);
9997  return ret;
9998 }
9999 
10010 char *
10011 empty_trashcan_omp (openvas_connection_t *connection,
10012  credentials_t * credentials, params_t *params,
10013  cmd_response_data_t* response_data)
10014 {
10015  GString *xml;
10016  const char* no_redirect;
10017  gchar *ret;
10018  entity_t entity;
10019 
10020  no_redirect = params_value (params, "no_redirect");
10021 
10022  xml = g_string_new ("");
10023 
10024  /* Empty the trash. */
10025 
10026  if (openvas_connection_sendf (connection,
10027  "<empty_trashcan/>")
10028  == -1)
10029  {
10030  g_string_free (xml, TRUE);
10031  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10032  return gsad_message (credentials,
10033  "Internal error", __FUNCTION__, __LINE__,
10034  "An internal error occurred while emptying the trashcan. "
10035  "Diagnostics: Failure to send command to manager daemon.",
10036  "/omp?cmd=get_trash", response_data);
10037  }
10038 
10039  if (read_entity_and_string_c (connection, &entity, &xml))
10040  {
10041  g_string_free (xml, TRUE);
10042  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10043  return gsad_message (credentials,
10044  "Internal error", __FUNCTION__, __LINE__,
10045  "An internal error occurred while emptying the trashcan. "
10046  "Diagnostics: Failure to read response from manager daemon.",
10047  "/omp?cmd=get_trash", response_data);
10048  }
10049 
10050  /* Cleanup, and return trash page. */
10051 
10052  ret = response_from_entity (connection, credentials, params, entity,
10053  (no_redirect && strcmp (no_redirect, "0")),
10054  NULL, "get_trash",
10055  NULL, "get_trash",
10056  "Empty Trashcan", response_data);
10057  free_entity (entity);
10058  g_string_free (xml, FALSE);
10059  return ret;
10060 }
10061 
10073 static char *
10074 new_tag (openvas_connection_t *connection, credentials_t *credentials,
10075  params_t *params, const char *extra_xml,
10076  cmd_response_data_t* response_data)
10077 {
10078  GString *xml;
10079  gchar *end;
10080  const char *resource_type, *resource_id, *tag_id, *tag_name;
10081 
10082  resource_type = params_value (params, "resource_type");
10083  resource_id = params_value (params, "resource_id");
10084 
10085  tag_id = params_value (params, "tag_id");
10086  tag_name = params_value (params, "tag_name");
10087 
10088  xml = g_string_new ("<new_tag>");
10089 
10090  if (extra_xml)
10091  g_string_append (xml, extra_xml);
10092 
10093  end = g_markup_printf_escaped ("<tag id=\"%s\"/>"
10094  "<resource_type>%s</resource_type>"
10095  "<resource_id>%s</resource_id>"
10096  "<tag_name>%s%s</tag_name>"
10097  "<tag_value></tag_value>"
10098  "<comment></comment>"
10099  "<active>1</active>"
10100  "</new_tag>",
10101  tag_id ? tag_id : "0",
10102  resource_type ? resource_type : "",
10103  resource_id ? resource_id : "",
10104  tag_name ? tag_name : (resource_type
10105  ? resource_type
10106  : "default"),
10107  tag_name ? "" : ":unnamed");
10108  g_string_append (xml, end);
10109  g_free (end);
10110 
10111  return xsl_transform_omp (connection, credentials, params,
10112  g_string_free (xml, FALSE), response_data);
10113 }
10114 
10125 char *
10126 new_tag_omp (openvas_connection_t *connection, credentials_t *credentials,
10127  params_t *params, cmd_response_data_t* response_data)
10128 {
10129  return new_tag (connection, credentials, params, NULL, response_data);
10130 }
10131 
10142 char *
10143 create_tag_omp (openvas_connection_t *connection, credentials_t *credentials,
10144  params_t *params, cmd_response_data_t* response_data)
10145 {
10146  char *ret;
10147  const char* no_redirect;
10148  gchar *response;
10149  const char *name, *comment, *value, *resource_type, *resource_id, *active;
10150  entity_t entity;
10151 
10152  no_redirect = params_value (params, "no_redirect");
10153  name = params_value (params, "tag_name");
10154  comment = params_value (params, "comment");
10155  value = params_value (params, "tag_value");
10156  resource_type = params_value (params, "resource_type");
10157  resource_id = params_value (params, "resource_id");
10158  active = params_value (params, "active");
10159 
10160  CHECK_PARAM_INVALID (name, "Create Tag", "new_tag")
10161  CHECK_PARAM_INVALID (comment, "Create Tag", "new_tag")
10162  CHECK_PARAM_INVALID (value, "Create Tag", "new_tag")
10163  CHECK_PARAM_INVALID (resource_type, "Create Tag", "new_tag")
10164  CHECK_PARAM_INVALID (resource_id, "Create Tag", "new_tag")
10165  CHECK_PARAM_INVALID (active, "Create Tag", "new_tag")
10166 
10167  response = NULL;
10168  entity = NULL;
10169  switch (ompf (connection, credentials,
10170  &response,
10171  &entity,
10172  response_data,
10173  "<create_tag>"
10174  "<name>%s</name>"
10175  "<comment>%s</comment>"
10176  "<value>%s</value>"
10177  "<resource id=\"%s\">"
10178  "<type>%s</type>"
10179  "</resource>"
10180  "<active>%s</active>"
10181  "</create_tag>",
10182  name,
10183  comment,
10184  value,
10185  resource_id,
10186  resource_type,
10187  active))
10188  {
10189  case 0:
10190  case -1:
10191  break;
10192  case 1:
10193  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10194  return gsad_message (credentials,
10195  "Internal error", __FUNCTION__, __LINE__,
10196  "An internal error occurred while creating a new tag. "
10197  "No new tag was created. "
10198  "Diagnostics: Failure to send command to manager daemon.",
10199  "/omp?cmd=get_targets", response_data);
10200  case 2:
10201  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10202  return gsad_message (credentials,
10203  "Internal error", __FUNCTION__, __LINE__,
10204  "An internal error occurred while creating a new tag. "
10205  "It is unclear whether the tag has been created or not. "
10206  "Diagnostics: Failure to receive response from manager daemon.",
10207  "/omp?cmd=get_tags", response_data);
10208  default:
10209  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10210  return gsad_message (credentials,
10211  "Internal error", __FUNCTION__, __LINE__,
10212  "An internal error occurred while creating a new tag. "
10213  "It is unclear whether the tag has been created or not. "
10214  "Diagnostics: Internal Error.",
10215  "/omp?cmd=get_tags", response_data);
10216  }
10217 
10218  if (entity_attribute (entity, "id"))
10219  params_add (params, "tag_id", entity_attribute (entity, "id"));
10220  ret = response_from_entity (connection, credentials, params, entity,
10221  (no_redirect && strcmp (no_redirect, "0")),
10222  NULL, "get_tags",
10223  NULL, "new_tag",
10224  "Create Tag", response_data);
10225 
10226  free_entity (entity);
10227  g_free (response);
10228  return ret;
10229 }
10230 
10241 char *
10242 delete_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10243  params_t *params, cmd_response_data_t* response_data)
10244 {
10245  return delete_resource (connection, "tag", credentials, params, 0, NULL,
10246  response_data);
10247 }
10248 
10259 char *
10260 delete_trash_tag_omp (openvas_connection_t *connection,
10261  credentials_t * credentials, params_t *params,
10262  cmd_response_data_t* response_data)
10263 {
10264  return delete_resource (connection, "tag", credentials, params, 1,
10265  "get_trash", response_data);
10266 }
10267 
10279 char *
10280 edit_tag (openvas_connection_t *connection, credentials_t * credentials,
10281  params_t *params, const char *extra_xml,
10282  cmd_response_data_t* response_data)
10283 {
10284  GString *xml;
10285  gchar *edit;
10286  const char *tag_id;
10287 
10288  tag_id = params_value (params, "tag_id");
10289  if (tag_id == NULL)
10290  {
10291  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10292  return gsad_message (credentials,
10293  "Internal error", __FUNCTION__, __LINE__,
10294  "An internal error occurred while editing a tag. "
10295  "The tag remains as it was. "
10296  "Diagnostics: Required parameter was NULL.",
10297  "/omp?cmd=get_tags", response_data);
10298  }
10299 
10300  if (openvas_connection_sendf (connection,
10301  "<get_tags"
10302  " tag_id=\"%s\""
10303  "/>",
10304  tag_id)
10305  == -1)
10306  {
10307  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10308  return gsad_message (credentials,
10309  "Internal error", __FUNCTION__, __LINE__,
10310  "An internal error occurred while getting tag info. "
10311  "Diagnostics: Failure to send command to manager daemon.",
10312  "/omp?cmd=get_tags", response_data);
10313  }
10314 
10315  xml = g_string_new ("");
10316 
10317  edit = g_markup_printf_escaped ("<edit_tag>"
10318  "<tag id=\"%s\"/>",
10319  tag_id);
10320 
10321  g_string_append (xml, edit);
10322  g_free (edit);
10323 
10324  if (extra_xml)
10325  g_string_append (xml, extra_xml);
10326 
10327  if (read_string_c (connection, &xml))
10328  {
10329  g_string_free (xml, TRUE);
10330  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10331  return gsad_message (credentials,
10332  "Internal error", __FUNCTION__, __LINE__,
10333  "An internal error occurred while getting target info. "
10334  "Diagnostics: Failure to receive response from manager daemon.",
10335  "/omp?cmd=get_tags", response_data);
10336  }
10337 
10338  /* Cleanup, and return transformed XML. */
10339 
10340  g_string_append (xml, "</edit_tag>");
10341  return xsl_transform_omp (connection, credentials, params,
10342  g_string_free (xml, FALSE), response_data);
10343 }
10344 
10355 char *
10356 edit_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10357  params_t *params, cmd_response_data_t* response_data)
10358 {
10359  return edit_tag (connection, credentials, params, NULL, response_data);
10360 }
10361 
10372 char *
10373 save_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10374  params_t *params, cmd_response_data_t* response_data)
10375 {
10376  gchar *response;
10377  const char *name, *comment, *value, *resource_type, *resource_id, *active;
10378  const char *tag_id, *no_redirect;
10379  entity_t entity;
10380  char* ret;
10381 
10382  no_redirect = params_value (params, "no_redirect");
10383  tag_id = params_value (params, "tag_id");
10384  name = params_value (params, "tag_name");
10385  comment = params_value (params, "comment");
10386  value = params_value (params, "tag_value");
10387  resource_type = params_value (params, "resource_type");
10388  resource_id = params_value (params, "resource_id");
10389  active = params_value (params, "active");
10390 
10391  CHECK_PARAM_INVALID (tag_id, "Save Tag", "edit_tag")
10392  CHECK_PARAM_INVALID (name, "Save Tag", "edit_tag")
10393  CHECK_PARAM_INVALID (comment, "Save Tag", "edit_tag")
10394  CHECK_PARAM_INVALID (value, "Save Tag", "edit_tag")
10395  CHECK_PARAM_INVALID (resource_type, "Save Tag", "edit_tag")
10396  CHECK_PARAM_INVALID (resource_id, "Save Tag", "edit_tag")
10397  CHECK_PARAM_INVALID (active, "Save Tag", "edit_tag")
10398 
10399  response = NULL;
10400  entity = NULL;
10401  switch (ompf (connection, credentials,
10402  &response,
10403  &entity,
10404  response_data,
10405  "<modify_tag tag_id=\"%s\">"
10406  "<name>%s</name>"
10407  "<comment>%s</comment>"
10408  "<value>%s</value>"
10409  "<resource id=\"%s\">"
10410  "<type>%s</type>"
10411  "</resource>"
10412  "<active>%s</active>"
10413  "</modify_tag>",
10414  tag_id,
10415  name,
10416  comment,
10417  value,
10418  resource_id,
10419  resource_type,
10420  active))
10421  {
10422  case 0:
10423  case -1:
10424  break;
10425  case 1:
10426  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10427  return gsad_message (credentials,
10428  "Internal error", __FUNCTION__, __LINE__,
10429  "An internal error occurred while saving a tag. "
10430  "The tag remains the same. "
10431  "Diagnostics: Failure to send command to "
10432  "manager daemon.",
10433  "/omp?cmd=get_targets", response_data);
10434  case 2:
10435  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10436  return gsad_message (credentials,
10437  "Internal error", __FUNCTION__, __LINE__,
10438  "An internal error occurred while saving a tag. "
10439  "It is unclear whether the tag has been saved "
10440  "or not. "
10441  "Diagnostics: Failure to receive response from "
10442  "manager daemon.",
10443  "/omp?cmd=get_tags", response_data);
10444  default:
10445  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10446  return gsad_message (credentials,
10447  "Internal error", __FUNCTION__, __LINE__,
10448  "An internal error occurred while saving a tag. "
10449  "It is unclear whether the tag has been saved "
10450  "or not. "
10451  "Diagnostics: Internal Error.",
10452  "/omp?cmd=get_tags", response_data);
10453  }
10454 
10455  ret = response_from_entity (connection, credentials, params, entity,
10456  (no_redirect && strcmp (no_redirect, "0")),
10457  NULL, "get_tags",
10458  NULL, "edit_tag",
10459  "Save Tag", response_data);
10460 
10461  free_entity (entity);
10462  g_free (response);
10463  return ret;
10464 
10465 }
10466 
10481 char *
10482 export_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10483  params_t *params, enum content_type * content_type,
10484  char **content_disposition, gsize *content_length,
10485  cmd_response_data_t* response_data)
10486 {
10487  return export_resource (connection, "tag", credentials, params, content_type,
10488  content_disposition, content_length, response_data);
10489 }
10490 
10505 char *
10506 export_tags_omp (openvas_connection_t *connection, credentials_t * credentials,
10507  params_t *params, enum content_type * content_type,
10508  char **content_disposition, gsize *content_length,
10509  cmd_response_data_t* response_data)
10510 {
10511  return export_many (connection, "tag", credentials, params, content_type,
10512  content_disposition, content_length, response_data);
10513 }
10514 
10526 static char *
10527 get_tag (openvas_connection_t *connection, credentials_t * credentials,
10528  params_t *params, const char *extra_xml,
10529  cmd_response_data_t* response_data)
10530 {
10531  return get_one (connection, "tag", credentials, params, extra_xml, NULL,
10532  response_data);
10533 }
10534 
10545 char *
10546 get_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10547  params_t *params, cmd_response_data_t* response_data)
10548 {
10549  return get_tag (connection, credentials, params, NULL, response_data);
10550 }
10551 
10563 static char *
10564 get_tags (openvas_connection_t *connection, credentials_t * credentials,
10565  params_t *params, const char *extra_xml,
10566  cmd_response_data_t* response_data)
10567 {
10568  return get_many (connection, "tag", credentials, params, extra_xml, NULL,
10569  response_data);
10570 }
10571 
10582 char *
10583 get_tags_omp (openvas_connection_t *connection, credentials_t * credentials,
10584  params_t *params, cmd_response_data_t* response_data)
10585 {
10586  return get_tags (connection, credentials, params, NULL, response_data);
10587 }
10588 
10599 char *
10600 toggle_tag_omp (openvas_connection_t *connection, credentials_t * credentials,
10601  params_t *params, cmd_response_data_t* response_data)
10602 {
10603  gchar *html, *response;
10604  const char *no_redirect, *tag_id, *enable;
10605  entity_t entity;
10606 
10607  no_redirect = params_value (params, "no_redirect");
10608  tag_id = params_value (params, "tag_id");
10609  enable = params_value (params, "enable");
10610 
10611  if (tag_id == NULL)
10612  {
10613  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10614  return gsad_message (credentials,
10615  "Internal error", __FUNCTION__, __LINE__,
10616  "An internal error occurred while modifying a tag. "
10617  "The tag was not modified. "
10618  "Diagnostics: Required parameter tag_id was NULL.",
10619  "/omp?cmd=get_tasks", response_data);
10620  }
10621  if (enable == NULL)
10622  {
10623  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10624  return gsad_message (credentials,
10625  "Internal error", __FUNCTION__, __LINE__,
10626  "An internal error occurred while modifying a tag. "
10627  "The tag was not modified. "
10628  "Diagnostics: Required parameter enable was NULL.",
10629  "/omp?cmd=get_tasks", response_data);
10630  }
10631 
10632  /* Delete the resource and get all resources. */
10633 
10634  if (openvas_connection_sendf (connection,
10635  "<modify_tag tag_id=\"%s\">"
10636  "<active>%s</active>"
10637  "</modify_tag>",
10638  tag_id,
10639  enable)
10640  == -1)
10641  {
10642  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10643  return gsad_message (credentials,
10644  "Internal error", __FUNCTION__, __LINE__,
10645  "An internal error occurred while modifying a tag. "
10646  "The tag is not modified. "
10647  "Diagnostics: Failure to send command to"
10648  " manager daemon.",
10649  "/omp?cmd=get_tasks", response_data);
10650  }
10651 
10652  entity = NULL;
10653  if (read_entity_and_text_c (connection, &entity, &response))
10654  {
10655  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10656  return gsad_message (credentials,
10657  "Internal error", __FUNCTION__, __LINE__,
10658  "An internal error occurred while modifying a tag. "
10659  "It is unclear whether the tag has been modified"
10660  " or not. "
10661  "Diagnostics: Failure to read response from"
10662  " manager daemon.",
10663  "/omp?cmd=get_tasks", response_data);
10664  }
10665 
10666  if (! omp_success (entity))
10667  set_http_status_from_entity (entity, response_data);
10668  html = response_from_entity (connection, credentials, params, entity,
10669  (no_redirect && strcmp (no_redirect, "0")),
10670  NULL, "get_tags",
10671  NULL, "get_tags",
10672  "Toggle Tag", response_data);
10673 
10674  free_entity (entity);
10675  g_free (response);
10676 
10677  return html;
10678 }
10679 
10691 char *
10692 edit_target (openvas_connection_t *connection, credentials_t * credentials,
10693  params_t *params, const char *extra_xml,
10694  cmd_response_data_t* response_data)
10695 {
10696  GString *xml;
10697  gchar *edit;
10698  const char *target_id, *next, *filter, *first, *max;
10699 
10700  target_id = params_value (params, "target_id");
10701  filter = params_value (params, "filter");
10702  first = params_value (params, "first");
10703  max = params_value (params, "max");
10704  next = params_value (params, "next");
10705 
10706  if (target_id == NULL)
10707  {
10708  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10709  return gsad_message (credentials,
10710  "Internal error", __FUNCTION__, __LINE__,
10711  "An internal error occurred while editing a target. "
10712  "The target remains as it was. "
10713  "Diagnostics: Required parameter was NULL.",
10714  "/omp?cmd=get_targets", response_data);
10715  }
10716 
10717  if (next == NULL)
10718  next = "get_target";
10719 
10720  if (openvas_connection_sendf (connection,
10721  "<get_targets"
10722  " target_id=\"%s\""
10723  " details=\"1\"/>",
10724  target_id)
10725  == -1)
10726  {
10727  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10728  return gsad_message (credentials,
10729  "Internal error", __FUNCTION__, __LINE__,
10730  "An internal error occurred while getting target info. "
10731  "Diagnostics: Failure to send command to manager daemon.",
10732  "/omp?cmd=get_targets", response_data);
10733  }
10734 
10735  xml = g_string_new ("");
10736 
10737  if (extra_xml)
10738  g_string_append (xml, extra_xml);
10739 
10740 
10741  edit = g_markup_printf_escaped ("<edit_target>"
10742  "<target id=\"%s\"/>"
10743  /* Page that follows. */
10744  "<next>%s</next>"
10745  /* Passthroughs. */
10746  "<filters><term>%s</term></filters>"
10747  "<targets start=\"%s\" max=\"%s\"/>",
10748  target_id,
10749  next,
10750  filter,
10751  first,
10752  max);
10753  g_string_append (xml, edit);
10754  g_free (edit);
10755 
10756  if (read_string_c (connection, &xml))
10757  {
10758  g_string_free (xml, TRUE);
10759  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10760  return gsad_message (credentials,
10761  "Internal error", __FUNCTION__, __LINE__,
10762  "An internal error occurred while getting target info. "
10763  "Diagnostics: Failure to receive response from manager daemon.",
10764  "/omp?cmd=get_targets", response_data);
10765  }
10766 
10767  if (command_enabled (credentials, "GET_CREDENTIALS"))
10768  {
10769  /* Get the credentials. */
10770 
10771  if (openvas_connection_sendf (connection,
10772  "<get_credentials"
10773  " filter=\"rows=-1 sort=name\"/>")
10774  == -1)
10775  {
10776  g_string_free (xml, TRUE);
10777  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10778  return gsad_message (credentials,
10779  "Internal error", __FUNCTION__, __LINE__,
10780  "An internal error occurred while getting targets list. "
10781  "The current list of targets is not available. "
10782  "Diagnostics: Failure to send command to manager daemon.",
10783  "/omp?cmd=get_targets", response_data);
10784  }
10785 
10786  if (read_string_c (connection, &xml))
10787  {
10788  g_string_free (xml, TRUE);
10789  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10790  return gsad_message (credentials,
10791  "Internal error", __FUNCTION__, __LINE__,
10792  "An internal error occurred while getting targets list. "
10793  "The current list of targets is not available. "
10794  "Diagnostics: Failure to receive response from manager daemon.",
10795  "/omp?cmd=get_targets", response_data);
10796  }
10797  }
10798 
10799  if (command_enabled (credentials, "GET_PORT_LISTS"))
10800  {
10801  /* Get the port lists. */
10802 
10803  if (openvas_connection_sendf (connection,
10804  "<get_port_lists"
10805  " filter=\"rows=-1 sort=name\"/>")
10806  == -1)
10807  {
10808  g_string_free (xml, TRUE);
10809  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10810  return gsad_message (credentials,
10811  "Internal error", __FUNCTION__, __LINE__,
10812  "An internal error occurred while getting targets list. "
10813  "The current list of targets is not available. "
10814  "Diagnostics: Failure to send command to manager daemon.",
10815  "/omp?cmd=get_tasks", response_data);
10816  }
10817 
10818  if (read_string_c (connection, &xml))
10819  {
10820  g_string_free (xml, TRUE);
10821  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10822  return gsad_message (credentials,
10823  "Internal error", __FUNCTION__, __LINE__,
10824  "An internal error occurred while getting targets list. "
10825  "The current list of targets is not available. "
10826  "Diagnostics: Failure to receive response from manager daemon.",
10827  "/omp?cmd=get_tasks", response_data);
10828  }
10829  }
10830 
10831  /* Cleanup, and return transformed XML. */
10832 
10833  g_string_append (xml, "</edit_target>");
10834  return xsl_transform_omp (connection, credentials, params,
10835  g_string_free (xml, FALSE), response_data);
10836 }
10837 
10848 char *
10849 edit_target_omp (openvas_connection_t *connection, credentials_t * credentials,
10850  params_t *params, cmd_response_data_t* response_data)
10851 {
10852  return edit_target (connection, credentials, params, NULL, response_data);
10853 }
10854 
10866 static char *
10867 get_target (openvas_connection_t *connection, credentials_t * credentials,
10868  params_t *params, const char *extra_xml,
10869  cmd_response_data_t* response_data)
10870 {
10871  return get_one (connection, "target", credentials, params, extra_xml,
10872  "tasks=\"1\"", response_data);
10873 }
10874 
10885 char *
10886 get_target_omp (openvas_connection_t *connection, credentials_t * credentials,
10887  params_t *params, cmd_response_data_t* response_data)
10888 {
10889  return get_target (connection, credentials, params, NULL, response_data);
10890 }
10891 
10903 static char *
10904 get_targets (openvas_connection_t *connection, credentials_t * credentials,
10905  params_t *params, const char *extra_xml,
10906  cmd_response_data_t* response_data)
10907 {
10908  return get_many (connection, "target", credentials, params, extra_xml, NULL,
10909  response_data);
10910 }
10911 
10922 char *
10923 get_targets_omp (openvas_connection_t *connection, credentials_t * credentials,
10924  params_t *params, cmd_response_data_t* response_data)
10925 {
10926  return get_targets (connection, credentials, params, NULL, response_data);
10927 }
10928 
10939 char *
10940 save_target_omp (openvas_connection_t *connection, credentials_t * credentials,
10941  params_t *params, cmd_response_data_t* response_data)
10942 {
10943  gchar *html, *response;
10944  const char *no_redirect, *name, *hosts, *exclude_hosts, *comment;
10945  const char *target_ssh_credential, *port, *target_smb_credential;
10946  const char *target_esxi_credential, *target_snmp_credential, *target_source;
10947  const char *target_id, *port_list_id, *reverse_lookup_only;
10948  const char *reverse_lookup_unify, *alive_tests, *in_use;
10949  GString *command;
10950 
10951  no_redirect = params_value (params, "no_redirect");
10952  alive_tests = params_value (params, "alive_tests");
10953  name = params_value (params, "name");
10954  comment = params_value (params, "comment");
10955  in_use = params_value (params, "in_use");
10956  target_id = params_value (params, "target_id");
10957 
10958  CHECK_PARAM_INVALID (name, "Save Target", "edit_target");
10959  CHECK_PARAM_INVALID (target_id, "Save Target", "edit_target");
10960  CHECK_PARAM_INVALID (comment, "Save Target", "edit_target");
10961  CHECK_PARAM_INVALID (alive_tests, "Save Target", "edit_target");
10962  CHECK_PARAM_INVALID (in_use, "Save Target", "edit_target");
10963 
10964  if (strcmp (in_use, "0"))
10965  {
10966  entity_t entity;
10967  int ret;
10968 
10969  /* Target is in use. Modify fewer fields. */
10970 
10971  command = g_string_new ("");
10972  xml_string_append (command,
10973  "<modify_target target_id=\"%s\">"
10974  "<name>%s</name>"
10975  "<comment>%s</comment>"
10976  "<alive_tests>%s</alive_tests>"
10977  "</modify_target>",
10978  target_id,
10979  name ? name : "",
10980  comment ? comment : "",
10981  alive_tests);
10982 
10983  response = NULL;
10984  entity = NULL;
10985  ret = omp (connection, credentials, &response, &entity, response_data,
10986  command->str);
10987  g_string_free (command, TRUE);
10988  switch (ret)
10989  {
10990  case 0:
10991  case -1:
10992  break;
10993  case 1:
10994  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10995  return gsad_message (credentials,
10996  "Internal error", __FUNCTION__, __LINE__,
10997  "An internal error occurred while saving a target. "
10998  "The target remains the same. "
10999  "Diagnostics: Failure to send command to manager daemon.",
11000  "/omp?cmd=get_targets", response_data);
11001  case 2:
11002  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11003  return gsad_message (credentials,
11004  "Internal error", __FUNCTION__, __LINE__,
11005  "An internal error occurred while saving a target. "
11006  "It is unclear whether the target has been saved or not. "
11007  "Diagnostics: Failure to receive response from manager daemon.",
11008  "/omp?cmd=get_targets", response_data);
11009  default:
11010  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11011  return gsad_message (credentials,
11012  "Internal error", __FUNCTION__, __LINE__,
11013  "An internal error occurred while saving a target. "
11014  "It is unclear whether the target has been saved or not. "
11015  "Diagnostics: Internal Error.",
11016  "/omp?cmd=get_targets", response_data);
11017  }
11018 
11019  html = response_from_entity (connection, credentials, params, entity,
11020  (no_redirect && strcmp (no_redirect, "0")),
11021  NULL, "get_targets",
11022  NULL, "edit_target",
11023  "Save Target", response_data);
11024 
11025  free_entity (entity);
11026  g_free (response);
11027  return html;
11028  }
11029 
11030  hosts = params_value (params, "hosts");
11031  exclude_hosts = params_value (params, "exclude_hosts");
11032  reverse_lookup_only = params_value (params, "reverse_lookup_only");
11033  reverse_lookup_unify = params_value (params, "reverse_lookup_unify");
11034  target_source = params_value (params, "target_source");
11035  port_list_id = params_value (params, "port_list_id");
11036  target_ssh_credential = params_value (params, "ssh_credential_id");
11037  port = params_value (params, "port");
11038  target_smb_credential = params_value (params, "smb_credential_id");
11039  target_esxi_credential = params_value (params, "esxi_credential_id");
11040  target_snmp_credential = params_value (params, "snmp_credential_id");
11041 
11042  CHECK_PARAM_INVALID (target_source, "Save Target", "edit_target");
11043  CHECK_PARAM_INVALID (port_list_id, "Save Target", "edit_target");
11044  CHECK_PARAM_INVALID (target_ssh_credential, "Save Target", "edit_target");
11045  CHECK_PARAM_INVALID (target_smb_credential, "Save Target", "edit_target");
11046  CHECK_PARAM_INVALID (target_esxi_credential, "Save Target", "edit_target");
11047  CHECK_PARAM_INVALID (target_snmp_credential, "Save Target", "edit_target");
11048 
11049  if (strcmp (target_ssh_credential, "--")
11050  && strcmp (target_ssh_credential, "0"))
11051  CHECK_PARAM_INVALID (port, "Save Target", "edit_target");
11052 
11053  if (hosts == NULL && strcmp (target_source, "manual") == 0)
11054  {
11055  return new_target (connection, credentials, params,
11056  GSAD_MESSAGE_INVALID_PARAM ("Modify Target"),
11057  response_data);
11058  }
11059  if (strcmp (target_source, "import") == 0 && name == NULL)
11060  {
11061  gchar *msg;
11062  msg = g_strdup_printf (GSAD_MESSAGE_INVALID,
11063  "Given target_source was invalid",
11064  "Modify Target");
11065  html = new_target (connection, credentials, params, msg, response_data);
11066  g_free (msg);
11067  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11068  return html;
11069  }
11070 
11071  {
11072  int ret;
11073  gchar *ssh_credentials_element, *smb_credentials_element;
11074  gchar *esxi_credentials_element, *snmp_credentials_element;
11075  gchar* comment_element;
11076  entity_t entity;
11077 
11078  if (comment)
11079  comment_element = g_strdup_printf ("<comment>%s</comment>", comment);
11080  else
11081  comment_element = g_strdup ("");
11082 
11083  if (strcmp (target_ssh_credential, "--") == 0)
11084  ssh_credentials_element = g_strdup ("");
11085  else
11086  ssh_credentials_element =
11087  g_strdup_printf ("<ssh_credential id=\"%s\">"
11088  "<port>%s</port>"
11089  "</ssh_credential>",
11090  target_ssh_credential,
11091  port);
11092 
11093  if (strcmp (target_smb_credential, "--") == 0)
11094  smb_credentials_element = g_strdup ("");
11095  else
11096  smb_credentials_element =
11097  g_strdup_printf ("<smb_credential id=\"%s\"/>",
11098  target_smb_credential);
11099 
11100  if (strcmp (target_esxi_credential, "--") == 0)
11101  esxi_credentials_element = g_strdup ("");
11102  else
11103  esxi_credentials_element =
11104  g_strdup_printf ("<esxi_credential id=\"%s\"/>",
11105  target_esxi_credential);
11106 
11107  if (strcmp (target_snmp_credential, "--") == 0)
11108  snmp_credentials_element = g_strdup ("");
11109  else
11110  snmp_credentials_element =
11111  g_strdup_printf ("<snmp_credential id=\"%s\"/>",
11112  target_snmp_credential);
11113 
11114  command = g_string_new ("");
11115  xml_string_append (command,
11116  "<modify_target target_id=\"%s\">"
11117  "<name>%s</name>"
11118  "<hosts>%s</hosts>"
11119  "<exclude_hosts>%s</exclude_hosts>"
11120  "<reverse_lookup_only>%s</reverse_lookup_only>"
11121  "<reverse_lookup_unify>%s</reverse_lookup_unify>"
11122  "<port_list id=\"%s\"/>"
11123  "<alive_tests>%s</alive_tests>",
11124  target_id,
11125  name,
11126  strcmp (target_source, "file") == 0
11127  ? params_value (params, "file")
11128  : hosts,
11129  exclude_hosts ? exclude_hosts : "",
11130  reverse_lookup_only ? reverse_lookup_only : "0",
11131  reverse_lookup_unify ? reverse_lookup_unify : "0",
11132  port_list_id,
11133  alive_tests);
11134 
11135  g_string_append_printf (command,
11136  "%s%s%s%s%s"
11137  "</modify_target>",
11138  comment_element,
11139  ssh_credentials_element,
11140  smb_credentials_element,
11141  esxi_credentials_element,
11142  snmp_credentials_element);
11143 
11144  g_free (comment_element);
11145  g_free (ssh_credentials_element);
11146  g_free (smb_credentials_element);
11147  g_free (esxi_credentials_element);
11148  g_free (snmp_credentials_element);
11149 
11150  /* Modify the target. */
11151 
11152  ret = openvas_connection_sendf (connection, "%s", command->str);
11153  g_string_free (command, TRUE);
11154 
11155  if (ret == -1)
11156  {
11157  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11158  return gsad_message (credentials,
11159  "Internal error", __FUNCTION__, __LINE__,
11160  "An internal error occurred while modifying target. "
11161  "No target was modified. "
11162  "Diagnostics: Failure to send command to manager daemon.",
11163  "/omp?cmd=get_targets", response_data);
11164  }
11165 
11166  entity = NULL;
11167  if (read_entity_and_text_c (connection, &entity, &response))
11168  {
11169  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11170  return gsad_message (credentials,
11171  "Internal error", __FUNCTION__, __LINE__,
11172  "An internal error occurred while modifying a target. "
11173  "It is unclear whether the target has been modified or not. "
11174  "Diagnostics: Failure to receive response from manager daemon.",
11175  "/omp?cmd=get_targets", response_data);
11176  }
11177 
11178  html = response_from_entity (connection, credentials, params, entity,
11179  (no_redirect && strcmp (no_redirect, "0")),
11180  NULL, "get_targets",
11181  NULL, "edit_target",
11182  "Save Target", response_data);
11183  }
11184 
11185  /* Pass response to handler of following page. */
11186 
11187  return html;
11188 }
11189 
11204 char *
11205 export_target_omp (openvas_connection_t *connection,
11206  credentials_t * credentials, params_t *params,
11207  enum content_type * content_type, char **content_disposition,
11208  gsize *content_length, cmd_response_data_t* response_data)
11209 {
11210  return export_resource (connection, "target", credentials, params,
11211  content_type, content_disposition, content_length,
11212  response_data);
11213 }
11214 
11229 char *
11230 export_targets_omp (openvas_connection_t *connection,
11231  credentials_t * credentials, params_t *params,
11232  enum content_type * content_type, char **content_disposition,
11233  gsize *content_length, cmd_response_data_t* response_data)
11234 {
11235  return export_many (connection, "target", credentials, params, content_type,
11236  content_disposition, content_length, response_data);
11237 }
11238 
11250 static char *
11251 new_config (openvas_connection_t *connection, credentials_t *credentials,
11252  params_t *params, const char *extra_xml,
11253  cmd_response_data_t* response_data)
11254 {
11255  GString *xml;
11256  int ret;
11257  entity_t entity = NULL;
11258  gchar *response = NULL;
11259 
11260  xml = g_string_new ("<new_config>");
11261  if (extra_xml)
11262  g_string_append (xml, extra_xml);
11263 
11264  /* Get Scanners. */
11265  ret = omp (connection, credentials, &response, &entity, response_data,
11266  "<get_scanners/>");
11267  switch (ret)
11268  {
11269  case 0:
11270  case -1:
11271  break;
11272  case 1:
11273  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11274  return gsad_message (credentials,
11275  "Internal error", __FUNCTION__, __LINE__,
11276  "An internal error occurred while getting scanners"
11277  " for new config. Diagnostics: Failure to send"
11278  " command to manager daemon.",
11279  "/omp?cmd=get_configs", response_data);
11280  case 2:
11281  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11282  return gsad_message (credentials,
11283  "Internal error", __FUNCTION__, __LINE__,
11284  "An internal error occurred while getting scanners"
11285  " for new config. " "Diagnostics: Failure to"
11286  " receive response from manager daemon.",
11287  "/omp?cmd=get_configs", response_data);
11288  default:
11289  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11290  return gsad_message (credentials,
11291  "Internal error", __FUNCTION__, __LINE__,
11292  "An internal error occurred while getting scanners"
11293  "for new config. It is unclear whether the config"
11294  " has been saved or not. "
11295  "Diagnostics: Internal Error.",
11296  "/omp?cmd=get_configs", response_data);
11297  }
11298  g_string_append (xml, response);
11299  g_free (response);
11300  free_entity (entity);
11301 
11302  g_string_append (xml, "</new_config>");
11303  return xsl_transform_omp (connection, credentials, params,
11304  g_string_free (xml, FALSE), response_data);
11305 }
11306 
11317 char *
11318 new_config_omp (openvas_connection_t *connection, credentials_t *credentials,
11319  params_t *params, cmd_response_data_t* response_data)
11320 {
11321  return new_config (connection, credentials, params, NULL, response_data);
11322 }
11323 
11335 static char *
11336 upload_config (openvas_connection_t *connection, credentials_t *credentials,
11337  params_t *params, const char *extra_xml,
11338  cmd_response_data_t* response_data)
11339 {
11340  GString *xml;
11341 
11342  xml = g_string_new ("<upload_config>");
11343  if (extra_xml)
11344  g_string_append (xml, extra_xml);
11345  g_string_append (xml, "</upload_config>");
11346 
11347  return xsl_transform_omp (connection, credentials, params,
11348  g_string_free (xml, FALSE), response_data);
11349 }
11350 
11361 char *
11362 upload_config_omp (openvas_connection_t *connection, credentials_t *credentials,
11363  params_t *params, cmd_response_data_t* response_data)
11364 {
11365  return upload_config (connection, credentials, params, NULL, response_data);
11366 }
11367 
11378 char *
11379 create_config_omp (openvas_connection_t *connection,
11380  credentials_t * credentials, params_t *params,
11381  cmd_response_data_t* response_data)
11382 {
11383  gchar *html, *response;
11384  const char *no_redirect, *name, *comment, *base, *scanner = NULL;
11385  entity_t entity;
11386 
11387  no_redirect = params_value (params, "no_redirect");
11388  name = params_value (params, "name");
11389  comment = params_value (params, "comment");
11390  base = params_value (params, "base");
11391 
11392  CHECK_PARAM_INVALID (name, "New Config", "new_config");
11393  CHECK_PARAM_INVALID (comment, "New Config", "new_config");
11394  CHECK_PARAM_INVALID (base, "New Config", "new_config");
11395  if (!strcmp (base, "0"))
11396  {
11397  scanner = params_value (params, "scanner_id");
11398  CHECK_PARAM_INVALID (scanner, "New Config", "new_config");
11399  }
11400 
11401  /* Create the config. */
11402  switch (ompf (connection, credentials,
11403  &response,
11404  &entity,
11405  response_data,
11406  "<create_config>"
11407  "<name>%s</name>"
11408  "<copy>%s</copy>"
11409  "<comment>%s</comment>"
11410  "<scanner>%s</scanner>"
11411  "</create_config>",
11412  name, base, comment,
11413  scanner ?: ""))
11414  {
11415  case 0:
11416  case -1:
11417  break;
11418  case 1:
11419  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11420  return gsad_message (credentials,
11421  "Internal error", __FUNCTION__, __LINE__,
11422  "An internal error occurred while creating a new config. "
11423  "No new config was created. "
11424  "Diagnostics: Failure to send command to manager daemon.",
11425  "/omp?cmd=get_configs", response_data);
11426  case 2:
11427  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11428  return gsad_message (credentials,
11429  "Internal error", __FUNCTION__, __LINE__,
11430  "An internal error occurred while creating a new config. "
11431  "It is unclear whether the config has been created or not. "
11432  "Diagnostics: Failure to receive response from manager daemon.",
11433  "/omp?cmd=get_configs", response_data);
11434  default:
11435  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11436  return gsad_message (credentials,
11437  "Internal error", __FUNCTION__, __LINE__,
11438  "An internal error occurred while creating a new config. "
11439  "It is unclear whether the config has been created or not. "
11440  "Diagnostics: Internal Error.",
11441  "/omp?cmd=get_configs", response_data);
11442  }
11443 
11444  if (entity_attribute (entity, "id"))
11445  params_add (params, "config_id", entity_attribute (entity, "id"));
11446  html = response_from_entity (connection, credentials, params, entity,
11447  (no_redirect && strcmp (no_redirect, "0")),
11448  NULL, "get_configs",
11449  NULL, "new_config",
11450  "Create Config", response_data);
11451 
11452  free_entity (entity);
11453  g_free (response);
11454  return html;
11455 }
11456 
11467 char *
11468 import_config_omp (openvas_connection_t *connection,
11469  credentials_t * credentials, params_t *params,
11470  cmd_response_data_t* response_data)
11471 {
11472  const char *no_redirect;
11473  gchar *command, *html, *response;
11474  entity_t entity;
11475  int ret;
11476 
11477  no_redirect = params_value (params, "no_redirect");
11478 
11479  /* Create the config. */
11480 
11481  response = NULL;
11482  entity = NULL;
11483  command = g_strdup_printf ("<create_config>"
11484  "%s"
11485  "</create_config>",
11486  params_value (params, "xml_file"));
11487  ret = omp (connection, credentials, &response, &entity, response_data,
11488  command);
11489  g_free (command);
11490  switch (ret)
11491  {
11492  case 0:
11493  case -1:
11494  break;
11495  case 1:
11496  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11497  return gsad_message (credentials,
11498  "Internal error", __FUNCTION__, __LINE__,
11499  "An internal error occurred while importing a config. "
11500  "The schedule remains the same. "
11501  "Diagnostics: Failure to send command to manager daemon.",
11502  "/omp?cmd=get_configs", response_data);
11503  case 2:
11504  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11505  return gsad_message (credentials,
11506  "Internal error", __FUNCTION__, __LINE__,
11507  "An internal error occurred while importing a config. "
11508  "It is unclear whether the schedule has been saved or not. "
11509  "Diagnostics: Failure to receive response from manager daemon.",
11510  "/omp?cmd=get_configs", response_data);
11511  default:
11512  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11513  return gsad_message (credentials,
11514  "Internal error", __FUNCTION__, __LINE__,
11515  "An internal error occurred while importing a config. "
11516  "It is unclear whether the schedule has been saved or not. "
11517  "Diagnostics: Internal Error.",
11518  "/omp?cmd=get_configs", response_data);
11519  }
11520 
11521  /* Cleanup, and return transformed XML. */
11522 
11523  html = response_from_entity (connection, credentials, params, entity,
11524  (no_redirect && strcmp (no_redirect, "0")),
11525  NULL, "get_configs",
11526  NULL, "new_config",
11527  "Import Config", response_data);
11528  free_entity (entity);
11529  g_free (response);
11530  return html;
11531 }
11532 
11544 static char *
11545 get_configs (openvas_connection_t *connection, credentials_t *credentials,
11546  params_t *params, const char *extra_xml,
11547  cmd_response_data_t* response_data)
11548 {
11549  return get_many (connection, "config", credentials, params, extra_xml, NULL,
11550  response_data);
11551 }
11552 
11563 char *
11564 get_configs_omp (openvas_connection_t *connection, credentials_t * credentials,
11565  params_t *params, cmd_response_data_t* response_data)
11566 {
11567  return get_configs (connection, credentials, params, NULL, response_data);
11568 }
11569 
11582 static char *
11583 get_config (openvas_connection_t *connection, credentials_t * credentials,
11584  params_t *params, const char *extra_xml, int edit,
11585  cmd_response_data_t* response_data)
11586 {
11587  GString *xml;
11588  const char *config_id;
11589 
11590  config_id = params_value (params, "config_id");
11591 
11592  if (config_id == NULL)
11593  return get_configs (connection, credentials, params, extra_xml,
11594  response_data);
11595 
11596  xml = g_string_new ("<get_config_response>");
11597  if (edit) g_string_append (xml, "<edit/>");
11598 
11599  if (extra_xml)
11600  g_string_append (xml, extra_xml);
11601  /* Get the config families. */
11602 
11603  if (openvas_connection_sendf (connection,
11604  "<get_configs"
11605  " config_id=\"%s\""
11606  " families=\"1\""
11607  " tasks=\"1\""
11608  " preferences=\"1\"/>",
11609  config_id)
11610  == -1)
11611  {
11612  g_string_free (xml, TRUE);
11613  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11614  return gsad_message (credentials,
11615  "Internal error", __FUNCTION__, __LINE__,
11616  "An internal error occurred while getting the config. "
11617  "The config is not available. "
11618  "Diagnostics: Failure to send command to manager daemon.",
11619  "/omp?cmd=get_configs", response_data);
11620  }
11621 
11622  if (read_string_c (connection, &xml))
11623  {
11624  g_string_free (xml, TRUE);
11625  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11626  return gsad_message (credentials,
11627  "Internal error", __FUNCTION__, __LINE__,
11628  "An internal error occurred while getting the config. "
11629  "The config is not available. "
11630  "Diagnostics: Failure to receive response from manager daemon.",
11631  "/omp?cmd=get_configs", response_data);
11632  }
11633 
11634  /* Get all the families. */
11635 
11636  if (openvas_connection_sendf (connection, "<get_nvt_families/>") == -1)
11637  {
11638  g_string_free (xml, TRUE);
11639  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11640  return gsad_message (credentials,
11641  "Internal error", __FUNCTION__, __LINE__,
11642  "An internal error occurred while getting the config. "
11643  "The config is not available. "
11644  "Diagnostics: Failure to send command to manager daemon.",
11645  "/omp?cmd=get_configs", response_data);
11646  }
11647 
11648  if (read_string_c (connection, &xml))
11649  {
11650  g_string_free (xml, TRUE);
11651  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11652  return gsad_message (credentials,
11653  "Internal error", __FUNCTION__, __LINE__,
11654  "An internal error occurred while getting the config. "
11655  "The config is not available. "
11656  "Diagnostics: Failure to receive response from manager daemon.",
11657  "/omp?cmd=get_configs", response_data);
11658  }
11659 
11660  if (edit)
11661  {
11662  /* Get OSP scanners */
11663  if (openvas_connection_sendf (connection, "<get_scanners filter=\"type=1\"/>")
11664  == -1)
11665  {
11666  g_string_free (xml, TRUE);
11667  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11668  return gsad_message
11669  (credentials, "Internal error", __FUNCTION__, __LINE__,
11670  "An internal error occurred while getting the config. "
11671  "The config is not available. "
11672  "Diagnostics: Failure to send command to manager daemon.",
11673  "/omp?cmd=get_configs", response_data);
11674  }
11675 
11676  if (read_string_c (connection, &xml))
11677  {
11678  g_string_free (xml, TRUE);
11679  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11680  return gsad_message
11681  (credentials, "Internal error", __FUNCTION__, __LINE__,
11682  "An internal error occurred while getting the config. "
11683  "The config is not available. "
11684  "Diagnostics: Failure to receive response from manager daemon.",
11685  "/omp?cmd=get_configs", response_data);
11686  }
11687 
11688  }
11689 
11690  /* Get Credentials */
11691  if (openvas_connection_sendf (connection, "<get_credentials/>") == -1)
11692  {
11693  g_string_free (xml, TRUE);
11694  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11695  return gsad_message
11696  (credentials, "Internal error", __FUNCTION__, __LINE__,
11697  "An internal error occurred while getting the config. "
11698  "The config is not available. "
11699  "Diagnostics: Failure to send command to manager daemon.",
11700  "/omp?cmd=get_configs", response_data);
11701  }
11702  if (read_string_c (connection, &xml))
11703  {
11704  g_string_free (xml, TRUE);
11705  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11706  return gsad_message
11707  (credentials, "Internal error", __FUNCTION__, __LINE__,
11708  "An internal error occurred while getting the config. "
11709  "The config is not available. "
11710  "Diagnostics: Failure to receive response from manager daemon.",
11711  "/omp?cmd=get_configs", response_data);
11712  }
11713 
11714  /* Get the permissions */
11715 
11716  g_string_append (xml, "<permissions>");
11717 
11718  if (openvas_connection_sendf (connection,
11719  "<get_permissions"
11720  " filter=\"name:^.*(config)s?$"
11721  " and resource_uuid=%s"
11722  " first=1 rows=-1\"/>",
11723  config_id)
11724  == -1)
11725  {
11726  g_string_free (xml, TRUE);
11727  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11728  return gsad_message (credentials,
11729  "Internal error", __FUNCTION__, __LINE__,
11730  "An internal error occurred while getting permissions list. "
11731  "The current list of resources is not available. "
11732  "Diagnostics: Failure to send command to manager daemon.",
11733  "/omp?cmd=get_resources", response_data);
11734  }
11735 
11736  if (read_string_c (connection, &xml))
11737  {
11738  g_string_free (xml, TRUE);
11739  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11740  return gsad_message (credentials,
11741  "Internal error", __FUNCTION__, __LINE__,
11742  "An internal error occurred while getting permissions list. "
11743  "The current list of resources is not available. "
11744  "Diagnostics: Failure to receive response from manager daemon.",
11745  "/omp?cmd=get_resources", response_data);
11746  }
11747 
11748  g_string_append (xml, "</permissions>");
11749 
11750  /* Cleanup, and return transformed XML. */
11751 
11752  g_string_append (xml, "</get_config_response>");
11753  return xsl_transform_omp (connection, credentials, params,
11754  g_string_free (xml, FALSE), response_data);
11755 }
11756 
11767 char *
11768 get_config_omp (openvas_connection_t *connection, credentials_t * credentials,
11769  params_t *params, cmd_response_data_t* response_data)
11770 {
11771  return get_config (connection, credentials, params, NULL, 0, response_data);
11772 }
11773 
11784 static char *
11785 edit_config (openvas_connection_t *connection, credentials_t * credentials,
11786  params_t *params, const char *extra_xml,
11787  cmd_response_data_t* response_data)
11788 {
11789  return get_config (connection, credentials, params, extra_xml, 1,
11790  response_data);
11791 }
11792 
11803 char *
11804 edit_config_omp (openvas_connection_t *connection, credentials_t * credentials,
11805  params_t *params, cmd_response_data_t* response_data)
11806 {
11807  return edit_config (connection, credentials, params, NULL, response_data);
11808 }
11809 
11820 char *
11821 sync_config_omp (openvas_connection_t *connection, credentials_t * credentials,
11822  params_t *params, cmd_response_data_t* response_data)
11823 {
11824  GString *xml;
11825  const char *config_id, *next;
11826  char *ret;
11827 
11828  config_id = params_value (params, "config_id");
11829  CHECK_PARAM (config_id, "Synchronize Config", get_configs);
11830 
11831  if (openvas_connection_sendf (connection, "<sync_config config_id=\"%s\"/>",
11832  config_id)
11833  == -1)
11834  {
11835  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11836  return gsad_message
11837  (credentials, "Internal error", __FUNCTION__, __LINE__,
11838  "An internal error occurred while synchronizing a config. "
11839  "The config is not synchronized. "
11840  "Diagnostics: Failure to send command to manager daemon.",
11841  "/omp?cmd=get_configs", response_data);
11842  }
11843 
11844  xml = g_string_new ("");
11845 
11846  if (read_string_c (connection, &xml))
11847  {
11848  g_string_free (xml, TRUE);
11849  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11850  return gsad_message
11851  (credentials, "Internal error", __FUNCTION__, __LINE__,
11852  "An internal error occurred while synchronizing a config. "
11853  "It is unclear whether the config has been synchronized or not. "
11854  "Diagnostics: Failure to receive response from manager daemon.",
11855  "/omp?cmd=get_configs", response_data);
11856  }
11857 
11858  next = params_value (params, "next");
11859  if (next && !strcmp (next, "get_config"))
11860  ret = get_config (connection, credentials, params, xml->str, 0,
11861  response_data);
11862  else
11863  ret = get_configs (connection, credentials, params, xml->str,
11864  response_data);
11865  g_string_free (xml, TRUE);
11866  return ret;
11867 }
11868 
11869 
11883 static char *
11884 save_osp_prefs (credentials_t *credentials, openvas_connection_t *connection,
11885  params_t *params, const char *next, const char *fail_next,
11886  int *success, cmd_response_data_t* response_data)
11887 {
11888  GHashTableIter iter;
11889  gpointer param_name, val;
11890  char *ret;
11891  const char *config_id;
11892 
11893  config_id = params_value (params, "config_id");
11894  g_hash_table_iter_init (&iter, params);
11895  ret = NULL;
11896  while (g_hash_table_iter_next (&iter, &param_name, &val))
11897  {
11898  gchar *value;
11899  param_t *param = val;
11900 
11901  g_free (ret);
11902  ret = NULL;
11903 
11904  if (!g_str_has_prefix (param_name, "osp_pref_"))
11905  continue;
11906  value = param->value_size ? g_base64_encode ((guchar *) param->value,
11907  param->value_size)
11908  : g_strdup ("");
11909 
11910  /* Send the name without the osp_pref_ prefix. */
11911  param_name = ((char *) param_name) + 9;
11912  if (openvas_connection_sendf (connection,
11913  "<modify_config config_id=\"%s\">"
11914  "<preference><name>%s</name>"
11915  "<value>%s</value></preference>"
11916  "</modify_config>",
11917  config_id,
11918  (char *) param_name,
11919  value)
11920  == -1)
11921  {
11922  g_free (value);
11923  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11924  return gsad_message
11925  (credentials, "Internal error", __FUNCTION__, __LINE__,
11926  "An internal error occurred while saving a config. It is"
11927  " unclear whether the entire config has been saved. "
11928  "Diagnostics: Failure to send command to manager daemon.",
11929  "/omp?cmd=get_configs", response_data);
11930  }
11931  g_free (value);
11932 
11933  ret = check_modify_config (credentials, connection, params, next,
11934  fail_next, success, response_data);
11935  if (*success == 0)
11936  return ret;
11937  }
11938  return ret;
11939 }
11940 
11951 char *
11952 save_config_omp (openvas_connection_t *connection, credentials_t * credentials,
11953  params_t *params, cmd_response_data_t* response_data)
11954 {
11955  int omp_ret;
11956  char *ret, *osp_ret;
11957  params_t *preferences, *selects, *trends;
11958  const char *config_id, *name, *comment, *scanner_id;
11959  int success;
11960 
11961  config_id = params_value (params, "config_id");
11962  name = params_value (params, "name");
11963  comment = params_value (params, "comment");
11964  scanner_id = params_value (params, "scanner_id");
11965 
11966  CHECK_PARAM_INVALID (config_id, "Save Config", "edit_config");
11967  CHECK_PARAM_INVALID (name, "Save Config", "edit_config");
11968  CHECK_PARAM_INVALID (comment, "Save Config", "edit_config");
11969 
11970  /* Save name and comment. */
11971 
11972  if (scanner_id)
11973  omp_ret = openvas_connection_sendf_xml (connection,
11974  "<modify_config config_id=\"%s\">"
11975  "<name>%s</name>"
11976  "<comment>%s</comment>"
11977  "<scanner>%s</scanner>"
11978  "</modify_config>",
11979  params_value (params, "config_id"),
11980  name,
11981  comment,
11982  scanner_id);
11983  else
11984  omp_ret = openvas_connection_sendf_xml (connection,
11985  "<modify_config config_id=\"%s\">"
11986  "<name>%s</name>"
11987  "<comment>%s</comment>"
11988  "</modify_config>",
11989  params_value (params, "config_id"),
11990  name,
11991  comment);
11992 
11993  if (omp_ret == -1)
11994  {
11995  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11996  return gsad_message (credentials,
11997  "Internal error", __FUNCTION__, __LINE__,
11998  "An internal error occurred while saving a config. "
11999  "It is unclear whether the entire config has been saved. "
12000  "Diagnostics: Failure to send command to manager daemon.",
12001  "/omp?cmd=get_configs", response_data);
12002  }
12003 
12004  ret = check_modify_config (credentials, connection, params,
12005  "get_config", "edit_config",
12006  &success, response_data);
12007  if (success == 0)
12008  {
12009  return ret;
12010  }
12011 
12012  /* Save preferences. */
12013 
12014  preferences = params_values (params, "preference:");
12015  if (preferences)
12016  {
12017  params_iterator_t iter;
12018  char *param_name;
12019  param_t *param;
12020 
12021  params_iterator_init (&iter, preferences);
12022  while (params_iterator_next (&iter, &param_name, &param))
12023  {
12024  gchar *value;
12025 
12026  value = param->value_size
12027  ? g_base64_encode ((guchar *) param->value,
12028  param->value_size)
12029  : g_strdup ("");
12030 
12031  if (openvas_connection_sendf (connection,
12032  "<modify_config config_id=\"%s\">"
12033  "<preference>"
12034  "<name>%s</name>"
12035  "<value>%s</value>"
12036  "</preference>"
12037  "</modify_config>",
12038  params_value (params, "config_id"),
12039  param_name,
12040  value)
12041  == -1)
12042  {
12043  g_free (value);
12044  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12045  return gsad_message (credentials,
12046  "Internal error", __FUNCTION__, __LINE__,
12047  "An internal error occurred while saving a config. "
12048  "It is unclear whether the entire config has been saved. "
12049  "Diagnostics: Failure to send command to manager daemon.",
12050  "/omp?cmd=get_configs", response_data);
12051  }
12052  g_free (value);
12053  g_free (ret);
12054 
12055  ret = check_modify_config (credentials, connection, params,
12056  "get_config", "edit_config",
12057  &success, response_data);
12058  if (success == 0)
12059  {
12060  return ret;
12061  }
12062  }
12063  }
12064 
12065  /* OSP config file preference. */
12066  osp_ret = save_osp_prefs (credentials, connection, params,
12067  "get_config", "edit_config",
12068  &success, response_data);
12069 
12070  if (osp_ret)
12071  {
12072  g_free (ret);
12073  ret = osp_ret;
12074  }
12075 
12076  if (success == 0)
12077  {
12078  if (osp_ret == NULL)
12079  {
12080  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12081  return gsad_message (credentials,
12082  "Internal error", __FUNCTION__, __LINE__,
12083  "An internal error occurred while saving a config. "
12084  "It is unclear whether the entire config has been saved. "
12085  "Diagnostics: save_osp_prefs returned NULL unexpectedly.",
12086  "/omp?cmd=get_configs", response_data);
12087  }
12088  return ret;
12089  }
12090 
12091  /* Update the config. */
12092 
12093  trends = params_values (params, "trend:");
12094  selects = params_values (params, "select:");
12095 
12096  if (trends || selects || params_value (params, "trend"))
12097  {
12098  if (openvas_connection_sendf (connection,
12099  "<modify_config config_id=\"%s\">"
12100  "<family_selection>"
12101  "<growing>%i</growing>",
12102  params_value (params, "config_id"),
12103  trends
12104  && params_value (params, "trend")
12105  && strcmp (params_value (params, "trend"), "0"))
12106  == -1)
12107  {
12108  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12109  return gsad_message (credentials,
12110  "Internal error", __FUNCTION__, __LINE__,
12111  "An internal error occurred while saving a config. "
12112  "It is unclear whether the entire config has been saved. "
12113  "Diagnostics: Failure to send command to manager daemon.",
12114  "/omp?cmd=get_configs", response_data);
12115  }
12116 
12117  if (selects)
12118  {
12119  gchar *family;
12120  params_iterator_t iter;
12121  param_t *param;
12122 
12123  params_iterator_init (&iter, selects);
12124  while (params_iterator_next (&iter, &family, &param))
12125  if (openvas_connection_sendf (connection,
12126  "<family>"
12127  "<name>%s</name>"
12128  "<all>1</all>"
12129  "<growing>%i</growing>"
12130  "</family>",
12131  family,
12132  trends && member1 (trends, family))
12133  == -1)
12134  {
12135  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12136  return gsad_message (credentials,
12137  "Internal error", __FUNCTION__, __LINE__,
12138  "An internal error occurred while saving a config. "
12139  "It is unclear whether the entire config has been saved. "
12140  "Diagnostics: Failure to send command to manager daemon.",
12141  "/omp?cmd=get_configs", response_data);
12142  }
12143  }
12144 
12145  if (trends)
12146  {
12147  gchar *family;
12148  params_iterator_t iter;
12149  param_t *param;
12150 
12151  params_iterator_init (&iter, trends);
12152  while (params_iterator_next (&iter, &family, &param))
12153  {
12154  if (param->value_size == 0) continue;
12155  if (param->value[0] == '0') continue;
12156  if (selects && member (selects, family)) continue;
12157  if (openvas_connection_sendf (connection,
12158  "<family>"
12159  "<name>%s</name>"
12160  "<all>0</all>"
12161  "<growing>1</growing>"
12162  "</family>",
12163  family)
12164  == -1)
12165  {
12166  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12167  return gsad_message (credentials,
12168  "Internal error", __FUNCTION__, __LINE__,
12169  "An internal error occurred while saving a config. "
12170  "It is unclear whether the entire config has been saved. "
12171  "Diagnostics: Failure to send command to manager daemon.",
12172  "/omp?cmd=get_configs", response_data);
12173  }
12174  }
12175  }
12176 
12177  if (openvas_connection_sendf (connection,
12178  "</family_selection>"
12179  "</modify_config>")
12180  == -1)
12181  {
12182  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12183  return gsad_message (credentials,
12184  "Internal error", __FUNCTION__, __LINE__,
12185  "An internal error occurred while saving a config. "
12186  "It is unclear whether the entire config has been saved. "
12187  "Diagnostics: Failure to send command to manager daemon.",
12188  "/omp?cmd=get_configs", response_data);
12189  }
12190 
12191  g_free (ret);
12192  ret = check_modify_config (credentials, connection, params,
12193  "get_config", "edit_config",
12194  NULL, response_data);
12195  }
12196  return ret;
12197 }
12198 
12210 static char *
12211 get_config_family (openvas_connection_t *connection,
12212  credentials_t * credentials, params_t *params, int edit,
12213  cmd_response_data_t* response_data)
12214 {
12215  GString *xml;
12216  const char *config_id, *name, *family, *sort_field, *sort_order;
12217 
12218  config_id = params_value (params, "config_id");
12219  name = params_value (params, "name");
12220  family = params_value (params, "family");
12221 
12222  if ((config_id == NULL) || (name == NULL) || (family == NULL))
12223  {
12224  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12225  return gsad_message (credentials,
12226  "Internal error", __FUNCTION__, __LINE__,
12227  "An internal error occurred while getting config family. "
12228  "Diagnostics: Required parameter was NULL.",
12229  "/omp?cmd=get_configs", response_data);
12230  }
12231 
12232  xml = g_string_new ("<get_config_family_response>");
12233  if (edit) g_string_append (xml, "<edit/>");
12234  /* @todo Would it be better include this in the get_nvts response? */
12235  g_string_append_printf (xml,
12236  "<config id=\"%s\">"
12237  "<name>%s</name><family>%s</family>"
12238  "</config>",
12239  config_id,
12240  name,
12241  family);
12242 
12243  /* Get the details for all NVT's in the config in the family. */
12244 
12245  sort_field = params_value (params, "sort_field");
12246  sort_order = params_value (params, "sort_order");
12247 
12248  if (openvas_connection_sendf (connection,
12249  "<get_nvts"
12250  " config_id=\"%s\" details=\"1\""
12251  " family=\"%s\" timeout=\"1\" preference_count=\"1\""
12252  " sort_field=\"%s\" sort_order=\"%s\"/>",
12253  config_id,
12254  family,
12255  sort_field ? sort_field : "nvts.name",
12256  sort_order ? sort_order : "ascending")
12257  == -1)
12258  {
12259  g_string_free (xml, TRUE);
12260  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12261  return gsad_message (credentials,
12262  "Internal error", __FUNCTION__, __LINE__,
12263  "An internal error occurred while getting list of configs. "
12264  "The current list of configs is not available. "
12265  "Diagnostics: Failure to send command to manager daemon.",
12266  "/omp?cmd=get_configs", response_data);
12267  }
12268 
12269  if (read_string_c (connection, &xml))
12270  {
12271  g_string_free (xml, TRUE);
12272  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12273  return gsad_message (credentials,
12274  "Internal error", __FUNCTION__, __LINE__,
12275  "An internal error occurred while getting list of configs. "
12276  "The current list of configs is not available. "
12277  "Diagnostics: Failure to receive response from manager daemon.",
12278  "/omp?cmd=get_configs", response_data);
12279  }
12280 
12281  if (edit)
12282  {
12283  /* Get the details for all NVT's in the family. */
12284 
12285  g_string_append (xml, "<all>");
12286 
12287  if (openvas_connection_sendf (connection,
12288  "<get_nvts"
12289  " details=\"1\""
12290  " timeout=\"1\""
12291  " family=\"%s\""
12292  " preferences_config_id=\"%s\""
12293  " preference_count=\"1\""
12294  " sort_field=\"%s\""
12295  " sort_order=\"%s\"/>",
12296  family,
12297  config_id,
12298  sort_field ? sort_field : "nvts.name",
12299  sort_order ? sort_order : "ascending")
12300  == -1)
12301  {
12302  g_string_free (xml, TRUE);
12303  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12304  return gsad_message (credentials,
12305  "Internal error", __FUNCTION__, __LINE__,
12306  "An internal error occurred while getting list of configs. "
12307  "The current list of configs is not available. "
12308  "Diagnostics: Failure to send command to manager daemon.",
12309  "/omp?cmd=get_configs", response_data);
12310  }
12311 
12312  if (read_string_c (connection, &xml))
12313  {
12314  g_string_free (xml, TRUE);
12315  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12316  return gsad_message (credentials,
12317  "Internal error", __FUNCTION__, __LINE__,
12318  "An internal error occurred while getting list of configs. "
12319  "The current list of configs is not available. "
12320  "Diagnostics: Failure to receive response from manager daemon.",
12321  "/omp?cmd=get_configs", response_data);
12322  }
12323 
12324  g_string_append (xml, "</all>");
12325  }
12326 
12327  g_string_append (xml, "</get_config_family_response>");
12328  return xsl_transform_omp (connection, credentials, params,
12329  g_string_free (xml, FALSE), response_data);
12330 }
12331 
12342 char *
12343 get_config_family_omp (openvas_connection_t *connection,
12344  credentials_t * credentials, params_t *params,
12345  cmd_response_data_t* response_data)
12346 {
12347  return get_config_family (connection, credentials, params, 0, response_data);
12348 }
12349 
12360 char *
12361 edit_config_family_omp (openvas_connection_t *connection,
12362  credentials_t * credentials, params_t *params,
12363  cmd_response_data_t* response_data)
12364 {
12365  return get_config_family (connection, credentials, params, 1, response_data);
12366 }
12367 
12378 char *
12379 save_config_family_omp (openvas_connection_t *connection,
12380  credentials_t * credentials, params_t *params,
12381  cmd_response_data_t* response_data)
12382 {
12383  char *ret;
12384  const char *config_id, *family;
12385  params_t *nvts;
12386 
12387  config_id = params_value (params, "config_id");
12388  family = params_value (params, "family");
12389 
12390  if ((config_id == NULL) || (family == NULL))
12391  {
12392  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12393  return gsad_message (credentials,
12394  "Internal error", __FUNCTION__, __LINE__,
12395  "An internal error occurred while saving getting config family. "
12396  "The config has not been saved. "
12397  "Diagnostics: Required parameter was NULL.",
12398  "/omp?cmd=get_configs", response_data);
12399  }
12400 
12401  /* Set the NVT selection. */
12402 
12403  if (openvas_connection_sendf (connection,
12404  "<modify_config config_id=\"%s\">"
12405  "<nvt_selection>"
12406  "<family>%s</family>",
12407  config_id,
12408  family)
12409  == -1)
12410  {
12411  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12412  return gsad_message (credentials,
12413  "Internal error", __FUNCTION__, __LINE__,
12414  "An internal error occurred while saving a config. "
12415  "It is unclear whether the entire config has been saved. "
12416  "Diagnostics: Failure to send command to manager daemon.",
12417  "/omp?cmd=get_configs", response_data);
12418  }
12419 
12420  nvts = params_values (params, "nvt:");
12421  if (nvts)
12422  {
12423  params_iterator_t iter;
12424  char *name;
12425  param_t *param;
12426 
12427  params_iterator_init (&iter, nvts);
12428  while (params_iterator_next (&iter, &name, &param))
12429  if (openvas_connection_sendf (connection,
12430  "<nvt oid=\"%s\"/>",
12431  name)
12432  == -1)
12433  {
12434  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12435  return gsad_message (credentials,
12436  "Internal error", __FUNCTION__, __LINE__,
12437  "An internal error occurred while saving a config. "
12438  "It is unclear whether the entire config has been saved. "
12439  "Diagnostics: Failure to send command to manager daemon.",
12440  "/omp?cmd=get_configs", response_data);
12441  }
12442  }
12443 
12444  if (openvas_connection_sendf (connection,
12445  "</nvt_selection>"
12446  "</modify_config>")
12447  == -1)
12448  {
12449  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12450  return gsad_message (credentials,
12451  "Internal error", __FUNCTION__, __LINE__,
12452  "An internal error occurred while saving a config. "
12453  "It is unclear whether the entire config has been saved. "
12454  "Diagnostics: Failure to send command to manager daemon.",
12455  "/omp?cmd=get_configs", response_data);
12456  }
12457 
12458  ret = check_modify_config (credentials, connection, params,
12459  "get_config_family", "edit_config_family",
12460  NULL, response_data);
12461 
12462  return ret;
12463 }
12464 
12476 static char *
12477 get_config_nvt (openvas_connection_t *connection, credentials_t * credentials,
12478  params_t *params, int edit, cmd_response_data_t* response_data)
12479 {
12480  GString *xml;
12481  const char *config_id, *name, *family, *sort_field, *sort_order, *nvt;
12482 
12483  config_id = params_value (params, "config_id");
12484  name = params_value (params, "name");
12485  family = params_value (params, "family");
12486  nvt = params_value (params, "oid");
12487 
12488  if ((config_id == NULL) || (name == NULL))
12489  {
12490  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12491  return gsad_message (credentials,
12492  "Internal error", __FUNCTION__, __LINE__,
12493  "An internal error occurred while getting config family. "
12494  "Diagnostics: Required parameter was NULL.",
12495  "/omp?cmd=get_configs", response_data);
12496  }
12497 
12498  xml = g_string_new ("<get_config_nvt_response>");
12499  if (edit) g_string_append (xml, "<edit/>");
12500  /* @todo Would it be better include this in the get_nvts response? */
12501  g_string_append_printf (xml,
12502  "<config id=\"%s\">"
12503  "<name>%s</name><family>%s</family>"
12504  "</config>",
12505  config_id,
12506  name,
12507  family ? family : "");
12508 
12509  sort_field = params_value (params, "sort_field");
12510  sort_order = params_value (params, "sort_order");
12511 
12512  if (openvas_connection_sendf (connection,
12513  "<get_nvts"
12514  " config_id=\"%s\" nvt_oid=\"%s\""
12515  " details=\"1\" preferences=\"1\""
12516  " sort_field=\"%s\" sort_order=\"%s\"/>",
12517  config_id,
12518  nvt,
12519  sort_field ? sort_field : "nvts.name",
12520  sort_order ? sort_order : "ascending")
12521  == -1)
12522  {
12523  g_string_free (xml, TRUE);
12524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12525  return gsad_message (credentials,
12526  "Internal error", __FUNCTION__, __LINE__,
12527  "An internal error occurred while getting list of configs. "
12528  "The current list of configs is not available. "
12529  "Diagnostics: Failure to send command to manager daemon.",
12530  "/omp?cmd=get_configs", response_data);
12531  }
12532 
12533  if (read_string_c (connection, &xml))
12534  {
12535  g_string_free (xml, TRUE);
12536  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12537  return gsad_message (credentials,
12538  "Internal error", __FUNCTION__, __LINE__,
12539  "An internal error occurred while getting list of configs. "
12540  "The current list of configs is not available. "
12541  "Diagnostics: Failure to receive response from manager daemon.",
12542  "/omp?cmd=get_configs", response_data);
12543  }
12544 
12545  g_string_append (xml, "</get_config_nvt_response>");
12546 
12547  if (openvas_connection_sendf (connection,
12548  "<get_notes"
12549  " nvt_oid=\"%s\""
12550  " sort_field=\"notes.text\"/>",
12551  nvt)
12552  == -1)
12553  {
12554  g_string_free (xml, TRUE);
12555  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12556  return gsad_message (credentials,
12557  "Internal error", __FUNCTION__, __LINE__,
12558  "An internal error occurred while getting list of notes. "
12559  "The current list of notes is not available. "
12560  "Diagnostics: Failure to send command to manager daemon.",
12561  "/omp?cmd=get_configs", response_data);
12562  }
12563 
12564  if (read_string_c (connection, &xml))
12565  {
12566  g_string_free (xml, TRUE);
12567  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12568  return gsad_message (credentials,
12569  "Internal error", __FUNCTION__, __LINE__,
12570  "An internal error occurred while getting list of notes. "
12571  "The current list of notes is not available. "
12572  "Diagnostics: Failure to receive response from manager daemon.",
12573  "/omp?cmd=get_configs", response_data);
12574  }
12575 
12576  if (openvas_connection_sendf (connection,
12577  "<get_overrides"
12578  " nvt_oid=\"%s\""
12579  " sort_field=\"overrides.text\"/>",
12580  nvt)
12581  == -1)
12582  {
12583  g_string_free (xml, TRUE);
12584  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12585  return gsad_message (credentials,
12586  "Internal error", __FUNCTION__, __LINE__,
12587  "An internal error occurred while getting list of overrides. "
12588  "The current list of overrides is not available. "
12589  "Diagnostics: Failure to send command to manager daemon.",
12590  "/omp?cmd=get_configs", response_data);
12591  }
12592 
12593  if (read_string_c (connection, &xml))
12594  {
12595  g_string_free (xml, TRUE);
12596  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12597  return gsad_message (credentials,
12598  "Internal error", __FUNCTION__, __LINE__,
12599  "An internal error occurred while getting list of overrides. "
12600  "The current list of overrides is not available. "
12601  "Diagnostics: Failure to receive response from manager daemon.",
12602  "/omp?cmd=get_configs", response_data);
12603  }
12604 
12605  return xsl_transform_omp (connection, credentials, params,
12606  g_string_free (xml, FALSE), response_data);
12607 }
12608 
12619 char *
12620 get_config_nvt_omp (openvas_connection_t *connection,
12621  credentials_t * credentials, params_t *params,
12622  cmd_response_data_t* response_data)
12623 {
12624  return get_config_nvt (connection, credentials, params, 0, response_data);
12625 }
12626 
12637 char *
12638 edit_config_nvt_omp (openvas_connection_t *connection,
12639  credentials_t * credentials, params_t *params,
12640  cmd_response_data_t* response_data)
12641 {
12642  return get_config_nvt (connection, credentials, params, 1, response_data);
12643 }
12644 
12655 char *
12656 save_config_nvt_omp (openvas_connection_t *connection,
12657  credentials_t * credentials, params_t *params,
12658  cmd_response_data_t* response_data)
12659 {
12660  params_t *preferences;
12661  const char *config_id;
12662  int success;
12663  char *modify_config_ret;
12664 
12665  modify_config_ret = NULL;
12666  config_id = params_value (params, "config_id");
12667 
12668  preferences = params_values (params, "preference:");
12669  if (preferences)
12670  {
12671  param_t *preference;
12672  gchar *preference_name;
12673  params_iterator_t iter;
12674 
12675  /* Save preferences. */
12676 
12677  params_iterator_init (&iter, preferences);
12678  while (params_iterator_next (&iter, &preference_name, &preference))
12679  {
12680  int type_start, type_end, count, ret, is_timeout = 0;
12681  gchar *value;
12682 
12683  g_free (modify_config_ret);
12684  modify_config_ret = NULL;
12685 
12686  /* Passwords and files have checkboxes to control whether they
12687  * must be reset. This works around the need for the Manager to
12688  * send the actual password or show the actual file. */
12689 
12690  /* LDAPsearch[entry]:Timeout value */
12691  count = sscanf (preference_name,
12692  "%*[^[][%n%*[^]]%n]:",
12693  &type_start,
12694  &type_end);
12695  if (count == 0 && type_start > 0 && type_end > 0)
12696  {
12697  if (strncmp (preference_name + type_start,
12698  "password",
12699  type_end - type_start)
12700  == 0)
12701  {
12702  int found = 0;
12703  params_t *passwords;
12704 
12705  passwords = params_values (params, "password:");
12706  if (passwords)
12707  {
12708  param_t *password;
12709  gchar *password_name;
12710  params_iterator_t password_params;
12711 
12712  params_iterator_init (&password_params, passwords);
12713  while (params_iterator_next (&password_params,
12714  &password_name,
12715  &password))
12716  if (strcmp (password_name, preference_name) == 0)
12717  {
12718  found = 1;
12719  break;
12720  }
12721  }
12722  if (found == 0)
12723  /* Skip modifying the password preference. */
12724  continue;
12725  }
12726  else if (strncmp (preference_name + type_start,
12727  "file",
12728  type_end - type_start)
12729  == 0)
12730  {
12731  int found = 0;
12732  params_t *files;
12733 
12734  files = params_values (params, "file:");
12735  if (files)
12736  {
12737  param_t *file;
12738  gchar *file_name;
12739  params_iterator_t file_params;
12740 
12741  params_iterator_init (&file_params, files);
12742  while (params_iterator_next (&file_params,
12743  &file_name,
12744  &file))
12745  if (strcmp (file_name, preference_name) == 0)
12746  {
12747  found = 1;
12748  break;
12749  }
12750  }
12751  if (found == 0)
12752  /* Skip modifying the file preference. */
12753  continue;
12754  }
12755  else if (strncmp (preference_name + type_start,
12756  "scanner",
12757  type_end - type_start)
12758  == 0)
12759  {
12760  /* Presume it's the timeout. */
12761  is_timeout = 1;
12762  }
12763  }
12764 
12765  value = preference->value_size
12766  ? g_base64_encode ((guchar *) preference->value,
12767  preference->value_size)
12768  : g_strdup ("");
12769 
12770  if (is_timeout)
12771  {
12772  const char *timeout;
12773  gchar *preference_name_escaped;
12774 
12775  timeout = params_value (params, "timeout");
12776 
12777  if (timeout == NULL)
12778  {
12779  g_free (value);
12780  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12781  return gsad_message (credentials,
12782  "Internal error", __FUNCTION__, __LINE__,
12783  "An internal error occurred while saving a config. "
12784  "It is unclear whether the entire config has been saved. "
12785  "Diagnostics: Required parameter was NULL.",
12786  "/omp?cmd=get_configs", response_data);
12787  }
12788 
12789  preference_name_escaped = g_markup_escape_text (preference_name,
12790  -1);
12791 
12792  if (strcmp (timeout, "0") == 0)
12793  /* Leave out the value to clear the preference. */
12794  ret = openvas_connection_sendf (connection,
12795  "<modify_config"
12796  " config_id=\"%s\">"
12797  "<preference>"
12798  "<name>%s</name>"
12799  "</preference>"
12800  "</modify_config>",
12801  config_id,
12802  preference_name_escaped);
12803  else
12804  ret = openvas_connection_sendf (connection,
12805  "<modify_config"
12806  " config_id=\"%s\">"
12807  "<preference>"
12808  "<name>%s</name>"
12809  "<value>%s</value>"
12810  "</preference>"
12811  "</modify_config>",
12812  config_id,
12813  preference_name_escaped,
12814  value);
12815 
12816  g_free (preference_name_escaped);
12817  }
12818  else
12819  {
12820  gchar *preference_name_escaped;
12821  preference_name_escaped = g_markup_escape_text (preference_name,
12822  -1);
12823  ret = openvas_connection_sendf (connection,
12824  "<modify_config"
12825  " config_id=\"%s\">"
12826  "<preference>"
12827  "<nvt oid=\"%s\"/>"
12828  "<name>%s</name>"
12829  "<value>%s</value>"
12830  "</preference>"
12831  "</modify_config>",
12832  config_id,
12833  params_value (params, "oid"),
12834  preference_name_escaped,
12835  value);
12836  g_free (preference_name_escaped);
12837  }
12838 
12839  if (ret == -1)
12840  {
12841  g_free (value);
12842  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12843  return gsad_message (credentials,
12844  "Internal error", __FUNCTION__, __LINE__,
12845  "An internal error occurred while saving a config. "
12846  "It is unclear whether the entire config has been saved. "
12847  "Diagnostics: Failure to send command to manager daemon.",
12848  "/omp?cmd=get_configs", response_data);
12849  }
12850  g_free (value);
12851 
12852  modify_config_ret = check_modify_config (credentials, connection,
12853  params,
12854  "get_config_nvt",
12855  "edit_config_nvt",
12856  &success, response_data);
12857  if (success == 0)
12858  {
12859  return modify_config_ret;
12860  }
12861  }
12862  }
12863 
12864  return modify_config_ret;
12865 }
12866 
12877 char *
12878 delete_config_omp (openvas_connection_t *connection,
12879  credentials_t * credentials, params_t *params,
12880  cmd_response_data_t* response_data)
12881 {
12882  return delete_resource (connection, "config", credentials, params, 0,
12883  "get_configs", response_data);
12884 }
12885 
12899 char *
12900 export_config_omp (openvas_connection_t *connection,
12901  credentials_t * credentials, params_t *params,
12902  enum content_type * content_type, char **content_disposition,
12903  gsize *content_length, cmd_response_data_t* response_data)
12904 {
12905  return export_resource (connection, "config", credentials, params,
12906  content_type, content_disposition, content_length,
12907  response_data);
12908 }
12909 
12924 char *
12925 export_configs_omp (openvas_connection_t *connection,
12926  credentials_t * credentials, params_t *params,
12927  enum content_type * content_type, char **content_disposition,
12928  gsize *content_length, cmd_response_data_t* response_data)
12929 {
12930  return export_many (connection, "config", credentials, params, content_type,
12931  content_disposition, content_length, response_data);
12932 }
12933 
12947 char *
12948 export_note_omp (openvas_connection_t *connection, credentials_t * credentials,
12949  params_t *params, enum content_type * content_type,
12950  char **content_disposition, gsize *content_length,
12951  cmd_response_data_t* response_data)
12952 {
12953  return export_resource (connection, "note", credentials, params, content_type,
12954  content_disposition, content_length, response_data);
12955 }
12956 
12971 char *
12972 export_notes_omp (openvas_connection_t *connection, credentials_t * credentials,
12973  params_t *params, enum content_type * content_type,
12974  char **content_disposition, gsize *content_length,
12975  cmd_response_data_t* response_data)
12976 {
12977  return export_many (connection, "note", credentials, params, content_type,
12978  content_disposition, content_length, response_data);
12979 }
12980 
12994 char *
12995 export_override_omp (openvas_connection_t *connection,
12996  credentials_t * credentials, params_t *params,
12997  enum content_type * content_type,
12998  char **content_disposition, gsize *content_length,
12999  cmd_response_data_t* response_data)
13000 {
13001  return export_resource (connection, "override", credentials, params,
13002  content_type, content_disposition, content_length,
13003  response_data);
13004 }
13005 
13020 char *
13021 export_overrides_omp (openvas_connection_t *connection,
13022  credentials_t * credentials, params_t *params,
13023  enum content_type * content_type,
13024  char **content_disposition, gsize *content_length,
13025  cmd_response_data_t* response_data)
13026 {
13027  return export_many (connection, "override", credentials, params, content_type,
13028  content_disposition, content_length, response_data);
13029 }
13030 
13045 char *
13046 export_port_list_omp (openvas_connection_t *connection,
13047  credentials_t * credentials, params_t *params,
13048  enum content_type * content_type,
13049  char **content_disposition, gsize *content_length,
13050  cmd_response_data_t* response_data)
13051 {
13052  return export_resource (connection, "port_list", credentials, params,
13053  content_type, content_disposition, content_length,
13054  response_data);
13055 }
13056 
13071 char *
13072 export_port_lists_omp (openvas_connection_t *connection,
13073  credentials_t * credentials, params_t *params,
13074  enum content_type * content_type,
13075  char **content_disposition, gsize *content_length,
13076  cmd_response_data_t* response_data)
13077 {
13078  return export_many (connection, "port_list", credentials, params,
13079  content_type, content_disposition, content_length,
13080  response_data);
13081 }
13082 
13096 char *
13097 export_preference_file_omp (openvas_connection_t *connection,
13098  credentials_t * credentials, params_t *params,
13099  enum content_type * content_type,
13100  char **content_disposition, gsize *content_length,
13101  cmd_response_data_t* response_data)
13102 {
13103  GString *xml;
13104  entity_t entity, preference_entity, value_entity;
13105  const char *config_id, *oid, *preference_name;
13106 
13107  *content_length = 0;
13108 
13109  config_id = params_value (params, "config_id");
13110  oid = params_value (params, "oid");
13111  preference_name = params_value (params, "preference_name");
13112 
13113  xml = g_string_new ("<get_preferences_response>");
13114 
13115  if (config_id == NULL || oid == NULL || preference_name == NULL)
13116  g_string_append (xml, GSAD_MESSAGE_INVALID_PARAM ("Export Preference File"));
13117  else
13118  {
13119  if (openvas_connection_sendf (connection,
13120  "<get_preferences"
13121  " config_id=\"%s\""
13122  " nvt_oid=\"%s\""
13123  " preference=\"%s\"/>",
13124  config_id,
13125  oid,
13126  preference_name)
13127  == -1)
13128  {
13129  g_string_free (xml, TRUE);
13130  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13131  return gsad_message (credentials,
13132  "Internal error", __FUNCTION__, __LINE__,
13133  "An internal error occurred while getting a preference file. "
13134  "The file could not be delivered. "
13135  "Diagnostics: Failure to send command to manager daemon.",
13136  "/omp?cmd=get_tasks", response_data);
13137  }
13138 
13139  entity = NULL;
13140  if (read_entity_c (connection, &entity))
13141  {
13142  g_string_free (xml, TRUE);
13143  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13144  return gsad_message (credentials,
13145  "Internal error", __FUNCTION__, __LINE__,
13146  "An internal error occurred while getting a preference file. "
13147  "The file could not be delivered. "
13148  "Diagnostics: Failure to receive response from manager daemon.",
13149  "/omp?cmd=get_tasks", response_data);
13150  }
13151 
13152  preference_entity = entity_child (entity, "preference");
13153  if (preference_entity != NULL
13154  && (value_entity = entity_child (preference_entity, "value")))
13155  {
13156  char *content = strdup (entity_text (value_entity));
13157  *content_type = GSAD_CONTENT_TYPE_OCTET_STREAM;
13158  *content_disposition = g_strdup_printf ("attachment; filename=\"pref_file.bin\"");
13159  *content_length = strlen (content);
13160  free_entity (entity);
13161  g_string_free (xml, TRUE);
13162  return content;
13163  }
13164  else
13165  {
13166  free_entity (entity);
13167  g_string_free (xml, TRUE);
13168  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13169  return gsad_message (credentials,
13170  "Internal error", __FUNCTION__, __LINE__,
13171  "An internal error occurred while getting a preference file. "
13172  "The file could not be delivered. "
13173  "Diagnostics: Failure to receive file from manager daemon.",
13174  "/omp?cmd=get_tasks", response_data);
13175  }
13176  }
13177 
13178  g_string_append (xml, "</get_preferences_response>");
13179  return xsl_transform_omp (connection, credentials, params,
13180  g_string_free (xml, FALSE), response_data);
13181 }
13182 
13197 char *
13198 export_report_format_omp (openvas_connection_t *connection,
13199  credentials_t * credentials, params_t *params,
13200  enum content_type * content_type,
13201  char **content_disposition, gsize *content_length,
13202  cmd_response_data_t* response_data)
13203 {
13204  return export_resource (connection, "report_format", credentials, params,
13205  content_type, content_disposition, content_length,
13206  response_data);
13207 }
13208 
13223 char *
13224 export_report_formats_omp (openvas_connection_t *connection,
13225  credentials_t * credentials, params_t *params,
13226  enum content_type * content_type,
13227  char **content_disposition, gsize *content_length,
13228  cmd_response_data_t* response_data)
13229 {
13230  return export_many (connection, "report_format", credentials, params,
13231  content_type, content_disposition, content_length,
13232  response_data);
13233 }
13234 
13245 char *
13246 delete_report_omp (openvas_connection_t *connection,
13247  credentials_t * credentials, params_t *params,
13248  cmd_response_data_t* response_data)
13249 {
13250  return delete_resource (connection, "report", credentials, params, 0, NULL,
13251  response_data);
13252 }
13253 
13270 char *
13271 get_report (openvas_connection_t *connection, credentials_t * credentials,
13272  params_t *params, const char *commands,
13273  gsize *report_len, gchar **content_type, char **content_disposition,
13274  const char *extra_xml, int *error,
13275  cmd_response_data_t* response_data)
13276 {
13277  GString *xml, *commands_xml;
13278  entity_t entity;
13279  entity_t report_entity;
13280  unsigned int first, max;
13281  GString *levels, *delta_states;
13282  const char *alert_id, *search_phrase, *min_qod, *type, *zone;
13283  const char *autofp, *autofp_value, *notes, *overrides, *result_hosts_only;
13284  const char *apply_overrides;
13285  const char *report_id, *sort_field, *sort_order, *result_id, *delta_report_id;
13286  const char *format_id, *first_result, *max_results, *host, *pos;
13287  const char *given_filt_id, *filt_id, *filter, *apply_filter, *report_section;
13288  const char *build_filter, *filter_extra;
13289  const char *host_search_phrase, *host_levels;
13290  const char *host_first_result, *host_max_results;
13291  int ret;
13292  int ignore_filter, ignore_pagination;
13293  gchar *built_filter;
13294  gchar *fname_format, *esc_response;
13295 
13296  build_filter = params_value (params, "build_filter");
13297 
13298  if (params_given (params, "apply_filter")
13299  && params_valid (params, "apply_filter"))
13300  apply_filter = params_value (params, "apply_filter");
13301  else
13302  apply_filter = "no_pagination";
13303 
13304  if (params_given (params, "report_section")
13305  && params_valid (params, "report_section"))
13306  report_section = params_value (params, "report_section");
13307  else
13308  report_section = "";
13309 
13310  ignore_filter = (strcmp (apply_filter, "full")
13311  && strcmp (apply_filter, "no_pagination")
13312  && strcmp (report_section, "")
13313  && strcmp (report_section, "results")
13314  && strcmp (report_section, "summary"));
13315 
13316  if (params_given (params, "ignore_pagination"))
13317  {
13318  const char *ignore_pagination_str = params_value (params,
13319  "ignore_pagination");
13320  ignore_pagination = (ignore_pagination_str
13321  && strcmp (ignore_pagination_str, "")
13322  && strcmp (ignore_pagination_str, "0"));
13323  }
13324  else
13325  {
13326  ignore_pagination = (strcmp (apply_filter, "full")
13327  && strcmp (report_section, "")
13328  && strcmp (report_section, "results")
13329  && strcmp (report_section, "summary"));
13330  }
13331 
13332  alert_id = params_value (params, "alert_id");
13333  if (alert_id == NULL)
13334  params_given (params, "alert_id") || (alert_id = "0");
13335 
13336  search_phrase = params_value (params, "search_phrase");
13337  if (search_phrase == NULL)
13338  params_given (params, "search_phrase") || (search_phrase = "");
13339 
13340  zone = params_value (params, "timezone");
13341  if (zone == NULL)
13342  params_given (params, "zone") || (zone = "");
13343 
13344  min_qod = params_value (params, "min_qod");
13345 
13346  type = params_value (params, "type");
13347  host = params_value (params, "host");
13348  pos = params_value (params, "pos");
13349 
13350  autofp = params_value (params, "autofp");
13351  if (autofp == NULL)
13352  params_given (params, "autofp") || (autofp = "0");
13353 
13354  autofp_value = params_value (params, "autofp_value");
13355  if (autofp_value == NULL)
13356  params_given (params, "autofp_value") || (autofp_value = "1");
13357 
13358  notes = params_value (params, "notes");
13359  if (notes == NULL)
13360  {
13361  if (params_given (params, "max_results"))
13362  /* Use the max_results param to determine if the request is from
13363  * the Result Filtering form, because the notes param is only sent
13364  * when the checkbox is ticked. */
13365  notes = "0";
13366  else
13367  params_given (params, "notes") || (notes = "1");
13368  }
13369 
13370  overrides = params_value (params, "overrides");
13371  if (overrides == NULL)
13372  {
13373  if (params_given (params, "max_results"))
13374  /* Use the max_results param to check for filtering form as above */
13375  overrides = "0";
13376  else
13377  params_given (params, "overrides") || (overrides = "1");
13378  }
13379 
13380  apply_overrides = params_value (params, "apply_overrides");
13381  if (apply_overrides == NULL)
13382  {
13383  if (params_given (params, "max_results"))
13384  /* Use the max_results param to check for filtering form as above */
13385  apply_overrides = "0";
13386  else
13387  params_given (params, "apply_overrides") || (apply_overrides = "1");
13388  }
13389 
13390  result_hosts_only = params_value (params, "result_hosts_only");
13391  if (result_hosts_only == NULL)
13392  {
13393  if (params_given (params, "max_results"))
13394  /* Use the max_results params to determine if the request is from
13395  * the Result Filtering form, because the result_hosts_only param is
13396  * only sent when the checkbox is ticked. */
13397  result_hosts_only = "0";
13398  else
13399  params_given (params, "result_hosts_only") || (result_hosts_only = "1");
13400  }
13401 
13402  if (content_type) *content_type = NULL;
13403  if (report_len) *report_len = 0;
13404 
13405  if (autofp == NULL || strlen (autofp) == 0) autofp = "0";
13406 
13407  if (autofp_value == NULL || strlen (autofp_value) == 0) autofp_value = "1";
13408 
13409  if (strcmp (autofp, "2") == 0)
13410  autofp_value = "2";
13411 
13412  if (notes == NULL || strlen (notes) == 0) notes = "1";
13413 
13414  if (overrides == NULL || strlen (overrides) == 0) overrides = "1";
13415 
13416  if (result_hosts_only == NULL || strlen (result_hosts_only) == 0)
13417  result_hosts_only = "1";
13418 
13419  /* Run any extra commands. */
13420 
13421  commands_xml = g_string_new ("");
13422  if (commands)
13423  {
13424  if (openvas_connection_sendf (connection, "%s", commands)
13425  == -1)
13426  {
13427  g_string_free (commands_xml, TRUE);
13428  if (error) *error = 1;
13429  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13430  return gsad_message (credentials,
13431  "Internal error", __FUNCTION__, __LINE__,
13432  "An internal error occurred while getting a report. "
13433  "The report could not be delivered. "
13434  "Diagnostics: Failure to send extra commands to manager daemon.",
13435  "/omp?cmd=get_tasks", response_data);
13436  }
13437 
13438  if (read_string_c (connection, &commands_xml))
13439  {
13440  g_string_free (commands_xml, TRUE);
13441  if (error) *error = 1;
13442  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13443  return gsad_message (credentials,
13444  "Internal error", __FUNCTION__, __LINE__,
13445  "An internal error occurred while getting a report. "
13446  "The report could not be delivered. "
13447  "Diagnostics: Failure to receive response from manager daemon.",
13448  "/omp?cmd=get_tasks", response_data);
13449  }
13450  }
13451 
13452  /* Get the report. */
13453 
13454  if (params_value (params, "delta_states"))
13455  delta_states = g_string_new (params_value (params, "delta_states"));
13456  else
13457  {
13458  delta_states = g_string_new ("");
13459  if (params_value (params, "delta_state_changed")
13460  && atoi (params_value (params, "delta_state_changed")))
13461  g_string_append (delta_states, "c");
13462  if (params_value (params, "delta_state_gone")
13463  && atoi (params_value (params, "delta_state_gone")))
13464  g_string_append (delta_states, "g");
13465  if (params_value (params, "delta_state_new")
13466  && atoi (params_value (params, "delta_state_new")))
13467  g_string_append (delta_states, "n");
13468  if (params_value (params, "delta_state_same")
13469  && atoi (params_value (params, "delta_state_same")))
13470  g_string_append (delta_states, "s");
13471  }
13472 
13473  if (strlen (delta_states->str) == 0) g_string_append (delta_states, "gn");
13474 
13475  if (params_value (params, "levels"))
13476  levels = g_string_new (params_value (params, "levels"));
13477  else
13478  {
13479  levels = g_string_new ("");
13480  if (params_value (params, "level_high")
13481  && atoi (params_value (params, "level_high")))
13482  g_string_append (levels, "h");
13483  if (params_value (params, "level_medium")
13484  && atoi (params_value (params, "level_medium")))
13485  g_string_append (levels, "m");
13486  if (params_value (params, "level_low")
13487  && atoi (params_value (params, "level_low")))
13488  g_string_append (levels, "l");
13489  if (params_value (params, "level_log")
13490  && atoi (params_value (params, "level_log")))
13491  g_string_append (levels, "g");
13492  if (params_value (params, "level_false_positive")
13493  && atoi (params_value (params, "level_false_positive")))
13494  g_string_append (levels, "f");
13495  }
13496 
13497  if (type && (strcmp (type, "assets") == 0))
13498  {
13499  if (strlen (levels->str) == 0)
13500  g_string_append (levels, "");
13501  }
13502  else if (strlen (levels->str) == 0)
13503  g_string_append (levels, "hml");
13504 
13505  sort_field = params_value (params, "sort_field");
13506  sort_order = params_value (params, "sort_order");
13507  report_id = params_value (params, "report_id");
13508 
13509  if (report_id == NULL
13510  && (type == NULL
13511  || (strcmp (type, "prognostic")
13512  && strcmp (type, "assets"))))
13513  return get_reports (connection, credentials, params, extra_xml,
13514  response_data);
13515 
13516  if (strcmp (alert_id, "0"))
13517  {
13518  const char *status, *esc_filter;
13519 
13520  esc_filter = params_value (params, "esc_filter");
13521  if (esc_filter == NULL)
13522  params_given (params, "esc_filter")
13523  || (esc_filter = "first=1 rows=-1"
13524  " result_hosts_only=0"
13525  " apply_overrides=1"
13526  " notes=1 overrides=1"
13527  " sort-reverse=severity");
13528 
13529  if (ignore_filter)
13530  ret = openvas_connection_sendf_xml (connection,
13531  "<get_reports"
13532  " report_id=\"%s\""
13533  " filter=\"first=1 rows=-1"
13534  " result_hosts_only=0"
13535  " apply_overrides=1"
13536  " notes=1 overrides=1"
13537  " sort-reverse=severity\""
13538  " alert_id=\"%s\"/>",
13539  report_id,
13540  alert_id);
13541  else
13542  ret = openvas_connection_sendf_xml (connection,
13543  "<get_reports"
13544  " report_id=\"%s\""
13545  " ignore_pagination=\"%d\""
13546  " filter=\"%s\""
13547  " alert_id=\"%s\"/>",
13548  report_id,
13549  ignore_pagination,
13550  esc_filter ? esc_filter : "",
13551  alert_id);
13552  if (ret == -1)
13553  {
13554  g_string_free (commands_xml, TRUE);
13555  g_string_free (delta_states, TRUE);
13556  g_string_free (levels, TRUE);
13557  if (error) *error = 1;
13558  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13559  return gsad_message (credentials,
13560  "Internal error", __FUNCTION__, __LINE__,
13561  "An internal error occurred while getting a report. "
13562  "The report could not be delivered. "
13563  "Diagnostics: Failure to send command to manager daemon.",
13564  "/omp?cmd=get_tasks", response_data);
13565  }
13566 
13567  if (read_entity_and_text_c (connection, &entity, &esc_response))
13568  {
13569  g_string_free (commands_xml, TRUE);
13570  g_string_free (delta_states, TRUE);
13571  g_string_free (levels, TRUE);
13572  if (error) *error = 1;
13573  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13574  return gsad_message (credentials,
13575  "Internal error", __FUNCTION__, __LINE__,
13576  "An internal error occurred while getting a report. "
13577  "The report could not be delivered. "
13578  "Diagnostics: Failure to receive response from manager daemon.",
13579  "/omp?cmd=get_tasks", response_data);
13580  }
13581  status = entity_attribute (entity, "status");
13582  if ((status == NULL)
13583  || (strlen (status) == 0))
13584  {
13585  free_entity (entity);
13586  g_string_free (commands_xml, TRUE);
13587  g_string_free (delta_states, TRUE);
13588  if (error) *error = 1;
13589  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13590  return gsad_message (credentials,
13591  "Internal error", __FUNCTION__, __LINE__,
13592  "An internal error occurred while getting a report. "
13593  "The report could not be delivered. "
13594  "Diagnostics: Failure to parse response from manager daemon.",
13595  "/omp?cmd=get_tasks", response_data);
13596  }
13597  free_entity (entity);
13598  }
13599  else
13600  esc_response = NULL;
13601 
13602  result_id = params_value (params, "result_id");
13603  delta_report_id = params_value (params, "delta_report_id");
13604  format_id = params_value (params, "report_format_id");
13605 
13606  first_result = params_value (params, "first_result");
13607  if (first_result == NULL
13608  || sscanf (first_result, "%u", &first) != 1)
13609  first_result = "1";
13610 
13611  max_results = params_value (params, "max_results");
13612  if (max_results == NULL
13613  || sscanf (max_results, "%u", &max) != 1)
13614  max_results = G_STRINGIFY (RESULTS_PER_PAGE);
13615 
13616  if (openvas_connection_sendf (connection,
13617  "<get_reports"
13618  "%s%s"
13619  " details=\"%i\""
13620  "%s%s%s",
13621  (type && (strcmp (type, "prognostic") == 0))
13622  ? " type=\"prognostic\""
13623  : "",
13624  (type && (strcmp (type, "assets") == 0))
13625  ? " type=\"assets\""
13626  : "",
13627  (type
13628  && (strcmp (type, "assets") == 0)
13629  && host)
13630  || delta_report_id
13631  || strcmp (report_section, "summary"),
13632  host ? " host=\"" : "",
13633  host ? host : "",
13634  host ? "\"" : "")
13635  == -1)
13636  {
13637  g_string_free (delta_states, TRUE);
13638  g_string_free (commands_xml, TRUE);
13639  g_string_free (levels, TRUE);
13640  if (error) *error = 1;
13641  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13642  return gsad_message (credentials,
13643  "Internal error", __FUNCTION__, __LINE__,
13644  "An internal error occurred while getting a report. "
13645  "The report could not be delivered. "
13646  "Diagnostics: Failure to send command to manager daemon.",
13647  "/omp?cmd=get_tasks", response_data);
13648  }
13649 
13650  given_filt_id = params_value (params, "filt_id");
13651  filter = params_value (params, "filter");
13652  filter_extra = params_value (params, "filter_extra");
13653 
13654  if (params_given (params, "filt_id"))
13655  {
13656  g_tree_replace (credentials->last_filt_ids, g_strdup ("report_result"),
13657  g_strdup (given_filt_id));
13658  filt_id = given_filt_id;
13659  }
13660  else if (filter == NULL || strcmp (filter, "") == 0)
13661  filt_id = g_tree_lookup (credentials->last_filt_ids, "report_result");
13662  else
13663  filt_id = NULL;
13664 
13665  if (filter == NULL)
13666  filter = "";
13667 
13668  if ((build_filter && (strcmp (build_filter, "1") == 0))
13669  || ((filter == NULL || strcmp (filter, "") == 0)
13670  && (filter_extra == NULL || strcmp (filter_extra, "") == 0)))
13671  {
13672  GString *filter_buffer;
13673  filter_buffer = g_string_new ("");
13674 
13675  g_string_append_printf (filter_buffer,
13676  "autofp=%s"
13677  " apply_overrides=%i"
13678  " notes=%i"
13679  " overrides=%i"
13680  " result_hosts_only=%i"
13681  " first=%s"
13682  " rows=%s"
13683  " sort%s=%s"
13684  " levels=%s",
13685  strcmp (autofp, "0") ? autofp_value : "0",
13686  apply_overrides
13687  ? (strcmp (apply_overrides, "0") ? 1 : 0)
13688  : 1,
13689  strcmp (notes, "0") ? 1 : 0,
13690  strcmp (overrides, "0") ? 1 : 0,
13691  strcmp (result_hosts_only, "0") ? 1 : 0,
13692  first_result,
13693  max_results,
13694  sort_order
13695  ? strcmp (sort_order, "ascending")
13696  ? "-reverse"
13697  : ""
13698  : ((sort_field == NULL
13699  || strcmp (sort_field, "type") == 0
13700  || strcmp (sort_field, "severity") == 0)
13701  ? "-reverse"
13702  : ""),
13703  sort_field ? sort_field : "severity",
13704  levels->str);
13705 
13706  if (search_phrase && strcmp (search_phrase, ""))
13707  {
13708  gchar *search_phrase_escaped;
13709  search_phrase_escaped = g_markup_escape_text (search_phrase, -1);
13710  g_string_append_printf (filter_buffer,
13711  " \"%s\"",
13712  search_phrase_escaped);
13713  g_free (search_phrase_escaped);
13714  }
13715 
13716  if (delta_states->str && strcmp (delta_states->str, "")
13717  && delta_report_id && strcmp (delta_report_id, ""))
13718  g_string_append_printf (filter_buffer,
13719  " delta_states=%s",
13720  delta_states->str);
13721 
13722  if (min_qod && strcmp (min_qod, ""))
13723  g_string_append_printf (filter_buffer,
13724  " min_qod=%s",
13725  min_qod);
13726 
13727  if (zone && strcmp (zone, ""))
13728  g_string_append_printf (filter_buffer,
13729  " timezone=%s",
13730  zone);
13731 
13732  if (filter && strcmp (filter, ""))
13733  g_string_append_printf (filter_buffer,
13734  " %s",
13735  filter);
13736 
13737  built_filter = g_string_free (filter_buffer, FALSE);
13738  }
13739  else if (filter || filter_extra)
13740  built_filter = g_strdup_printf ("%s%s%s",
13741  filter ? filter : "",
13742  filter && filter_extra ? " " : "",
13743  filter_extra ? filter_extra : "");
13744  else
13745  built_filter = NULL;
13746 
13747  if (type && (strcmp (type, "prognostic") == 0))
13748  {
13749  host_search_phrase = params_value (params, "host_search_phrase");
13750  if (host_search_phrase == NULL)
13751  params_given (params, "host_search_phrase")
13752  || (host_search_phrase = "");
13753 
13754  host_levels = params_value (params, "host_levels");
13755  if (host_levels == NULL)
13756  params_given (params, "host_levels")
13757  || (host_levels = "");
13758 
13759  host_first_result = params_value (params, "host_first_result");
13760  if (host_first_result == NULL
13761  || sscanf (host_first_result, "%u", &first) != 1)
13762  host_first_result = "1";
13763 
13764  host_max_results = params_value (params, "host_max_results");
13765  if (host_max_results == NULL
13766  || sscanf (host_max_results, "%u", &max) != 1)
13767  host_max_results = G_STRINGIFY (RESULTS_PER_PAGE);
13768 
13769  if (host_search_phrase == NULL)
13770  {
13771  g_string_free (delta_states, TRUE);
13772  g_string_free (commands_xml, TRUE);
13773  g_string_free (levels, TRUE);
13774  xml = g_string_new ("");
13775  g_string_append_printf (xml, GSAD_MESSAGE_INVALID,
13776  "Given host search_phrase was invalid",
13777  "Get Report");
13778  if (error) *error = 1;
13779  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
13780  return g_string_free (xml, FALSE);
13781  }
13782 
13783  if (openvas_connection_sendf_xml (connection,
13784  " host_search_phrase=\"%s\""
13785  " host_levels=\"%s\""
13786  " host_first_result=\"%s\""
13787  " host_max_results=\"%s\"",
13788  host_search_phrase,
13789  host_levels,
13790  host_first_result,
13791  host_max_results))
13792  {
13793  g_string_free (delta_states, TRUE);
13794  g_string_free (commands_xml, TRUE);
13795  g_string_free (levels, TRUE);
13796  if (error) *error = 1;
13797  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13798  return gsad_message (credentials,
13799  "Internal error", __FUNCTION__, __LINE__,
13800  "An internal error occurred while getting a report. "
13801  "The report could not be delivered. "
13802  "Diagnostics: Failure to send command to manager daemon.",
13803  "/omp?cmd=get_tasks", response_data);
13804  }
13805  }
13806  else
13807  {
13808  host_search_phrase = NULL;
13809  host_levels = NULL;
13810  host_first_result = NULL;
13811  host_max_results = NULL;
13812  }
13813 
13814  /* Don't apply default filter when applying result filter checkboxes/textboxes
13815  */
13816  if (sort_field == NULL && sort_order == NULL)
13817  if ((filt_id == NULL || strcmp (filt_id, "") == 0)
13818  && (filter == NULL || strcmp (filter, "") == 0))
13819  filt_id = "-2";
13820 
13821  if (ignore_filter)
13822  ret = openvas_connection_sendf_xml (connection,
13823  " filt_id=\"0\""
13824  " filter=\"first=1 rows=-1"
13825  " result_hosts_only=0 apply_overrides=1"
13826  " notes=1 overrides=1"
13827  " sort-reverse=severity\""
13828  " report_id=\"%s\""
13829  " delta_report_id=\"%s\""
13830  " format_id=\"%s\"/>",
13831  (type && ((strcmp (type, "assets") == 0)
13832  || (strcmp (type, "prognostic")
13833  == 0)))
13834  ? ""
13835  : report_id,
13836  delta_report_id ? delta_report_id : "0",
13837  format_id
13838  ? format_id
13839  : "a994b278-1f62-11e1-96ac-406186ea4fc5");
13840  else
13841  ret = openvas_connection_sendf_xml (connection,
13842  " ignore_pagination=\"%d\""
13843  " filt_id=\"%s\""
13844  " filter=\"%s\""
13845  " pos=\"%s\""
13846  " notes_details=\"1\""
13847  " overrides_details=\"1\""
13848  " report_id=\"%s\""
13849  " delta_report_id=\"%s\""
13850  " format_id=\"%s\"/>",
13851  ignore_pagination,
13852  filt_id ? filt_id : "0",
13853  built_filter ? built_filter : "",
13854  pos ? pos : "1",
13855  (type && ((strcmp (type, "assets") == 0)
13856  || (strcmp (type, "prognostic")
13857  == 0)))
13858  ? ""
13859  : report_id,
13860  delta_report_id ? delta_report_id : "0",
13861  format_id
13862  ? format_id
13863  : "a994b278-1f62-11e1-96ac-406186ea4fc5",
13864  first_result,
13865  max_results,
13866  sort_field ? sort_field : "severity",
13867  sort_order
13868  ? sort_order
13869  : ((sort_field == NULL
13870  || strcmp (sort_field, "type") == 0
13871  || strcmp (sort_field, "severity") == 0)
13872  ? "descending"
13873  : "ascending"),
13874  levels->str,
13875  delta_states->str,
13876  search_phrase,
13877  min_qod,
13878  zone);
13879  if (ret == -1)
13880  {
13881  g_string_free (delta_states, TRUE);
13882  g_string_free (commands_xml, TRUE);
13883  g_string_free (levels, TRUE);
13884  if (error) *error = 1;
13885  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13886  return gsad_message (credentials,
13887  "Internal error", __FUNCTION__, __LINE__,
13888  "An internal error occurred while getting a report. "
13889  "The report could not be delivered. "
13890  "Diagnostics: Failure to send command to manager daemon.",
13891  "/omp?cmd=get_tasks", response_data);
13892  }
13893 
13894  g_string_free (delta_states, TRUE);
13895 
13896  if (format_id)
13897  {
13898  g_string_free (commands_xml, TRUE);
13899  g_string_free (levels, TRUE);
13900  if ((strcmp (format_id, "a994b278-1f62-11e1-96ac-406186ea4fc5") == 0)
13901  || strcmp (format_id, "5057e5cc-b825-11e4-9d0e-28d24461215b") == 0)
13902  {
13903  const char *extension, *requested_content_type;
13904  /* Manager sends XML report as plain XML. */
13905 
13906  if (read_entity_c (connection, &entity))
13907  {
13908  if (error) *error = 1;
13909  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13910  return gsad_message (credentials,
13911  "Internal error", __FUNCTION__, __LINE__,
13912  "An internal error occurred while getting a report. "
13913  "The report could not be delivered. "
13914  "Diagnostics: Failure to receive response from manager daemon.",
13915  "/omp?cmd=get_tasks", response_data);
13916  }
13917  entity_t report = entity_child (entity, "report");
13918  if (report == NULL)
13919  {
13920  free_entity (entity);
13921  if (error) *error = 1;
13922  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13923  return gsad_message (credentials,
13924  "Internal error", __FUNCTION__, __LINE__,
13925  "An internal error occurred while getting a report. "
13926  "The report could not be delivered. "
13927  "Diagnostics: Response from manager daemon did not contain a report.",
13928  "/omp?cmd=get_tasks", response_data);
13929  }
13930  extension = entity_attribute (report, "extension");
13931  requested_content_type = entity_attribute (report, "content_type");
13932  if (extension && requested_content_type && content_type
13933  && content_disposition)
13934  {
13935  gchar *file_name;
13936  ret = setting_get_value (connection,
13937  "e1a2ae0b-736e-4484-b029-330c9e15b900",
13938  &fname_format,
13939  response_data);
13940  if (ret)
13941  {
13942  switch (ret)
13943  {
13944  case 1:
13945  response_data->http_status_code
13946  = MHD_HTTP_INTERNAL_SERVER_ERROR;
13947  return gsad_message (credentials,
13948  "Internal error", __FUNCTION__, __LINE__,
13949  "An internal error occurred while getting a setting. "
13950  "The setting could not be delivered. "
13951  "Diagnostics: Failure to send command to manager daemon.",
13952  "/omp?cmd=get_tasks",
13953  response_data);
13954  case 2:
13955  response_data->http_status_code
13956  = MHD_HTTP_INTERNAL_SERVER_ERROR;
13957  return gsad_message (credentials,
13958  "Internal error", __FUNCTION__, __LINE__,
13959  "An internal error occurred while getting a setting. "
13960  "The setting could not be delivered. "
13961  "Diagnostics: Failure to receive response from manager daemon.",
13962  "/omp?cmd=get_tasks",
13963  response_data);
13964  default:
13965  response_data->http_status_code
13966  = MHD_HTTP_INTERNAL_SERVER_ERROR;
13967  return gsad_message (credentials,
13968  "Internal error", __FUNCTION__, __LINE__,
13969  "An internal error occurred while getting a setting. "
13970  "The setting could not be delivered. "
13971  "Diagnostics: Internal error.",
13972  "/omp?cmd=get_tasks",
13973  response_data);
13974  }
13975  }
13976 
13977  if (fname_format == NULL)
13978  {
13979  g_warning ("%s : File name format setting not found.",
13980  __FUNCTION__);
13981  fname_format = "%T-%U";
13982  }
13983 
13984  file_name = format_file_name (fname_format,
13985  credentials,
13986  "report",
13987  (type
13988  && ((strcmp (type, "assets") == 0)
13989  || (strcmp (type, "prognostic")
13990  == 0)))
13991  ? type
13992  : report_id,
13993  report);
13994  if (file_name == NULL)
13995  file_name = g_strdup_printf ("%s-%s",
13996  "report",
13997  (type
13998  && ((strcmp (type, "assets") == 0)
13999  || (strcmp (type, "prognostic")
14000  == 0)))
14001  ? type
14002  : report_id);
14003 
14004  *content_type = g_strdup (requested_content_type);
14005  *content_disposition
14006  = g_strdup_printf ("attachment; filename=\"%s.%s\"",
14007  file_name,
14008  extension);
14009 
14010  g_free (file_name);
14011  }
14012  xml = g_string_new ("");
14013  print_entity_to_string (report, xml);
14014  free_entity (entity);
14015  if (error) *error = 1;
14016  return g_string_free (xml, FALSE);
14017  }
14018  else
14019  {
14020  /* "nbe", "pdf", "dvi", "html", "html-pdf"... */
14021 
14022  if (report_len == NULL)
14023  {
14024  if (error) *error = 1;
14025  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14026  return gsad_message (credentials,
14027  "Internal error", __FUNCTION__, __LINE__,
14028  "An internal error occurred while getting a report. "
14029  "The report could not be delivered. "
14030  "Diagnostics: Parameter error.",
14031  "/omp?cmd=get_tasks", response_data);
14032  }
14033 
14034  entity = NULL;
14035  if (read_entity_c (connection, &entity))
14036  {
14037  if (error) *error = 1;
14038  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14039  return gsad_message (credentials,
14040  "Internal error", __FUNCTION__, __LINE__,
14041  "An internal error occurred while getting a report. "
14042  "The report could not be delivered. "
14043  "Diagnostics: Failure to receive response from manager daemon.",
14044  "/omp?cmd=get_tasks", response_data);
14045  }
14046 
14047  report_entity = entity_child (entity, "report");
14048  if (report_entity != NULL)
14049  {
14050  const char *extension, *requested_content_type;
14051  char *report_encoded;
14052  gchar *report_decoded;
14053  extension = entity_attribute (report_entity, "extension");
14054  requested_content_type = entity_attribute (report_entity,
14055  "content_type");
14056  report_encoded = entity_text (report_entity);
14057  report_decoded =
14058  (gchar *) g_base64_decode (report_encoded, report_len);
14059  /* g_base64_decode can return NULL (Glib 2.12.4-2), at least
14060  * when *report_len is zero. */
14061  if (report_decoded == NULL)
14062  {
14063  report_decoded = g_strdup ("");
14064  *report_len = 0;
14065  }
14066  if (extension && requested_content_type && content_type
14067  && content_disposition)
14068  {
14069  gchar *file_name;
14070  const char *id;
14071  if (report_id)
14072  id = report_id;
14073  else if (type && (strcmp (type, "prognostic") == 0))
14074  id = "prognostic";
14075  else
14076  id = "ERROR";
14077 
14078  ret = setting_get_value
14079  (connection,
14080  "e1a2ae0b-736e-4484-b029-330c9e15b900",
14081  &fname_format,
14082  response_data);
14083  if (ret)
14084  {
14085  switch (ret)
14086  {
14087  case 1:
14088  response_data->http_status_code
14089  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14090  return gsad_message (credentials,
14091  "Internal error", __FUNCTION__, __LINE__,
14092  "An internal error occurred while getting a setting. "
14093  "The setting could not be delivered. "
14094  "Diagnostics: Failure to send command to manager daemon.",
14095  "/omp?cmd=get_tasks",
14096  response_data);
14097  case 2:
14098  response_data->http_status_code
14099  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14100  return gsad_message (credentials,
14101  "Internal error", __FUNCTION__, __LINE__,
14102  "An internal error occurred while getting a setting. "
14103  "The setting could not be delivered. "
14104  "Diagnostics: Failure to receive response from manager daemon.",
14105  "/omp?cmd=get_tasks",
14106  response_data);
14107  default:
14108  response_data->http_status_code
14109  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14110  return gsad_message (credentials,
14111  "Internal error", __FUNCTION__, __LINE__,
14112  "An internal error occurred while getting a setting. "
14113  "The setting could not be delivered. "
14114  "Diagnostics: Internal error.",
14115  "/omp?cmd=get_tasks",
14116  response_data);
14117  }
14118  }
14119 
14120  if (fname_format == NULL)
14121  {
14122  g_warning ("%s : File name format setting not found.",
14123  __FUNCTION__);
14124  fname_format = "%T-%U";
14125  }
14126 
14127  file_name = format_file_name (fname_format, credentials,
14128  "report", id, report_entity);
14129  if (file_name == NULL)
14130  file_name = g_strdup_printf ("%s-%s",
14131  "report", id);
14132 
14133  *content_type = g_strdup (requested_content_type);
14134  *content_disposition
14135  = g_strdup_printf ("attachment; filename=\"%s.%s\"",
14136  file_name,
14137  extension);
14138 
14139  g_free (file_name);
14140  }
14141  free_entity (entity);
14142  if (error) *error = 1;
14143  return report_decoded;
14144  }
14145  else
14146  {
14147  free_entity (entity);
14148  if (error) *error = 1;
14149  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14150  return gsad_message (credentials,
14151  "Internal error", __FUNCTION__, __LINE__,
14152  "An internal error occurred while getting a report. "
14153  "The report could not be delivered. "
14154  "Diagnostics: Failure to receive report from manager daemon.",
14155  "/omp?cmd=get_tasks", response_data);
14156  }
14157  }
14158  }
14159  else
14160  {
14161  gchar *task_id;
14162 
14163  task_id = NULL;
14164 
14165  /* Format is NULL, send XSL transformed XML. */
14166 
14167  if (delta_report_id && result_id && strcmp (result_id, "0"))
14168  xml = g_string_new ("<get_delta_result>");
14169  else if (host || (type && (strcmp (type, "prognostic") == 0)))
14170  {
14171  if (type && (strcmp (type, "prognostic") == 0))
14172  {
14173  xml = g_string_new ("<get_prognostic_report>");
14174 
14175  xml_string_append (xml,
14176  "<host_search_phrase>"
14177  "%s"
14178  "</host_search_phrase>"
14179  "<host_levels>%s</host_levels>"
14180  "<results start=\"%s\" max=\"%s\"/>",
14181  host_search_phrase,
14182  host_levels,
14183  host_first_result,
14184  host_max_results);
14185  }
14186  else
14187  xml = g_string_new ("<get_asset>");
14188  xml_string_append (xml,
14189  "<search_phrase>%s</search_phrase>"
14190  "<levels>%s</levels>"
14191  "<hosts start=\"%s\" max=\"%s\"/>",
14192  search_phrase,
14193  levels->str,
14194  first_result,
14195  max_results);
14196  }
14197  else
14198  xml = g_string_new ("<get_report>");
14199 
14200  if (extra_xml)
14201  g_string_append (xml, extra_xml);
14202 
14203  if (commands)
14204  g_string_append (xml, commands_xml->str);
14205  g_string_free (commands_xml, TRUE);
14206  g_string_free (levels, TRUE);
14207 
14208  if (strcmp (alert_id, "0"))
14209  {
14210  g_string_append_printf (xml, "<get_reports_alert_response>");
14211  if (esc_response)
14212  {
14213  g_string_append (xml, esc_response);
14214  g_free (esc_response);
14215  }
14216  g_string_append_printf (xml, "</get_reports_alert_response>");
14217  }
14218  else if (delta_report_id)
14219  g_string_append_printf (xml,
14220  "<delta>%s</delta>"
14221  "<result id=\"%s\"/>",
14222  delta_report_id,
14223  result_id ? result_id : "0");
14224 
14225  entity = NULL;
14226  if (read_entity_and_string_c (connection, &entity, &xml))
14227  {
14228  if (error) *error = 1;
14229  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14230  return gsad_message (credentials,
14231  "Internal error", __FUNCTION__, __LINE__,
14232  "An internal error occurred while getting a report. "
14233  "The report could not be delivered. "
14234  "Diagnostics: Failure to receive response from manager daemon.",
14235  "/omp?cmd=get_tasks", response_data);
14236  }
14237 
14238  if ((filt_id == NULL) && (params_value (params, "filter") == NULL))
14239  {
14240  entity_t term;
14241 
14242  /* Add the filter from the report as a param, because it's easier to
14243  * get from the envelope for things like the New Note icon. */
14244 
14245  term = entity_child (entity, "report");
14246  if (term
14247  && ((term = entity_child (term, "report")))
14248  && ((term = entity_child (term, "filters")))
14249  && ((term = entity_child (term, "term"))))
14250  {
14251  param_t *param;
14252  param = params_add (params, "filter", entity_text (term));
14253  param->valid = 1;
14254  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
14255  }
14256  }
14257 
14258  if ((type && (strcmp (type, "prognostic") == 0))
14259  && (command_enabled (credentials, "GET_REPORT_FORMATS")))
14260  {
14261  if (openvas_connection_sendf
14262  (connection,
14263  "<get_report_formats"
14264  " filter=\"rows=-1 sort=name\"/>")
14265  == -1)
14266  {
14267  g_string_free (xml, TRUE);
14268  if (error) *error = 1;
14269  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14270  return gsad_message (credentials,
14271  "Internal error", __FUNCTION__, __LINE__,
14272  "An internal error occurred while getting a report. "
14273  "The report could not be delivered. "
14274  "Diagnostics: Failure to send command to manager daemon.",
14275  "/omp?cmd=get_tasks", response_data);
14276  }
14277 
14278  if (read_string_c (connection, &xml))
14279  {
14280  g_string_free (xml, TRUE);
14281  if (error) *error = 1;
14282  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14283  return gsad_message (credentials,
14284  "Internal error", __FUNCTION__, __LINE__,
14285  "An internal error occurred while getting a report. "
14286  "The report could not be delivered. "
14287  "Diagnostics: Failure to receive response from manager daemon.",
14288  "/omp?cmd=get_tasks", response_data);
14289  }
14290  }
14291 
14292  if (type && (strcmp (type, "prognostic") == 0))
14293  {
14294  if (command_enabled (credentials, "GET_FILTERS"))
14295  {
14296  /* Get the filters. */
14297 
14298  g_string_append (xml, "<filters>");
14299 
14300  if (openvas_connection_sendf_xml (connection,
14301  "<get_filters"
14302  " filter=\"type=result\"/>")
14303  == -1)
14304  {
14305  g_string_free (xml, TRUE);
14306  if (error) *error = 1;
14307  response_data->http_status_code
14308  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14309  return gsad_message (credentials,
14310  "Internal error", __FUNCTION__, __LINE__,
14311  "An internal error occurred while getting the filter list. "
14312  "The current list of filters is not available. "
14313  "Diagnostics: Failure to send command to manager daemon.",
14314  "/omp?cmd=get_tasks", response_data);
14315  }
14316 
14317  if (read_string_c (connection, &xml))
14318  {
14319  g_string_free (xml, TRUE);
14320  if (error) *error = 1;
14321  response_data->http_status_code
14322  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14323  return gsad_message (credentials,
14324  "Internal error", __FUNCTION__, __LINE__,
14325  "An internal error occurred while getting the filter list. "
14326  "The current list of filters is not available. "
14327  "Diagnostics: Failure to receive response from manager daemon.",
14328  "/omp?cmd=get_tasks", response_data);
14329  }
14330 
14331  g_string_append (xml, "</filters>");
14332  }
14333 
14334  g_string_append (xml, "</get_prognostic_report>");
14335  return g_string_free (xml, FALSE);
14336  }
14337 
14338  if (type && (strcmp (type, "assets") == 0))
14339  {
14340  if (host)
14341  g_string_append (xml, "</get_asset>");
14342  else
14343  g_string_append (xml, "</get_report>");
14344  return g_string_free (xml, FALSE);
14345  }
14346 
14347  report_entity = entity_child (entity, "report");
14348  if (report_entity)
14349  report_entity = entity_child (report_entity, "report");
14350  if (report_entity)
14351  {
14352  const char *id;
14353  entity_t task_entity, name;
14354 
14355  id = NULL;
14356  task_entity = entity_child (report_entity, "task");
14357  if (task_entity)
14358  {
14359  id = entity_attribute (task_entity, "id");
14360  name = entity_child (task_entity, "name");
14361  }
14362  else
14363  name = NULL;
14364  if (id)
14365  task_id = g_strdup (id);
14366  if (delta_report_id && result_id && id && name)
14367  g_string_append_printf (xml,
14368  "<task id=\"%s\"><name>%s</name></task>",
14369  id,
14370  entity_text (name));
14371 
14372  free_entity (entity);
14373  }
14374 
14375  if (task_id)
14376  {
14377  if (openvas_connection_sendf (connection,
14378  "<get_tasks task_id=\"%s\" details=\"0\" />",
14379  task_id)
14380  == -1)
14381  {
14382  g_free (task_id);
14383  g_string_free (xml, TRUE);
14384  if (error) *error = 1;
14385  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14386  return gsad_message (credentials,
14387  "Internal error", __FUNCTION__, __LINE__,
14388  "An internal error occurred while getting a report. "
14389  "The report could not be delivered. "
14390  "Diagnostics: Failure to send command to manager daemon.",
14391  "/omp?cmd=get_tasks", response_data);
14392  }
14393 
14394  if (read_string_c (connection, &xml))
14395  {
14396  g_free (task_id);
14397  g_string_free (xml, TRUE);
14398  if (error) *error = 1;
14399  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14400  return gsad_message (credentials,
14401  "Internal error", __FUNCTION__, __LINE__,
14402  "An internal error occurred while getting a report. "
14403  "The report could not be delivered. "
14404  "Diagnostics: Failure to send command to manager daemon.",
14405  "/omp?cmd=get_tasks", response_data);
14406  }
14407 
14408  g_free (task_id);
14409  }
14410 
14411  if (delta_report_id && result_id && strcmp (result_id, "0"))
14412  {
14413  g_string_append (xml, "</get_delta_result>");
14414  return g_string_free (xml, FALSE);
14415  }
14416 
14417  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
14418  {
14419  gchar *default_report_format, *err;
14420 
14421  /* Get Default Report Format. */
14422 
14423  err = setting_get_value_error (credentials,
14424  connection,
14425  "353304fc-645e-11e6-ba7a-28d24461215b",
14426  &default_report_format,
14427  response_data);
14428  if (err)
14429  {
14430  g_string_free (xml, TRUE);
14431  if (error) *error = 1;
14432  return err;
14433  }
14434 
14435  g_string_append_printf (xml,
14436  "<report_format_id>%s</report_format_id>",
14437  default_report_format);
14438 
14439  /* Get all the report formats. */
14440 
14441  if (openvas_connection_sendf
14442  (connection,
14443  "<get_report_formats"
14444  " filter=\"rows=-1 sort=name\"/>")
14445  == -1)
14446  {
14447  g_string_free (xml, TRUE);
14448  if (error) *error = 1;
14449  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14450  return gsad_message (credentials,
14451  "Internal error", __FUNCTION__, __LINE__,
14452  "An internal error occurred while getting a report. "
14453  "The report could not be delivered. "
14454  "Diagnostics: Failure to send command to manager daemon.",
14455  "/omp?cmd=get_tasks", response_data);
14456  }
14457 
14458  if (read_string_c (connection, &xml))
14459  {
14460  g_string_free (xml, TRUE);
14461  if (error) *error = 1;
14462  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14463  return gsad_message (credentials,
14464  "Internal error", __FUNCTION__, __LINE__,
14465  "An internal error occurred while getting a report. "
14466  "The report could not be delivered. "
14467  "Diagnostics: Failure to receive response from manager daemon.",
14468  "/omp?cmd=get_tasks", response_data);
14469  }
14470  }
14471 
14472  if (command_enabled (credentials, "GET_ALERTS"))
14473  {
14474  if (openvas_connection_sendf
14475  (connection,
14476  "<get_alerts"
14477  " filter=\"rows=-1 sort=name\"/>")
14478  == -1)
14479  {
14480  g_string_free (xml, TRUE);
14481  if (error) *error = 1;
14482  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14483  return gsad_message (credentials,
14484  "Internal error", __FUNCTION__, __LINE__,
14485  "An internal error occurred while getting a report. "
14486  "The report could not be delivered. "
14487  "Diagnostics: Failure to send command to manager daemon.",
14488  "/omp?cmd=get_tasks", response_data);
14489  }
14490 
14491  if (read_string_c (connection, &xml))
14492  {
14493  g_string_free (xml, TRUE);
14494  if (error) *error = 1;
14495  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14496  return gsad_message (credentials,
14497  "Internal error", __FUNCTION__, __LINE__,
14498  "An internal error occurred while getting a report. "
14499  "The report could not be delivered. "
14500  "Diagnostics: Failure to receive response from manager daemon.",
14501  "/omp?cmd=get_tasks", response_data);
14502  }
14503  }
14504 
14505  if (command_enabled (credentials, "GET_FILTERS"))
14506  {
14507  /* Get the filters. */
14508 
14509  g_string_append (xml, "<filters>");
14510 
14511  if (openvas_connection_sendf_xml (connection,
14512  "<get_filters"
14513  " filter=\"type=result\"/>")
14514  == -1)
14515  {
14516  g_string_free (xml, TRUE);
14517  if (error) *error = 1;
14518  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14519  return gsad_message (credentials,
14520  "Internal error", __FUNCTION__, __LINE__,
14521  "An internal error occurred while getting the filter list. "
14522  "The current list of filters is not available. "
14523  "Diagnostics: Failure to send command to manager daemon.",
14524  "/omp?cmd=get_tasks", response_data);
14525  }
14526 
14527  if (read_string_c (connection, &xml))
14528  {
14529  g_string_free (xml, TRUE);
14530  if (error) *error = 1;
14531  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14532  return gsad_message (credentials,
14533  "Internal error", __FUNCTION__, __LINE__,
14534  "An internal error occurred while getting the filter list. "
14535  "The current list of filters is not available. "
14536  "Diagnostics: Failure to receive response from manager daemon.",
14537  "/omp?cmd=get_tasks", response_data);
14538  }
14539 
14540  g_string_append (xml, "</filters>");
14541  }
14542 
14543  /* Get tag names */
14544 
14545  if (openvas_connection_sendf (connection,
14546  "<get_tags"
14547  " filter=\"resource_type=report"
14548  " first=1"
14549  " rows=-1\""
14550  " names_only=\"1\""
14551  "/>")
14552  == -1)
14553  {
14554  g_string_free (xml, TRUE);
14555  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14556  return gsad_message (credentials,
14557  "Internal error", __FUNCTION__, __LINE__,
14558  "An internal error occurred while getting tag names list. "
14559  "The current list of resources is not available. "
14560  "Diagnostics: Failure to send command to manager daemon.",
14561  "/omp?cmd=get_resources", response_data);
14562  }
14563 
14564  if (read_string_c (connection, &xml))
14565  {
14566  g_string_free (xml, TRUE);
14567  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14568  return gsad_message (credentials,
14569  "Internal error", __FUNCTION__, __LINE__,
14570  "An internal error occurred while getting tag names list. "
14571  "The current list of resources is not available. "
14572  "Diagnostics: Failure to receive response from manager daemon.",
14573  "/omp?cmd=get_resources", response_data);
14574  }
14575 
14576  g_string_append (xml, "</get_report>");
14577  return g_string_free (xml, FALSE);
14578  }
14579 }
14580 
14594 char *
14595 get_report_omp (openvas_connection_t *connection, credentials_t * credentials,
14596  params_t *params, gsize *report_len, gchar ** content_type,
14597  char **content_disposition, cmd_response_data_t* response_data)
14598 {
14599  char *result;
14600  int error = 0;
14601 
14602  result = get_report (connection, credentials, params, NULL, report_len,
14603  content_type, content_disposition, NULL, &error,
14604  response_data);
14605 
14606  return error ? result : xsl_transform_omp (connection, credentials, params,
14607  result, response_data);
14608 }
14609 
14621 static char *
14622 get_reports (openvas_connection_t *connection, credentials_t * credentials,
14623  params_t *params, const char *extra_xml,
14624  cmd_response_data_t* response_data)
14625 {
14626  const char *overrides;
14627 
14628  overrides = params_value (params, "overrides");
14629  if (overrides)
14630  /* User toggled overrides. Set the overrides value in the filter. */
14631  params_toggle_overrides (params, overrides);
14632 
14633  return get_many (connection, "report", credentials, params, extra_xml, NULL,
14634  response_data);
14635 }
14636 
14647 char *
14648 get_reports_omp (openvas_connection_t *connection, credentials_t * credentials,
14649  params_t *params, cmd_response_data_t* response_data)
14650 {
14651  return get_reports (connection, credentials, params, NULL, response_data);
14652 }
14653 
14664 static char *
14665 get_report_section (openvas_connection_t *connection,
14666  credentials_t * credentials, params_t *params,
14667  const char *extra_xml, cmd_response_data_t* response_data)
14668 {
14669  GString *xml;
14670  const char *report_id, *report_section, *type;
14671  char *result;
14672  int error = 0;
14673 
14674  report_section = params_value (params, "report_section");
14675  report_id = params_value (params, "report_id");
14676  type = params_value (params, "type");
14677 
14678  if (report_section == NULL)
14679  report_section = "results";
14680 
14681  if (report_id == NULL && (type == NULL || strcmp (type, "prognostic")))
14682  {
14683  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
14684  return gsad_message (credentials,
14685  "Internal error", __FUNCTION__, __LINE__,
14686  "An internal error occurred."
14687  " Diagnostics: report_id was NULL.",
14688  "/omp?cmd=get_tasks", response_data);
14689  }
14690 
14691  if (!strcmp (report_section, "results"))
14692  {
14693  char *result;
14694 
14695  result = get_report (connection, credentials, params, NULL, NULL, NULL,
14696  NULL, extra_xml, &error, response_data);
14697 
14698  return error ? result : xsl_transform_omp (connection, credentials,
14699  params, result, response_data);
14700  }
14701 
14702  result = get_report (connection, credentials, params, NULL, NULL, NULL,
14703  NULL, NULL, &error, response_data);
14704  if (error)
14705  return result;
14706 
14707  xml = g_string_new ("");
14708  g_string_append_printf (xml, "<get_report_%s_response>", report_section);
14709  if (extra_xml)
14710  g_string_append (xml, extra_xml);
14711  g_string_append (xml, result);
14712  if (strcmp (params_value (params, "report_section"), "topology") == 0)
14713  {
14714  int ret;
14715  char *response;
14716 
14717  ret = omp (connection, credentials,
14718  &response,
14719  NULL,
14720  response_data,
14721  "<get_report_formats"
14722  " filter=\"rows=-1\"/>");
14723 
14724  switch (ret)
14725  {
14726  case 0:
14727  case -1:
14728  break;
14729  case 1:
14730  g_string_free (xml, TRUE);
14731  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14732  return gsad_message (credentials,
14733  "Internal error", __FUNCTION__, __LINE__,
14734  "An internal error occurred while getting the "
14735  "result formats list. "
14736  "Diagnostics: Failure to send command to manager daemon.",
14737  "/omp?cmd=get_tasks", response_data);
14738  case 2:
14739  g_string_free (xml, TRUE);
14740  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14741  return gsad_message (credentials,
14742  "Internal error", __FUNCTION__, __LINE__,
14743  "An internal error occurred while getting the "
14744  "result formats list. "
14745  "Diagnostics: Failure to receive response from manager daemon.",
14746  "/omp?cmd=get_tasks", response_data);
14747  default:
14748  g_string_free (xml, TRUE);
14749  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14750  return gsad_message (credentials,
14751  "Internal error", __FUNCTION__, __LINE__,
14752  "An internal error occurred while getting the "
14753  "result formats list. "
14754  "Diagnostics: Internal Error.",
14755  "/omp?cmd=get_tasks", response_data);
14756  }
14757 
14758  g_string_append (xml, response);
14759  g_free (response);
14760  }
14761 
14762  g_string_append_printf (xml, "</get_report_%s_response>", report_section);
14763 
14764  return xsl_transform_omp (connection, credentials, params,
14765  g_string_free (xml, FALSE), response_data);
14766 }
14767 
14778 char *
14779 get_report_section_omp (openvas_connection_t *connection,
14780  credentials_t * credentials, params_t *params,
14781  cmd_response_data_t* response_data)
14782 {
14783  return get_report_section (connection, credentials, params, NULL, response_data);
14784 }
14785 
14797 char *
14798 download_ssl_cert (openvas_connection_t *connection,
14799  credentials_t * credentials, params_t *params,
14800  gsize *response_size, cmd_response_data_t* response_data)
14801 {
14802  const char *ssl_cert;
14803  gchar *cert;
14804  char *unescaped;
14805 
14806  ssl_cert = params_value (params, "ssl_cert");
14807  if (ssl_cert == NULL)
14808  {
14809  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
14810  return gsad_message (credentials,
14811  "Internal error", __FUNCTION__, __LINE__,
14812  "An internal error occurred."
14813  " Diagnostics: ssl_cert was NULL.",
14814  "/omp?cmd=get_reports", response_data);
14815  }
14816  /* The Base64 comes URI escaped as it may contain special characters. */
14817  unescaped = g_uri_unescape_string (ssl_cert, NULL);
14818 
14819  cert = g_strdup_printf ("-----BEGIN CERTIFICATE-----\n"
14820  "%s\n-----END CERTIFICATE-----\n",
14821  unescaped);
14822 
14823  *response_size = strlen (cert);
14824 
14825  g_free (unescaped);
14826  return cert;
14827 }
14828 
14840 char *
14841 download_ca_pub (openvas_connection_t *connection, credentials_t * credentials,
14842  params_t *params, gsize *response_size,
14843  cmd_response_data_t* response_data)
14844 {
14845  const char *ca_pub;
14846  char *unescaped;
14847 
14848  ca_pub = params_value (params, "ca_pub");
14849  if (ca_pub == NULL)
14850  {
14851  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
14852  return gsad_message (credentials,
14853  "Internal error", __FUNCTION__, __LINE__,
14854  "An internal error occurred."
14855  " Diagnostics: ca_pub was NULL.",
14856  "/omp?cmd=get_reports", response_data);
14857  }
14858  /* The Base64 comes URI escaped as it may contain special characters. */
14859  unescaped = g_uri_unescape_string (ca_pub, NULL);
14860  *response_size = strlen (unescaped);
14861  return unescaped;
14862 }
14863 
14875 char *
14876 download_key_pub (openvas_connection_t *connection, credentials_t * credentials,
14877  params_t *params, gsize *response_size,
14878  cmd_response_data_t* response_data)
14879 {
14880  const char *key_pub;
14881  char *unescaped;
14882 
14883  key_pub = params_value (params, "key_pub");
14884  if (key_pub == NULL)
14885  {
14886  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
14887  return gsad_message (credentials,
14888  "Internal error", __FUNCTION__, __LINE__,
14889  "An internal error occurred."
14890  " Diagnostics: key_pub was NULL.",
14891  "/omp?cmd=get_reports", response_data);
14892  }
14893 
14894  /* The Base64 comes URI escaped as it may contain special characters. */
14895  unescaped = g_uri_unescape_string (key_pub, NULL);
14896  *response_size = strlen (unescaped);
14897  return unescaped;
14898 }
14899 
14914 char *
14915 export_result_omp (openvas_connection_t *connection,
14916  credentials_t * credentials, params_t *params,
14917  enum content_type * content_type, char **content_disposition,
14918  gsize *content_length, cmd_response_data_t* response_data)
14919 {
14920  return export_resource (connection, "result", credentials, params,
14921  content_type, content_disposition, content_length,
14922  response_data);
14923 }
14924 
14939 char *
14940 export_results_omp (openvas_connection_t *connection,
14941  credentials_t * credentials, params_t *params,
14942  enum content_type * content_type,
14943  char **content_disposition, gsize *content_length,
14944  cmd_response_data_t* response_data)
14945 {
14946  return export_many (connection, "result", credentials, params, content_type,
14947  content_disposition, content_length, response_data);
14948 }
14949 
14961 char *
14962 get_results (openvas_connection_t *connection, credentials_t * credentials,
14963  params_t *params, const char *extra_xml,
14964  cmd_response_data_t* response_data)
14965 {
14966  const char *overrides;
14967  overrides = params_value (params, "overrides");
14968 
14969  if (overrides)
14970  /* User toggled overrides. Set the overrides value in the filter. */
14971  params_toggle_overrides (params, overrides);
14972 
14973  return get_many (connection, "result", credentials, params, extra_xml, NULL,
14974  response_data);
14975 }
14976 
14987 char *
14988 get_results_omp (openvas_connection_t *connection, credentials_t *credentials,
14989  params_t *params, cmd_response_data_t* response_data)
14990 {
14991  return get_results (connection, credentials,
14992  params,
14993  NULL,
14994  response_data);
14995 }
14996 
15014 static char *
15015 get_result (openvas_connection_t *connection, credentials_t *credentials,
15016  params_t *params, const char *result_id, const char *task_id,
15017  const char *task_name, const char *apply_overrides,
15018  const char *commands, const char *report_id, const char *autofp,
15019  const char *extra_xml, cmd_response_data_t* response_data)
15020 {
15021  GString *xml;
15022 
15023  if (apply_overrides == NULL)
15024  apply_overrides = "1";
15025 
15026  if (autofp == NULL)
15027  autofp = "0";
15028 
15029  xml = g_string_new ("<get_result>");
15030 
15031  if (extra_xml)
15032  g_string_append (xml, extra_xml);
15033 
15034  xml_string_append (xml,
15035  "<task id=\"%s\"><name>%s</name></task>"
15036  "<report id=\"%s\"/>",
15037  task_id,
15038  task_name,
15039  report_id);
15040 
15041  /* Get the result. */
15042 
15043  if (openvas_connection_sendf (connection,
15044  "<commands>"
15045  "%s"
15046  "<get_results"
15047  " result_id=\"%s\""
15048  " get_counts=\"0\""
15049  "%s%s%s"
15050  " filter=\"autofp=%s"
15051  " apply_overrides=%s"
15052  " overrides=%s"
15053  " notes=1\""
15054  " overrides_details=\"1\""
15055  " notes_details=\"1\""
15056  " details=\"1\"/>"
15057  "</commands>",
15058  commands ? commands : "",
15059  result_id,
15060  task_id ? " task_id=\"" : "",
15061  task_id ? task_id : "",
15062  task_id ? "\"" : "",
15063  autofp,
15064  apply_overrides,
15065  apply_overrides)
15066  == -1)
15067  {
15068  g_string_free (xml, TRUE);
15069  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15070  return gsad_message (credentials,
15071  "Internal error", __FUNCTION__, __LINE__,
15072  "An internal error occurred while getting a result. "
15073  "Diagnostics: Failure to send command to manager daemon.",
15074  "/omp?cmd=get_tasks", response_data);
15075  }
15076 
15077  if (read_string_c (connection, &xml))
15078  {
15079  g_string_free (xml, TRUE);
15080  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15081  return gsad_message (credentials,
15082  "Internal error", __FUNCTION__, __LINE__,
15083  "An internal error occurred while getting a result. "
15084  "Diagnostics: Failure to receive response from manager daemon.",
15085  "/omp?cmd=get_tasks", response_data);
15086  }
15087 
15088  /* Get tag names */
15089 
15090  if (openvas_connection_sendf (connection,
15091  "<get_tags"
15092  " filter=\"resource_type=result"
15093  " first=1"
15094  " rows=-1\""
15095  " names_only=\"1\""
15096  "/>")
15097  == -1)
15098  {
15099  g_string_free (xml, TRUE);
15100  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15101  return gsad_message (credentials,
15102  "Internal error", __FUNCTION__, __LINE__,
15103  "An internal error occurred while getting tag names list. "
15104  "The current list of resources is not available. "
15105  "Diagnostics: Failure to send command to manager daemon.",
15106  "/omp?cmd=get_resources", response_data);
15107  }
15108 
15109  if (read_string_c (connection, &xml))
15110  {
15111  g_string_free (xml, TRUE);
15112  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15113  return gsad_message (credentials,
15114  "Internal error", __FUNCTION__, __LINE__,
15115  "An internal error occurred while getting tag names list. "
15116  "The current list of resources is not available. "
15117  "Diagnostics: Failure to receive response from manager daemon.",
15118  "/omp?cmd=get_resources", response_data);
15119  }
15120 
15121  /* Cleanup, and return transformed XML. */
15122 
15123  g_string_append (xml, "</get_result>");
15124  return xsl_transform_omp (connection, credentials, params,
15125  g_string_free (xml, FALSE), response_data);
15126 }
15127 
15138 char *
15139 get_result_omp (openvas_connection_t *connection, credentials_t *credentials,
15140  params_t *params, cmd_response_data_t* response_data)
15141 {
15142  return get_result (connection, credentials, params,
15143  params_value (params, "result_id"),
15144  params_value (params, "task_id"),
15145  params_value (params, "name"),
15146  params_value (params, "apply_overrides"),
15147  NULL,
15148  params_value (params, "report_id"),
15149  params_value (params, "autofp"),
15150  NULL,
15151  response_data);
15152 }
15153 
15165 static char *
15166 get_result_page (openvas_connection_t *connection, credentials_t *credentials,
15167  params_t *params, const char *extra_xml,
15168  cmd_response_data_t* response_data)
15169 {
15170  return get_result (connection, credentials, params,
15171  params_value (params, "result_id"),
15172  params_value (params, "task_id"),
15173  params_value (params, "name"),
15174  params_value (params, "apply_overrides"),
15175  NULL,
15176  params_value (params, "report_id"),
15177  params_value (params, "autofp"),
15178  extra_xml,
15179  response_data);
15180 
15181 }
15182 
15194 static char *
15195 get_notes (openvas_connection_t *connection, credentials_t *credentials,
15196  params_t *params, const char *extra_xml,
15197  cmd_response_data_t* response_data)
15198 {
15199  return get_many (connection, "note", credentials, params, extra_xml, NULL,
15200  response_data);
15201 }
15202 
15213 char *
15214 get_notes_omp (openvas_connection_t *connection, credentials_t *credentials,
15215  params_t *params, cmd_response_data_t* response_data)
15216 {
15217  return get_notes (connection, credentials, params, NULL, response_data);
15218 }
15219 
15231 static char *
15232 get_note (openvas_connection_t *connection, credentials_t *credentials,
15233  params_t *params, const char *extra_xml,
15234  cmd_response_data_t* response_data)
15235 {
15236  return get_one (connection, "note", credentials, params, extra_xml, NULL,
15237  response_data);
15238 }
15239 
15250 char *
15251 get_note_omp (openvas_connection_t *connection, credentials_t *credentials,
15252  params_t *params, cmd_response_data_t* response_data)
15253 {
15254  return get_note (connection, credentials, params, NULL, response_data);
15255 }
15256 
15268 char *
15269 new_note (openvas_connection_t *connection, credentials_t *credentials,
15270  params_t *params, const char *extra_xml,
15271  cmd_response_data_t* response_data)
15272 {
15273  GString *xml;
15274  const char *oid, *hosts, *port, *severity, *task_id, *task_name, *result_id;
15275  const char *next;
15276  /* Passthroughs. */
15277  const char *report_id, *first_result, *max_results, *sort_field;
15278  const char *sort_order, *levels, *autofp, *notes;
15279  const char *overrides, *result_hosts_only, *search_phrase;
15280 
15281  result_id = params_value (params, "result_id");
15282  task_id = params_value (params, "task_id");
15283 
15284  next = params_value (params, "next");
15285  first_result = params_value (params, "first_result");
15286  max_results = params_value (params, "max_results");
15287  levels = params_value (params, "levels");
15288  autofp = params_value (params, "autofp");
15289  notes = params_value (params, "notes");
15290  report_id = params_value (params, "report_id");
15291  search_phrase = params_value (params, "search_phrase");
15292  sort_field = params_value (params, "sort_field");
15293  sort_order = params_value (params, "sort_order");
15294  task_name = params_value (params, "name");
15295  severity = params_value (params, "severity");
15296  result_hosts_only = params_value (params, "result_hosts_only");
15297 
15298  hosts = params_value (params, "hosts");
15299  oid = params_value (params, "oid");
15300  port = params_value (params, "port");
15301  overrides = params_value (params, "overrides");
15302 
15303  if (result_id == NULL || task_id == NULL)
15304  {
15305  xml = g_string_new ("");
15306 
15307  xml_string_append (xml,
15308  "<new_note>"
15309  "<nvt id=\"%s\"/>"
15310  "<next>%s</next>",
15311  oid ? oid : "0",
15312  next ? next : "");
15313 
15314  if (extra_xml)
15315  g_string_append (xml, extra_xml);
15316 
15317  if (openvas_connection_sendf (connection,
15318  "<get_tasks"
15319  " schedules_only=\"1\""
15320  " details=\"0\"/>")
15321  == -1)
15322  {
15323  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15324  return gsad_message (credentials,
15325  "Internal error", __FUNCTION__, __LINE__,
15326  "An internal error occurred while creating a new note. "
15327  "No new note was created. "
15328  "Diagnostics: Failure to send command to manager daemon.",
15329  "/omp?cmd=get_notes", response_data);
15330  }
15331 
15332  if (read_string_c (connection, &xml))
15333  {
15334  g_string_free (xml, TRUE);
15335  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15336  return gsad_message (credentials,
15337  "Internal error", __FUNCTION__, __LINE__,
15338  "An internal error occurred while creating a new note. "
15339  "No new note was created. "
15340  "Diagnostics: Failure to receive response from manager daemon.",
15341  "/omp?cmd=get_notes", response_data);
15342  }
15343 
15344  g_string_append (xml, "</new_note>");
15345  return xsl_transform_omp (connection, credentials, params,
15346  g_string_free (xml, FALSE), response_data);
15347  }
15348 
15349  if (openvas_connection_sendf (connection,
15350  "<get_results"
15351  " result_id=\"%s\""
15352  " task_id=\"%s\""
15353  " notes_details=\"1\""
15354  " notes=\"1\""
15355  " result_hosts_only=\"1\"/>",
15356  result_id,
15357  task_id)
15358  == -1)
15359  {
15360  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15361  return gsad_message (credentials,
15362  "Internal error", __FUNCTION__, __LINE__,
15363  "An internal error occurred while creating a new note. "
15364  "No new note was created. "
15365  "Diagnostics: Failure to send command to manager daemon.",
15366  "/omp?cmd=get_notes", response_data);
15367  }
15368 
15369  xml = g_string_new ("");
15370 
15371  xml_string_append (xml,
15372  "<new_note>"
15373  "<nvt id=\"%s\"/>"
15374  "<hosts>%s</hosts>"
15375  "<port>%s</port>"
15376  "<severity>%s</severity>"
15377  "<task id=\"%s\">"
15378  "<name>%s</name>"
15379  "</task>"
15380  "<result id=\"%s\"/>"
15381  "<next>%s</next>"
15382  /* Passthroughs. */
15383  "<report id=\"%s\"/>"
15384  "<first_result>%s</first_result>"
15385  "<max_results>%s</max_results>"
15386  "<sort_field>%s</sort_field>"
15387  "<sort_order>%s</sort_order>"
15388  "<levels>%s</levels>"
15389  "<autofp>%s</autofp>"
15390  "<notes>%s</notes>"
15391  "<overrides>%s</overrides>"
15392  "<result_hosts_only>%s</result_hosts_only>"
15393  "<search_phrase>%s</search_phrase>",
15394  oid,
15395  hosts,
15396  /* port is NULL for CVE scan results. */
15397  port ? port : "",
15398  severity,
15399  task_id,
15400  task_name,
15401  result_id,
15402  next ? next : "",
15403  report_id,
15404  first_result,
15405  max_results,
15406  sort_field,
15407  sort_order,
15408  levels,
15409  autofp,
15410  notes,
15411  overrides,
15412  result_hosts_only,
15413  search_phrase);
15414 
15415  if (extra_xml)
15416  g_string_append (xml, extra_xml);
15417 
15418  if (read_string_c (connection, &xml))
15419  {
15420  g_string_free (xml, TRUE);
15421  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15422  return gsad_message (credentials,
15423  "Internal error", __FUNCTION__, __LINE__,
15424  "An internal error occurred while creating a new note. "
15425  "It is unclear whether the note has been created or not. "
15426  "Diagnostics: Failure to receive response from manager daemon.",
15427  "/omp?cmd=get_notes", response_data);
15428  }
15429 
15430  /* Cleanup, and return transformed XML. */
15431 
15432  g_string_append (xml, "</new_note>");
15433  return xsl_transform_omp (connection, credentials, params,
15434  g_string_free (xml, FALSE), response_data);
15435 }
15436 
15447 char *
15448 new_note_omp (openvas_connection_t *connection, credentials_t *credentials,
15449  params_t *params, cmd_response_data_t* response_data)
15450 {
15451  return new_note (connection, credentials, params, NULL, response_data);
15452 }
15453 
15464 char *
15465 create_note_omp (openvas_connection_t *connection, credentials_t *credentials,
15466  params_t *params, cmd_response_data_t* response_data)
15467 {
15468  char *ret;
15469  gchar *response;
15470  const char *no_redirect, *oid, *severity, *port, *hosts;
15471  const char *text, *task_id, *note_result_id;
15472  /* For get_report. */
15473  const char *active, *days;
15474  entity_t entity;
15475 
15476  no_redirect = params_value (params, "no_redirect");
15477  oid = params_value (params, "oid");
15478  CHECK_PARAM_INVALID (oid, "Create Note", "new_note");
15479 
15480  if (params_valid (params, "severity"))
15481  severity = params_value (params, "severity");
15482  else if (params_given (params, "severity")
15483  && strcmp (params_original_value (params, "severity"), ""))
15484  severity = NULL;
15485  else
15486  severity = "";
15487  CHECK_PARAM_INVALID (severity, "Create Note", "new_note");
15488 
15489  port = params_value (params, "port");
15490  if (port == NULL)
15491  port = "";
15492  if (strcmp (port, "--") == 0)
15493  {
15494  int num = -1;
15495 
15496  port = params_value (params, "port_manual");
15497  if (port)
15498  num = atoi (port);
15499  if (num < 0 || num > 65535)
15500  port = NULL;
15501  }
15502  CHECK_PARAM_INVALID (port, "Create Note", "new_note");
15503 
15504  if (params_valid (params, "hosts"))
15505  {
15506  hosts = params_value (params, "hosts");
15507  if (strcmp (hosts, "--") == 0)
15508  {
15509  if (params_valid (params, "hosts_manual"))
15510  hosts = params_value (params, "hosts_manual");
15511  else if (params_given (params, "hosts_manual")
15512  && strcmp (params_original_value (params, "hosts_manual"),
15513  ""))
15514  hosts = NULL;
15515  else
15516  hosts = "";
15517  }
15518  }
15519  else if (strcmp (params_original_value (params, "hosts"), ""))
15520  hosts = NULL;
15521  else
15522  hosts = "";
15523  CHECK_PARAM_INVALID (hosts, "Create Note", "new_note");
15524 
15525  if (params_valid (params, "note_task_id"))
15526  {
15527  task_id = params_value (params, "note_task_id");
15528  if (task_id && (strcmp (task_id, "0") == 0))
15529  task_id = params_value (params, "note_task_uuid");
15530  }
15531  else if (params_given (params, "note_task_id")
15532  && strcmp (params_original_value (params, "note_task_id"), ""))
15533  task_id = NULL;
15534  else
15535  task_id = "";
15536 
15537  active = params_value (params, "active");
15538  CHECK_PARAM_INVALID (active, "Create Note", "new_note");
15539 
15540  text = params_value (params, "text");
15541  days = params_value (params, "days");
15542 
15543  note_result_id = params_value (params, "note_result_id");
15544  if (note_result_id && (strcmp (note_result_id, "0") == 0))
15545  note_result_id = params_value (params, "note_result_uuid");
15546 
15547  response = NULL;
15548  entity = NULL;
15549  switch (ompf (connection, credentials,
15550  &response,
15551  &entity,
15552  response_data,
15553  "<create_note>"
15554  "<active>%s</active>"
15555  "<nvt oid=\"%s\"/>"
15556  "<hosts>%s</hosts>"
15557  "<port>%s</port>"
15558  "<severity>%s</severity>"
15559  "<text>%s</text>"
15560  "<task id=\"%s\"/>"
15561  "<result id=\"%s\"/>"
15562  "</create_note>",
15563  strcmp (active, "1")
15564  ? active
15565  : (days ? days : "-1"),
15566  oid,
15567  hosts,
15568  port,
15569  severity,
15570  text ? text : "",
15571  task_id,
15572  note_result_id))
15573  {
15574  case 0:
15575  case -1:
15576  break;
15577  case 1:
15578  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15579  return gsad_message (credentials,
15580  "Internal error", __FUNCTION__, __LINE__,
15581  "An internal error occurred while creating a new note. "
15582  "No new note was created. "
15583  "Diagnostics: Failure to send command to manager daemon.",
15584  "/omp?cmd=get_notes", response_data);
15585  case 2:
15586  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15587  return gsad_message (credentials,
15588  "Internal error", __FUNCTION__, __LINE__,
15589  "An internal error occurred while creating a new note. "
15590  "It is unclear whether the note has been created or not. "
15591  "Diagnostics: Failure to receive response from manager daemon.",
15592  "/omp?cmd=get_notes", response_data);
15593  default:
15594  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15595  return gsad_message (credentials,
15596  "Internal error", __FUNCTION__, __LINE__,
15597  "An internal error occurred while creating a new note. "
15598  "It is unclear whether the note has been created or not. "
15599  "Diagnostics: Internal Error.",
15600  "/omp?cmd=get_notes", response_data);
15601  }
15602 
15603  if (entity_attribute (entity, "id"))
15604  params_add (params, "note_id", entity_attribute (entity, "id"));
15605  ret = response_from_entity (connection, credentials, params, entity,
15606  (no_redirect && strcmp (no_redirect, "0")),
15607  NULL, "get_notes",
15608  NULL, "new_note",
15609  "Create Note", response_data);
15610  free_entity (entity);
15611  g_free (response);
15612  return ret;
15613 }
15614 
15625 char *
15626 delete_note_omp (openvas_connection_t *connection, credentials_t * credentials,
15627  params_t *params, cmd_response_data_t* response_data)
15628 {
15629  return delete_resource (connection, "note", credentials, params, 0, NULL,
15630  response_data);
15631 }
15632 
15643 char *
15644 delete_trash_note_omp (openvas_connection_t *connection,
15645  credentials_t * credentials, params_t *params,
15646  cmd_response_data_t* response_data)
15647 {
15648  return delete_resource (connection, "note", credentials, params, 1,
15649  "get_trash", response_data);
15650 }
15651 
15663 char *
15664 edit_note (openvas_connection_t *connection, credentials_t *credentials,
15665  params_t *params, const char *extra_xml,
15666  cmd_response_data_t* response_data)
15667 {
15668  GString *xml;
15669  const char *note_id;
15670 
15671  note_id = params_value (params, "note_id");
15672 
15673  if (openvas_connection_sendf (connection,
15674  "<get_notes"
15675  " note_id=\"%s\""
15676  " details=\"1\""
15677  " result=\"1\"/>",
15678  note_id)
15679  == -1)
15680  {
15681  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15682  return gsad_message (credentials,
15683  "Internal error", __FUNCTION__, __LINE__,
15684  "An internal error occurred while editing a note. "
15685  "The note remains as it was. "
15686  "Diagnostics: Failure to send command to manager daemon.",
15687  "/omp?cmd=get_notes", response_data);
15688  }
15689 
15690  xml = g_string_new ("");
15691 
15692  xml_string_append (xml, "<edit_note>");
15693 
15694  if (extra_xml)
15695  g_string_append (xml, extra_xml);
15696 
15697  if (read_string_c (connection, &xml))
15698  {
15699  g_string_free (xml, TRUE);
15700  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15701  return gsad_message (credentials,
15702  "Internal error", __FUNCTION__, __LINE__,
15703  "An internal error occurred while editing a note. "
15704  "The note remains as it was. "
15705  "Diagnostics: Failure to receive response from manager daemon.",
15706  "/omp?cmd=get_notes", response_data);
15707  }
15708 
15709  /* Cleanup, and return transformed XML. */
15710 
15711  g_string_append (xml, "</edit_note>");
15712  return xsl_transform_omp (connection, credentials, params,
15713  g_string_free (xml, FALSE), response_data);
15714 }
15715 
15726 char *
15727 edit_note_omp (openvas_connection_t *connection, credentials_t *credentials,
15728  params_t *params, cmd_response_data_t* response_data)
15729 {
15730  return edit_note (connection, credentials, params, NULL, response_data);
15731 }
15732 
15743 char *
15744 save_note_omp (openvas_connection_t *connection, credentials_t * credentials,
15745  params_t *params, cmd_response_data_t* response_data)
15746 {
15747  gchar *response;
15748  entity_t entity;
15749  const char *no_redirect;
15750  const char *note_id, *text, *hosts, *port, *severity, *note_task_id;
15751  const char *note_result_id, *active, *days;
15752  char *ret;
15753 
15754  no_redirect = params_value (params, "no_redirect");
15755  note_id = params_value (params, "note_id");
15756 
15757  text = params_value (params, "text");
15758  if (text == NULL)
15759  params_given (params, "text") || (text = "");
15760 
15761  if (params_valid (params, "hosts"))
15762  hosts = params_value (params, "hosts");
15763  else if (strcmp (params_original_value (params, "hosts"), ""))
15764  hosts = NULL;
15765  else
15766  hosts = "";
15767 
15768  if (params_valid (params, "port"))
15769  port = params_value (params, "port");
15770  else if (strcmp (params_original_value (params, "port"), ""))
15771  port = NULL;
15772  else
15773  port = "";
15774 
15775  if (params_valid (params, "severity"))
15776  severity = params_value (params, "severity");
15777  else if (strcmp (params_original_value (params, "severity"), ""))
15778  severity = NULL;
15779  else
15780  severity = "";
15781 
15782  note_task_id = params_value (params, "note_task_id");
15783  note_result_id = params_value (params, "note_result_id");
15784 
15785  active = params_value (params, "active");
15786  days = params_value (params, "days");
15787 
15788  CHECK_PARAM_INVALID (note_task_id, "Save Note", "edit_note");
15789  CHECK_PARAM_INVALID (note_result_id, "Save Note", "edit_note");
15790  CHECK_PARAM_INVALID (active, "Save Note", "edit_note");
15791  CHECK_PARAM_INVALID (note_id, "Save Note", "edit_note");
15792  CHECK_PARAM_INVALID (text, "Save Note", "edit_note");
15793  CHECK_PARAM_INVALID (hosts, "Save Note", "edit_note");
15794  CHECK_PARAM_INVALID (port, "Save Note", "edit_note");
15795  CHECK_PARAM_INVALID (severity, "Save Note", "edit_note");
15796  CHECK_PARAM_INVALID (days, "Save Note", "edit_note");
15797 
15798  response = NULL;
15799  entity = NULL;
15800  switch (ompf (connection, credentials,
15801  &response,
15802  &entity,
15803  response_data,
15804  "<modify_note note_id=\"%s\">"
15805  "<active>%s</active>"
15806  "<hosts>%s</hosts>"
15807  "<port>%s</port>"
15808  "<severity>%s</severity>"
15809  "<text>%s</text>"
15810  "<task id=\"%s\"/>"
15811  "<result id=\"%s\"/>"
15812  "</modify_note>",
15813  note_id,
15814  strcmp (active, "1")
15815  ? active
15816  : (days ? days : "-1"),
15817  hosts ? hosts : "",
15818  port ? port : "",
15819  severity ? severity : "",
15820  text ? text : "",
15821  note_task_id,
15822  note_result_id))
15823  {
15824  case 0:
15825  case -1:
15826  break;
15827  case 1:
15828  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15829  return gsad_message (credentials,
15830  "Internal error", __FUNCTION__, __LINE__,
15831  "An internal error occurred while saving a note. "
15832  "The note remains the same. "
15833  "Diagnostics: Failure to send command to manager daemon.",
15834  "/omp?cmd=get_notes", response_data);
15835  case 2:
15836  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15837  return gsad_message (credentials,
15838  "Internal error", __FUNCTION__, __LINE__,
15839  "An internal error occurred while saving a note. "
15840  "It is unclear whether the note has been saved or not. "
15841  "Diagnostics: Failure to receive response from manager daemon.",
15842  "/omp?cmd=get_notes", response_data);
15843  default:
15844  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15845  return gsad_message (credentials,
15846  "Internal error", __FUNCTION__, __LINE__,
15847  "An internal error occurred while saving a note. "
15848  "It is unclear whether the note has been saved or not. "
15849  "Diagnostics: Internal Error.",
15850  "/omp?cmd=get_notes", response_data);
15851  }
15852 
15853  ret = response_from_entity (connection, credentials, params, entity,
15854  (no_redirect && strcmp (no_redirect, "0")),
15855  NULL, "get_notes",
15856  NULL, "edit_note",
15857  "Save Note", response_data);
15858 
15859  free_entity (entity);
15860  g_free (response);
15861  return ret;
15862 }
15863 
15875 static char *
15876 get_overrides (openvas_connection_t *connection, credentials_t *credentials,
15877  params_t *params, const char *extra_xml,
15878  cmd_response_data_t* response_data)
15879 {
15880  return get_many (connection, "override", credentials, params, extra_xml, NULL,
15881  response_data);
15882 }
15883 
15894 char *
15895 get_overrides_omp (openvas_connection_t *connection, credentials_t *credentials,
15896  params_t *params, cmd_response_data_t* response_data)
15897 {
15898  return get_overrides (connection, credentials, params, NULL, response_data);
15899 }
15900 
15912 static char *
15913 get_override (openvas_connection_t *connection, credentials_t *credentials,
15914  params_t *params, const char *extra_xml,
15915  cmd_response_data_t* response_data)
15916 {
15917  return get_one (connection, "override", credentials, params, extra_xml, NULL,
15918  response_data);
15919 }
15920 
15931 char *
15932 get_override_omp (openvas_connection_t *connection, credentials_t *credentials,
15933  params_t *params, cmd_response_data_t* response_data)
15934 {
15935  return get_override (connection, credentials, params, NULL, response_data);
15936 }
15937 
15949 char *
15950 new_override (openvas_connection_t *connection, credentials_t *credentials,
15951  params_t *params, const char *extra_xml,
15952  cmd_response_data_t* response_data)
15953 {
15954  GString *xml;
15955  const char *oid, *hosts, *port, *severity, *task_id, *task_name, *result_id;
15956  const char *next;
15957  /* Passthroughs. */
15958  const char *report_id, *first_result, *max_results, *sort_field;
15959  const char *sort_order, *levels, *autofp, *notes;
15960  const char *overrides, *result_hosts_only, *search_phrase;
15961 
15962  result_id = params_value (params, "result_id");
15963  task_id = params_value (params, "task_id");
15964 
15965  next = params_value (params, "next");
15966  first_result = params_value (params, "first_result");
15967  max_results = params_value (params, "max_results");
15968  levels = params_value (params, "levels");
15969  autofp = params_value (params, "autofp");
15970  notes = params_value (params, "notes");
15971  report_id = params_value (params, "report_id");
15972  search_phrase = params_value (params, "search_phrase");
15973  sort_field = params_value (params, "sort_field");
15974  sort_order = params_value (params, "sort_order");
15975  task_name = params_value (params, "name");
15976  severity = params_value (params, "severity");
15977  result_hosts_only = params_value (params, "result_hosts_only");
15978 
15979  hosts = params_value (params, "hosts");
15980  oid = params_value (params, "oid");
15981  port = params_value (params, "port");
15982  overrides = params_value (params, "overrides");
15983 
15984  if (result_id == NULL || task_id == NULL)
15985  {
15986  xml = g_string_new ("");
15987 
15988  xml_string_append (xml,
15989  "<new_override>"
15990  "<nvt id=\"%s\"/>"
15991  "<next>%s</next>",
15992  oid ? oid : "0",
15993  next ? next : "");
15994 
15995  if (extra_xml)
15996  g_string_append (xml, extra_xml);
15997 
15998  if (openvas_connection_sendf (connection,
15999  "<get_tasks"
16000  " schedules_only=\"1\""
16001  " details=\"0\"/>")
16002  == -1)
16003  {
16004  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16005  return gsad_message (credentials,
16006  "Internal error", __FUNCTION__, __LINE__,
16007  "An internal error occurred while creating a new override. "
16008  "No new override was created. "
16009  "Diagnostics: Failure to send command to manager daemon.",
16010  "/omp?cmd=get_overrides", response_data);
16011  }
16012 
16013  if (read_string_c (connection, &xml))
16014  {
16015  g_string_free (xml, TRUE);
16016  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16017  return gsad_message (credentials,
16018  "Internal error", __FUNCTION__, __LINE__,
16019  "An internal error occurred while creating a new override. "
16020  "No new override was created. "
16021  "Diagnostics: Failure to receive response from manager daemon.",
16022  "/omp?cmd=get_overrides", response_data);
16023  }
16024 
16025  g_string_append (xml, "</new_override>");
16026  return xsl_transform_omp (connection, credentials, params,
16027  g_string_free (xml, FALSE), response_data);
16028  }
16029 
16030  if (openvas_connection_sendf (connection,
16031  "<get_results"
16032  " result_id=\"%s\""
16033  " task_id=\"%s\""
16034  " notes_details=\"1\""
16035  " notes=\"1\""
16036  " overrides_details=\"1\""
16037  " overrides=\"1\""
16038  " result_hosts_only=\"1\"/>",
16039  result_id,
16040  task_id)
16041  == -1)
16042  {
16043  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16044  return gsad_message (credentials,
16045  "Internal error", __FUNCTION__, __LINE__,
16046  "An internal error occurred while creating a new override. "
16047  "No new override was created. "
16048  "Diagnostics: Failure to send command to manager daemon.",
16049  "/omp?cmd=get_overrides", response_data);
16050  }
16051 
16052  xml = g_string_new ("");
16053 
16054  xml_string_append (xml,
16055  "<new_override>"
16056  "<nvt id=\"%s\"/>"
16057  "<hosts>%s</hosts>"
16058  "<port>%s</port>"
16059  "<severity>%s</severity>"
16060  "<task id=\"%s\">"
16061  "<name>%s</name>"
16062  "</task>"
16063  "<result id=\"%s\"/>"
16064  "<next>%s</next>"
16065  /* Passthroughs. */
16066  "<report id=\"%s\"/>"
16067  "<first_result>%s</first_result>"
16068  "<max_results>%s</max_results>"
16069  "<sort_field>%s</sort_field>"
16070  "<sort_order>%s</sort_order>"
16071  "<levels>%s</levels>"
16072  "<autofp>%s</autofp>"
16073  "<notes>%s</notes>"
16074  "<overrides>%s</overrides>"
16075  "<result_hosts_only>%s</result_hosts_only>"
16076  "<search_phrase>%s</search_phrase>",
16077  oid,
16078  hosts,
16079  /* port is NULL for CVE scan results. */
16080  port ? port : "",
16081  severity,
16082  task_id,
16083  task_name,
16084  result_id,
16085  next ? next : "",
16086  report_id,
16087  first_result,
16088  max_results,
16089  sort_field,
16090  sort_order,
16091  levels,
16092  autofp,
16093  notes,
16094  overrides,
16095  result_hosts_only,
16096  search_phrase);
16097 
16098  if (extra_xml)
16099  g_string_append (xml, extra_xml);
16100 
16101  if (read_string_c (connection, &xml))
16102  {
16103  g_string_free (xml, TRUE);
16104  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16105  return gsad_message (credentials,
16106  "Internal error", __FUNCTION__, __LINE__,
16107  "An internal error occurred while creating a new override. "
16108  "It is unclear whether the override has been created or not. "
16109  "Diagnostics: Failure to receive response from manager daemon.",
16110  "/omp?cmd=get_overrides", response_data);
16111  }
16112 
16113  /* Cleanup, and return transformed XML. */
16114 
16115  g_string_append (xml, "</new_override>");
16116  return xsl_transform_omp (connection, credentials, params,
16117  g_string_free (xml, FALSE), response_data);
16118 }
16119 
16130 char *
16131 new_override_omp (openvas_connection_t *connection, credentials_t *credentials,
16132  params_t *params, cmd_response_data_t* response_data)
16133 {
16134  return new_override (connection, credentials, params, NULL, response_data);
16135 }
16136 
16147 char *
16148 create_override_omp (openvas_connection_t *connection,
16149  credentials_t *credentials, params_t *params,
16150  cmd_response_data_t* response_data)
16151 {
16152  char *ret;
16153  gchar *response;
16154  const char *no_redirect;
16155  const char *oid, *severity, *custom_severity, *new_severity, *port, *hosts;
16156  const char *text, *task_id, *override_result_id;
16157  /* For get_report. */
16158  const char *active, *days;
16159  entity_t entity;
16160 
16161  no_redirect = params_value (params, "no_redirect");
16162  oid = params_value (params, "oid");
16163  CHECK_PARAM_INVALID (oid, "Create Override", "new_override");
16164 
16165  if (params_valid (params, "severity"))
16166  severity = params_value (params, "severity");
16167  else if (params_given (params, "severity")
16168  && strcmp (params_original_value (params, "severity"), ""))
16169  severity = NULL;
16170  else
16171  severity = "";
16172  CHECK_PARAM_INVALID (severity, "Create Override", "new_override");
16173 
16174  custom_severity = params_value (params, "custom_severity");
16175  CHECK_PARAM_INVALID (custom_severity, "Create Override", "new_override");
16176 
16177  if (custom_severity != NULL && strcmp (custom_severity, "0"))
16178  {
16179  if (params_valid (params, "new_severity"))
16180  new_severity = params_value (params, "new_severity");
16181  else if (params_original_value (params, "new_severity") == NULL
16182  || strcmp (params_original_value (params, "new_severity"), ""))
16183  new_severity = NULL;
16184  else
16185  new_severity = "";
16186  CHECK_PARAM_INVALID (new_severity, "Create Override", "new_override");
16187  }
16188  else
16189  {
16190  if (params_valid (params, "new_severity_from_list"))
16191  new_severity = params_value (params, "new_severity_from_list");
16192  else if (params_original_value (params, "new_severity_from_list") == NULL
16193  || strcmp (params_original_value (params,
16194  "new_severity_from_list"),
16195  ""))
16196  new_severity = NULL;
16197  else
16198  new_severity = "";
16199  CHECK_PARAM_INVALID (new_severity, "Create Override", "new_override");
16200  }
16201 
16202  port = params_value (params, "port");
16203  if (port == NULL)
16204  port = "";
16205  if (strcmp (port, "--") == 0)
16206  {
16207  int num = -1;
16208 
16209  port = params_value (params, "port_manual");
16210  if (port)
16211  num = atoi (port);
16212  if (num < 0 || num > 65535)
16213  port = NULL;
16214  }
16215  CHECK_PARAM_INVALID (port, "Create Override", "new_override");
16216 
16217  if (params_valid (params, "hosts"))
16218  {
16219  hosts = params_value (params, "hosts");
16220  if (strcmp (hosts, "--") == 0)
16221  {
16222  if (params_valid (params, "hosts_manual"))
16223  hosts = params_value (params, "hosts_manual");
16224  else if (params_given (params, "hosts_manual")
16225  && strcmp (params_original_value (params, "hosts_manual"),
16226  ""))
16227  hosts = NULL;
16228  else
16229  hosts = "";
16230  }
16231  }
16232  else if (strcmp (params_original_value (params, "hosts"), ""))
16233  hosts = NULL;
16234  else
16235  hosts = "";
16236  CHECK_PARAM_INVALID (hosts, "Create Override", "new_override");
16237 
16238  if (params_valid (params, "override_task_id"))
16239  {
16240  task_id = params_value (params, "override_task_id");
16241  if (task_id && (strcmp (task_id, "0") == 0))
16242  task_id = params_value (params, "override_task_uuid");
16243  }
16244  else
16245  task_id = "";
16246 
16247  active = params_value (params, "active");
16248  CHECK_PARAM_INVALID (active, "Create Override", "new_override");
16249 
16250  text = params_value (params, "text");
16251  days = params_value (params, "days");
16252 
16253  override_result_id = params_value (params, "override_result_id");
16254  if (override_result_id && (strcmp (override_result_id, "0") == 0))
16255  override_result_id = params_value (params, "override_result_uuid");
16256 
16257  response = NULL;
16258  entity = NULL;
16259  switch (ompf (connection, credentials,
16260  &response,
16261  &entity,
16262  response_data,
16263  "<create_override>"
16264  "<active>%s</active>"
16265  "<nvt oid=\"%s\"/>"
16266  "<hosts>%s</hosts>"
16267  "<port>%s</port>"
16268  "<severity>%s</severity>"
16269  "<new_severity>%s</new_severity>"
16270  "<text>%s</text>"
16271  "<task id=\"%s\"/>"
16272  "<result id=\"%s\"/>"
16273  "</create_override>",
16274  strcmp (active, "1")
16275  ? active
16276  : (days ? days : "-1"),
16277  oid,
16278  hosts,
16279  port,
16280  severity,
16281  new_severity,
16282  text ? text : "",
16283  task_id,
16284  override_result_id))
16285  {
16286  case 0:
16287  case -1:
16288  break;
16289  case 1:
16290  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16291  return gsad_message (credentials,
16292  "Internal error", __FUNCTION__, __LINE__,
16293  "An internal error occurred while creating a new override. "
16294  "No new override was created. "
16295  "Diagnostics: Failure to send command to manager daemon.",
16296  "/omp?cmd=get_overrides", response_data);
16297  case 2:
16298  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16299  return gsad_message (credentials,
16300  "Internal error", __FUNCTION__, __LINE__,
16301  "An internal error occurred while creating a new override. "
16302  "It is unclear whether the override has been created or not. "
16303  "Diagnostics: Failure to receive response from manager daemon.",
16304  "/omp?cmd=get_overrides", response_data);
16305  default:
16306  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16307  return gsad_message (credentials,
16308  "Internal error", __FUNCTION__, __LINE__,
16309  "An internal error occurred while creating a new override. "
16310  "It is unclear whether the override has been created or not. "
16311  "Diagnostics: Internal Error.",
16312  "/omp?cmd=get_overrides", response_data);
16313  }
16314 
16315  if (entity_attribute (entity, "id"))
16316  params_add (params, "override_id", entity_attribute (entity, "id"));
16317  ret = response_from_entity (connection, credentials, params, entity,
16318  (no_redirect && strcmp (no_redirect, "0")),
16319  NULL, "get_overrides",
16320  NULL, "new_override",
16321  "Create Override", response_data);
16322  free_entity (entity);
16323  g_free (response);
16324  return ret;
16325 }
16326 
16337 char *
16338 delete_override_omp (openvas_connection_t *connection,
16339  credentials_t * credentials, params_t *params,
16340  cmd_response_data_t* response_data)
16341 {
16342  return delete_resource (connection, "override", credentials, params, 0, NULL,
16343  response_data);
16344 }
16345 
16356 char *
16357 delete_trash_override_omp (openvas_connection_t *connection,
16358  credentials_t * credentials, params_t *params,
16359  cmd_response_data_t* response_data)
16360 {
16361  return delete_resource (connection, "override", credentials, params, 1,
16362  "get_trash", response_data);
16363 }
16364 
16376 char *
16377 edit_override (openvas_connection_t *connection, credentials_t *credentials,
16378  params_t *params, const char *extra_xml,
16379  cmd_response_data_t* response_data)
16380 {
16381  GString *xml;
16382  const char *override_id;
16383 
16384  override_id = params_value (params, "override_id");
16385 
16386  if (openvas_connection_sendf (connection,
16387  "<get_overrides"
16388  " override_id=\"%s\""
16389  " details=\"1\""
16390  " result=\"1\"/>",
16391  override_id)
16392  == -1)
16393  {
16394  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16395  return gsad_message (credentials,
16396  "Internal error", __FUNCTION__, __LINE__,
16397  "An internal error occurred while editing an override. "
16398  "The override remains as it was. "
16399  "Diagnostics: Failure to send command to manager daemon.",
16400  "/omp?cmd=get_overrides", response_data);
16401  }
16402 
16403  xml = g_string_new ("");
16404 
16405  xml_string_append (xml, "<edit_override>");
16406 
16407  if (extra_xml)
16408  g_string_append (xml, extra_xml);
16409 
16410  if (read_string_c (connection, &xml))
16411  {
16412  g_string_free (xml, TRUE);
16413  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16414  return gsad_message (credentials,
16415  "Internal error", __FUNCTION__, __LINE__,
16416  "An internal error occurred while editing an override. "
16417  "The override remains as it was. "
16418  "Diagnostics: Failure to receive response from manager daemon.",
16419  "/omp?cmd=get_overrides", response_data);
16420  }
16421 
16422  /* Cleanup, and return transformed XML. */
16423 
16424  g_string_append (xml, "</edit_override>");
16425  return xsl_transform_omp (connection, credentials, params,
16426  g_string_free (xml, FALSE), response_data);
16427 }
16428 
16439 char *
16440 edit_override_omp (openvas_connection_t *connection, credentials_t *credentials,
16441  params_t *params, cmd_response_data_t* response_data)
16442 {
16443  return edit_override (connection, credentials, params, NULL, response_data);
16444 }
16445 
16456 char *
16457 save_override_omp (openvas_connection_t *connection,
16458  credentials_t * credentials, params_t *params,
16459  cmd_response_data_t* response_data)
16460 {
16461  gchar *response;
16462  entity_t entity;
16463  const char *no_redirect, *override_id, *text, *hosts, *port;
16464  const char *severity, *custom_severity, *new_severity;
16465  const char *override_task_id, *override_result_id, *active, *days;
16466  char *ret;
16467 
16468  no_redirect = params_value (params, "no_redirect");
16469  override_id = params_value (params, "override_id");
16470 
16471  text = params_value (params, "text");
16472  if (text == NULL)
16473  params_given (params, "text") || (text = "");
16474 
16475  if (params_valid (params, "hosts"))
16476  hosts = params_value (params, "hosts");
16477  else if (strcmp (params_original_value (params, "hosts"), ""))
16478  hosts = NULL;
16479  else
16480  hosts = "";
16481 
16482  if (params_valid (params, "port"))
16483  port = params_value (params, "port");
16484  else if (strcmp (params_original_value (params, "port"), ""))
16485  port = NULL;
16486  else
16487  port = "";
16488 
16489  if (params_valid (params, "severity"))
16490  severity = params_value (params, "severity");
16491  else if (strcmp (params_original_value (params, "severity"), ""))
16492  severity = NULL;
16493  else
16494  severity = "";
16495 
16496  custom_severity = params_value (params, "custom_severity");
16497  if (custom_severity && strcmp (custom_severity, "0") != 0)
16498  new_severity = params_value (params, "new_severity");
16499  else
16500  new_severity = params_value (params, "new_severity_from_list");
16501 
16502  override_task_id = params_value (params, "override_task_id");
16503  override_result_id = params_value (params, "override_result_id");
16504 
16505  active = params_value (params, "active");
16506  days = params_value (params, "days");
16507 
16508  CHECK_PARAM_INVALID (override_task_id, "Save Override", "edit_override");
16509  CHECK_PARAM_INVALID (override_result_id, "Save Override", "edit_override");
16510  CHECK_PARAM_INVALID (active, "Save Override", "edit_override");
16511  CHECK_PARAM_INVALID (override_id, "Save Override", "edit_override");
16512  CHECK_PARAM_INVALID (text, "Save Override", "edit_override");
16513  CHECK_PARAM_INVALID (hosts, "Save Override", "edit_override");
16514  CHECK_PARAM_INVALID (port, "Save Override", "edit_override");
16515  CHECK_PARAM_INVALID (severity, "Save Override", "edit_override");
16516  CHECK_PARAM_INVALID (new_severity, "Save Override", "edit_override");
16517  CHECK_PARAM_INVALID (days, "Save Override", "edit_override");
16518 
16519  response = NULL;
16520  entity = NULL;
16521  switch (ompf (connection, credentials,
16522  &response,
16523  &entity,
16524  response_data,
16525  "<modify_override override_id=\"%s\">"
16526  "<active>%s</active>"
16527  "<hosts>%s</hosts>"
16528  "<port>%s</port>"
16529  "<severity>%s</severity>"
16530  "<new_severity>%s</new_severity>"
16531  "<text>%s</text>"
16532  "<task id=\"%s\"/>"
16533  "<result id=\"%s\"/>"
16534  "</modify_override>",
16535  override_id,
16536  strcmp (active, "1")
16537  ? active
16538  : (days ? days : "-1"),
16539  hosts ? hosts : "",
16540  port ? port : "",
16541  severity ? severity : "",
16542  new_severity,
16543  text ? text : "",
16544  override_task_id,
16545  override_result_id))
16546  {
16547  case 0:
16548  case -1:
16549  break;
16550  case 1:
16551  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16552  return gsad_message (credentials,
16553  "Internal error", __FUNCTION__, __LINE__,
16554  "An internal error occurred while saving a override. "
16555  "The override remains the same. "
16556  "Diagnostics: Failure to send command to manager daemon.",
16557  "/omp?cmd=get_overrides", response_data);
16558  case 2:
16559  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16560  return gsad_message (credentials,
16561  "Internal error", __FUNCTION__, __LINE__,
16562  "An internal error occurred while saving a override. "
16563  "It is unclear whether the override has been saved or not. "
16564  "Diagnostics: Failure to receive response from manager daemon.",
16565  "/omp?cmd=get_overrides", response_data);
16566  default:
16567  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16568  return gsad_message (credentials,
16569  "Internal error", __FUNCTION__, __LINE__,
16570  "An internal error occurred while saving a override. "
16571  "It is unclear whether the override has been saved or not. "
16572  "Diagnostics: Internal Error.",
16573  "/omp?cmd=get_overrides", response_data);
16574  }
16575 
16576  ret = response_from_entity (connection, credentials, params, entity,
16577  (no_redirect && strcmp (no_redirect, "0")),
16578  NULL, "get_overrides",
16579  NULL, "edit_override",
16580  "Save Override", response_data);
16581 
16582  free_entity (entity);
16583  g_free (response);
16584  return ret;
16585 }
16586 
16587 
16588 /* Scanners. */
16589 
16601 static char *
16602 get_scanners (openvas_connection_t *connection, credentials_t *credentials,
16603  params_t *params, const char *extra_xml,
16604  cmd_response_data_t* response_data)
16605 {
16606  return get_many (connection, "scanner", credentials, params, extra_xml, NULL,
16607  response_data);
16608 }
16609 
16620 char *
16621 get_scanners_omp (openvas_connection_t *connection, credentials_t * credentials,
16622  params_t *params, cmd_response_data_t* response_data)
16623 {
16624  return get_scanners (connection, credentials, params, NULL, response_data);
16625 }
16626 
16638 static char *
16639 get_scanner (openvas_connection_t *connection, credentials_t * credentials,
16640  params_t *params, const char *extra_xml,
16641  cmd_response_data_t* response_data)
16642 {
16643  return get_one (connection, "scanner", credentials, params, extra_xml, NULL,
16644  response_data);
16645 }
16646 
16657 char *
16658 get_scanner_omp (openvas_connection_t *connection, credentials_t * credentials,
16659  params_t *params, cmd_response_data_t* response_data)
16660 {
16661  return get_scanner (connection, credentials, params, NULL, response_data);
16662 }
16663 
16677 char *
16678 export_scanner_omp (openvas_connection_t *connection,
16679  credentials_t * credentials, params_t *params,
16680  enum content_type * content_type,
16681  char **content_disposition, gsize *content_length,
16682  cmd_response_data_t* response_data)
16683 {
16684  return export_resource (connection, "scanner", credentials, params,
16685  content_type, content_disposition, content_length,
16686  response_data);
16687 }
16688 
16702 char *
16703 export_scanners_omp (openvas_connection_t *connection,
16704  credentials_t * credentials, params_t *params,
16705  enum content_type * content_type,
16706  char **content_disposition, gsize *content_length,
16707  cmd_response_data_t* response_data)
16708 {
16709  return export_many (connection, "scanner", credentials, params, content_type,
16710  content_disposition, content_length, response_data);
16711 }
16712 
16724 static char *
16725 new_scanner (openvas_connection_t *connection, credentials_t *credentials,
16726  params_t *params, const char *extra_xml,
16727  cmd_response_data_t* response_data)
16728 {
16729  GString *xml;
16730 
16731  if (openvas_connection_sendf (connection,
16732  "<get_credentials"
16733  " filter=\"first=1 rows=-1\" />")
16734  == -1)
16735  {
16736  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16737  return gsad_message (credentials,
16738  "Internal error", __FUNCTION__, __LINE__,
16739  "An internal error occurred while getting the credentials list. "
16740  "Diagnostics: Failure to send command to manager daemon.",
16741  "/omp?cmd=get_tasks", response_data);
16742  }
16743 
16744  xml = g_string_new ("<new_scanner>");
16745  if (extra_xml)
16746  g_string_append (xml, extra_xml);
16747 
16748  if (read_string_c (connection, &xml))
16749  {
16750  g_string_free (xml, TRUE);
16751  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16752  return gsad_message (credentials,
16753  "Internal error", __FUNCTION__, __LINE__,
16754  "An internal error occurred while getting the credentials list. "
16755  "Diagnostics: Failure to receive response from manager daemon.",
16756  "/omp?cmd=get_tasks", response_data);
16757  }
16758 
16759  g_string_append (xml, "</new_scanner>");
16760  return xsl_transform_omp (connection, credentials, params,
16761  g_string_free (xml, FALSE), response_data);
16762 }
16763 
16774 char *
16775 new_scanner_omp (openvas_connection_t *connection, credentials_t *credentials,
16776  params_t *params, cmd_response_data_t* response_data)
16777 {
16778  return new_scanner (connection, credentials, params, NULL, response_data);
16779 }
16780 
16791 char *
16792 verify_scanner_omp (openvas_connection_t *connection,
16793  credentials_t * credentials, params_t *params,
16794  cmd_response_data_t* response_data)
16795 {
16796  gchar *html, *response;
16797  const char *scanner_id, *next;
16798  int ret;
16799  entity_t entity;
16800 
16801  scanner_id = params_value (params, "scanner_id");
16802  CHECK_PARAM (scanner_id, "Verify Scanner", get_scanners);
16803 
16804 
16805  ret = ompf (connection, credentials,
16806  &response,
16807  &entity,
16808  response_data,
16809  "<verify_scanner scanner_id=\"%s\"/>",
16810  scanner_id);
16811 
16812  switch (ret)
16813  {
16814  case 0:
16815  case -1:
16816  break;
16817  case 1:
16818  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16819  return gsad_message (credentials,
16820  "Internal error", __FUNCTION__, __LINE__,
16821  "An internal error occurred while verifying a scanner. "
16822  "The scanner was not verified. "
16823  "Diagnostics: Failure to send command to manager daemon.",
16824  "/omp?cmd=get_scanners", response_data);
16825  case 2:
16826  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16827  return gsad_message (credentials,
16828  "Internal error", __FUNCTION__, __LINE__,
16829  "An internal error occurred while verifying a scanner. "
16830  "It is unclear whether the scanner was verified or not. "
16831  "Diagnostics: Failure to send command to manager daemon.",
16832  "/omp?cmd=get_scanners", response_data);
16833  default:
16834  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16835  return gsad_message (credentials,
16836  "Internal error", __FUNCTION__, __LINE__,
16837  "An internal error occurred while verifying a scanner. "
16838  "It is unclear whether the scanner was verified or not. "
16839  "Diagnostics: Failure to send command to manager daemon.",
16840  "/omp?cmd=get_scanners", response_data);
16841  }
16842 
16843  if (omp_success (entity))
16844  {
16845  html = next_page (connection, credentials, params, response,
16846  response_data);
16847  if (html == NULL)
16848  {
16849  free_entity (entity);
16850  g_free (response);
16851  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16852  return gsad_message (credentials,
16853  "Internal error", __FUNCTION__, __LINE__,
16854  "An internal error occurred while verifying a scanner. "
16855  "It is unclear whether the scanner was verified or not. "
16856  "Diagnostics: Failure to receive response from manager daemon.",
16857  "/omp?cmd=get_scanners", response_data);
16858  }
16859  }
16860  else
16861  {
16862  set_http_status_from_entity (entity, response_data);
16863  next = params_value (params, "next");
16864  if (next && !strcmp (next, "get_scanner"))
16865  html = get_scanner (connection, credentials, params, response,
16866  response_data);
16867  else
16868  html = get_scanners (connection, credentials, params, response,
16869  response_data);
16870  }
16871 
16872  free_entity (entity);
16873  g_free (response);
16874  return html;
16875 }
16876 
16887 char *
16888 create_scanner_omp (openvas_connection_t *connection,
16889  credentials_t * credentials, params_t *params,
16890  cmd_response_data_t* response_data)
16891 {
16892  int ret;
16893  char *html;
16894  gchar *response = NULL;
16895  const char *no_redirect;
16896  const char *name, *comment, *host, *port, *type, *ca_pub, *credential_id;
16897  entity_t entity = NULL;
16898 
16899  no_redirect = params_value (params, "no_redirect");
16900  name = params_value (params, "name");
16901  comment = params_value (params, "comment");
16902  host = params_value (params, "scanner_host");
16903  port = params_value (params, "port");
16904  type = params_value (params, "scanner_type");
16905  ca_pub = params_value (params, "ca_pub");
16906  credential_id = params_value (params, "credential_id");
16907  CHECK_PARAM_INVALID (name, "Create Scanner", "new_scanner");
16908  CHECK_PARAM_INVALID (comment, "Create Scanner", "new_scanner");
16909  CHECK_PARAM_INVALID (host, "Create Scanner", "new_scanner");
16910  CHECK_PARAM_INVALID (port, "Create Scanner", "new_scanner");
16911  CHECK_PARAM_INVALID (type, "Create Scanner", "new_scanner");
16912  if (params_given (params, "ca_pub"))
16913  CHECK_PARAM_INVALID (ca_pub, "Create Scanner", "new_scanner");
16914  CHECK_PARAM_INVALID (credential_id, "Create Scanner", "new_scanner");
16915 
16916  if (ca_pub)
16917  ret = ompf (connection, credentials, &response, &entity, response_data,
16918  "<create_scanner>"
16919  "<name>%s</name><comment>%s</comment>"
16920  "<host>%s</host><port>%s</port><type>%s</type>"
16921  "<ca_pub>%s</ca_pub>"
16922  "<credential id=\"%s\"/>"
16923  "</create_scanner>",
16924  name, comment, host, port, type, ca_pub, credential_id);
16925  else
16926  ret = ompf (connection, credentials, &response, &entity, response_data,
16927  "<create_scanner>"
16928  "<name>%s</name><comment>%s</comment>"
16929  "<host>%s</host><port>%s</port><type>%s</type>"
16930  "<credential id=\"%s\"/>"
16931  "</create_scanner>",
16932  name, comment, host, port, type, credential_id);
16933  switch (ret)
16934  {
16935  case 0:
16936  case -1:
16937  break;
16938  case 1:
16939  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16940  return gsad_message
16941  (credentials, "Internal error", __FUNCTION__, __LINE__,
16942  "An internal error occurred while creating a new scanner. "
16943  "No new scanner was created. "
16944  "Diagnostics: Failure to send command to manager daemon.",
16945  "/omp?cmd=get_scanners", response_data);
16946  case 2:
16947  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16948  return gsad_message
16949  (credentials, "Internal error", __FUNCTION__, __LINE__,
16950  "An internal error occurred while creating a new scanner. "
16951  "It is unclear whether the scanner has been created or not. "
16952  "Diagnostics: Failure to receive response from manager daemon.",
16953  "/omp?cmd=get_scanners", response_data);
16954  default:
16955  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16956  return gsad_message
16957  (credentials, "Internal error", __FUNCTION__, __LINE__,
16958  "An internal error occurred while creating a new scanner. "
16959  "It is unclear whether the scanner has been created or not. "
16960  "Diagnostics: Internal Error.",
16961  "/omp?cmd=get_scanners", response_data);
16962  }
16963 
16964  if (entity_attribute (entity, "id"))
16965  params_add (params, "scanner_id", entity_attribute (entity, "id"));
16966  html = response_from_entity (connection, credentials, params, entity,
16967  (no_redirect && strcmp (no_redirect, "0")),
16968  NULL, "get_scanners",
16969  NULL, "new_scanner",
16970  "Create Scanner", response_data);
16971  free_entity (entity);
16972  g_free (response);
16973  return html;
16974 }
16975 
16986 char *
16987 delete_scanner_omp (openvas_connection_t *connection,
16988  credentials_t * credentials, params_t *params,
16989  cmd_response_data_t* response_data)
16990 {
16991  return delete_resource (connection, "scanner", credentials, params, 0,
16992  "get_scanners", response_data);
16993 }
16994 
17005 char *
17006 delete_trash_scanner_omp (openvas_connection_t *connection,
17007  credentials_t * credentials, params_t *params,
17008  cmd_response_data_t* response_data)
17009 {
17010  return delete_resource (connection, "scanner", credentials, params, 1,
17011  "get_trash", response_data);
17012 }
17013 
17025 char *
17026 edit_scanner (openvas_connection_t *connection, credentials_t * credentials,
17027  params_t *params, const char *extra_xml,
17028  cmd_response_data_t* response_data)
17029 {
17030  GString *xml;
17031  const char *scanner_id, *next;
17032 
17033  scanner_id = params_value (params, "scanner_id");
17034  next = params_value (params, "next");
17035 
17036  if (scanner_id == NULL)
17037  {
17038  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
17039  return gsad_message (credentials,
17040  "Internal error", __FUNCTION__, __LINE__,
17041  "An internal error occurred while editing a scanner. "
17042  "The scanner remains as it was. "
17043  "Diagnostics: Required parameter was NULL.",
17044  "/omp?cmd=get_tasks", response_data);
17045  }
17046 
17047  if (next == NULL)
17048  next = "get_scanner";
17049 
17050  if (openvas_connection_sendf (connection,
17051  "<commands>"
17052  "<get_scanners scanner_id=\"%s\" details=\"1\" />"
17053  "<get_credentials filter=\"first=1 rows=-1\" />"
17054  "</commands>",
17055  scanner_id)
17056  == -1)
17057  {
17058  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17059  return gsad_message (credentials,
17060  "Internal error", __FUNCTION__, __LINE__,
17061  "An internal error occurred while getting scanner info. "
17062  "Diagnostics: Failure to send command to manager daemon.",
17063  "/omp?cmd=get_tasks", response_data);
17064  }
17065 
17066  xml = g_string_new ("");
17067 
17068  if (extra_xml)
17069  g_string_append (xml, extra_xml);
17070 
17071  g_string_append_printf (xml,
17072  "<edit_scanner>"
17073  "<scanner id=\"%s\"/>"
17074  /* Page that follows. */
17075  "<next>%s</next>",
17076  scanner_id,
17077  next);
17078 
17079  if (read_string_c (connection, &xml))
17080  {
17081  g_string_free (xml, TRUE);
17082  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17083  return gsad_message (credentials,
17084  "Internal error", __FUNCTION__, __LINE__,
17085  "An internal error occurred while getting scanner info. "
17086  "Diagnostics: Failure to receive response from manager daemon.",
17087  "/omp?cmd=get_tasks", response_data);
17088  }
17089 
17090  /* Cleanup, and return transformed XML. */
17091 
17092  g_string_append (xml, "</edit_scanner>");
17093  return xsl_transform_omp (connection, credentials, params,
17094  g_string_free (xml, FALSE), response_data);
17095 }
17096 
17107 char *
17108 edit_scanner_omp (openvas_connection_t *connection, credentials_t * credentials,
17109  params_t *params, cmd_response_data_t* response_data)
17110 {
17111  return edit_scanner (connection, credentials, params, NULL, response_data);
17112 }
17113 
17124 char *
17125 save_scanner_omp (openvas_connection_t *connection, credentials_t * credentials,
17126  params_t *params, cmd_response_data_t* response_data)
17127 {
17128  gchar *response = NULL;
17129  entity_t entity = NULL;
17130  const char *no_redirect;
17131  const char *scanner_id, *name, *comment, *port, *host, *type, *ca_pub;
17132  const char *credential_id, *which_cert;
17133  char *html;
17134  int ret, is_unix_socket, in_use;
17135 
17136  no_redirect = params_value (params, "no_redirect");
17137  scanner_id = params_value (params, "scanner_id");
17138  name = params_value (params, "name");
17139  comment = params_value (params, "comment");
17140  host = params_value (params, "scanner_host");
17141  is_unix_socket = (host && *host == '/') ? 1 : 0;
17142  port = params_value (params, "port");
17143  type = params_value (params, "scanner_type");
17144  which_cert = params_value (params, "which_cert");
17145  ca_pub = params_value (params, "ca_pub");
17146  credential_id = params_value (params, "credential_id");
17147  CHECK_PARAM_INVALID (scanner_id, "Edit Scanner", "edit_scanner");
17148  CHECK_PARAM_INVALID (name, "Edit Scanner", "edit_scanner");
17149  if (params_given (params, "scanner_host") == 0)
17150  in_use = 1;
17151  else
17152  {
17153  in_use = 0;
17154  CHECK_PARAM_INVALID (host, "Edit Scanner", "edit_scanner");
17155  CHECK_PARAM_INVALID (port, "Edit Scanner", "edit_scanner");
17156  CHECK_PARAM_INVALID (type, "Edit Scanner", "edit_scanner");
17157  }
17158  if (is_unix_socket == 0)
17159  {
17160  CHECK_PARAM_INVALID (ca_pub, "Edit Scanner", "edit_scanner");
17161  CHECK_PARAM_INVALID (credential_id, "Edit Scanner", "edit_scanner");
17162  CHECK_PARAM_INVALID (which_cert, "Edit Scanner", "edit_scanner");
17163  }
17164 
17165  if (is_unix_socket)
17166  {
17167  ret = ompf (connection, credentials, &response, &entity, response_data,
17168  "<modify_scanner scanner_id=\"%s\">"
17169  "<name>%s</name>"
17170  "<comment>%s</comment>"
17171  "</modify_scanner>",
17172  scanner_id, name, comment ?: "");
17173  }
17174  else if (strcmp (which_cert, "new") == 0
17175  || strcmp (which_cert, "default") == 0)
17176  {
17177  if (ca_pub == NULL)
17178  ca_pub = "";
17179  if (in_use)
17180  ret = ompf (connection, credentials, &response, &entity, response_data,
17181  "<modify_scanner scanner_id=\"%s\">"
17182  "<name>%s</name>"
17183  "<comment>%s</comment>"
17184  "<ca_pub>%s</ca_pub>"
17185  "<credential id=\"%s\"/>"
17186  "</modify_scanner>",
17187  scanner_id, name, comment ?: "",
17188  strcmp (which_cert, "new") == 0 ? ca_pub : "",
17189  credential_id);
17190  else
17191  ret = ompf (connection, credentials, &response, &entity, response_data,
17192  "<modify_scanner scanner_id=\"%s\">"
17193  "<name>%s</name>"
17194  "<comment>%s</comment>"
17195  "<host>%s</host>"
17196  "<port>%s</port>"
17197  "<type>%s</type>"
17198  "<ca_pub>%s</ca_pub>"
17199  "<credential id=\"%s\"/>"
17200  "</modify_scanner>",
17201  scanner_id, name, comment ?: "", host, port, type,
17202  strcmp (which_cert, "new") == 0 ? ca_pub : "",
17203  credential_id);
17204  }
17205  else
17206  {
17207  /* Using existing CA cert. */
17208  if (in_use)
17209  ret = ompf (connection, credentials, &response, &entity, response_data,
17210  "<modify_scanner scanner_id=\"%s\">"
17211  "<name>%s</name>"
17212  "<comment>%s</comment>"
17213  "<credential id=\"%s\"/>"
17214  "</modify_scanner>",
17215  scanner_id, name, comment ?: "", credential_id);
17216  else
17217  ret = ompf (connection, credentials, &response, &entity, response_data,
17218  "<modify_scanner scanner_id=\"%s\">"
17219  "<name>%s</name>"
17220  "<comment>%s</comment>"
17221  "<host>%s</host>"
17222  "<port>%s</port>"
17223  "<type>%s</type>"
17224  "<credential id=\"%s\"/>"
17225  "</modify_scanner>",
17226  scanner_id, name, comment ?: "", host, port, type,
17227  credential_id);
17228  }
17229 
17230  switch (ret)
17231  {
17232  case 0:
17233  case -1:
17234  break;
17235  case 1:
17236  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17237  return gsad_message
17238  (credentials, "Internal error", __FUNCTION__, __LINE__,
17239  "An internal error occurred while saving a scanner. "
17240  "The scanner remains the same. "
17241  "Diagnostics: Failure to send command to manager daemon.",
17242  "/omp?cmd=get_scanners", response_data);
17243  case 2:
17244  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17245  return gsad_message
17246  (credentials, "Internal error", __FUNCTION__, __LINE__,
17247  "An internal error occurred while saving a scanner. "
17248  "It is unclear whether the scanner has been saved or not. "
17249  "Diagnostics: Failure to receive response from manager daemon.",
17250  "/omp?cmd=get_scanners", response_data);
17251  default:
17252  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17253  return gsad_message
17254  (credentials, "Internal error", __FUNCTION__, __LINE__,
17255  "An internal error occurred while saving a scanner. "
17256  "It is unclear whether the scanner has been saved or not. "
17257  "Diagnostics: Internal Error.",
17258  "/omp?cmd=get_scanners", response_data);
17259  }
17260 
17261  html = response_from_entity (connection, credentials, params, entity,
17262  (no_redirect && strcmp (no_redirect, "0")),
17263  NULL, "get_scanners",
17264  NULL, "edit_scanner",
17265  "Save Scanner", response_data);
17266 
17267  free_entity (entity);
17268  g_free (response);
17269  return html;
17270 }
17271 
17272 
17273 /* Schedules. */
17274 
17286 static char *
17287 get_schedule (openvas_connection_t *connection, credentials_t * credentials,
17288  params_t *params, const char *extra_xml,
17289  cmd_response_data_t* response_data)
17290 {
17291  return get_one (connection, "schedule", credentials, params, extra_xml,
17292  "tasks=\"1\"", response_data);
17293 }
17294 
17305 char *
17306 get_schedule_omp (openvas_connection_t *connection, credentials_t * credentials,
17307  params_t *params, cmd_response_data_t* response_data)
17308 {
17309  return get_schedule (connection, credentials, params, NULL, response_data);
17310 }
17311 
17323 static char *
17324 get_schedules (openvas_connection_t *connection, credentials_t *credentials,
17325  params_t *params, const char *extra_xml,
17326  cmd_response_data_t* response_data)
17327 {
17328  return get_many (connection, "schedule", credentials, params, extra_xml, NULL,
17329  response_data);
17330 }
17331 
17342 char *
17343 get_schedules_omp (openvas_connection_t *connection,
17344  credentials_t * credentials, params_t *params,
17345  cmd_response_data_t* response_data)
17346 {
17347  return get_schedules (connection, credentials, params, NULL, response_data);
17348 }
17349 
17361 static char *
17362 new_schedule (openvas_connection_t *connection, credentials_t *credentials,
17363  params_t *params, const char *extra_xml,
17364  cmd_response_data_t* response_data)
17365 {
17366  GString *xml;
17367  time_t now;
17368  struct tm *now_broken;
17369  xml = g_string_new ("<new_schedule>");
17370  now = time (NULL);
17371  now_broken = localtime (&now);
17372  g_string_append_printf (xml,
17373  "<time>"
17374  "<minute>%s%i</minute>"
17375  "<hour>%s%i</hour>"
17376  "<day_of_month>%s%i</day_of_month>"
17377  "<month>%s%i</month>"
17378  "<year>%i</year>"
17379  "</time>",
17380  (now_broken->tm_min > 9 ? "" : "0"),
17381  now_broken->tm_min,
17382  (now_broken->tm_hour > 9 ? "" : "0"),
17383  now_broken->tm_hour,
17384  (now_broken->tm_mday > 9 ? "" : "0"),
17385  now_broken->tm_mday,
17386  ((now_broken->tm_mon + 1) > 9 ? "" : "0"),
17387  (now_broken->tm_mon + 1),
17388  (now_broken->tm_year + 1900));
17389 
17390  if (extra_xml)
17391  g_string_append (xml, extra_xml);
17392  g_string_append (xml, "</new_schedule>");
17393  return xsl_transform_omp (connection, credentials, params,
17394  g_string_free (xml, FALSE), response_data);
17395 }
17396 
17407 char *
17408 new_schedule_omp (openvas_connection_t *connection, credentials_t *credentials,
17409  params_t *params, cmd_response_data_t* response_data)
17410 {
17411  return new_schedule (connection, credentials, params, NULL, response_data);
17412 }
17413 
17424 char *
17425 create_schedule_omp (openvas_connection_t *connection,
17426  credentials_t * credentials, params_t *params,
17427  cmd_response_data_t* response_data)
17428 {
17429  char *ret;
17430  gchar *response;
17431  const char *no_redirect;
17432  const char *name, *comment, *hour, *minute, *day_of_month, *month, *year;
17433  const char *period, *period_unit, *duration, *duration_unit, *timezone;
17434  entity_t entity;
17435 
17436  no_redirect = params_value (params, "no_redirect");
17437  name = params_value (params, "name");
17438  comment = params_value (params, "comment");
17439  hour = params_value (params, "hour");
17440  minute = params_value (params, "minute");
17441  day_of_month = params_value (params, "day_of_month");
17442  duration = params_value (params, "duration");
17443  duration_unit = params_value (params, "duration_unit");
17444  month = params_value (params, "month");
17445  period = params_value (params, "period");
17446  period_unit = params_value (params, "period_unit");
17447  year = params_value (params, "year");
17448  timezone = params_value (params, "timezone");
17449 
17450  CHECK_PARAM_INVALID (name, "Create Schedule", "new_schedule");
17451  CHECK_PARAM_INVALID (comment, "Create Schedule", "new_schedule");
17452  CHECK_PARAM_INVALID (hour, "Create Schedule", "new_schedule");
17453  CHECK_PARAM_INVALID (minute, "Create Schedule", "new_schedule");
17454  CHECK_PARAM_INVALID (day_of_month, "Create Schedule", "new_schedule");
17455  CHECK_PARAM_INVALID (duration, "Create Schedule", "new_schedule");
17456  CHECK_PARAM_INVALID (duration_unit, "Create Schedule", "new_schedule");
17457  CHECK_PARAM_INVALID (month, "Create Schedule", "new_schedule");
17458  CHECK_PARAM_INVALID (period, "Create Schedule", "new_schedule");
17459  CHECK_PARAM_INVALID (period_unit, "period_unit", "new_schedule");
17460  CHECK_PARAM_INVALID (year, "Create Schedule", "new_schedule");
17461  CHECK_PARAM_INVALID (timezone, "Create Schedule", "new_schedule");
17462 
17463  response = NULL;
17464  entity = NULL;
17465  switch (ompf (connection, credentials,
17466  &response,
17467  &entity,
17468  response_data,
17469  "<create_schedule>"
17470  "<name>%s</name>"
17471  "<comment>%s</comment>"
17472  "<first_time>"
17473  "<hour>%s</hour>"
17474  "<minute>%s</minute>"
17475  "<day_of_month>%s</day_of_month>"
17476  "<month>%s</month>"
17477  "<year>%s</year>"
17478  "</first_time>"
17479  "<period>"
17480  "<unit>%s</unit>"
17481  "%s"
17482  "</period>"
17483  "<duration>"
17484  "<unit>%s</unit>"
17485  "%s"
17486  "</duration>"
17487  "<timezone>%s</timezone>"
17488  "</create_schedule>",
17489  name,
17490  comment,
17491  hour,
17492  minute,
17493  day_of_month,
17494  month,
17495  year,
17496  (strcmp (period_unit, "")
17497  ? period_unit
17498  : "second"),
17499  period,
17500  (strcmp (duration_unit, "")
17501  ? duration_unit
17502  : "second"),
17503  duration,
17504  timezone))
17505  {
17506  case 0:
17507  case -1:
17508  break;
17509  case 1:
17510  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17511  return gsad_message (credentials,
17512  "Internal error", __FUNCTION__, __LINE__,
17513  "An internal error occurred while creating a new schedule. "
17514  "No new schedule was created. "
17515  "Diagnostics: Failure to send command to manager daemon.",
17516  "/omp?cmd=get_schedules", response_data);
17517  case 2:
17518  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17519  return gsad_message (credentials,
17520  "Internal error", __FUNCTION__, __LINE__,
17521  "An internal error occurred while creating a new schedule. "
17522  "It is unclear whether the schedule has been created or not. "
17523  "Diagnostics: Failure to receive response from manager daemon.",
17524  "/omp?cmd=get_schedules", response_data);
17525  default:
17526  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17527  return gsad_message (credentials,
17528  "Internal error", __FUNCTION__, __LINE__,
17529  "An internal error occurred while creating a new schedule. "
17530  "It is unclear whether the schedule has been created or not. "
17531  "Diagnostics: Internal Error.",
17532  "/omp?cmd=get_schedules", response_data);
17533  }
17534 
17535  if (entity_attribute (entity, "id"))
17536  params_add (params, "schedule_id", entity_attribute (entity, "id"));
17537  ret = response_from_entity (connection, credentials, params, entity,
17538  (no_redirect && strcmp (no_redirect, "0")),
17539  NULL, "get_schedules",
17540  NULL, "new_schedule",
17541  "Create Schedule", response_data);
17542  free_entity (entity);
17543  g_free (response);
17544  return ret;
17545 }
17546 
17557 char *
17558 delete_schedule_omp (openvas_connection_t *connection,
17559  credentials_t * credentials, params_t *params,
17560  cmd_response_data_t* response_data)
17561 {
17562  return delete_resource (connection, "schedule", credentials, params, 0,
17563  "get_schedules", response_data);
17564 }
17565 
17576 char *
17577 get_system_reports_omp (openvas_connection_t *connection,
17578  credentials_t * credentials, params_t *params,
17579  cmd_response_data_t* response_data)
17580 {
17581  GString *xml;
17582  time_t now, duration, duration_start;
17583  struct tm start_time, end_time;
17584 
17585  const char *slave_id, *given_duration, *range_type;
17586  const char *start_year, *start_month, *start_day, *start_hour, *start_minute;
17587  const char *end_year, *end_month, *end_day, *end_hour, *end_minute;
17588 
17589  slave_id = params_value (params, "slave_id");
17590 
17591  now = time (NULL);
17592 
17593  given_duration = params_value (params, "duration");
17594  range_type = params_value (params, "range_type");
17595  if (!range_type)
17596  range_type = "duration";
17597 
17598  duration = given_duration ? atoi (given_duration) : 86400;
17599  duration_start = now - duration;
17600 
17601  xml = g_string_new ("<get_system_reports>");
17602 
17603  g_string_append_printf (xml,
17604  "<slave id=\"%s\"/>",
17605  slave_id ? slave_id : "0");
17606  if (strcmp (range_type, "duration") == 0)
17607  {
17608  struct tm *time_broken;
17609  time_broken = localtime (&now);
17610  end_time.tm_year = time_broken->tm_year;
17611  end_time.tm_mon = time_broken->tm_mon;
17612  end_time.tm_mday = time_broken->tm_mday;
17613  end_time.tm_hour = time_broken->tm_hour;
17614  end_time.tm_min = time_broken->tm_min;
17615 
17616  time_broken = localtime (&duration_start);
17617  start_time.tm_year = time_broken->tm_year;
17618  start_time.tm_mon = time_broken->tm_mon;
17619  start_time.tm_mday = time_broken->tm_mday;
17620  start_time.tm_hour = time_broken->tm_hour;
17621  start_time.tm_min = time_broken->tm_min;
17622 
17623  g_string_append_printf (xml,
17624  "<duration>%ld</duration>",
17625  duration);
17626  }
17627  else
17628  {
17629  struct tm *time_broken;
17630  time_broken = localtime (&now);
17631 
17632  start_year = params_value (params, "start_year");
17633  start_month = params_value (params, "start_month");
17634  start_day = params_value (params, "start_day");
17635  start_hour = params_value (params, "start_hour");
17636  start_minute = params_value (params, "start_minute");
17637 
17638  end_year = params_value (params, "end_year");
17639  end_month = params_value (params, "end_month");
17640  end_day = params_value (params, "end_day");
17641  end_hour = params_value (params, "end_hour");
17642  end_minute = params_value (params, "end_minute");
17643 
17644  start_time.tm_year = start_year ? atoi (start_year) - 1900
17645  : time_broken->tm_year;
17646  start_time.tm_mon = start_month ? atoi (start_month) - 1
17647  : time_broken->tm_mon;
17648  start_time.tm_mday = start_day ? atoi (start_day)
17649  : time_broken->tm_mday;
17650  start_time.tm_hour = start_hour ? atoi (start_hour)
17651  : time_broken->tm_hour;
17652  start_time.tm_min = start_minute ? atoi (start_minute)
17653  : time_broken->tm_min;
17654 
17655  end_time.tm_year = end_year ? atoi (end_year) - 1900
17656  : time_broken->tm_year;
17657  end_time.tm_mon = end_month ? atoi (end_month) - 1
17658  : time_broken->tm_mon;
17659  end_time.tm_mday = end_day ? atoi (end_day)
17660  : time_broken->tm_mday;
17661  end_time.tm_hour = end_hour ? atoi (end_hour)
17662  : time_broken->tm_hour;
17663  end_time.tm_min = end_minute ? atoi (end_minute)
17664  : time_broken->tm_min;
17665  }
17666 
17667  g_string_append_printf (xml,
17668  "<start_time>"
17669  "<minute>%i</minute>"
17670  "<hour>%i</hour>"
17671  "<day_of_month>%i</day_of_month>"
17672  "<month>%i</month>"
17673  "<year>%i</year>"
17674  "</start_time>",
17675  start_time.tm_min,
17676  start_time.tm_hour,
17677  start_time.tm_mday,
17678  start_time.tm_mon + 1,
17679  start_time.tm_year + 1900);
17680 
17681  g_string_append_printf (xml,
17682  "<end_time>"
17683  "<minute>%i</minute>"
17684  "<hour>%i</hour>"
17685  "<day_of_month>%i</day_of_month>"
17686  "<month>%i</month>"
17687  "<year>%i</year>"
17688  "</end_time>",
17689  end_time.tm_min,
17690  end_time.tm_hour,
17691  end_time.tm_mday,
17692  end_time.tm_mon + 1,
17693  end_time.tm_year + 1900);
17694 
17695  /* Get the system reports. */
17696 
17697  if (openvas_connection_sendf (connection,
17698  "<get_system_reports brief=\"1\" slave_id=\"%s\"/>",
17699  slave_id ? slave_id : "0")
17700  == -1)
17701  {
17702  g_string_free (xml, TRUE);
17703  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17704  return gsad_message (credentials,
17705  "Internal error", __FUNCTION__, __LINE__,
17706  "An internal error occurred while getting the system reports. "
17707  "The current list of system reports is not available. "
17708  "Diagnostics: Failure to send command to manager daemon.",
17709  "/omp?cmd=get_tasks", response_data);
17710  }
17711 
17712  if (read_string_c (connection, &xml))
17713  {
17714  g_string_free (xml, TRUE);
17715  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17716  return gsad_message (credentials,
17717  "Internal error", __FUNCTION__, __LINE__,
17718  "An internal error occurred while getting the system reports. "
17719  "The current list of system reports is not available. "
17720  "Diagnostics: Failure to receive response from manager daemon.",
17721  "/omp?cmd=get_tasks", response_data);
17722  }
17723 
17724  if (command_enabled (credentials, "GET_SCANNERS"))
17725  {
17726  /* Get the OMP scanners. */
17727 
17728  if (openvas_connection_sendf (connection,
17729  "<get_scanners"
17730  " filter=\"sort=name rows=-1 type=4\"/>")
17731  == -1)
17732  {
17733  g_string_free (xml, TRUE);
17734  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17735  return gsad_message (credentials,
17736  "Internal error", __FUNCTION__, __LINE__,
17737  "An internal error occurred while getting the system reports. "
17738  "The current list of system reports is not available. "
17739  "Diagnostics: Failure to send command to manager daemon.",
17740  "/omp?cmd=get_tasks", response_data);
17741  }
17742 
17743  if (read_string_c (connection, &xml))
17744  {
17745  g_string_free (xml, TRUE);
17746  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17747  return gsad_message (credentials,
17748  "Internal error", __FUNCTION__, __LINE__,
17749  "An internal error occurred while getting the system reports. "
17750  "The current list of system reports is not available. "
17751  "Diagnostics: Failure to receive response from manager daemon.",
17752  "/omp?cmd=get_tasks", response_data);
17753  }
17754  }
17755 
17756  /* Cleanup, and return transformed XML. */
17757 
17758  g_string_append (xml, "</get_system_reports>");
17759  return xsl_transform_omp (connection, credentials, params,
17760  g_string_free (xml, FALSE), response_data);
17761 }
17762 
17776 char *
17777 get_system_report_omp (openvas_connection_t *connection,
17778  credentials_t *credentials, const char *url,
17779  params_t *params,
17780  enum content_type *content_type,
17781  gsize *content_length,
17782  cmd_response_data_t* response_data)
17783 {
17784  entity_t entity;
17785  entity_t report_entity;
17786  char name[501];
17787  time_t now;
17788  struct tm *now_broken;
17789  const char *slave_id, *duration;
17790  const char *start_year, *start_month, *start_day, *start_hour, *start_minute;
17791  const char *end_year, *end_month, *end_day, *end_hour, *end_minute;
17792  struct tm start_time, end_time;
17793  gchar *start_time_str, *end_time_str;
17794  gchar *omp_command;
17795 
17796  *content_length = 0;
17797 
17798  if (url == NULL)
17799  return NULL;
17800 
17801  slave_id = params_value (params, "slave_id");
17802 
17803  /* fan/report.png */
17804  if (sscanf (url, "%500[^ /]./report.png", name) == 1)
17805  {
17806  duration = params_value (params, "duration");
17807 
17808  if (duration && strcmp (duration, ""))
17809  {
17810  omp_command = g_markup_printf_escaped ("<get_system_reports"
17811  " name=\"%s\""
17812  " duration=\"%s\""
17813  " slave_id=\"%s\"/>",
17814  name,
17815  duration,
17816  slave_id ? slave_id : "0");
17817  }
17818  else
17819  {
17820  now = time (NULL);
17821  now_broken = localtime (&now);
17822 
17823  start_year = params_value (params, "start_year");
17824  start_month = params_value (params, "start_month");
17825  start_day = params_value (params, "start_day");
17826  start_hour = params_value (params, "start_hour");
17827  start_minute = params_value (params, "start_minute");
17828 
17829  end_year = params_value (params, "end_year");
17830  end_month = params_value (params, "end_month");
17831  end_day = params_value (params, "end_day");
17832  end_hour = params_value (params, "end_hour");
17833  end_minute = params_value (params, "end_minute");
17834 
17835  start_time.tm_year = start_year ? atoi (start_year) - 1900
17836  : now_broken->tm_year;
17837  start_time.tm_mon = start_month ? atoi (start_month) - 1
17838  : now_broken->tm_mon;
17839  start_time.tm_mday = start_day ? atoi (start_day)
17840  : now_broken->tm_mday;
17841  start_time.tm_hour = start_hour ? atoi (start_hour)
17842  : now_broken->tm_hour;
17843  start_time.tm_min = start_minute ? atoi (start_minute)
17844  : now_broken->tm_min;
17845  start_time.tm_zone = now_broken->tm_zone;
17846 
17847  end_time.tm_year = end_year ? atoi (end_year) - 1900
17848  : now_broken->tm_year;
17849  end_time.tm_mon = end_month ? atoi (end_month) - 1
17850  : now_broken->tm_mon;
17851  end_time.tm_mday = end_day ? atoi (end_day) : now_broken->tm_mday;
17852  end_time.tm_hour = end_hour ? atoi (end_hour) : now_broken->tm_hour;
17853  end_time.tm_min = end_minute ? atoi (end_minute) : now_broken->tm_min;
17854  end_time.tm_zone = now_broken->tm_zone;
17855 
17856 
17857 
17858  start_time_str
17859  = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:00",
17860  start_time.tm_year + 1900,
17861  start_time.tm_mon + 1,
17862  start_time.tm_mday,
17863  start_time.tm_hour,
17864  start_time.tm_min);
17865 
17866  end_time_str
17867  = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:00",
17868  end_time.tm_year + 1900,
17869  end_time.tm_mon + 1,
17870  end_time.tm_mday,
17871  end_time.tm_hour,
17872  end_time.tm_min);
17873 
17874  omp_command
17875  = g_markup_printf_escaped ("<get_system_reports"
17876  " name=\"%s\""
17877  " start_time=\"%s\""
17878  " end_time=\"%s\""
17879  " slave_id=\"%s\"/>",
17880  name,
17881  start_time_str,
17882  end_time_str,
17883  slave_id ? slave_id : "0");
17884  g_free (start_time_str);
17885  g_free (end_time_str);
17886  }
17887 
17888  if (openvas_connection_sendf (connection,
17889  "%s",
17890  omp_command)
17891  == -1)
17892  {
17893  g_free (omp_command);
17894  return NULL;
17895  }
17896  g_free (omp_command);
17897 
17898  entity = NULL;
17899  if (read_entity_c (connection, &entity))
17900  {
17901  return NULL;
17902  }
17903 
17904  report_entity = entity_child (entity, "system_report");
17905  if (report_entity == NULL)
17906  {
17907  free_entity (entity);
17908  return NULL;
17909  }
17910 
17911  report_entity = entity_child (report_entity, "report");
17912  if (report_entity == NULL)
17913  {
17914  free_entity (entity);
17915  return NULL;
17916  }
17917  else
17918  {
17919  char *content_64 = entity_text (report_entity);
17920  char *content = NULL;
17921 
17922  if (content_64 && strlen (content_64))
17923  {
17924  content = (char *) g_base64_decode (content_64,
17925  content_length);
17926 
17927 #if 1
17928  *content_type = GSAD_CONTENT_TYPE_IMAGE_PNG;
17929  //*content_disposition = g_strdup_printf ("attachment; filename=\"xxx.png\"");
17930 #else
17931  g_free (content);
17932  content = g_strdup ("helo");
17933 #endif
17934  }
17935 
17936  free_entity (entity);
17937  return content;
17938  }
17939  }
17940 
17941  return NULL;
17942 }
17943 
17955 static char *
17956 get_report_format (openvas_connection_t *connection,
17957  credentials_t * credentials, params_t *params,
17958  const char *extra_xml, cmd_response_data_t* response_data)
17959 {
17960  return get_one (connection, "report_format", credentials, params, extra_xml,
17961  "alerts =\"1\" params=\"1\"", response_data);
17962 }
17963 
17974 char *
17975 get_report_format_omp (openvas_connection_t *connection,
17976  credentials_t * credentials, params_t *params,
17977  cmd_response_data_t* response_data)
17978 {
17979  return get_report_format (connection, credentials, params, NULL,
17980  response_data);
17981 }
17982 
17994 static char *
17995 get_report_formats (openvas_connection_t *connection,
17996  credentials_t * credentials, params_t *params,
17997  const char *extra_xml, cmd_response_data_t* response_data)
17998 {
17999  return get_many (connection, "report_format", credentials, params, extra_xml,
18000  NULL, response_data);
18001 }
18002 
18013 char *
18014 get_report_formats_omp (openvas_connection_t *connection,
18015  credentials_t * credentials, params_t *params,
18016  cmd_response_data_t* response_data)
18017 {
18018  return get_report_formats (connection, credentials, params, NULL,
18019  response_data);
18020 }
18021 
18033 static char *
18034 new_report_format (openvas_connection_t *connection, credentials_t *credentials,
18035  params_t *params, const char *extra_xml,
18036  cmd_response_data_t* response_data)
18037 {
18038  GString *xml;
18039  xml = g_string_new ("<new_report_format>");
18040  if (extra_xml)
18041  g_string_append (xml, extra_xml);
18042  g_string_append (xml, "</new_report_format>");
18043  return xsl_transform_omp (connection, credentials, params,
18044  g_string_free (xml, FALSE), response_data);
18045 }
18046 
18057 char *
18058 new_report_format_omp (openvas_connection_t *connection,
18059  credentials_t *credentials, params_t *params,
18060  cmd_response_data_t* response_data)
18061 {
18062  return new_report_format (connection, credentials, params, NULL,
18063  response_data);
18064 }
18065 
18076 char *
18077 delete_report_format_omp (openvas_connection_t *connection,
18078  credentials_t * credentials, params_t *params,
18079  cmd_response_data_t* response_data)
18080 {
18081  return delete_resource (connection, "report_format", credentials, params, 0,
18082  "get_report_formats", response_data);
18083 }
18084 
18096 static char *
18097 edit_report_format (openvas_connection_t *connection,
18098  credentials_t * credentials, params_t *params,
18099  const char *extra_xml, cmd_response_data_t* response_data)
18100 {
18101  gchar *all_rfs_response, *response, *ext_extra_xml;
18102 
18103  if (simple_ompf (connection, "getting Report Formats",
18104  credentials, &all_rfs_response, response_data,
18105  "<get_report_formats"
18106  " filter=\"rows=-1\"/>"))
18107  {
18108  return all_rfs_response;
18109  }
18110 
18111  ext_extra_xml = g_strdup_printf ("%s"
18112  "<all_formats>%s</all_formats>",
18113  extra_xml ? extra_xml : "",
18114  all_rfs_response);
18115  g_free (all_rfs_response);
18116 
18117  response = edit_resource (connection, "report_format", credentials, params,
18118  NULL, ext_extra_xml, response_data);
18119  g_free (ext_extra_xml);
18120  return response;
18121 }
18122 
18133 char *
18134 edit_report_format_omp (openvas_connection_t *connection,
18135  credentials_t * credentials, params_t *params,
18136  cmd_response_data_t* response_data)
18137 {
18138  return edit_report_format (connection, credentials, params, NULL,
18139  response_data);
18140 }
18141 
18152 char *
18153 import_report_format_omp (openvas_connection_t *connection,
18154  credentials_t * credentials, params_t *params,
18155  cmd_response_data_t* response_data)
18156 {
18157  const char* no_redirect;
18158  gchar *command, *html, *response;
18159  entity_t entity;
18160  int ret;
18161 
18162  no_redirect = params_value (params, "no_redirect");
18163 
18164  /* Create the report format. */
18165 
18166  response = NULL;
18167  entity = NULL;
18168  command = g_strdup_printf ("<create_report_format>"
18169  "%s"
18170  "</create_report_format>",
18171  params_value (params, "xml_file"));
18172  ret = omp (connection, credentials, &response, &entity, response_data,
18173  command);
18174  g_free (command);
18175  switch (ret)
18176  {
18177  case 0:
18178  case -1:
18179  break;
18180  case 1:
18181  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18182  return gsad_message (credentials,
18183  "Internal error", __FUNCTION__, __LINE__,
18184  "An internal error occurred while importing a report format. "
18185  "The schedule remains the same. "
18186  "Diagnostics: Failure to send command to manager daemon.",
18187  "/omp?cmd=get_report_formats", response_data);
18188  case 2:
18189  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18190  return gsad_message (credentials,
18191  "Internal error", __FUNCTION__, __LINE__,
18192  "An internal error occurred while importing a report format. "
18193  "It is unclear whether the schedule has been saved or not. "
18194  "Diagnostics: Failure to receive response from manager daemon.",
18195  "/omp?cmd=get_report_formats", response_data);
18196  default:
18197  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18198  return gsad_message (credentials,
18199  "Internal error", __FUNCTION__, __LINE__,
18200  "An internal error occurred while importing a report format. "
18201  "It is unclear whether the schedule has been saved or not. "
18202  "Diagnostics: Internal Error.",
18203  "/omp?cmd=get_report_formats", response_data);
18204  }
18205 
18206  /* Cleanup, and return transformed XML. */
18207 
18208  if (entity_attribute (entity, "id"))
18209  params_add (params, "report_format_id", entity_attribute (entity, "id"));
18210  html = response_from_entity (connection, credentials, params, entity,
18211  (no_redirect && strcmp (no_redirect, "0")),
18212  NULL, "get_report_formats",
18213  NULL, "new_report_format",
18214  "Create Report Format", response_data);
18215 
18216  free_entity (entity);
18217  g_free (response);
18218  return html;
18219 }
18220 
18231 char *
18232 save_report_format_omp (openvas_connection_t *connection,
18233  credentials_t * credentials, params_t *params,
18234  cmd_response_data_t* response_data)
18235 {
18236  int ret;
18237  gchar *html, *response;
18238  params_t *preferences, *id_list_params, *include_id_lists;
18239  const char *no_redirect, *report_format_id, *name, *summary, *enable;
18240  entity_t entity;
18241 
18242  no_redirect = params_value (params, "no_redirect");
18243  report_format_id = params_value (params, "report_format_id");
18244  name = params_value (params, "name");
18245  summary = params_value (params, "summary");
18246  enable = params_value (params, "enable");
18247 
18248  CHECK_PARAM_INVALID (report_format_id, "Save Report Format",
18249  "edit_report_format");
18250  CHECK_PARAM_INVALID (name, "Save Report Format", "edit_report_format");
18251  CHECK_PARAM_INVALID (summary, "Save Report Format", "edit_report_format");
18252  CHECK_PARAM_INVALID (enable, "Save Report Format", "edit_report_format");
18253 
18254  id_list_params = params_values (params, "id_list:");
18255  include_id_lists = params_values (params, "include_id_list:");
18256  if (include_id_lists)
18257  {
18258  GHashTable *id_lists;
18259  param_t *param;
18260  gchar *param_name, *pref_name, *value, *old_values, *new_values;
18261  params_iterator_t iter;
18262  GHashTableIter hash_table_iter;
18263 
18264  id_lists = g_hash_table_new_full (g_str_hash, g_str_equal,
18265  g_free, g_free);
18266 
18267  params_iterator_init (&iter, include_id_lists);
18268  while (params_iterator_next (&iter, &param_name, &param))
18269  {
18270  if (param->value == NULL)
18271  continue;
18272 
18273  g_hash_table_insert (id_lists, g_strdup (param_name), g_strdup (""));
18274  }
18275 
18276  params_iterator_init (&iter, id_list_params);
18277  while (params_iterator_next (&iter, &param_name, &param))
18278  {
18279  if (param->value == NULL)
18280  continue;
18281 
18282  gchar *colon_pos = strchr (param->value, ':');
18283 
18284  pref_name = g_strndup (param->value, colon_pos - param->value);
18285  value = g_strdup (colon_pos + 1);
18286 
18287  old_values = g_hash_table_lookup (id_lists, pref_name);
18288 
18289  if (old_values && strcmp (old_values, ""))
18290  {
18291  new_values = g_strdup_printf ("%s,%s", old_values, value);
18292  g_hash_table_insert (id_lists, pref_name, new_values);
18293  g_free (value);
18294  }
18295  else if (old_values)
18296  {
18297  g_hash_table_insert (id_lists, pref_name, value);
18298  }
18299  }
18300 
18301  g_hash_table_iter_init (&hash_table_iter, id_lists);
18302  while (g_hash_table_iter_next (&hash_table_iter,
18303  (void**)&pref_name, (void**)&value))
18304  {
18305  gchar *value_64;
18306 
18307  value_64 = strlen (value)
18308  ? g_base64_encode ((guchar *) value, strlen (value))
18309  : g_strdup ("");
18310 
18311  response = NULL;
18312  entity = NULL;
18313  ret = ompf (connection, credentials,
18314  &response,
18315  &entity,
18316  response_data,
18317  "<modify_report_format"
18318  " report_format_id=\"%s\">"
18319  "<param>"
18320  "<name>%s</name>"
18321  "<value>%s</value>"
18322  "</param>"
18323  "</modify_report_format>",
18324  report_format_id,
18325  pref_name,
18326  value_64);
18327  g_free (value_64);
18328  switch (ret)
18329  {
18330  case 0:
18331  break;
18332  case 1:
18333  response_data->http_status_code
18334  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18335  return gsad_message (credentials,
18336  "Internal error", __FUNCTION__, __LINE__,
18337  "An internal error occurred while saving a Report Format. "
18338  "The Report Format was not saved. "
18339  "Diagnostics: Failure to send command to manager daemon.",
18340  "/omp?cmd=get_report_formats",
18341  response_data);
18342  case 2:
18343  response_data->http_status_code
18344  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18345  return gsad_message (credentials,
18346  "Internal error", __FUNCTION__, __LINE__,
18347  "An internal error occurred while saving a Report Format. "
18348  "It is unclear whether the Report Format has been saved or not. "
18349  "Diagnostics: Failure to receive response from manager daemon.",
18350  "/omp?cmd=get_report_formats",
18351  response_data);
18352  case -1:
18353  default:
18354  response_data->http_status_code
18355  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18356  return gsad_message (credentials,
18357  "Internal error", __FUNCTION__, __LINE__,
18358  "An internal error occurred while saving a Report Format. "
18359  "It is unclear whether the Report Format has been saved or not. "
18360  "Diagnostics: Internal Error.",
18361  "/omp?cmd=get_report_formats",
18362  response_data);
18363  }
18364 
18365  /* TODO Check if succeeded. response_from_entity_if_failed? */
18366  }
18367  }
18368 
18369  /* Modify the Report Format. */
18370 
18371  preferences = params_values (params, "preference:");
18372  if (preferences)
18373  {
18374  param_t *param;
18375  gchar *param_name;
18376  params_iterator_t iter;
18377 
18378  /* The naming is a bit subtle here, because the HTTP request
18379  * parameters are called "param"s and so are the OMP report format
18380  * parameters. */
18381 
18382  params_iterator_init (&iter, preferences);
18383  while (params_iterator_next (&iter, &param_name, &param))
18384  {
18385  int type_start, type_end, count;
18386  /* LDAPsearch[entry]:Timeout value */
18387  count = sscanf (param_name,
18388  "%*[^[][%n%*[^]]%n]:",
18389  &type_start,
18390  &type_end);
18391  if (count == 0 && type_start > 0 && type_end > 0)
18392  {
18393  gchar *value;
18394 
18395  value = param->value_size
18396  ? g_base64_encode ((guchar *) param->value,
18397  param->value_size)
18398  : g_strdup ("");
18399 
18400  response = NULL;
18401  entity = NULL;
18402  ret = ompf (connection, credentials,
18403  &response,
18404  &entity,
18405  response_data,
18406  "<modify_report_format"
18407  " report_format_id=\"%s\">"
18408  "<param>"
18409  "<name>%s</name>"
18410  "<value>%s</value>"
18411  "</param>"
18412  "</modify_report_format>",
18413  report_format_id,
18414  param_name + type_end + 2,
18415  value);
18416  g_free (value);
18417  switch (ret)
18418  {
18419  case 0:
18420  break;
18421  case 1:
18422  response_data->http_status_code
18423  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18424  return gsad_message (credentials,
18425  "Internal error", __FUNCTION__, __LINE__,
18426  "An internal error occurred while saving a Report Format. "
18427  "The Report Format was not saved. "
18428  "Diagnostics: Failure to send command to manager daemon.",
18429  "/omp?cmd=get_report_formats",
18430  response_data);
18431  case 2:
18432  response_data->http_status_code
18433  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18434  return gsad_message (credentials,
18435  "Internal error", __FUNCTION__, __LINE__,
18436  "An internal error occurred while saving a Report Format. "
18437  "It is unclear whether the Report Format has been saved or not. "
18438  "Diagnostics: Failure to receive response from manager daemon.",
18439  "/omp?cmd=get_report_formats",
18440  response_data);
18441  case -1:
18442  default:
18443  response_data->http_status_code
18444  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18445  return gsad_message (credentials,
18446  "Internal error", __FUNCTION__, __LINE__,
18447  "An internal error occurred while saving a Report Format. "
18448  "It is unclear whether the Report Format has been saved or not. "
18449  "Diagnostics: Internal Error.",
18450  "/omp?cmd=get_report_formats",
18451  response_data);
18452  }
18453 
18454  /* TODO Check if succeeded. response_from_entity_if_failed? */
18455  }
18456  }
18457  }
18458 
18459  response = NULL;
18460  entity = NULL;
18461  ret = ompf (connection, credentials,
18462  &response,
18463  &entity,
18464  response_data,
18465  "<modify_report_format"
18466  " report_format_id=\"%s\">"
18467  "<name>%s</name>"
18468  "<summary>%s</summary>"
18469  "<active>%s</active>"
18470  "</modify_report_format>",
18471  report_format_id,
18472  name,
18473  summary,
18474  enable);
18475 
18476  switch (ret)
18477  {
18478  case 0:
18479  break;
18480  case -1:
18481  return response;
18482  case 1:
18483  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18484  return gsad_message (credentials,
18485  "Internal error", __FUNCTION__, __LINE__,
18486  "An internal error occurred while saving a Report Format. "
18487  "The Report Format was not saved. "
18488  "Diagnostics: Failure to send command to manager daemon.",
18489  "/omp?cmd=get_report_formats", response_data);
18490  case 2:
18491  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18492  return gsad_message (credentials,
18493  "Internal error", __FUNCTION__, __LINE__,
18494  "An internal error occurred while saving a Report Format. "
18495  "It is unclear whether the Report Format has been saved or not. "
18496  "Diagnostics: Failure to receive response from manager daemon.",
18497  "/omp?cmd=get_report_formats", response_data);
18498  default:
18499  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18500  return gsad_message (credentials,
18501  "Internal error", __FUNCTION__, __LINE__,
18502  "An internal error occurred while saving a Report Format. "
18503  "It is unclear whether the Report Format has been saved or not. "
18504  "Diagnostics: Internal Error.",
18505  "/omp?cmd=get_report_formats", response_data);
18506  }
18507 
18508  html = response_from_entity (connection, credentials, params, entity,
18509  (no_redirect && strcmp (no_redirect, "0")),
18510  NULL, "get_report_formats",
18511  NULL, "edit_report_format",
18512  "Save Report Format", response_data);
18513  free_entity (entity);
18514  g_free (response);
18515  return html;
18516 }
18517 
18528 char *
18529 verify_report_format_omp (openvas_connection_t *connection,
18530  credentials_t * credentials, params_t *params,
18531  cmd_response_data_t* response_data)
18532 {
18533  int ret;
18534  gchar *html, *response;
18535  const char *report_format_id;
18536  entity_t entity;
18537 
18538  report_format_id = params_value (params, "report_format_id");
18539  if (report_format_id == NULL)
18540  {
18541  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
18542  return gsad_message (credentials,
18543  "Internal error", __FUNCTION__, __LINE__,
18544  "An internal error occurred while verifying a report format. "
18545  "Diagnostics: Required parameter was NULL.",
18546  "/omp?cmd=get_report_formats", response_data);
18547  }
18548 
18549  /* Verify the report format. */
18550 
18551  response = NULL;
18552  entity = NULL;
18553  ret = ompf (connection, credentials,
18554  &response,
18555  &entity,
18556  response_data,
18557  "<verify_report_format report_format_id=\"%s\"/>",
18558  report_format_id);
18559 
18560  switch (ret)
18561  {
18562  case 0:
18563  case -1:
18564  break;
18565  case 1:
18566  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18567  return gsad_message (credentials,
18568  "Internal error", __FUNCTION__, __LINE__,
18569  "An internal error occurred while verifying a report format. "
18570  "The report format was not verified. "
18571  "Diagnostics: Failure to send command to manager daemon.",
18572  "/omp?cmd=get_report_formats", response_data);
18573  case 2:
18574  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18575  return gsad_message (credentials,
18576  "Internal error", __FUNCTION__, __LINE__,
18577  "An internal error occurred while verifying a report format. "
18578  "It is unclear whether the report format was verified or not. "
18579  "Diagnostics: Failure to send command to manager daemon.",
18580  "/omp?cmd=get_report_formats", response_data);
18581  default:
18582  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18583  return gsad_message (credentials,
18584  "Internal error", __FUNCTION__, __LINE__,
18585  "An internal error occurred while verifying a report format. "
18586  "It is unclear whether the report format was verified or not. "
18587  "Diagnostics: Failure to send command to manager daemon.",
18588  "/omp?cmd=get_report_formats", response_data);
18589  }
18590 
18591  if (omp_success (entity))
18592  {
18593  html = next_page (connection, credentials, params, response,
18594  response_data);
18595  if (html == NULL)
18596  {
18597  free_entity (entity);
18598  g_free (response);
18599  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18600  return gsad_message (credentials,
18601  "Internal error", __FUNCTION__, __LINE__,
18602  "An internal error occurred while verifying a report format. "
18603  "It is unclear whether the report format was verified or not. "
18604  "Diagnostics: Failure to receive response from manager daemon.",
18605  "/omp?cmd=get_report_formats", response_data);
18606  }
18607  }
18608  else
18609  {
18610  set_http_status_from_entity (entity, response_data);
18611  html = get_report_formats (connection, credentials, params, response,
18612  response_data);
18613  }
18614  free_entity (entity);
18615  g_free (response);
18616  return html;
18617 }
18618 
18629 char *
18630 run_wizard_omp (openvas_connection_t *connection, credentials_t *credentials,
18631  params_t *params, cmd_response_data_t* response_data)
18632 {
18633  const char *no_redirect, *name;
18634  int ret;
18635  GString *run;
18636  param_t *param;
18637  gchar *param_name, *html, *response;
18638  params_iterator_t iter;
18639  params_t *wizard_params;
18640  entity_t entity;
18641 
18642  /* The naming is a bit subtle here, because the HTTP request
18643  * parameters are called "param"s and so are the OMP wizard
18644  * parameters. */
18645 
18646  no_redirect = params_value (params, "no_redirect");
18647  name = params_value (params, "name");
18648  if (name == NULL)
18649  {
18650  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
18651  return gsad_message (credentials,
18652  "Internal error", __FUNCTION__, __LINE__,
18653  "An internal error occurred while trying to start a wizard. "
18654  "Diagnostics: Required parameter 'name' was NULL.",
18655  "/omp?cmd=get_tasks", response_data);
18656  }
18657  run = g_string_new ("<run_wizard>");
18658 
18659  g_string_append_printf (run,
18660  "<name>%s</name>"
18661  "<params>",
18662  name);
18663 
18664  wizard_params = params_values (params, "event_data:");
18665  if (wizard_params)
18666  {
18667  params_iterator_init (&iter, wizard_params);
18668  while (params_iterator_next (&iter, &param_name, &param))
18669  xml_string_append (run,
18670  "<param>"
18671  "<name>%s</name>"
18672  "<value>%s</value>"
18673  "</param>",
18674  param_name,
18675  param->value);
18676  }
18677 
18678  g_string_append (run, "</params></run_wizard>");
18679 
18680  response = NULL;
18681  entity = NULL;
18682  ret = omp (connection, credentials, &response, &entity, response_data,
18683  run->str);
18684  g_string_free (run, TRUE);
18685  switch (ret)
18686  {
18687  case 0:
18688  case -1:
18689  break;
18690  case 1:
18691  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18692  return gsad_message (credentials,
18693  "Internal error", __FUNCTION__, __LINE__,
18694  "An internal error occurred while running a wizard. "
18695  "The wizard did not start. "
18696  "Diagnostics: Failure to send command to manager daemon.",
18697  "/omp?cmd=get_tasks", response_data);
18698  case 2:
18699  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18700  return gsad_message (credentials,
18701  "Internal error", __FUNCTION__, __LINE__,
18702  "An internal error occurred while running a wizard. "
18703  "It is unclear whether the wizard started or not. "
18704  "Diagnostics: Failure to receive response from manager daemon.",
18705  "/omp?cmd=get_tasks", response_data);
18706  default:
18707  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18708  return gsad_message (credentials,
18709  "Internal error", __FUNCTION__, __LINE__,
18710  "An internal error occurred while running a wizard. "
18711  "It is unclear whether the wizard started or not. "
18712  "Diagnostics: Internal Error.",
18713  "/omp?cmd=get_tasks", response_data);
18714  }
18715 
18716  html = response_from_entity (connection, credentials, params, entity,
18717  (no_redirect && strcmp (no_redirect, "0")),
18718  NULL, "wizard",
18719  NULL, "wizard",
18720  "Run Wizard", response_data);
18721  free_entity (entity);
18722  g_free (response);
18723  return html;
18724 }
18725 
18726 #define GET_TRASH_RESOURCE(capability, command, name) \
18727  if (command_enabled (credentials, capability)) \
18728  { \
18729  if (openvas_connection_sendf \
18730  (connection, \
18731  "<" command \
18732  " filter=\"rows=-1 sort=name\"" \
18733  " trash=\"1\"/>") \
18734  == -1) \
18735  { \
18736  g_string_free (xml, TRUE); \
18737  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
18738  return gsad_message \
18739  (credentials, \
18740  "Internal error", __FUNCTION__, __LINE__, \
18741  "An internal error occurred while getting " \
18742  name " list for trash." \
18743  "Diagnostics: Failure to send command to" \
18744  " manager daemon.", \
18745  "/omp?cmd=get_trash", response_data); \
18746  } \
18747  \
18748  if (read_string_c (connection, &xml)) \
18749  { \
18750  g_string_free (xml, TRUE); \
18751  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
18752  return gsad_message \
18753  (credentials, \
18754  "Internal error", __FUNCTION__, __LINE__, \
18755  "An internal error occurred while getting " name " list."\
18756  "Diagnostics: Failure to receive response from" \
18757  " manager daemon.", \
18758  "/omp?cmd=get_tasks", response_data); \
18759  } \
18760  }
18761 
18773 char *
18774 get_trash (openvas_connection_t *connection, credentials_t * credentials,
18775  params_t *params, const char *extra_xml,
18776  cmd_response_data_t* response_data)
18777 {
18778  GString *xml;
18779 
18780  xml = g_string_new ("<get_trash>");
18781 
18782  if (extra_xml)
18783  g_string_append (xml, extra_xml);
18784 
18785  GET_TRASH_RESOURCE ("GET_AGENTS", "get_agents", "agents");
18786 
18787  GET_TRASH_RESOURCE ("GET_CONFIGS", "get_configs", "configs");
18788 
18789  GET_TRASH_RESOURCE ("GET_CREDENTIALS", "get_credentials", "credentials");
18790 
18791  GET_TRASH_RESOURCE ("GET_ALERTS", "get_alerts", "alerts");
18792 
18793  GET_TRASH_RESOURCE ("GET_GROUPS", "get_groups", "groups");
18794 
18795  GET_TRASH_RESOURCE ("GET_FILTERS", "get_filters", "filters");
18796 
18797  GET_TRASH_RESOURCE ("GET_NOTES", "get_notes", "notes");
18798 
18799  GET_TRASH_RESOURCE ("GET_OVERRIDES", "get_overrides", "overrides");
18800 
18801  GET_TRASH_RESOURCE ("GET_PERMISSIONS", "get_permissions", "permissions");
18802 
18803  GET_TRASH_RESOURCE ("GET_PORT_LISTS", "get_port_lists", "port lists");
18804 
18805  GET_TRASH_RESOURCE ("GET_REPORT_FORMATS", "get_report_formats",
18806  "report formats");
18807 
18808  GET_TRASH_RESOURCE ("GET_ROLES", "get_roles", "roles");
18809 
18810  GET_TRASH_RESOURCE ("GET_SCANNERS", "get_scanners", "scanners");
18811 
18812  GET_TRASH_RESOURCE ("GET_SCHEDULES", "get_schedules", "schedules");
18813 
18814  GET_TRASH_RESOURCE ("GET_TAGS", "get_tags", "tags");
18815 
18816  GET_TRASH_RESOURCE ("GET_TARGETS", "get_targets", "targets");
18817 
18818  GET_TRASH_RESOURCE ("GET_TASKS", "get_tasks", "tasks");
18819 
18820  /* Cleanup, and return transformed XML. */
18821 
18822  g_string_append (xml, "</get_trash>");
18823  return xsl_transform_omp (connection, credentials, params,
18824  g_string_free (xml, FALSE), response_data);
18825 }
18826 #undef GET_TRASH_RESOURCE
18827 
18838 char *
18839 get_trash_omp (openvas_connection_t *connection, credentials_t * credentials,
18840  params_t *params, cmd_response_data_t* response_data)
18841 {
18842  return get_trash (connection, credentials, params, NULL, response_data);
18843 }
18844 
18856 static char *
18857 get_my_settings (openvas_connection_t *connection, credentials_t * credentials,
18858  params_t *params, const char *extra_xml,
18859  cmd_response_data_t* response_data)
18860 {
18861  GString *xml;
18862 
18863  xml = g_string_new ("<get_my_settings>");
18864 
18865  if (extra_xml)
18866  g_string_append (xml, extra_xml);
18867 
18868  /* Get the settings. */
18869 
18870  if (openvas_connection_sendf (connection,
18871  "<get_settings"
18872  " sort_field=\"name\""
18873  " sort_order=\"ascending\"/>")
18874  == -1)
18875  {
18876  g_string_free (xml, TRUE);
18877  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18878  return gsad_message (credentials,
18879  "Internal error", __FUNCTION__, __LINE__,
18880  "An internal error occurred while getting the settings. "
18881  "The current list of settings is not available. "
18882  "Diagnostics: Failure to send command to manager daemon.",
18883  "/omp?cmd=get_tasks", response_data);
18884  }
18885 
18886  if (read_string_c (connection, &xml))
18887  {
18888  g_string_free (xml, TRUE);
18889  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18890  return gsad_message (credentials,
18891  "Internal error", __FUNCTION__, __LINE__,
18892  "An internal error occurred while getting the settings. "
18893  "The current list of settings is not available. "
18894  "Diagnostics: Failure to receive response from manager daemon.",
18895  "/omp?cmd=get_tasks", response_data);
18896  }
18897 
18898  buffer_languages_xml (xml);
18899 
18900  g_string_append (xml, "</get_my_settings>");
18901  return xsl_transform_omp (connection, credentials, params,
18902  g_string_free (xml, FALSE), response_data);
18903 }
18904 
18915 char *
18916 get_my_settings_omp (openvas_connection_t *connection,
18917  credentials_t * credentials, params_t *params,
18918  cmd_response_data_t* response_data)
18919 {
18920  GString *commands;
18921  int ret;
18922  entity_t entity;
18923  gchar *response;
18924 
18925  commands = g_string_new ("<commands>");
18926  if (command_enabled (credentials, "GET_ALERTS"))
18927  g_string_append (commands, "<get_alerts/>");
18928  if (command_enabled (credentials, "GET_CONFIGS"))
18929  g_string_append (commands, "<get_configs/>");
18930  if (command_enabled (credentials, "GET_FILTERS"))
18931  g_string_append (commands, "<get_filters/>");
18932  if (command_enabled (credentials, "GET_CREDENTIALS"))
18933  g_string_append (commands, "<get_credentials/>");
18934  if (command_enabled (credentials, "GET_PORT_LISTS"))
18935  g_string_append (commands, "<get_port_lists/>");
18936  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
18937  g_string_append (commands, "<get_report_formats/>");
18938  if (command_enabled (credentials, "GET_SCANNERS"))
18939  g_string_append (commands, "<get_scanners/>");
18940  if (command_enabled (credentials, "GET_SCHEDULES"))
18941  g_string_append (commands, "<get_schedules/>");
18942  if (command_enabled (credentials, "GET_TARGETS"))
18943  g_string_append (commands, "<get_targets/>");
18944  g_string_append (commands, "</commands>");
18945 
18946  /* Get Filters and other resource lists. */
18947  response = NULL;
18948  entity = NULL;
18949  ret = omp (connection, credentials, &response, &entity, response_data,
18950  commands->str);
18951  g_string_free (commands, TRUE);
18952  switch (ret)
18953  {
18954  case 0:
18955  case -1:
18956  break;
18957  case 1:
18958  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18959  return gsad_message (credentials,
18960  "Internal error", __FUNCTION__, __LINE__,
18961  "An internal error occurred while getting resources "
18962  "for the settings. "
18963  "Diagnostics: Failure to send command to manager daemon.",
18964  "/omp?cmd=get_my_settings", response_data);
18965  case 2:
18966  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18967  return gsad_message (credentials,
18968  "Internal error", __FUNCTION__, __LINE__,
18969  "An internal error occurred while getting resources "
18970  "for the settings. "
18971  "Diagnostics: Failure to receive response from manager daemon.",
18972  "/omp?cmd=get_alerts", response_data);
18973  default:
18974  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18975  return gsad_message (credentials,
18976  "Internal error", __FUNCTION__, __LINE__,
18977  "An internal error occurred while getting resources "
18978  "for the settings. "
18979  "It is unclear whether the task has been saved or not. "
18980  "Diagnostics: Internal Error.",
18981  "/omp?cmd=get_alerts", response_data);
18982  }
18983  free_entity (entity);
18984 
18985  return get_my_settings (connection, credentials, params, response,
18986  response_data);
18987 }
18988 
19000 static char *
19001 edit_my_settings (openvas_connection_t *connection,
19002  credentials_t * credentials, params_t *params,
19003  const char *extra_xml, cmd_response_data_t* response_data)
19004 {
19005  GString *commands, *xml;
19006  int ret;
19007  gchar *filters_xml;
19008  entity_t entity;
19009 
19010  /* Get the Filters and other resources. */
19011  commands = g_string_new ("<commands>");
19012  if (command_enabled (credentials, "GET_ALERTS"))
19013  g_string_append (commands, "<get_alerts/>");
19014  if (command_enabled (credentials, "GET_CONFIGS"))
19015  g_string_append (commands, "<get_configs/>");
19016  if (command_enabled (credentials, "GET_CREDENTIALS"))
19017  g_string_append (commands, "<get_credentials/>");
19018  if (command_enabled (credentials, "GET_FILTERS"))
19019  g_string_append (commands, "<get_filters/>");
19020  if (command_enabled (credentials, "GET_PORT_LISTS"))
19021  g_string_append (commands, "<get_port_lists/>");
19022  if (command_enabled (credentials, "GET_REPORT_FORMAT"))
19023  g_string_append (commands, "<get_report_formats/>");
19024  if (command_enabled (credentials, "GET_SCANNERS"))
19025  g_string_append (commands, "<get_scanners/>");
19026  if (command_enabled (credentials, "GET_SCHEDULES"))
19027  g_string_append (commands, "<get_schedules/>");
19028  if (command_enabled (credentials, "GET_TARGETS"))
19029  g_string_append (commands, "<get_targets/>");
19030  g_string_append (commands, "</commands>");
19031 
19032  filters_xml = NULL;
19033  entity = NULL;
19034  ret = omp (connection, credentials, &filters_xml, &entity, response_data,
19035  commands->str);
19036  g_string_free (commands, TRUE);
19037  switch (ret)
19038  {
19039  case 0:
19040  case -1:
19041  break;
19042  case 1:
19043  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19044  return gsad_message (credentials,
19045  "Internal error", __FUNCTION__, __LINE__,
19046  "An internal error occurred while getting resources "
19047  "for the settings. "
19048  "Diagnostics: Failure to send command to manager daemon.",
19049  "/omp?cmd=get_my_settings", response_data);
19050  case 2:
19051  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19052  return gsad_message (credentials,
19053  "Internal error", __FUNCTION__, __LINE__,
19054  "An internal error occurred while getting resources "
19055  "for the alert. "
19056  "Diagnostics: Failure to receive response from manager daemon.",
19057  "/omp?cmd=get_my_settings", response_data);
19058  default:
19059  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19060  return gsad_message (credentials,
19061  "Internal error", __FUNCTION__, __LINE__,
19062  "An internal error occurred while getting resources "
19063  "for the settings. "
19064  "Diagnostics: Internal Error.",
19065  "/omp?cmd=get_my_settings", response_data);
19066  }
19067  free_entity (entity);
19068 
19069  xml = g_string_new ("<edit_my_settings>");
19070 
19071  if (extra_xml)
19072  g_string_append (xml, extra_xml);
19073 
19074  g_string_append (xml, filters_xml);
19075  g_free (filters_xml);
19076 
19077  /* Get the settings. */
19078 
19079  if (openvas_connection_sendf (connection,
19080  "<get_settings"
19081  " sort_field=\"name\""
19082  " sort_order=\"ascending\"/>")
19083  == -1)
19084  {
19085  g_string_free (xml, TRUE);
19086  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19087  return gsad_message (credentials,
19088  "Internal error", __FUNCTION__, __LINE__,
19089  "An internal error occurred while getting the settings. "
19090  "The current list of settings is not available. "
19091  "Diagnostics: Failure to send command to manager daemon.",
19092  "/omp?cmd=get_my_settings", response_data);
19093  }
19094 
19095  if (read_string_c (connection, &xml))
19096  {
19097  g_string_free (xml, TRUE);
19098  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19099  return gsad_message (credentials,
19100  "Internal error", __FUNCTION__, __LINE__,
19101  "An internal error occurred while getting the settings. "
19102  "The current list of settings is not available. "
19103  "Diagnostics: Failure to receive response from manager daemon.",
19104  "/omp?cmd=get_my_settings", response_data);
19105  }
19106 
19107  buffer_languages_xml (xml);
19108 
19109  g_string_append (xml, "</edit_my_settings>");
19110  return xsl_transform_omp (connection, credentials, params,
19111  g_string_free (xml, FALSE), response_data);
19112 }
19113 
19124 char *
19125 edit_my_settings_omp (openvas_connection_t *connection,
19126  credentials_t * credentials, params_t *params,
19127  cmd_response_data_t* response_data)
19128 {
19129  return edit_my_settings (connection, credentials, params, NULL,
19130  response_data);
19131 }
19132 
19144 static int
19145 send_settings_filters (openvas_connection_t *connection, params_t *data,
19146  params_t *changed, GString *xml, int *modify_failed_flag,
19147  cmd_response_data_t* response_data)
19148 {
19149  if (data)
19150  {
19151  params_iterator_t iter;
19152  char *uuid;
19153  param_t *param;
19154  entity_t entity;
19155 
19156  params_iterator_init (&iter, data);
19157  while (params_iterator_next (&iter, &uuid, &param))
19158  {
19159  const char* changed_value = params_value (changed, uuid);
19160  if (changed_value == NULL
19161  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19162  {
19163  gchar *base64;
19164  if (param->value)
19165  base64 = g_base64_encode ((guchar*) param->value,
19166  strlen (param->value));
19167  else
19168  base64 = g_strdup("");
19169  if (openvas_connection_sendf_xml (connection,
19170  "<modify_setting setting_id=\"%s\">"
19171  "<value>%s</value>"
19172  "</modify_setting>",
19173  uuid,
19174  base64))
19175  {
19176  g_free (base64);
19177  return -1;
19178  }
19179 
19180  g_free (base64);
19181 
19182  entity = NULL;
19183  xml_string_append (xml,
19184  "<save_setting id=\"%s\">",
19185  uuid);
19186  if (read_entity_and_string_c (connection, &entity, &xml))
19187  {
19188  free_entity (entity);
19189  return -1;
19190  }
19191  xml_string_append (xml, "</save_setting>");
19192  if (! omp_success (entity))
19193  {
19194  set_http_status_from_entity (entity, response_data);
19195  if (modify_failed_flag)
19196  *modify_failed_flag = 1;
19197  }
19198  free_entity(entity);
19199  }
19200  }
19201  }
19202  return 0;
19203 }
19204 
19220 char *
19221 save_my_settings_omp (openvas_connection_t *connection,
19222  credentials_t * credentials, params_t *params,
19223  const char *accept_language, char **timezone,
19224  char **password, char **severity, char **language,
19225  cmd_response_data_t* response_data)
19226 {
19227  const char *lang, *text, *old_passwd, *passwd, *status, *max;
19228  const char *details_fname, *list_fname, *report_fname;
19229  gchar *lang_64, *text_64, *max_64, *fname_64;
19230  GString *xml;
19231  entity_t entity;
19232  params_t *changed, *defaults, *filters;
19233  int modify_failed = 0;
19234  const char *changed_value;
19235 
19236  changed = params_values (params, "settings_changed:");
19237  *timezone = NULL;
19238  *password = NULL;
19239  *severity = NULL;
19240  *language = NULL;
19241 
19242  text = params_value (params, "text");
19243  old_passwd = params_value (params, "old_password");
19244  passwd = params_value (params, "password");
19245  max = params_value (params, "max");
19246  lang = params_value (params, "lang");
19247  details_fname = params_value (params, "details_fname");
19248  list_fname = params_value (params, "list_fname");
19249  report_fname = params_value (params, "report_fname");
19250  if ((text == NULL)
19251  || (passwd == NULL)
19252  || (old_passwd == NULL)
19253  || (max == NULL)
19254  || (lang == NULL)
19255  || (details_fname == NULL)
19256  || (list_fname == NULL)
19257  || (report_fname == NULL))
19258  return edit_my_settings (connection, credentials, params,
19260  ("Save My Settings"),
19261  response_data);
19262 
19263  xml = g_string_new ("");
19264 
19265  changed_value = params_value (changed, "password");
19266  if ((strlen (passwd) || strlen (old_passwd))
19267  && (changed_value == NULL
19268  || (strcmp (changed_value, "") && strcmp (changed_value, "0"))))
19269  {
19270  gchar *passwd_64;
19271  omp_authenticate_info_opts_t auth_opts;
19272 
19273  /* Send Password setting */
19274 
19275  auth_opts = omp_authenticate_info_opts_defaults;
19276  auth_opts.username = credentials->username;
19277  auth_opts.password = old_passwd;
19278  switch (omp_authenticate_info_ext_c (connection, auth_opts))
19279  {
19280  case 0:
19281  break;
19282  case 1:
19283  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19284  return gsad_message (credentials,
19285  "Internal error", __FUNCTION__, __LINE__,
19286  "An internal error occurred while saving settings. "
19287  "The settings remains the same. "
19288  "Diagnostics: Manager closed connection during authenticate.",
19289  "/omp?cmd=get_my_settings", response_data);
19290  case 2:
19291  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
19292  return gsad_message (credentials,
19293  "Invalid Password", __FUNCTION__, __LINE__,
19294  "You tried to change your password, but the old"
19295  " password was not provided or was incorrect. "
19296  " Please enter the correct old password or remove"
19297  " old and new passwords to apply any other changes"
19298  " of your settings.",
19299  "/omp?cmd=get_my_settings", response_data);
19300  default:
19301  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19302  return gsad_message (credentials,
19303  "Internal error", __FUNCTION__, __LINE__,
19304  "An internal error occurred while saving settings. "
19305  "The settings remains the same. "
19306  "Diagnostics: Internal Error.",
19307  "/omp?cmd=get_my_settings", response_data);
19308  }
19309 
19310  passwd_64 = g_base64_encode ((guchar*) passwd, strlen (passwd));
19311 
19312  if (openvas_connection_sendf (connection,
19313  "<modify_setting>"
19314  "<name>Password</name>"
19315  "<value>%s</value>"
19316  "</modify_setting>",
19317  passwd_64 ? passwd_64 : "")
19318  == -1)
19319  {
19320  g_free (passwd_64);
19321  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19322  return gsad_message (credentials,
19323  "Internal error", __FUNCTION__, __LINE__,
19324  "An internal error occurred while saving settings. "
19325  "It is unclear whether all the settings were saved. "
19326  "Diagnostics: Failure to send command to manager daemon.",
19327  "/omp?cmd=get_my_settings", response_data);
19328  }
19329  g_free (passwd_64);
19330 
19331  entity = NULL;
19332  xml_string_append (xml, "<save_setting name=\"Password\">");
19333  if (read_entity_and_string_c (connection, &entity, &xml))
19334  {
19335  g_string_free (xml, TRUE);
19336  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19337  return gsad_message (credentials,
19338  "Internal error", __FUNCTION__, __LINE__,
19339  "An internal error occurred while saving settings. "
19340  "Diagnostics: Failure to receive response from manager daemon.",
19341  "/omp?cmd=get_my_settings", response_data);
19342  }
19343  xml_string_append (xml, "</save_setting>");
19344 
19345  status = entity_attribute (entity, "status");
19346  if (status && (strlen (status) > 0) && (status[0] == '2'))
19347  {
19348  g_free (credentials->password);
19349  credentials->password = g_strdup (passwd);
19350  *password = g_strdup (passwd);
19351  }
19352  else
19353  {
19354  set_http_status_from_entity (entity, response_data);
19355  modify_failed = 1;
19356  }
19357  }
19358 
19359  /* Send Timezone */
19360  changed_value = params_value (changed, "timezone");
19361  if (changed_value == NULL
19362  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19363  {
19364  text_64 = g_base64_encode ((guchar*) text, strlen (text));
19365 
19366  if (openvas_connection_sendf (connection,
19367  "<modify_setting>"
19368  "<name>Timezone</name>"
19369  "<value>%s</value>"
19370  "</modify_setting>",
19371  text_64 ? text_64 : "")
19372  == -1)
19373  {
19374  g_free (text_64);
19375  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19376  return gsad_message (credentials,
19377  "Internal error", __FUNCTION__, __LINE__,
19378  "An internal error occurred while saving settings. "
19379  "It is unclear whether all the settings were saved. "
19380  "Diagnostics: Failure to send command to manager daemon.",
19381  "/omp?cmd=get_my_settings", response_data);
19382  }
19383  g_free (text_64);
19384 
19385  entity = NULL;
19386  xml_string_append (xml, "<save_setting name=\"Timezone\">");
19387  if (read_entity_and_string_c (connection, &entity, &xml))
19388  {
19389  g_string_free (xml, TRUE);
19390  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19391  return gsad_message (credentials,
19392  "Internal error", __FUNCTION__, __LINE__,
19393  "An internal error occurred while saving settings. "
19394  "Diagnostics: Failure to receive response from manager daemon.",
19395  "/omp?cmd=get_my_settings", response_data);
19396  }
19397  xml_string_append (xml, "</save_setting>");
19398 
19399  status = entity_attribute (entity, "status");
19400  if (status && (strlen (status) > 0) && (status[0] == '2'))
19401  {
19402  g_free (credentials->timezone);
19403  credentials->timezone = g_strdup (strlen (text) ? text : "UTC");
19404  *timezone = g_strdup (strlen (text) ? text : "UTC");
19405 
19406  /* Set the timezone, so that the ENVELOPE/TIME
19407  * uses the right timezone. */
19408 
19409  if (setenv ("TZ", credentials->timezone, 1) == -1)
19410  {
19411  g_critical ("%s: failed to set TZ\n", __FUNCTION__);
19412  exit (EXIT_FAILURE);
19413  }
19414  tzset ();
19415  }
19416  else
19417  {
19418  set_http_status_from_entity (entity, response_data);
19419  modify_failed = 1;
19420  }
19421  }
19422 
19423  /* Send Rows Per Page */
19424  changed_value = params_value (changed, "max");
19425  if (changed_value == NULL
19426  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19427  {
19428  max_64 = g_base64_encode ((guchar*) max, strlen (max));
19429 
19430  if (openvas_connection_sendf (connection,
19431  "<modify_setting"
19432  " setting_id"
19433  "=\"5f5a8712-8017-11e1-8556-406186ea4fc5\">"
19434  "<value>%s</value>"
19435  "</modify_setting>",
19436  max_64 ? max_64 : "")
19437  == -1)
19438  {
19439  g_free (max_64);
19440  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19441  return gsad_message (credentials,
19442  "Internal error", __FUNCTION__, __LINE__,
19443  "An internal error occurred while saving settings. "
19444  "It is unclear whether all the settings were saved. "
19445  "Diagnostics: Failure to send command to manager daemon.",
19446  "/omp?cmd=get_my_settings", response_data);
19447  }
19448  g_free (max_64);
19449 
19450  entity = NULL;
19451  xml_string_append (xml,
19452  "<save_setting id=\"%s\">",
19453  "5f5a8712-8017-11e1-8556-406186ea4fc5");
19454  if (read_entity_and_string_c (connection, &entity, &xml))
19455  {
19456  g_string_free (xml, TRUE);
19457  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19458  return gsad_message (credentials,
19459  "Internal error", __FUNCTION__, __LINE__,
19460  "An internal error occurred while saving settings. "
19461  "It is unclear whether all the settings were saved. "
19462  "Diagnostics: Failure to receive response from manager daemon.",
19463  "/omp?cmd=get_my_settings", response_data);
19464  }
19465  xml_string_append (xml, "</save_setting>");
19466  if (! omp_success (entity))
19467  {
19468  set_http_status_from_entity (entity, response_data);
19469  modify_failed = 1;
19470  }
19471  }
19472 
19473  /* Send resource details export file name format. */
19474  changed_value = params_value (changed, "details_fname");
19475  if (changed_value == NULL
19476  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19477  {
19478  fname_64 = g_base64_encode ((guchar*) details_fname, strlen (details_fname));
19479 
19480  if (openvas_connection_sendf (connection,
19481  "<modify_setting"
19482  " setting_id"
19483  "=\"a6ac88c5-729c-41ba-ac0a-deea4a3441f2\">"
19484  "<value>%s</value>"
19485  "</modify_setting>",
19486  fname_64 ? fname_64 : "")
19487  == -1)
19488  {
19489  g_free (fname_64);
19490  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19491  return gsad_message (credentials,
19492  "Internal error", __FUNCTION__, __LINE__,
19493  "An internal error occurred while saving settings. "
19494  "It is unclear whether all the settings were saved. "
19495  "Diagnostics: Failure to send command to manager daemon.",
19496  "/omp?cmd=get_my_settings", response_data);
19497  }
19498  g_free (fname_64);
19499 
19500  entity = NULL;
19501  xml_string_append (xml,
19502  "<save_setting id=\"%s\">",
19503  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2");
19504  if (read_entity_and_string_c (connection, &entity, &xml))
19505  {
19506  g_string_free (xml, TRUE);
19507  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19508  return gsad_message (credentials,
19509  "Internal error", __FUNCTION__, __LINE__,
19510  "An internal error occurred while saving settings. "
19511  "It is unclear whether all the settings were saved. "
19512  "Diagnostics: Failure to receive response from manager daemon.",
19513  "/omp?cmd=get_my_settings", response_data);
19514  }
19515  xml_string_append (xml, "</save_setting>");
19516  if (omp_success (entity) != 1)
19517  {
19518  set_http_status_from_entity (entity, response_data);
19519  modify_failed = 1;
19520  }
19521  }
19522 
19523  /* Send resource list export file name format. */
19524  changed_value = params_value (changed, "list_fname");
19525  if (changed_value == NULL
19526  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19527  {
19528  fname_64 = g_base64_encode ((guchar*) list_fname, strlen (list_fname));
19529 
19530  if (openvas_connection_sendf (connection,
19531  "<modify_setting"
19532  " setting_id"
19533  "=\"0872a6ed-4f85-48c5-ac3f-a5ef5e006745\">"
19534  "<value>%s</value>"
19535  "</modify_setting>",
19536  fname_64 ? fname_64 : "")
19537  == -1)
19538  {
19539  g_free (fname_64);
19540  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19541  return gsad_message (credentials,
19542  "Internal error", __FUNCTION__, __LINE__,
19543  "An internal error occurred while saving settings. "
19544  "It is unclear whether all the settings were saved. "
19545  "Diagnostics: Failure to send command to manager daemon.",
19546  "/omp?cmd=get_my_settings", response_data);
19547  }
19548  g_free (fname_64);
19549 
19550  entity = NULL;
19551  xml_string_append (xml,
19552  "<save_setting id=\"%s\">",
19553  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2");
19554  if (read_entity_and_string_c (connection, &entity, &xml))
19555  {
19556  g_string_free (xml, TRUE);
19557  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19558  return gsad_message (credentials,
19559  "Internal error", __FUNCTION__, __LINE__,
19560  "An internal error occurred while saving settings. "
19561  "It is unclear whether all the settings were saved. "
19562  "Diagnostics: Failure to receive response from manager daemon.",
19563  "/omp?cmd=get_my_settings", response_data);
19564  }
19565  xml_string_append (xml, "</save_setting>");
19566  if (omp_success (entity) != 1)
19567  {
19568  set_http_status_from_entity (entity, response_data);
19569  modify_failed = 1;
19570  }
19571  }
19572 
19573  /* Send report export file name format. */
19574  changed_value = params_value (changed, "report_fname");
19575  if (changed_value == NULL
19576  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19577  {
19578  fname_64 = g_base64_encode ((guchar*) report_fname, strlen (report_fname));
19579 
19580  if (openvas_connection_sendf (connection,
19581  "<modify_setting"
19582  " setting_id"
19583  "=\"e1a2ae0b-736e-4484-b029-330c9e15b900\">"
19584  "<value>%s</value>"
19585  "</modify_setting>",
19586  fname_64 ? fname_64 : "")
19587  == -1)
19588  {
19589  g_free (fname_64);
19590  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19591  return gsad_message (credentials,
19592  "Internal error", __FUNCTION__, __LINE__,
19593  "An internal error occurred while saving settings. "
19594  "It is unclear whether all the settings were saved. "
19595  "Diagnostics: Failure to send command to manager daemon.",
19596  "/omp?cmd=get_my_settings", response_data);
19597  }
19598  g_free (fname_64);
19599 
19600  entity = NULL;
19601  xml_string_append (xml,
19602  "<save_setting id=\"%s\">",
19603  "e1a2ae0b-736e-4484-b029-330c9e15b900");
19604  if (read_entity_and_string_c (connection, &entity, &xml))
19605  {
19606  g_string_free (xml, TRUE);
19607  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19608  return gsad_message (credentials,
19609  "Internal error", __FUNCTION__, __LINE__,
19610  "An internal error occurred while saving settings. "
19611  "It is unclear whether all the settings were saved. "
19612  "Diagnostics: Failure to receive response from manager daemon.",
19613  "/omp?cmd=get_my_settings", response_data);
19614  }
19615  xml_string_append (xml, "</save_setting>");
19616  if (omp_success (entity) != 1)
19617  {
19618  set_http_status_from_entity (entity, response_data);
19619  modify_failed = 1;
19620  }
19621  }
19622 
19623  /* Send User Interface Language. */
19624  changed_value = params_value (changed, "lang");
19625  if (changed_value == NULL
19626  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19627  {
19628  lang_64 = g_base64_encode ((guchar*) lang, strlen (lang));
19629 
19630  if (openvas_connection_sendf (connection,
19631  "<modify_setting"
19632  " setting_id"
19633  "=\"6765549a-934e-11e3-b358-406186ea4fc5\">"
19634  "<value>%s</value>"
19635  "</modify_setting>",
19636  lang_64 ? lang_64 : "")
19637  == -1)
19638  {
19639  g_free (lang_64);
19640  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19641  return gsad_message (credentials,
19642  "Internal error", __FUNCTION__, __LINE__,
19643  "An internal error occurred while saving settings. "
19644  "It is unclear whether all the settings were saved. "
19645  "Diagnostics: Failure to send command to manager daemon.",
19646  "/omp?cmd=get_my_settings", response_data);
19647  }
19648  g_free (lang_64);
19649 
19650  entity = NULL;
19651  xml_string_append (xml,
19652  "<save_setting id=\"%s\">",
19653  "6765549a-934e-11e3-b358-406186ea4fc5");
19654  if (read_entity_and_string_c (connection, &entity, &xml))
19655  {
19656  g_string_free (xml, TRUE);
19657  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19658  return gsad_message (credentials,
19659  "Internal error", __FUNCTION__, __LINE__,
19660  "An internal error occurred while saving settings. "
19661  "It is unclear whether all the settings were saved. "
19662  "Diagnostics: Failure to receive response from manager daemon.",
19663  "/omp?cmd=get_my_settings", response_data);
19664  }
19665  xml_string_append (xml, "</save_setting>");
19666  if (omp_success (entity))
19667  {
19668  gchar *language_code;
19669  set_language_code (&language_code, lang);
19670  if (language_code)
19671  {
19672  g_free (credentials->language);
19673  credentials->language = language_code;
19674  *language = g_strdup (lang);
19675  }
19676  else
19677  {
19678  g_free (credentials->language);
19679  credentials->language = accept_language_to_env_fmt (accept_language);
19680  *language = NULL;
19681  }
19682  }
19683  else
19684  {
19685  set_http_status_from_entity (entity, response_data);
19686  modify_failed = 1;
19687  }
19688  }
19689 
19690  /* Send default resources */
19691 
19692  defaults = params_values (params, "settings_default:");
19693  if (send_settings_filters (connection, defaults, changed, xml,
19694  &modify_failed, response_data))
19695  {
19696  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19697  return gsad_message (credentials,
19698  "Internal error", __FUNCTION__, __LINE__,
19699  "An internal error occurred while saving settings. "
19700  "It is unclear whether all the settings were saved. "
19701  "Diagnostics: Failure to send command to manager daemon.",
19702  "/omp?cmd=get_my_settings", response_data);
19703  }
19704 
19705  /* Send resources filters */
19706 
19707  filters = params_values (params, "settings_filter:");
19708  if (send_settings_filters (connection, filters, changed, xml, &modify_failed,
19709  response_data))
19710  {
19711  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19712  return gsad_message (credentials,
19713  "Internal error", __FUNCTION__, __LINE__,
19714  "An internal error occurred while saving settings. "
19715  "It is unclear whether all the settings were saved. "
19716  "Diagnostics: Failure to send command to manager daemon.",
19717  "/omp?cmd=get_my_settings", response_data);
19718  }
19719 
19720  /* Send Severity Class. */
19721 
19722  changed_value = params_value (changed, "severity_class");
19723  if (changed_value == NULL
19724  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19725  {
19726  text = params_value (params, "severity_class");
19727  text_64 = (text
19728  ? g_base64_encode ((guchar*) text, strlen (text))
19729  : g_strdup (""));
19730 
19731  if (openvas_connection_sendf (connection,
19732  "<modify_setting"
19733  " setting_id"
19734  "=\"f16bb236-a32d-4cd5-a880-e0fcf2599f59\">"
19735  "<value>%s</value>"
19736  "</modify_setting>",
19737  text_64 ? text_64 : "")
19738  == -1)
19739  {
19740  g_free (text_64);
19741  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19742  return gsad_message (credentials,
19743  "Internal error", __FUNCTION__, __LINE__,
19744  "An internal error occurred while saving settings. "
19745  "It is unclear whether all the settings were saved. "
19746  "Diagnostics: Failure to send command to manager daemon.",
19747  "/omp?cmd=get_my_settings", response_data);
19748  }
19749  g_free (text_64);
19750 
19751  entity = NULL;
19752  xml_string_append (xml,
19753  "<save_setting id=\"%s\">",
19754  "f16bb236-a32d-4cd5-a880-e0fcf2599f59");
19755  if (read_entity_and_string_c (connection, &entity, &xml))
19756  {
19757  g_string_free (xml, TRUE);
19758  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19759  return gsad_message (credentials,
19760  "Internal error", __FUNCTION__, __LINE__,
19761  "An internal error occurred while saving settings. "
19762  "It is unclear whether all the settings were saved. "
19763  "Diagnostics: Failure to receive response from manager daemon.",
19764  "/omp?cmd=get_my_settings", response_data);
19765  }
19766  xml_string_append (xml, "</save_setting>");
19767 
19768  status = entity_attribute (entity, "status");
19769  if (status && (strlen (status) > 0) && (status[0] == '2'))
19770  {
19771  g_free (credentials->severity);
19772  if ((text != NULL) && (strlen (text) > 0))
19773  {
19774  credentials->severity = g_strdup (text);
19775  *severity = g_strdup (text);
19776  }
19777  }
19778  else
19779  {
19780  set_http_status_from_entity (entity, response_data);
19781  modify_failed = 1;
19782  }
19783  }
19784 
19785  /* Send Dynamic Severity setting. */
19786 
19787  changed_value = params_value (changed, "dynamic_severity");
19788  if (changed_value == NULL
19789  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19790  {
19791  text = params_value (params, "dynamic_severity");
19792  text_64 = (text
19793  ? g_base64_encode ((guchar*) text, strlen (text))
19794  : g_strdup (""));
19795 
19796  if (openvas_connection_sendf (connection,
19797  "<modify_setting"
19798  " setting_id"
19799  "=\"77ec2444-e7f2-4a80-a59b-f4237782d93f\">"
19800  "<value>%s</value>"
19801  "</modify_setting>",
19802  text_64 ? text_64 : "")
19803  == -1)
19804  {
19805  g_free (text_64);
19806  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19807  return gsad_message (credentials,
19808  "Internal error", __FUNCTION__, __LINE__,
19809  "An internal error occurred while saving settings. "
19810  "It is unclear whether all the settings were saved. "
19811  "Diagnostics: Failure to send command to manager daemon.",
19812  "/omp?cmd=get_my_settings", response_data);
19813  }
19814  g_free (text_64);
19815 
19816  entity = NULL;
19817  xml_string_append (xml,
19818  "<save_setting id=\"%s\">",
19819  "77ec2444-e7f2-4a80-a59b-f4237782d93f");
19820  if (read_entity_and_string_c (connection, &entity, &xml))
19821  {
19822  g_string_free (xml, TRUE);
19823  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19824  return gsad_message (credentials,
19825  "Internal error", __FUNCTION__, __LINE__,
19826  "An internal error occurred while saving settings. "
19827  "It is unclear whether all the settings were saved. "
19828  "Diagnostics: Failure to receive response from manager daemon.",
19829  "/omp?cmd=get_my_settings", response_data);
19830  }
19831  xml_string_append (xml, "</save_setting>");
19832  if (! omp_success (entity))
19833  {
19834  set_http_status_from_entity (entity, response_data);
19835  modify_failed = 1;
19836  }
19837  }
19838 
19839  /* Send Default Severity setting. */
19840  changed_value = params_value (changed, "default_severity");
19841  if (changed_value == NULL
19842  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19843  {
19844  text = params_value (params, "default_severity");
19845  text_64 = (text
19846  ? g_base64_encode ((guchar*) text, strlen (text))
19847  : g_strdup (""));
19848 
19849  if (openvas_connection_sendf (connection,
19850  "<modify_setting"
19851  " setting_id"
19852  "=\"7eda49c5-096c-4bef-b1ab-d080d87300df\">"
19853  "<value>%s</value>"
19854  "</modify_setting>",
19855  text_64 ? text_64 : "")
19856  == -1)
19857  {
19858  g_free (text_64);
19859  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19860  return gsad_message (credentials,
19861  "Internal error", __FUNCTION__, __LINE__,
19862  "An internal error occurred while saving settings. "
19863  "It is unclear whether all the settings were saved. "
19864  "Diagnostics: Failure to send command to manager daemon.",
19865  "/omp?cmd=get_my_settings", response_data);
19866  }
19867  g_free (text_64);
19868 
19869  entity = NULL;
19870  xml_string_append (xml,
19871  "<save_setting id=\"%s\">",
19872  "7eda49c5-096c-4bef-b1ab-d080d87300df");
19873  if (read_entity_and_string_c (connection, &entity, &xml))
19874  {
19875  g_string_free (xml, TRUE);
19876  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19877  return gsad_message (credentials,
19878  "Internal error", __FUNCTION__, __LINE__,
19879  "An internal error occurred while saving settings. "
19880  "It is unclear whether all the settings were saved. "
19881  "Diagnostics: Failure to receive response from manager daemon.",
19882  "/omp?cmd=get_my_settings", response_data);
19883  }
19884  xml_string_append (xml, "</save_setting>");
19885  if (! omp_success (entity))
19886  {
19887  set_http_status_from_entity (entity, response_data);
19888  modify_failed = 1;
19889  }
19890  }
19891 
19892  /* Send Auto Cache Rebuild setting. */
19893 
19894  changed_value = params_value (changed, "auto_cache_rebuild");
19895  if (changed_value == NULL
19896  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19897  {
19898  text = params_value (params, "auto_cache_rebuild");
19899  text_64 = (text
19900  ? g_base64_encode ((guchar*) text, strlen (text))
19901  : g_strdup (""));
19902 
19903  if (openvas_connection_sendf (connection,
19904  "<modify_setting"
19905  " setting_id"
19906  "=\"a09285b0-2d47-49b6-a4ef-946ee71f1d5c\">"
19907  "<value>%s</value>"
19908  "</modify_setting>",
19909  text_64 ? text_64 : "")
19910  == -1)
19911  {
19912  g_free (text_64);
19913  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19914  return gsad_message (credentials,
19915  "Internal error", __FUNCTION__, __LINE__,
19916  "An internal error occurred while saving settings. "
19917  "It is unclear whether all the settings were saved. "
19918  "Diagnostics: Failure to send command to manager daemon.",
19919  "/omp?cmd=get_my_settings", response_data);
19920  }
19921  g_free (text_64);
19922 
19923  entity = NULL;
19924  xml_string_append (xml,
19925  "<save_setting id=\"%s\">",
19926  "a09285b0-2d47-49b6-a4ef-946ee71f1d5c");
19927  if (read_entity_and_string_c (connection, &entity, &xml))
19928  {
19929  g_string_free (xml, TRUE);
19930  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19931  return gsad_message (credentials,
19932  "Internal error", __FUNCTION__, __LINE__,
19933  "An internal error occurred while saving settings. "
19934  "It is unclear whether all the settings were saved. "
19935  "Diagnostics: Failure to receive response from manager daemon.",
19936  "/omp?cmd=get_my_settings", response_data);
19937  }
19938  xml_string_append (xml, "</save_setting>");
19939  if (! omp_success (entity))
19940  {
19941  set_http_status_from_entity (entity, response_data);
19942  modify_failed = 1;
19943  }
19944  }
19945 
19946  if (modify_failed)
19947  return edit_my_settings (connection, credentials, params,
19948  g_string_free (xml, FALSE), response_data);
19949  else
19950  return get_my_settings (connection, credentials, params,
19951  g_string_free (xml, FALSE), response_data);
19952 }
19953 
19964 char *
19965 get_protocol_doc_omp (openvas_connection_t *connection,
19966  credentials_t * credentials, params_t *params,
19967  cmd_response_data_t* response_data)
19968 {
19969  GString *xml;
19970  entity_t help_response;
19971 
19972  xml = g_string_new ("");
19973  g_string_append_printf (xml, "<get_protocol_doc>");
19974 
19975  /* Get the resource. */
19976 
19977  if (openvas_connection_sendf (connection, "<help format=\"XML\"/>")
19978  == -1)
19979  {
19980  g_string_free (xml, TRUE);
19981  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19982  return gsad_message (credentials,
19983  "Internal error", __FUNCTION__, __LINE__,
19984  "An internal error occurred while getting the OMP doc. "
19985  "Diagnostics: Failure to send command to manager daemon.",
19986  "/omp?cmd=get_tasks", response_data);
19987  }
19988 
19989  help_response = NULL;
19990  if (read_entity_and_string_c (connection, &help_response, &xml))
19991  {
19992  g_string_free (xml, TRUE);
19993  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19994  return gsad_message (credentials,
19995  "Internal error", __FUNCTION__, __LINE__,
19996  "An internal error occurred while getting the OMP doc. "
19997  "Diagnostics: Failure to receive response from manager daemon.",
19998  "/omp?cmd=get_tasks", response_data);
19999  }
20000  free_entity (help_response);
20001 
20002  /* Cleanup, and return transformed XML. */
20003 
20004  g_string_append_printf (xml, "</get_protocol_doc>");
20005  return xsl_transform_omp (connection, credentials, params,
20006  g_string_free (xml, FALSE), response_data);
20007 }
20008 
20022 char *
20023 export_omp_doc_omp (openvas_connection_t *connection,
20024  credentials_t * credentials, params_t *params,
20025  enum content_type * content_type,
20026  char **content_disposition, gsize *content_length,
20027  cmd_response_data_t* response_data)
20028 {
20029  entity_t entity, response;
20030  char *content = NULL;
20031  const char *format;
20032  time_t now;
20033  struct tm *tm;
20034 
20035  *content_length = 0;
20036 
20037  format = params_value (params, "protocol_format")
20038  ? params_value (params, "protocol_format")
20039  : "xml";
20040 
20041  if (openvas_connection_sendf (connection,
20042  "<help format=\"%s\"/>",
20043  format)
20044  == -1)
20045  {
20046  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20047  return gsad_message (credentials,
20048  "Internal error", __FUNCTION__, __LINE__,
20049  "An internal error occurred while getting a list. "
20050  "The list could not be delivered. "
20051  "Diagnostics: Failure to send command to manager daemon.",
20052  "/omp?cmd=get_protocol_doc", response_data);
20053  }
20054 
20055  response = NULL;
20056  if (read_entity_and_text_c (connection, &response, &content))
20057  {
20058  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20059  return gsad_message (credentials,
20060  "Internal error", __FUNCTION__, __LINE__,
20061  "An internal error occurred while getting OMP doc. "
20062  "Diagnostics: Failure to receive response from manager daemon.",
20063  "/omp?cmd=get_protocol_doc", response_data);
20064  }
20065 
20066  if (strcmp (format, "xml") == 0)
20067  *content_length = strlen (content);
20068  else
20069  {
20070  char *content_64;
20071  entity = entity_child (response, "schema");
20072  if (entity == NULL)
20073  {
20074  free_entity (response);
20075  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20076  return gsad_message (credentials,
20077  "Internal error", __FUNCTION__, __LINE__,
20078  "An internal error occurred while getting OMP doc. "
20079  "Diagnostics: Schema element missing.",
20080  "/omp?cmd=get_protocol_doc", response_data);
20081  }
20082 
20083  content_64 = entity_text (entity);
20084  if (strlen (content_64) == 0)
20085  {
20086  free_entity (response);
20087  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20088  return gsad_message (credentials,
20089  "Internal error", __FUNCTION__, __LINE__,
20090  "An internal error occurred while getting OMP doc. "
20091  "Diagnostics: Schema empty.",
20092  "/omp?cmd=get_protocol_doc", response_data);
20093  }
20094 
20095  content = (char *) g_base64_decode (content_64, content_length);
20096  }
20097 
20098  now = time (NULL);
20099  tm = localtime (&now);
20100  *content_type = GSAD_CONTENT_TYPE_APP_XML;
20101  *content_disposition = g_strdup_printf ("attachment;"
20102  " filename=\"omp-%d-%d-%d.%s\"",
20103  tm->tm_mday,
20104  tm->tm_mon + 1,
20105  tm->tm_year +1900,
20106  format);
20107  free_entity (response);
20108  return content;
20109 }
20110 
20111 
20112 /* Groups. */
20113 
20125 static char *
20126 get_group (openvas_connection_t *connection, credentials_t * credentials,
20127  params_t *params, const char *extra_xml,
20128  cmd_response_data_t* response_data)
20129 {
20130  return get_one (connection, "group", credentials, params, extra_xml, NULL,
20131  response_data);
20132 }
20133 
20144 char *
20145 get_group_omp (openvas_connection_t *connection, credentials_t * credentials,
20146  params_t *params, cmd_response_data_t* response_data)
20147 {
20148  return get_group (connection, credentials, params, NULL, response_data);
20149 }
20150 
20162 static char *
20163 get_groups (openvas_connection_t *connection, credentials_t * credentials,
20164  params_t *params, const char *extra_xml,
20165  cmd_response_data_t* response_data)
20166 {
20167  return get_many (connection, "group", credentials, params, extra_xml, NULL,
20168  response_data);
20169 }
20170 
20181 char *
20182 get_groups_omp (openvas_connection_t *connection, credentials_t * credentials,
20183  params_t *params, cmd_response_data_t* response_data)
20184 {
20185  return get_groups (connection, credentials, params, NULL, response_data);
20186 }
20187 
20199 static char *
20200 new_group (openvas_connection_t *connection, credentials_t *credentials,
20201  params_t *params, const char *extra_xml,
20202  cmd_response_data_t* response_data)
20203 {
20204  GString *xml;
20205  xml = g_string_new ("<new_group>");
20206  if (extra_xml)
20207  g_string_append (xml, extra_xml);
20208  g_string_append (xml, "</new_group>");
20209  return xsl_transform_omp (connection, credentials, params,
20210  g_string_free (xml, FALSE), response_data);
20211 }
20212 
20223 char *
20224 new_group_omp (openvas_connection_t *connection, credentials_t *credentials,
20225  params_t *params, cmd_response_data_t* response_data)
20226 {
20227  return new_group (connection, credentials, params, NULL, response_data);
20228 }
20229 
20240 char *
20241 delete_trash_group_omp (openvas_connection_t *connection,
20242  credentials_t * credentials, params_t *params,
20243  cmd_response_data_t* response_data)
20244 {
20245  return delete_resource (connection, "group", credentials, params, 1,
20246  "get_trash", response_data);
20247 }
20248 
20259 char *
20260 delete_group_omp (openvas_connection_t *connection, credentials_t * credentials,
20261  params_t *params, cmd_response_data_t* response_data)
20262 {
20263  return delete_resource (connection, "group", credentials, params, 0,
20264  "get_groups", response_data);
20265 }
20266 
20277 char *
20278 create_group_omp (openvas_connection_t *connection, credentials_t *credentials,
20279  params_t *params, cmd_response_data_t* response_data)
20280 {
20281  gchar *html, *response, *command, *specials_element;
20282  const char *no_redirect, *name, *comment, *users, *grant_full;
20283  entity_t entity;
20284  GString *xml;
20285  int ret;
20286 
20287  no_redirect = params_value (params, "no_redirect");
20288  name = params_value (params, "name");
20289  comment = params_value (params, "comment");
20290  grant_full = params_value (params, "grant_full");
20291  users = params_value (params, "users");
20292 
20293  CHECK_PARAM_INVALID (name, "Create Group", "new_group");
20294  CHECK_PARAM_INVALID (comment, "Create Group", "new_group");
20295  CHECK_PARAM_INVALID (users, "Create Group", "new_group");
20296 
20297  /* Create the group. */
20298 
20299  xml = g_string_new ("");
20300 
20301  xml_string_append (xml,
20302  "<name>%s</name>"
20303  "<comment>%s</comment>"
20304  "<users>%s</users>",
20305  name,
20306  comment,
20307  users);
20308 
20309  if (grant_full)
20310  specials_element = g_strdup_printf ("<full/>");
20311  else
20312  specials_element = NULL;
20313 
20314  command = g_strdup_printf ("<create_group>"
20315  "%s"
20316  "<specials>"
20317  "%s"
20318  "</specials>"
20319  "</create_group>",
20320  xml->str,
20321  specials_element);
20322 
20323  g_string_free (xml, TRUE);
20324  g_free (specials_element);
20325 
20326  ret = omp (connection, credentials, &response, &entity, response_data,
20327  command);
20328  g_free (command);
20329  switch (ret)
20330  {
20331  case 0:
20332  case -1:
20333  break;
20334  case 1:
20335  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20336  return gsad_message (credentials,
20337  "Internal error", __FUNCTION__, __LINE__,
20338  "An internal error occurred while creating a new group. "
20339  "No new group was created. "
20340  "Diagnostics: Failure to send command to manager daemon.",
20341  "/omp?cmd=get_groups", response_data);
20342  case 2:
20343  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20344  return gsad_message (credentials,
20345  "Internal error", __FUNCTION__, __LINE__,
20346  "An internal error occurred while creating a new group. "
20347  "It is unclear whether the group has been created or not. "
20348  "Diagnostics: Failure to receive response from manager daemon.",
20349  "/omp?cmd=get_groups", response_data);
20350  default:
20351  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20352  return gsad_message (credentials,
20353  "Internal error", __FUNCTION__, __LINE__,
20354  "An internal error occurred while creating a new group. "
20355  "It is unclear whether the group has been created or not. "
20356  "Diagnostics: Internal Error.",
20357  "/omp?cmd=get_groups", response_data);
20358  }
20359 
20360  if (entity_attribute (entity, "id"))
20361  params_add (params, "group_id", entity_attribute (entity, "id"));
20362  html = response_from_entity (connection, credentials, params, entity,
20363  (no_redirect && strcmp (no_redirect, "0")),
20364  NULL, "get_groups",
20365  NULL, "new_group",
20366  "Create Group", response_data);
20367  free_entity (entity);
20368  g_free (response);
20369  return html;
20370 }
20371 
20383 char *
20384 edit_group (openvas_connection_t *connection, credentials_t * credentials,
20385  params_t *params, const char *extra_xml,
20386  cmd_response_data_t* response_data)
20387 {
20388  return edit_resource (connection, "group", credentials, params, NULL,
20389  extra_xml, response_data);
20390 }
20391 
20402 char *
20403 edit_group_omp (openvas_connection_t *connection, credentials_t * credentials,
20404  params_t *params, cmd_response_data_t* response_data)
20405 {
20406  return edit_group (connection, credentials, params, NULL, response_data);
20407 }
20408 
20422 char *
20423 export_group_omp (openvas_connection_t *connection,
20424  credentials_t * credentials, params_t *params,
20425  enum content_type * content_type, char **content_disposition,
20426  gsize *content_length, cmd_response_data_t* response_data)
20427 {
20428  return export_resource (connection, "group", credentials, params,
20429  content_type, content_disposition, content_length,
20430  response_data);
20431 }
20432 
20447 char *
20448 export_groups_omp (openvas_connection_t *connection,
20449  credentials_t * credentials, params_t *params,
20450  enum content_type * content_type, char **content_disposition,
20451  gsize *content_length, cmd_response_data_t* response_data)
20452 {
20453  return export_many (connection, "group", credentials, params, content_type,
20454  content_disposition, content_length, response_data);
20455 }
20456 
20467 char *
20468 save_group_omp (openvas_connection_t *connection, credentials_t * credentials,
20469  params_t *params, cmd_response_data_t* response_data)
20470 {
20471  int ret;
20472  gchar *html, *response;
20473  const char *no_redirect, *group_id, *name, *comment, *users;
20474  entity_t entity;
20475 
20476  no_redirect = params_value (params, "no_redirect");
20477  group_id = params_value (params, "group_id");
20478  name = params_value (params, "name");
20479  comment = params_value (params, "comment");
20480  users = params_value (params, "users");
20481 
20482  CHECK_PARAM_INVALID (group_id, "Save Group", "edit_group");
20483  CHECK_PARAM_INVALID (name, "Save Group", "edit_group");
20484  CHECK_PARAM_INVALID (comment, "Save Group", "edit_group");
20485  CHECK_PARAM_INVALID (users, "Save Group", "edit_group");
20486 
20487  /* Modify the Group. */
20488 
20489  response = NULL;
20490  entity = NULL;
20491  ret = ompf (connection, credentials,
20492  &response,
20493  &entity,
20494  response_data,
20495  "<modify_group group_id=\"%s\">"
20496  "<name>%s</name>"
20497  "<comment>%s</comment>"
20498  "<users>%s</users>"
20499  "</modify_group>",
20500  group_id,
20501  name,
20502  comment,
20503  users);
20504 
20505  switch (ret)
20506  {
20507  case 0:
20508  case -1:
20509  break;
20510  case 1:
20511  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20512  return gsad_message (credentials,
20513  "Internal error", __FUNCTION__, __LINE__,
20514  "An internal error occurred while saving a group. "
20515  "The group was not saved. "
20516  "Diagnostics: Failure to send command to manager daemon.",
20517  "/omp?cmd=get_groups", response_data);
20518  case 2:
20519  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20520  return gsad_message (credentials,
20521  "Internal error", __FUNCTION__, __LINE__,
20522  "An internal error occurred while saving a group. "
20523  "It is unclear whether the group has been saved or not. "
20524  "Diagnostics: Failure to receive response from manager daemon.",
20525  "/omp?cmd=get_groups", response_data);
20526  default:
20527  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20528  return gsad_message (credentials,
20529  "Internal error", __FUNCTION__, __LINE__,
20530  "An internal error occurred while saving a group. "
20531  "It is unclear whether the group has been saved or not. "
20532  "Diagnostics: Internal Error.",
20533  "/omp?cmd=get_groups", response_data);
20534  }
20535 
20536  html = response_from_entity (connection, credentials, params, entity,
20537  (no_redirect && strcmp (no_redirect, "0")),
20538  NULL, "get_groups",
20539  NULL, "edit_group",
20540  "Save Group", response_data);
20541  free_entity (entity);
20542  g_free (response);
20543  return html;
20544 }
20545 
20546 
20547 /* Permissions. */
20548 
20560 char *
20561 get_permission (openvas_connection_t *connection, credentials_t * credentials,
20562  params_t *params, const char *extra_xml,
20563  cmd_response_data_t* response_data)
20564 {
20565  return get_one (connection, "permission", credentials, params, extra_xml,
20566  "alerts=\"1\"", response_data);
20567 }
20568 
20579 char *
20580 get_permission_omp (openvas_connection_t *connection,
20581  credentials_t * credentials, params_t *params,
20582  cmd_response_data_t* response_data)
20583 {
20584  return get_permission (connection, credentials, params, NULL, response_data);
20585 }
20586 
20598 static char *
20599 get_permissions (openvas_connection_t *connection, credentials_t * credentials,
20600  params_t *params, const char *extra_xml,
20601  cmd_response_data_t* response_data)
20602 {
20603  return get_many (connection, "permission", credentials, params, extra_xml,
20604  NULL, response_data);
20605 }
20606 
20617 char *
20618 get_permissions_omp (openvas_connection_t *connection,
20619  credentials_t * credentials, params_t *params,
20620  cmd_response_data_t* response_data)
20621 {
20622  return get_permissions (connection, credentials, params, NULL, response_data);
20623 }
20624 
20635 char *
20636 delete_trash_permission_omp (openvas_connection_t *connection,
20637  credentials_t * credentials, params_t *params,
20638  cmd_response_data_t* response_data)
20639 {
20640  return delete_resource (connection, "permission", credentials, params, 1,
20641  "get_trash", response_data);
20642 }
20643 
20654 char *
20655 delete_permission_omp (openvas_connection_t *connection,
20656  credentials_t * credentials, params_t *params,
20657  cmd_response_data_t* response_data)
20658 {
20659  return delete_resource (connection, "permission", credentials, params, 0,
20660  NULL, response_data);
20661 }
20662 
20674 char *
20675 new_permission (openvas_connection_t *connection, credentials_t * credentials,
20676  params_t *params, const char *extra_xml,
20677  cmd_response_data_t* response_data)
20678 {
20679  GString *xml;
20680 
20681  xml = g_string_new ("<new_permission>");
20682 
20683  if (command_enabled (credentials, "GET_USERS"))
20684  {
20685  gchar *response;
20686  entity_t entity;
20687 
20688  response = NULL;
20689  entity = NULL;
20690  switch (omp (connection, credentials, &response, &entity, response_data,
20691  "<get_users filter=\"rows=-1 permission=get_users\"/>"))
20692  {
20693  case 0:
20694  case -1:
20695  break;
20696  case 1:
20697  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20698  return gsad_message (credentials,
20699  "Internal error", __FUNCTION__, __LINE__,
20700  "An internal error occurred getting the user list. "
20701  "No new user was created. "
20702  "Diagnostics: Failure to send command to manager daemon.",
20703  "/omp?cmd=get_users", response_data);
20704  case 2:
20705  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20706  return gsad_message (credentials,
20707  "Internal error", __FUNCTION__, __LINE__,
20708  "An internal error occurred getting the user list. "
20709  "No new user was created. "
20710  "Diagnostics: Failure to receive response from manager daemon.",
20711  "/omp?cmd=get_users", response_data);
20712  default:
20713  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20714  return gsad_message (credentials,
20715  "Internal error", __FUNCTION__, __LINE__,
20716  "An internal error occurred getting the user list. "
20717  "No new user was created. "
20718  "Diagnostics: Internal Error.",
20719  "/omp?cmd=get_users", response_data);
20720  }
20721 
20722  g_string_append (xml, response);
20723 
20724  free_entity (entity);
20725  g_free (response);
20726  }
20727 
20728  if (command_enabled (credentials, "GET_GROUPS"))
20729  {
20730  gchar *response;
20731  entity_t entity;
20732 
20733  response = NULL;
20734  entity = NULL;
20735  switch (omp (connection, credentials, &response, &entity, response_data,
20736  "<get_groups filter=\"rows=-1 permission=get_groups\"/>"))
20737  {
20738  case 0:
20739  case -1:
20740  break;
20741  case 1:
20742  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20743  return gsad_message (credentials,
20744  "Internal error", __FUNCTION__, __LINE__,
20745  "An internal error occurred getting the group list. "
20746  "No new user was created. "
20747  "Diagnostics: Failure to send command to manager daemon.",
20748  "/omp?cmd=get_users", response_data);
20749  case 2:
20750  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20751  return gsad_message (credentials,
20752  "Internal error", __FUNCTION__, __LINE__,
20753  "An internal error occurred getting the group list. "
20754  "No new user was created. "
20755  "Diagnostics: Failure to receive response from manager daemon.",
20756  "/omp?cmd=get_users", response_data);
20757  default:
20758  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20759  return gsad_message (credentials,
20760  "Internal error", __FUNCTION__, __LINE__,
20761  "An internal error occurred getting the group list. "
20762  "No new user was created. "
20763  "Diagnostics: Internal Error.",
20764  "/omp?cmd=get_users", response_data);
20765  }
20766 
20767  g_string_append (xml, response);
20768 
20769  free_entity (entity);
20770  g_free (response);
20771  }
20772 
20773  if (command_enabled (credentials, "GET_ROLES"))
20774  {
20775  gchar *response;
20776  entity_t entity;
20777 
20778  response = NULL;
20779  entity = NULL;
20780  switch (omp (connection, credentials, &response, &entity, response_data,
20781  "<get_roles filter=\"rows=-1 permission=get_roles\"/>"))
20782  {
20783  case 0:
20784  case -1:
20785  break;
20786  case 1:
20787  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20788  return gsad_message (credentials,
20789  "Internal error", __FUNCTION__, __LINE__,
20790  "An internal error occurred getting the role list. "
20791  "No new user was created. "
20792  "Diagnostics: Failure to send command to manager daemon.",
20793  "/omp?cmd=get_users", response_data);
20794  case 2:
20795  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20796  return gsad_message (credentials,
20797  "Internal error", __FUNCTION__, __LINE__,
20798  "An internal error occurred getting the role list. "
20799  "No new user was created. "
20800  "Diagnostics: Failure to receive response from manager daemon.",
20801  "/omp?cmd=get_users", response_data);
20802  default:
20803  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20804  return gsad_message (credentials,
20805  "Internal error", __FUNCTION__, __LINE__,
20806  "An internal error occurred getting the role list. "
20807  "No new user was created. "
20808  "Diagnostics: Internal Error.",
20809  "/omp?cmd=get_users", response_data);
20810  }
20811 
20812  g_string_append (xml, response);
20813 
20814  free_entity (entity);
20815  g_free (response);
20816  }
20817 
20818  if (extra_xml)
20819  g_string_append (xml, extra_xml);
20820 
20821  g_string_append (xml, "</new_permission>");
20822 
20823  return xsl_transform_omp (connection, credentials, params,
20824  g_string_free (xml, FALSE), response_data);
20825 }
20826 
20837 char *
20838 new_permission_omp (openvas_connection_t *connection,
20839  credentials_t * credentials, params_t *params,
20840  cmd_response_data_t* response_data)
20841 {
20842  return new_permission (connection, credentials, params, NULL, response_data);
20843 }
20844 
20855 char *
20856 create_permission_omp (openvas_connection_t *connection,
20857  credentials_t *credentials, params_t *params,
20858  cmd_response_data_t* response_data)
20859 {
20860  int ret;
20861  gchar *html, *response;
20862  const char *no_redirect, *name, *comment, *resource_id, *resource_type;
20863  const char *subject_id, *subject_type, *subject_name;
20864  entity_t entity;
20865 
20866  gchar *subject_response;
20867  entity_t get_subject_entity = NULL;
20868  entity_t subject_entity;
20869 
20870  no_redirect = params_value (params, "no_redirect");
20871  name = params_value (params, "permission");
20872  comment = params_value (params, "comment");
20873  resource_id = params_value (params, "id_or_empty");
20874  resource_type = params_value (params, "optional_resource_type");
20875  subject_type = params_value (params, "subject_type");
20876  subject_name = params_value (params, "subject_name");
20877 
20878  CHECK_PARAM_INVALID (name, "Create Permission", "new_permission");
20879  CHECK_PARAM_INVALID (comment, "Create Permission", "new_permission");
20880  if (params_given (params, "id_or_empty"))
20881  CHECK_PARAM_INVALID (resource_id, "Create Permission", "new_permission");
20882  CHECK_PARAM_INVALID (subject_type, "Create Permission", "new_permission");
20883  if (params_given (params, "optional_resource_type"))
20884  CHECK_PARAM_INVALID (resource_type, "Create Permission", "new_permission");
20885 
20886  if (params_given (params, "subject_name"))
20887  {
20888  CHECK_PARAM_INVALID (subject_name,
20889  "Create Permission", "new_permission");
20890  subject_id = NULL;
20891  ret = ompf (connection, credentials,
20892  &subject_response,
20893  &get_subject_entity,
20894  response_data,
20895  "<get_%ss filter=\"rows=1 name=%s\">"
20896  "</get_%ss>",
20897  subject_type,
20898  subject_name,
20899  subject_type);
20900 
20901  switch (ret)
20902  {
20903  case 0:
20904  case -1:
20905  break;
20906  case 1:
20907  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20908  return gsad_message (credentials,
20909  "Internal error", __FUNCTION__, __LINE__,
20910  "An internal error occurred while getting"
20911  " the subject for a permission. "
20912  "The permission was not created. "
20913  "Diagnostics: Failure to send command"
20914  " to manager daemon.",
20915  "/omp?cmd=get_permissions", response_data);
20916  case 2:
20917  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20918  return gsad_message (credentials,
20919  "Internal error", __FUNCTION__, __LINE__,
20920  "An internal error occurred while getting"
20921  " the subject for a permission. "
20922  "The permission was not created. "
20923  "Diagnostics: Failure to receive response"
20924  " from manager daemon.",
20925  "/omp?cmd=get_permissions", response_data);
20926  default:
20927  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20928  return gsad_message (credentials,
20929  "Internal error", __FUNCTION__, __LINE__,
20930  "An internal error occurred while getting"
20931  " the subject for a permission. "
20932  "The permission was not created. "
20933  "Diagnostics: Internal Error.",
20934  "/omp?cmd=get_permissions", response_data);
20935  }
20936 
20937  subject_entity = entity_child (get_subject_entity, subject_type);
20938 
20939  if (subject_entity)
20940  subject_id = entity_attribute (subject_entity, "id");
20941 
20942  if (subject_id == NULL)
20943  {
20944  gchar *msg;
20945  msg = g_strdup_printf ("<gsad_msg status_text=\"Subject not found\""
20946  " operation=\"create_permission\">"
20947  "Could not find a %s with name '%s'."
20948  "</gsad_msg>",
20949  subject_type,
20950  subject_name ? subject_name : "");
20951  return next_page (connection, credentials, params, msg,
20952  response_data);
20953  }
20954  }
20955  else if (strcmp (subject_type, "user") == 0)
20956  subject_id = params_value (params, "permission_user_id");
20957  else if (strcmp (subject_type, "group") == 0)
20958  subject_id = params_value (params, "permission_group_id");
20959  else if (strcmp (subject_type, "role") == 0)
20960  subject_id = params_value (params, "permission_role_id");
20961  else
20962  subject_id = NULL;
20963  CHECK_PARAM_INVALID (subject_id, "Create Permission", "new_permission");
20964 
20965  /* Create the permission(s). */
20966 
20967  if (strcmp (name, "task_proxy") == 0)
20968  {
20969  response = NULL;
20970  entity = NULL;
20971  ret = ompf (connection, credentials,
20972  &response,
20973  &entity,
20974  response_data,
20975  "<commands>"
20976  "<create_permission>"
20977  "<name>get_tasks</name>"
20978  "<comment>%s</comment>"
20979  "<resource id=\"%s\"/>"
20980  "<subject id=\"%s\"><type>%s</type></subject>"
20981  "</create_permission>"
20982  "<create_permission>"
20983  "<name>modify_task</name>"
20984  "<comment>%s</comment>"
20985  "<resource id=\"%s\"/>"
20986  "<subject id=\"%s\"><type>%s</type></subject>"
20987  "</create_permission>"
20988  "<create_permission>"
20989  "<name>start_task</name>"
20990  "<comment>%s</comment>"
20991  "<resource id=\"%s\"/>"
20992  "<subject id=\"%s\"><type>%s</type></subject>"
20993  "</create_permission>"
20994  "<create_permission>"
20995  "<name>stop_task</name>"
20996  "<comment>%s</comment>"
20997  "<resource id=\"%s\"/>"
20998  "<subject id=\"%s\"><type>%s</type></subject>"
20999  "</create_permission>"
21000  "<create_permission>"
21001  "<name>resume_task</name>"
21002  "<comment>%s</comment>"
21003  "<resource id=\"%s\"/>"
21004  "<subject id=\"%s\"><type>%s</type></subject>"
21005  "</create_permission>"
21006  "</commands>",
21007  comment ? comment : "",
21008  resource_id ? resource_id : "",
21009  subject_id,
21010  subject_type,
21011  comment ? comment : "",
21012  resource_id ? resource_id : "",
21013  subject_id,
21014  subject_type,
21015  comment ? comment : "",
21016  resource_id ? resource_id : "",
21017  subject_id,
21018  subject_type,
21019  comment ? comment : "",
21020  resource_id ? resource_id : "",
21021  subject_id,
21022  subject_type,
21023  comment ? comment : "",
21024  resource_id ? resource_id : "",
21025  subject_id,
21026  subject_type);
21027 
21028  if (get_subject_entity)
21029  free_entity (get_subject_entity);
21030 
21031  switch (ret)
21032  {
21033  case 0:
21034  case -1:
21035  break;
21036  case 1:
21037  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21038  return gsad_message (credentials,
21039  "Internal error", __FUNCTION__, __LINE__,
21040  "An internal error occurred while creating a permission. "
21041  "The permission was not created. "
21042  "Diagnostics: Failure to send command to manager daemon.",
21043  "/omp?cmd=get_permissions", response_data);
21044  case 2:
21045  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21046  return gsad_message (credentials,
21047  "Internal error", __FUNCTION__, __LINE__,
21048  "An internal error occurred while creating a permission. "
21049  "It is unclear whether the permission has been created or not. "
21050  "Diagnostics: Failure to receive response from manager daemon.",
21051  "/omp?cmd=get_permissions", response_data);
21052  default:
21053  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21054  return gsad_message (credentials,
21055  "Internal error", __FUNCTION__, __LINE__,
21056  "An internal error occurred while creating a permission. "
21057  "It is unclear whether the permission has been created or not. "
21058  "Diagnostics: Internal Error.",
21059  "/omp?cmd=get_permissions", response_data);
21060  }
21061 
21062  if (entity_attribute (entity, "id"))
21063  params_add (params, "permission_id", entity_attribute (entity, "id"));
21064  html = response_from_entity (connection, credentials, params, entity,
21065  (no_redirect && strcmp (no_redirect, "0")),
21066  NULL, "get_permissions",
21067  NULL, "new_permission",
21068  "Create Permission", response_data);
21069  }
21070  else
21071  {
21072  response = NULL;
21073  entity = NULL;
21074  ret = ompf (connection, credentials,
21075  &response,
21076  &entity,
21077  response_data,
21078  "<create_permission>"
21079  "<name>%s</name>"
21080  "<comment>%s</comment>"
21081  "<resource id=\"%s\">"
21082  "<type>%s</type>"
21083  "</resource>"
21084  "<subject id=\"%s\"><type>%s</type></subject>"
21085  "</create_permission>",
21086  name,
21087  comment ? comment : "",
21088  resource_id ? resource_id : "",
21089  resource_type ? resource_type : "",
21090  subject_id,
21091  subject_type);
21092 
21093  if (get_subject_entity)
21094  free_entity (get_subject_entity);
21095 
21096  switch (ret)
21097  {
21098  case 0:
21099  case -1:
21100  break;
21101  case 1:
21102  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21103  return gsad_message (credentials,
21104  "Internal error", __FUNCTION__, __LINE__,
21105  "An internal error occurred while creating a permission. "
21106  "The permission was not created. "
21107  "Diagnostics: Failure to send command to manager daemon.",
21108  "/omp?cmd=get_permissions", response_data);
21109  case 2:
21110  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21111  return gsad_message (credentials,
21112  "Internal error", __FUNCTION__, __LINE__,
21113  "An internal error occurred while creating a permission. "
21114  "It is unclear whether the permission has been created or not. "
21115  "Diagnostics: Failure to receive response from manager daemon.",
21116  "/omp?cmd=get_permissions", response_data);
21117  default:
21118  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21119  return gsad_message (credentials,
21120  "Internal error", __FUNCTION__, __LINE__,
21121  "An internal error occurred while creating a permission. "
21122  "It is unclear whether the permission has been created or not. "
21123  "Diagnostics: Internal Error.",
21124  "/omp?cmd=get_permissions", response_data);
21125  }
21126 
21127  if (entity_attribute (entity, "id"))
21128  params_add (params, "permission_id", entity_attribute (entity, "id"));
21129  html = response_from_entity (connection, credentials, params, entity,
21130  (no_redirect && strcmp (no_redirect, "0")),
21131  NULL, "get_permissions",
21132  NULL, "new_permission",
21133  "Create Permission", response_data);
21134  }
21135  free_entity (entity);
21136  g_free (response);
21137  return html;
21138 }
21139 
21140 #define CHECK_OMPF_RET \
21141  switch (ret) \
21142  { \
21143  case 0: \
21144  case -1: \
21145  break; \
21146  case 1: \
21147  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21148  return gsad_message (credentials, \
21149  "Internal error", __FUNCTION__, __LINE__, \
21150  "An internal error occurred while creating a permission. " \
21151  "The permission was not created. " \
21152  "Diagnostics: Failure to send command to manager daemon.", \
21153  "/omp?cmd=get_permissions", response_data); \
21154  case 2: \
21155  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21156  return gsad_message (credentials, \
21157  "Internal error", __FUNCTION__, __LINE__, \
21158  "An internal error occurred while creating a permission. " \
21159  "It is unclear whether the permission has been created or not. " \
21160  "Diagnostics: Failure to receive response from manager daemon.", \
21161  "/omp?cmd=get_permissions", response_data); \
21162  default: \
21163  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21164  return gsad_message (credentials, \
21165  "Internal error", __FUNCTION__, __LINE__, \
21166  "An internal error occurred while creating a permission. " \
21167  "It is unclear whether the permission has been created or not. " \
21168  "Diagnostics: Internal Error.", \
21169  "/omp?cmd=get_permissions", response_data); \
21170  } \
21171  if (omp_success (entity)) \
21172  { \
21173  successes ++; \
21174  free_entity (entity); \
21175  g_free (response); \
21176  } \
21177  else \
21178  { \
21179  html \
21180  = response_from_entity (connection, credentials, params, entity, \
21181  (no_redirect && strcmp (no_redirect, "0")), \
21182  NULL, "get_permissions", \
21183  NULL, "new_permissions", \
21184  "Create Permissions", response_data); \
21185  free_entity (entity); \
21186  g_free (response); \
21187  return html; \
21188  } \
21189 
21190 
21201 char *
21202 new_permissions (openvas_connection_t *connection, credentials_t * credentials,
21203  params_t *params, const char *extra_xml,
21204  cmd_response_data_t* response_data)
21205 {
21206  GString *xml;
21207  const char *resource_id, *restrict_type;
21208  params_t *related;
21209  params_iterator_t related_iterator;
21210  param_t *related_param;
21211  char *related_id;
21212 
21213  xml = g_string_new ("<new_permissions>");
21214 
21215  if (command_enabled (credentials, "GET_USERS"))
21216  {
21217  gchar *response;
21218  entity_t entity;
21219 
21220  response = NULL;
21221  entity = NULL;
21222  switch (omp (connection, credentials, &response, &entity, response_data,
21223  "<get_users filter=\"rows=-1 permission=get_users\"/>"))
21224  {
21225  case 0:
21226  case -1:
21227  break;
21228  case 1:
21229  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21230  return gsad_message (credentials,
21231  "Internal error", __FUNCTION__, __LINE__,
21232  "An internal error occurred getting the user list. "
21233  "No new user was created. "
21234  "Diagnostics: Failure to send command to manager daemon.",
21235  "/omp?cmd=get_users", response_data);
21236  case 2:
21237  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21238  return gsad_message (credentials,
21239  "Internal error", __FUNCTION__, __LINE__,
21240  "An internal error occurred getting the user list. "
21241  "No new user was created. "
21242  "Diagnostics: Failure to receive response from manager daemon.",
21243  "/omp?cmd=get_users", response_data);
21244  default:
21245  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21246  return gsad_message (credentials,
21247  "Internal error", __FUNCTION__, __LINE__,
21248  "An internal error occurred getting the user list. "
21249  "No new user was created. "
21250  "Diagnostics: Internal Error.",
21251  "/omp?cmd=get_users", response_data);
21252  }
21253 
21254  g_string_append (xml, response);
21255 
21256  free_entity (entity);
21257  g_free (response);
21258  }
21259 
21260  if (command_enabled (credentials, "GET_GROUPS"))
21261  {
21262  gchar *response;
21263  entity_t entity;
21264 
21265  response = NULL;
21266  entity = NULL;
21267  switch (omp (connection, credentials, &response, &entity, response_data,
21268  "<get_groups filter=\"rows=-1 permission=get_groups\"/>"))
21269  {
21270  case 0:
21271  case -1:
21272  break;
21273  case 1:
21274  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21275  return gsad_message (credentials,
21276  "Internal error", __FUNCTION__, __LINE__,
21277  "An internal error occurred getting the group list. "
21278  "No new user was created. "
21279  "Diagnostics: Failure to send command to manager daemon.",
21280  "/omp?cmd=get_users", response_data);
21281  case 2:
21282  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21283  return gsad_message (credentials,
21284  "Internal error", __FUNCTION__, __LINE__,
21285  "An internal error occurred getting the group list. "
21286  "No new user was created. "
21287  "Diagnostics: Failure to receive response from manager daemon.",
21288  "/omp?cmd=get_users", response_data);
21289  default:
21290  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21291  return gsad_message (credentials,
21292  "Internal error", __FUNCTION__, __LINE__,
21293  "An internal error occurred getting the group list. "
21294  "No new user was created. "
21295  "Diagnostics: Internal Error.",
21296  "/omp?cmd=get_users", response_data);
21297  }
21298 
21299  g_string_append (xml, response);
21300 
21301  free_entity (entity);
21302  g_free (response);
21303  }
21304 
21305  if (command_enabled (credentials, "GET_ROLES"))
21306  {
21307  gchar *response;
21308  entity_t entity;
21309 
21310  response = NULL;
21311  entity = NULL;
21312  switch (omp (connection, credentials, &response, &entity, response_data,
21313  "<get_roles filter=\"rows=-1 permission=get_roles\"/>"))
21314  {
21315  case 0:
21316  case -1:
21317  break;
21318  case 1:
21319  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21320  return gsad_message (credentials,
21321  "Internal error", __FUNCTION__, __LINE__,
21322  "An internal error occurred getting the role list. "
21323  "No new user was created. "
21324  "Diagnostics: Failure to send command to manager daemon.",
21325  "/omp?cmd=get_users", response_data);
21326  case 2:
21327  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21328  return gsad_message (credentials,
21329  "Internal error", __FUNCTION__, __LINE__,
21330  "An internal error occurred getting the role list. "
21331  "No new user was created. "
21332  "Diagnostics: Failure to receive response from manager daemon.",
21333  "/omp?cmd=get_users", response_data);
21334  default:
21335  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21336  return gsad_message (credentials,
21337  "Internal error", __FUNCTION__, __LINE__,
21338  "An internal error occurred getting the role list. "
21339  "No new user was created. "
21340  "Diagnostics: Internal Error.",
21341  "/omp?cmd=get_users", response_data);
21342  }
21343 
21344  g_string_append (xml, response);
21345 
21346  free_entity (entity);
21347  g_free (response);
21348  }
21349 
21350  resource_id = params_value (params, "resource_id");
21351  restrict_type = params_value (params, "restrict_type");
21352  if (resource_id && restrict_type)
21353  {
21354  gchar *get_command;
21355  gchar *response;
21356  entity_t entity;
21357 
21358  get_command = g_strdup_printf ("get_%ss", restrict_type);
21359  response = NULL;
21360  entity = NULL;
21361  switch (ompf (connection, credentials, &response, &entity, response_data,
21362  "<%s %s_id=\"%s\" details=\"0\"/>",
21363  get_command, restrict_type, resource_id))
21364  {
21365  case 0:
21366  case -1:
21367  break;
21368  case 1:
21369  g_free (get_command);
21370  response_data->http_status_code
21371  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21372  return gsad_message (credentials,
21373  "Internal error", __FUNCTION__, __LINE__,
21374  "An internal error occurred getting a resource. "
21375  "No new permission was created. "
21376  "Diagnostics: Failure to send command to manager daemon.",
21377  "/omp?cmd=get_users", response_data);
21378  case 2:
21379  g_free (get_command);
21380  response_data->http_status_code
21381  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21382  return gsad_message (credentials,
21383  "Internal error", __FUNCTION__, __LINE__,
21384  "An internal error occurred getting a resource. "
21385  "No new permission was created. "
21386  "Diagnostics: Failure to receive response from manager daemon.",
21387  "/omp?cmd=get_users", response_data);
21388  default:
21389  g_free (get_command);
21390  response_data->http_status_code
21391  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21392  return gsad_message (credentials,
21393  "Internal error", __FUNCTION__, __LINE__,
21394  "An internal error occurred getting a resource. "
21395  "No new permission was created. "
21396  "Diagnostics: Internal Error.",
21397  "/omp?cmd=get_users", response_data);
21398  }
21399  g_string_append (xml, response);
21400 
21401  free_entity (entity);
21402  g_free (response);
21403  }
21404 
21405  related = params_values (params, "related:");
21406  if (related)
21407  {
21408  params_iterator_init (&related_iterator, related);
21409  }
21410 
21411  while (related
21412  && params_iterator_next (&related_iterator,
21413  &related_id, &related_param))
21414  {
21415  const char* related_type;
21416  gchar *get_command;
21417  gchar *response;
21418  entity_t entity;
21419 
21420  related_type = related_param->value;
21421 
21422  get_command = g_strdup_printf ("get_%ss", related_type);
21423  response = NULL;
21424  entity = NULL;
21425  switch (ompf (connection, credentials, &response, &entity, response_data,
21426  "<%s %s_id=\"%s\" details=\"0\"/>",
21427  get_command, related_type, related_id))
21428  {
21429  case 0:
21430  case -1:
21431  break;
21432  case 1:
21433  g_free (get_command);
21434  response_data->http_status_code
21435  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21436  return gsad_message (credentials,
21437  "Internal error", __FUNCTION__, __LINE__,
21438  "An internal error occurred getting a resource. "
21439  "No new permission was created. "
21440  "Diagnostics: Failure to send command to manager daemon.",
21441  "/omp?cmd=get_users", response_data);
21442  case 2:
21443  g_free (get_command);
21444  response_data->http_status_code
21445  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21446  return gsad_message (credentials,
21447  "Internal error", __FUNCTION__, __LINE__,
21448  "An internal error occurred getting a resource. "
21449  "No new permission was created. "
21450  "Diagnostics: Failure to receive response from manager daemon.",
21451  "/omp?cmd=get_users", response_data);
21452  default:
21453  g_free (get_command);
21454  response_data->http_status_code
21455  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21456  return gsad_message (credentials,
21457  "Internal error", __FUNCTION__, __LINE__,
21458  "An internal error occurred getting a resource. "
21459  "No new permission was created. "
21460  "Diagnostics: Internal Error.",
21461  "/omp?cmd=get_users", response_data);
21462  }
21463  g_string_append (xml, response);
21464 
21465  free_entity (entity);
21466  g_free (response);
21467  }
21468 
21469  if (extra_xml)
21470  g_string_append (xml, extra_xml);
21471 
21472  g_string_append (xml, "</new_permissions>");
21473 
21474  return xsl_transform_omp (connection, credentials, params,
21475  g_string_free (xml, FALSE), response_data);
21476 }
21477 
21488 char *
21489 new_permissions_omp (openvas_connection_t *connection,
21490  credentials_t * credentials, params_t *params,
21491  cmd_response_data_t* response_data)
21492 {
21493  return new_permissions (connection, credentials, params, NULL, response_data);
21494 }
21495 
21506 char *
21507 create_permissions_omp (openvas_connection_t *connection,
21508  credentials_t *credentials, params_t *params,
21509  cmd_response_data_t* response_data)
21510 {
21511  int ret;
21512  gchar *html, *response, *summary_response, *next_url;
21513  int successes;
21514  const char *no_redirect, *permission, *comment, *resource_id, *resource_type;
21515  const char *subject_id, *subject_type, *subject_name;
21516  int include_related;
21517 
21518  entity_t entity;
21519 
21520  gchar *subject_response;
21521  entity_t get_subject_entity = NULL;
21522  entity_t subject_entity;
21523 
21524  no_redirect = params_value (params, "no_redirect");
21525  permission = params_value (params, "permission");
21526  comment = params_value (params, "comment");
21527  resource_id = params_value (params, "resource_id");
21528  resource_type = params_value (params, "resource_type");
21529  subject_type = params_value (params, "subject_type");
21530  subject_name = params_value (params, "subject_name");
21531 
21532  include_related = atoi (params_value (params, "include_related"));
21533  CHECK_PARAM (params_value (params, "include_related"),
21534  "Create Permission", new_permission);
21535 
21536  CHECK_PARAM (permission, "Create Permission", new_permission);
21537  CHECK_PARAM (comment, "Create Permission", new_permission);
21538  CHECK_PARAM (resource_id, "Create Permission", new_permission);
21539  CHECK_PARAM (subject_type, "Create Permission", new_permission);
21540  CHECK_PARAM (resource_type, "Create Permission", new_permission);
21541 
21542  if (params_given (params, "subject_name"))
21543  {
21544  CHECK_PARAM (subject_name, "Create Permission", new_permission);
21545  subject_id = NULL;
21546  ret = ompf (connection, credentials,
21547  &subject_response,
21548  &get_subject_entity,
21549  response_data,
21550  "<get_%ss filter=\"rows=1 name=%s\">"
21551  "</get_%ss>",
21552  subject_type,
21553  subject_name,
21554  subject_type);
21555 
21556  switch (ret)
21557  {
21558  case 0:
21559  case -1:
21560  break;
21561  case 1:
21562  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21563  return gsad_message (credentials,
21564  "Internal error", __FUNCTION__, __LINE__,
21565  "An internal error occurred while getting"
21566  " the subject for a permission. "
21567  "The permission was not created. "
21568  "Diagnostics: Failure to send command"
21569  " to manager daemon.",
21570  "/omp?cmd=get_permissions", response_data);
21571  case 2:
21572  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21573  return gsad_message (credentials,
21574  "Internal error", __FUNCTION__, __LINE__,
21575  "An internal error occurred while getting"
21576  " the subject for a permission. "
21577  "The permission was not created. "
21578  "Diagnostics: Failure to receive response"
21579  " from manager daemon.",
21580  "/omp?cmd=get_permissions", response_data);
21581  default:
21582  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21583  return gsad_message (credentials,
21584  "Internal error", __FUNCTION__, __LINE__,
21585  "An internal error occurred while getting"
21586  " the subject for a permission. "
21587  "The permission was not created. "
21588  "Diagnostics: Internal Error.",
21589  "/omp?cmd=get_permissions", response_data);
21590  }
21591 
21592  subject_entity = entity_child (get_subject_entity, subject_type);
21593 
21594  if (subject_entity)
21595  subject_id = entity_attribute (subject_entity, "id");
21596 
21597  if (subject_id == NULL)
21598  {
21599  gchar *msg;
21600  msg = g_strdup_printf ("<gsad_msg status_text=\"Subject not found\""
21601  " operation=\"create_permission\">"
21602  "Could not find a %s with name '%s'."
21603  "</gsad_msg>",
21604  subject_type,
21605  subject_name ? subject_name : "");
21606  return next_page (connection, credentials, params, msg,
21607  response_data);
21608  }
21609  }
21610  else if (strcmp (subject_type, "user") == 0)
21611  subject_id = params_value (params, "permission_user_id");
21612  else if (strcmp (subject_type, "group") == 0)
21613  subject_id = params_value (params, "permission_group_id");
21614  else if (strcmp (subject_type, "role") == 0)
21615  subject_id = params_value (params, "permission_role_id");
21616  else
21617  subject_id = NULL;
21618  CHECK_PARAM (subject_id, "Create Permission", new_permission);
21619 
21620  successes = 0;
21621 
21622  /* Create the permission(s). */
21623 
21624  // Main resource permissions
21625  if (include_related != 2)
21626  {
21627  if (strcmp (permission, "read") == 0
21628  || strcmp (permission, "proxy") == 0)
21629  {
21630  response = NULL;
21631  entity = NULL;
21632  ret = ompf (connection, credentials,
21633  &response,
21634  &entity,
21635  response_data,
21636  "<create_permission>"
21637  "<name>get_%ss</name>"
21638  "<comment>%s</comment>"
21639  "<resource id=\"%s\">"
21640  "</resource>"
21641  "<subject id=\"%s\"><type>%s</type></subject>"
21642  "</create_permission>",
21643  resource_type,
21644  comment ? comment : "",
21645  resource_id,
21646  subject_id,
21647  subject_type);
21648 
21650  }
21651 
21652  if ((strcmp (permission, "proxy") == 0)
21653  && strcmp (resource_type, "result")
21654  && strcmp (resource_type, "report"))
21655  {
21656  response = NULL;
21657  entity = NULL;
21658  ret = ompf (connection, credentials,
21659  &response,
21660  &entity,
21661  response_data,
21662  "<create_permission>"
21663  "<name>modify_%s</name>"
21664  "<comment>%s</comment>"
21665  "<resource id=\"%s\">"
21666  "</resource>"
21667  "<subject id=\"%s\"><type>%s</type></subject>"
21668  "</create_permission>",
21669  resource_type,
21670  comment ? comment : "",
21671  resource_id,
21672  subject_id,
21673  subject_type);
21674 
21676 
21677  if (strcmp (resource_type, "task") == 0)
21678  {
21679  response = NULL;
21680  entity = NULL;
21681  ret = ompf (connection, credentials,
21682  &response,
21683  &entity,
21684  response_data,
21685  "<create_permission>"
21686  "<name>start_%s</name>"
21687  "<comment>%s</comment>"
21688  "<resource id=\"%s\">"
21689  "</resource>"
21690  "<subject id=\"%s\"><type>%s</type></subject>"
21691  "</create_permission>",
21692  resource_type,
21693  comment ? comment : "",
21694  resource_id,
21695  subject_id,
21696  subject_type);
21697 
21699 
21700  response = NULL;
21701  entity = NULL;
21702  ret = ompf (connection, credentials,
21703  &response,
21704  &entity,
21705  response_data,
21706  "<create_permission>"
21707  "<name>stop_%s</name>"
21708  "<comment>%s</comment>"
21709  "<resource id=\"%s\">"
21710  "</resource>"
21711  "<subject id=\"%s\"><type>%s</type></subject>"
21712  "</create_permission>",
21713  resource_type,
21714  comment ? comment : "",
21715  resource_id,
21716  subject_id,
21717  subject_type);
21718 
21720 
21721  response = NULL;
21722  entity = NULL;
21723  ret = ompf (connection, credentials,
21724  &response,
21725  &entity,
21726  response_data,
21727  "<create_permission>"
21728  "<name>resume_%s</name>"
21729  "<comment>%s</comment>"
21730  "<resource id=\"%s\">"
21731  "</resource>"
21732  "<subject id=\"%s\"><type>%s</type></subject>"
21733  "</create_permission>",
21734  resource_type,
21735  comment ? comment : "",
21736  resource_id,
21737  subject_id,
21738  subject_type);
21739 
21741  }
21742 
21743  if (strcmp (resource_type, "alert") == 0)
21744  {
21745  response = NULL;
21746  entity = NULL;
21747  ret = ompf (connection, credentials,
21748  &response,
21749  &entity,
21750  response_data,
21751  "<create_permission>"
21752  "<name>test_%s</name>"
21753  "<comment>%s</comment>"
21754  "<resource id=\"%s\">"
21755  "</resource>"
21756  "<subject id=\"%s\"><type>%s</type></subject>"
21757  "</create_permission>",
21758  resource_type,
21759  comment ? comment : "",
21760  resource_id,
21761  subject_id,
21762  subject_type);
21763 
21765  }
21766 
21767  if (strcmp (resource_type, "agent") == 0
21768  || strcmp (resource_type, "report_format") == 0
21769  || strcmp (resource_type, "scanner") == 0)
21770  {
21771  response = NULL;
21772  entity = NULL;
21773  ret = ompf (connection, credentials,
21774  &response,
21775  &entity,
21776  response_data,
21777  "<create_permission>"
21778  "<name>verify_%s</name>"
21779  "<comment>%s</comment>"
21780  "<resource id=\"%s\">"
21781  "</resource>"
21782  "<subject id=\"%s\"><type>%s</type></subject>"
21783  "</create_permission>",
21784  resource_type,
21785  comment ? comment : "",
21786  resource_id,
21787  subject_id,
21788  subject_type);
21789 
21791  }
21792  }
21793  }
21794 
21795  // Related permissions
21796  if (include_related)
21797  {
21798  params_t *related;
21799  related = params_values (params, "related:");
21800  if (related)
21801  {
21802  params_iterator_t iter;
21803  char *name;
21804  param_t *param;
21805 
21806  params_iterator_init (&iter, related);
21807  while (params_iterator_next (&iter, &name, &param))
21808  {
21809  char *related_id = name;
21810  char *related_type = param->value;
21811 
21812  if (strcmp (permission, "read") == 0
21813  || strcmp (permission, "proxy") == 0)
21814  {
21815  response = NULL;
21816  entity = NULL;
21817  ret = ompf (connection, credentials,
21818  &response,
21819  &entity,
21820  response_data,
21821  "<create_permission>"
21822  "<name>get_%ss</name>"
21823  "<comment>%s</comment>"
21824  "<resource id=\"%s\">"
21825  "</resource>"
21826  "<subject id=\"%s\"><type>%s</type></subject>"
21827  "</create_permission>",
21828  related_type,
21829  comment ? comment : "",
21830  related_id,
21831  subject_id,
21832  subject_type);
21833 
21835  }
21836 
21837  if ((strcmp (permission, "proxy") == 0)
21838  && strcmp (related_type, "result")
21839  && strcmp (related_type, "report"))
21840  {
21841  response = NULL;
21842  entity = NULL;
21843  ret = ompf (connection, credentials,
21844  &response,
21845  &entity,
21846  response_data,
21847  "<create_permission>"
21848  "<name>modify_%s</name>"
21849  "<comment>%s</comment>"
21850  "<resource id=\"%s\">"
21851  "</resource>"
21852  "<subject id=\"%s\"><type>%s</type></subject>"
21853  "</create_permission>",
21854  related_type,
21855  comment ? comment : "",
21856  related_id,
21857  subject_id,
21858  subject_type);
21859 
21861 
21862  if (strcmp (related_type, "task") == 0)
21863  {
21864  response = NULL;
21865  entity = NULL;
21866  ret = ompf (connection, credentials,
21867  &response,
21868  &entity,
21869  response_data,
21870  "<create_permission>"
21871  "<name>start_%s</name>"
21872  "<comment>%s</comment>"
21873  "<resource id=\"%s\">"
21874  "</resource>"
21875  "<subject id=\"%s\"><type>%s</type></subject>"
21876  "</create_permission>",
21877  related_type,
21878  comment ? comment : "",
21879  related_id,
21880  subject_id,
21881  subject_type);
21882 
21884 
21885  response = NULL;
21886  entity = NULL;
21887  ret = ompf (connection, credentials,
21888  &response,
21889  &entity,
21890  response_data,
21891  "<create_permission>"
21892  "<name>stop_%s</name>"
21893  "<comment>%s</comment>"
21894  "<resource id=\"%s\">"
21895  "</resource>"
21896  "<subject id=\"%s\"><type>%s</type></subject>"
21897  "</create_permission>",
21898  related_type,
21899  comment ? comment : "",
21900  related_id,
21901  subject_id,
21902  subject_type);
21903 
21905 
21906  response = NULL;
21907  entity = NULL;
21908  ret = ompf (connection, credentials,
21909  &response,
21910  &entity,
21911  response_data,
21912  "<create_permission>"
21913  "<name>resume_%s</name>"
21914  "<comment>%s</comment>"
21915  "<resource id=\"%s\">"
21916  "</resource>"
21917  "<subject id=\"%s\"><type>%s</type></subject>"
21918  "</create_permission>",
21919  related_type,
21920  comment ? comment : "",
21921  related_id,
21922  subject_id,
21923  subject_type);
21924 
21926  }
21927 
21928  if (strcmp (related_type, "alert") == 0)
21929  {
21930  response = NULL;
21931  entity = NULL;
21932  ret = ompf (connection, credentials,
21933  &response,
21934  &entity,
21935  response_data,
21936  "<create_permission>"
21937  "<name>test_%s</name>"
21938  "<comment>%s</comment>"
21939  "<resource id=\"%s\">"
21940  "</resource>"
21941  "<subject id=\"%s\"><type>%s</type></subject>"
21942  "</create_permission>",
21943  related_type,
21944  comment ? comment : "",
21945  related_id,
21946  subject_id,
21947  subject_type);
21948 
21950  }
21951 
21952  if (strcmp (related_type, "agent") == 0
21953  || strcmp (related_type, "report_format") == 0
21954  || strcmp (related_type, "scanner") == 0)
21955  {
21956  response = NULL;
21957  entity = NULL;
21958  ret = ompf (connection, credentials,
21959  &response,
21960  &entity,
21961  response_data,
21962  "<create_permission>"
21963  "<name>verify_%s</name>"
21964  "<comment>%s</comment>"
21965  "<resource id=\"%s\">"
21966  "</resource>"
21967  "<subject id=\"%s\"><type>%s</type></subject>"
21968  "</create_permission>",
21969  related_type,
21970  comment ? comment : "",
21971  related_id,
21972  subject_id,
21973  subject_type);
21974 
21976  }
21977  }
21978  }
21979  }
21980  }
21981 
21982  if (get_subject_entity)
21983  free_entity (get_subject_entity);
21984 
21985  summary_response = g_strdup_printf("Successfully created %i permissions",
21986  successes);
21987 
21988  next_url = next_page_url (credentials, params,
21989  NULL, "new_permissions",
21990  "Create Permissions",
21991  G_STRINGIFY (MHD_HTTP_CREATED),
21992  summary_response);
21993 
21994  if (no_redirect)
21995  {
21996  html = action_result_page (connection, credentials, params, response_data,
21997  "Create Permissions",
21998  G_STRINGIFY (MHD_HTTP_CREATED),
21999  summary_response,
22000  NULL,
22001  next_url);
22002  g_free (next_url);
22003  }
22004  else
22005  {
22006  html = NULL;
22007  response_data->redirect = next_url;
22008  }
22009  return html;
22010 }
22011 
22012 #undef CHECK_OMPF_RET
22013 
22025 char *
22026 edit_permission (openvas_connection_t *connection, credentials_t * credentials,
22027  params_t *params, const char *extra_xml,
22028  cmd_response_data_t* response_data)
22029 {
22030  gchar *html;
22031  GString *extra;
22032 
22033  extra = g_string_new ("");
22034 
22035  if (command_enabled (credentials, "GET_USERS"))
22036  {
22037  gchar *response;
22038  entity_t entity;
22039 
22040  response = NULL;
22041  entity = NULL;
22042  switch (omp (connection, credentials, &response, &entity, response_data,
22043  "<get_users filter=\"rows=-1\"/>"))
22044  {
22045  case 0:
22046  case -1:
22047  break;
22048  case 1:
22049  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22050  return gsad_message (credentials,
22051  "Internal error", __FUNCTION__, __LINE__,
22052  "An internal error occurred getting the user list. "
22053  "No new user was created. "
22054  "Diagnostics: Failure to send command to manager daemon.",
22055  "/omp?cmd=get_users", response_data);
22056  case 2:
22057  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22058  return gsad_message (credentials,
22059  "Internal error", __FUNCTION__, __LINE__,
22060  "An internal error occurred getting the user list. "
22061  "No new user was created. "
22062  "Diagnostics: Failure to receive response from manager daemon.",
22063  "/omp?cmd=get_users", response_data);
22064  default:
22065  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22066  return gsad_message (credentials,
22067  "Internal error", __FUNCTION__, __LINE__,
22068  "An internal error occurred getting the user list. "
22069  "No new user was created. "
22070  "Diagnostics: Internal Error.",
22071  "/omp?cmd=get_users", response_data);
22072  }
22073 
22074  g_string_append (extra, response);
22075 
22076  free_entity (entity);
22077  g_free (response);
22078  }
22079 
22080  if (command_enabled (credentials, "GET_GROUPS"))
22081  {
22082  gchar *response;
22083  entity_t entity;
22084 
22085  response = NULL;
22086  entity = NULL;
22087  switch (omp (connection, credentials, &response, &entity, response_data,
22088  "<get_groups filter=\"rows=-1\"/>"))
22089  {
22090  case 0:
22091  case -1:
22092  break;
22093  case 1:
22094  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22095  return gsad_message (credentials,
22096  "Internal error", __FUNCTION__, __LINE__,
22097  "An internal error occurred getting the group list. "
22098  "No new user was created. "
22099  "Diagnostics: Failure to send command to manager daemon.",
22100  "/omp?cmd=get_users", response_data);
22101  case 2:
22102  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22103  return gsad_message (credentials,
22104  "Internal error", __FUNCTION__, __LINE__,
22105  "An internal error occurred getting the group list. "
22106  "No new user was created. "
22107  "Diagnostics: Failure to receive response from manager daemon.",
22108  "/omp?cmd=get_users", response_data);
22109  default:
22110  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22111  return gsad_message (credentials,
22112  "Internal error", __FUNCTION__, __LINE__,
22113  "An internal error occurred getting the group list. "
22114  "No new user was created. "
22115  "Diagnostics: Internal Error.",
22116  "/omp?cmd=get_users", response_data);
22117  }
22118 
22119  g_string_append (extra, response);
22120 
22121  free_entity (entity);
22122  g_free (response);
22123  }
22124 
22125  if (command_enabled (credentials, "GET_ROLES"))
22126  {
22127  gchar *response;
22128  entity_t entity;
22129 
22130  response = NULL;
22131  entity = NULL;
22132  switch (omp (connection, credentials, &response, &entity, response_data,
22133  "<get_roles filter=\"rows=-1\"/>"))
22134  {
22135  case 0:
22136  case -1:
22137  break;
22138  case 1:
22139  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22140  return gsad_message (credentials,
22141  "Internal error", __FUNCTION__, __LINE__,
22142  "An internal error occurred getting the role list. "
22143  "No new user was created. "
22144  "Diagnostics: Failure to send command to manager daemon.",
22145  "/omp?cmd=get_users", response_data);
22146  case 2:
22147  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22148  return gsad_message (credentials,
22149  "Internal error", __FUNCTION__, __LINE__,
22150  "An internal error occurred getting the role list. "
22151  "No new user was created. "
22152  "Diagnostics: Failure to receive response from manager daemon.",
22153  "/omp?cmd=get_users", response_data);
22154  default:
22155  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22156  return gsad_message (credentials,
22157  "Internal error", __FUNCTION__, __LINE__,
22158  "An internal error occurred getting the role list. "
22159  "No new user was created. "
22160  "Diagnostics: Internal Error.",
22161  "/omp?cmd=get_users", response_data);
22162  }
22163 
22164  g_string_append (extra, response);
22165 
22166  free_entity (entity);
22167  g_free (response);
22168  }
22169 
22170  if (extra_xml)
22171  g_string_append (extra, extra_xml);
22172  html = edit_resource (connection, "permission", credentials, params, NULL,
22173  extra->str, response_data);
22174  g_string_free (extra, TRUE);
22175  return html;
22176 }
22177 
22188 char *
22189 edit_permission_omp (openvas_connection_t *connection,
22190  credentials_t * credentials, params_t *params,
22191  cmd_response_data_t* response_data)
22192 {
22193  return edit_permission (connection, credentials, params, NULL, response_data);
22194 }
22195 
22209 char *
22210 export_permission_omp (openvas_connection_t *connection,
22211  credentials_t * credentials, params_t *params,
22212  enum content_type * content_type,
22213  char **content_disposition, gsize *content_length,
22214  cmd_response_data_t* response_data)
22215 {
22216  return export_resource (connection, "permission", credentials, params,
22217  content_type, content_disposition, content_length,
22218  response_data);
22219 }
22220 
22235 char *
22236 export_permissions_omp (openvas_connection_t *connection,
22237  credentials_t * credentials, params_t *params,
22238  enum content_type * content_type,
22239  char **content_disposition, gsize *content_length,
22240  cmd_response_data_t* response_data)
22241 {
22242  return export_many (connection, "permission", credentials, params,
22243  content_type, content_disposition, content_length,
22244  response_data);
22245 }
22246 
22257 char *
22258 save_permission_omp (openvas_connection_t *connection,
22259  credentials_t * credentials, params_t *params,
22260  cmd_response_data_t* response_data)
22261 {
22262  gchar *html, *response;
22263  const char *no_redirect;
22264  const char *permission_id, *name, *comment, *resource_id, *resource_type;
22265  const char *subject_id, *subject_type;
22266  entity_t entity;
22267  int ret;
22268 
22269  no_redirect = params_value (params, "no_redirect");
22270  permission_id = params_value (params, "permission_id");
22271  name = params_value (params, "permission");
22272  comment = params_value (params, "comment");
22273  subject_type = params_value (params, "subject_type");
22274  resource_id = params_value (params, "id_or_empty");
22275  resource_type = params_value (params, "optional_resource_type");
22276 
22277  CHECK_PARAM_INVALID (permission_id, "Save Permission", "edit_permission");
22278  CHECK_PARAM_INVALID (name, "Save Permission", "edit_permission");
22279  CHECK_PARAM_INVALID (comment, "Save Permission", "edit_permission");
22280  if (params_given (params, "id_or_empty"))
22281  CHECK_PARAM_INVALID (resource_id, "Save Permission", "edit_permission");
22282  CHECK_PARAM_INVALID (subject_type, "Save Permission", "edit_permission");
22283  if (params_given (params, "optional_resource_type"))
22284  CHECK_PARAM_INVALID (resource_type, "Save Permission", "edit_permission");
22285 
22286  if (strcmp (subject_type, "user") == 0)
22287  subject_id = params_value (params, "user_id");
22288  else if (strcmp (subject_type, "group") == 0)
22289  subject_id = params_value (params, "group_id");
22290  else if (strcmp (subject_type, "role") == 0)
22291  subject_id = params_value (params, "role_id");
22292  else
22293  subject_id = NULL;
22294  CHECK_PARAM_INVALID (subject_id, "Save Permission", "edit_permission");
22295 
22296  /* Modify the permission. */
22297 
22298  response = NULL;
22299  entity = NULL;
22300  ret = ompf (connection, credentials,
22301  &response,
22302  &entity,
22303  response_data,
22304  "<modify_permission permission_id=\"%s\">"
22305  "<name>%s</name>"
22306  "<comment>%s</comment>"
22307  "<subject id=\"%s\">"
22308  "<type>%s</type>"
22309  "</subject>"
22310  "<resource id=\"%s\">"
22311  "<type>%s</type>"
22312  "</resource>"
22313  "</modify_permission>",
22314  permission_id,
22315  name,
22316  comment,
22317  subject_id,
22318  subject_type,
22319  (resource_id && strlen (resource_id)) ? resource_id : "0",
22320  resource_type ? resource_type : "");
22321  switch (ret)
22322  {
22323  case 0:
22324  case -1:
22325  break;
22326  case 1:
22327  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22328  return gsad_message (credentials,
22329  "Internal error", __FUNCTION__, __LINE__,
22330  "An internal error occurred while modifying a permission. "
22331  "The permission was not modified. "
22332  "Diagnostics: Failure to send command to manager daemon.",
22333  "/omp?cmd=get_permissions", response_data);
22334  case 2:
22335  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22336  return gsad_message (credentials,
22337  "Internal error", __FUNCTION__, __LINE__,
22338  "An internal error occurred while modifying a permission. "
22339  "It is unclear whether the permission has been modified or not. "
22340  "Diagnostics: Failure to receive response from manager daemon.",
22341  "/omp?cmd=get_permissions", response_data);
22342  default:
22343  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22344  return gsad_message (credentials,
22345  "Internal error", __FUNCTION__, __LINE__,
22346  "An internal error occurred while modifying a permission. "
22347  "It is unclear whether the permission has been modified or not. "
22348  "Diagnostics: Internal Error.",
22349  "/omp?cmd=get_permissions", response_data);
22350  }
22351 
22352  html = response_from_entity (connection, credentials, params, entity,
22353  (no_redirect && strcmp (no_redirect, "0")),
22354  NULL, "get_permissions",
22355  NULL, "edit_permission",
22356  "Save Permission", response_data);
22357  free_entity (entity);
22358  g_free (response);
22359  return html;
22360 }
22361 
22362 
22363 /* Port lists. */
22364 
22376 static char *
22377 new_port_list (openvas_connection_t *connection, credentials_t *credentials,
22378  params_t *params, const char *extra_xml,
22379  cmd_response_data_t* response_data)
22380 {
22381  GString *xml;
22382  xml = g_string_new ("<new_port_list>");
22383  if (extra_xml)
22384  g_string_append (xml, extra_xml);
22385  g_string_append (xml, "</new_port_list>");
22386  return xsl_transform_omp (connection, credentials, params,
22387  g_string_free (xml, FALSE), response_data);
22388 }
22389 
22401 static char *
22402 upload_port_list (openvas_connection_t *connection, credentials_t *credentials,
22403  params_t *params, const char *extra_xml,
22404  cmd_response_data_t* response_data)
22405 {
22406  GString *xml;
22407 
22408  xml = g_string_new ("<upload_port_list>");
22409  if (extra_xml)
22410  g_string_append (xml, extra_xml);
22411  g_string_append (xml, "</upload_port_list>");
22412 
22413  return xsl_transform_omp (connection, credentials, params,
22414  g_string_free (xml, FALSE), response_data);
22415 }
22416 
22427 char *
22428 upload_port_list_omp (openvas_connection_t *connection,
22429  credentials_t *credentials, params_t *params,
22430  cmd_response_data_t* response_data)
22431 {
22432  return upload_port_list (connection, credentials, params, NULL,
22433  response_data);
22434 }
22435 
22446 char *
22447 create_port_list_omp (openvas_connection_t *connection,
22448  credentials_t * credentials, params_t *params,
22449  cmd_response_data_t* response_data)
22450 {
22451  gchar *html, *response;
22452  const char *no_redirect, *name, *comment, *port_range, *from_file;
22453  entity_t entity;
22454 
22455  no_redirect = params_value (params, "no_redirect");
22456  name = params_value (params, "name");
22457  comment = params_value (params, "comment");
22458  port_range = params_value (params, "port_range");
22459  from_file = params_value (params, "from_file");
22460 
22461  CHECK_PARAM_INVALID (name, "Create Port List", "new_port_list");
22462  CHECK_PARAM_INVALID (comment, "Create Port List", "new_port_list");
22463  CHECK_PARAM_INVALID (port_range, "Create Port List", "new_port_list");
22464  CHECK_PARAM_INVALID (from_file, "Create Port List", "new_port_list");
22465 
22466  /* Create the port_list. */
22467 
22468  switch (ompf (connection, credentials,
22469  &response,
22470  &entity,
22471  response_data,
22472  "<create_port_list>"
22473  "<name>%s</name>"
22474  "<port_range>%s</port_range>"
22475  "<comment>%s</comment>"
22476  "</create_port_list>",
22477  name,
22478  strcmp (from_file, "0")
22479  ? params_value (params, "file")
22480  : port_range,
22481  comment ? comment : ""))
22482  {
22483  case 0:
22484  case -1:
22485  break;
22486  case 1:
22487  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22488  return gsad_message (credentials,
22489  "Internal error", __FUNCTION__, __LINE__,
22490  "An internal error occurred while creating a new port list. "
22491  "No new port list was created. "
22492  "Diagnostics: Failure to send command to manager daemon.",
22493  "/omp?cmd=get_port_lists", response_data);
22494  case 2:
22495  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22496  return gsad_message (credentials,
22497  "Internal error", __FUNCTION__, __LINE__,
22498  "An internal error occurred while creating a new port list. "
22499  "It is unclear whether the port list has been created or not. "
22500  "Diagnostics: Failure to receive response from manager daemon.",
22501  "/omp?cmd=get_port_lists", response_data);
22502  default:
22503  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22504  return gsad_message (credentials,
22505  "Internal error", __FUNCTION__, __LINE__,
22506  "An internal error occurred while creating a new port list. "
22507  "It is unclear whether the port list has been created or not. "
22508  "Diagnostics: Internal Error.",
22509  "/omp?cmd=get_port_lists", response_data);
22510  }
22511 
22512  if (entity_attribute (entity, "id"))
22513  params_add (params, "port_list_id", entity_attribute (entity, "id"));
22514  html = response_from_entity (connection, credentials, params, entity,
22515  (no_redirect && strcmp (no_redirect, "0")),
22516  NULL, "get_port_lists",
22517  NULL, "new_port_list",
22518  "Create Port List", response_data);
22519  free_entity (entity);
22520  g_free (response);
22521  return html;
22522 }
22523 
22535 static char *
22536 new_port_range (openvas_connection_t *connection, credentials_t *credentials,
22537  params_t *params, const char *extra_xml,
22538  cmd_response_data_t* response_data)
22539 {
22540  GString *xml;
22541  xml = g_string_new ("<new_port_range>");
22542  if (extra_xml)
22543  g_string_append (xml, extra_xml);
22544  g_string_append (xml, "</new_port_range>");
22545  return xsl_transform_omp (connection, credentials, params,
22546  g_string_free (xml, FALSE), response_data);
22547 }
22548 
22559 char *
22560 new_port_range_omp (openvas_connection_t *connection,
22561  credentials_t *credentials, params_t *params,
22562  cmd_response_data_t* response_data)
22563 {
22564  return new_port_range (connection, credentials, params, NULL, response_data);
22565 }
22566 
22577 char *
22578 create_port_range_omp (openvas_connection_t *connection,
22579  credentials_t * credentials, params_t *params,
22580  cmd_response_data_t* response_data)
22581 {
22582  int ret;
22583  gchar *html, *response;
22584  const char *no_redirect, *port_list_id, *start, *end, *type;
22585  entity_t entity;
22586 
22587  no_redirect = params_value (params, "no_redirect");
22588  port_list_id = params_value (params, "port_list_id");
22589  start = params_value (params, "port_range_start");
22590  end = params_value (params, "port_range_end");
22591  type = params_value (params, "port_type");
22592 
22593  CHECK_PARAM_INVALID (port_list_id, "Create Port Range", "edit_port_list");
22594  CHECK_PARAM_INVALID (start, "Create Port Range", "edit_port_list");
22595  CHECK_PARAM_INVALID (end, "Create Port Range", "edit_port_list");
22596  CHECK_PARAM_INVALID (type, "Create Port Range", "edit_port_list");
22597 
22598  /* Create the port range. */
22599 
22600  response = NULL;
22601  entity = NULL;
22602  ret = ompf (connection, credentials,
22603  &response,
22604  &entity,
22605  response_data,
22606  "<create_port_range>"
22607  "<port_list id=\"%s\"/>"
22608  "<start>%s</start>"
22609  "<end>%s</end>"
22610  "<type>%s</type>"
22611  "</create_port_range>",
22612  port_list_id,
22613  start,
22614  end,
22615  type);
22616 
22617  switch (ret)
22618  {
22619  case 0:
22620  case -1:
22621  break;
22622  case 1:
22623  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22624  return gsad_message (credentials,
22625  "Internal error", __FUNCTION__, __LINE__,
22626  "An internal error occurred while creating a Port Range. "
22627  "The Port Range was not created. "
22628  "Diagnostics: Failure to send command to manager daemon.",
22629  "/omp?cmd=get_port_lists", response_data);
22630  case 2:
22631  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22632  return gsad_message (credentials,
22633  "Internal error", __FUNCTION__, __LINE__,
22634  "An internal error occurred while creating a Port Range. "
22635  "It is unclear whether the Port Range has been created or not. "
22636  "Diagnostics: Failure to receive response from manager daemon.",
22637  "/omp?cmd=get_port_lists", response_data);
22638  default:
22639  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22640  return gsad_message (credentials,
22641  "Internal error", __FUNCTION__, __LINE__,
22642  "An internal error occurred while creating a Port Range. "
22643  "It is unclear whether the Port Range has been created or not. "
22644  "Diagnostics: Internal Error.",
22645  "/omp?cmd=get_port_lists", response_data);
22646  }
22647 
22648  html = response_from_entity (connection, credentials, params, entity,
22649  (no_redirect && strcmp (no_redirect, "0")),
22650  NULL, "edit_port_list",
22651  NULL, "edit_port_list",
22652  "Create Port Range", response_data);
22653  free_entity (entity);
22654  g_free (response);
22655  return html;
22656 }
22657 
22669 static char *
22670 get_port_list (openvas_connection_t *connection,
22671  credentials_t * credentials, params_t *params,
22672  const char * extra_xml, cmd_response_data_t* response_data)
22673 {
22674  return get_one (connection, "port_list", credentials, params, extra_xml,
22675  "targets=\"1\" details=\"1\"", response_data);
22676 }
22677 
22688 char *
22689 get_port_list_omp (openvas_connection_t *connection,
22690  credentials_t * credentials, params_t *params,
22691  cmd_response_data_t* response_data)
22692 {
22693  return get_port_list (connection, credentials, params, NULL, response_data);
22694 }
22695 
22707 static char *
22708 get_port_lists (openvas_connection_t *connection,
22709  credentials_t * credentials, params_t *params,
22710  const char *extra_xml, cmd_response_data_t* response_data)
22711 {
22712  return get_many (connection, "port_list", credentials, params, extra_xml,
22713  NULL, response_data);
22714 }
22715 
22726 char *
22727 get_port_lists_omp (openvas_connection_t *connection,
22728  credentials_t * credentials, params_t *params,
22729  cmd_response_data_t* response_data)
22730 {
22731  return get_port_lists (connection, credentials, params, NULL, response_data);
22732 }
22733 
22744 char *
22745 new_port_list_omp (openvas_connection_t *connection, credentials_t *credentials,
22746  params_t *params, cmd_response_data_t* response_data)
22747 {
22748  return new_port_list (connection, credentials, params, NULL, response_data);
22749 }
22750 
22762 char *
22763 edit_port_list (openvas_connection_t *connection, credentials_t * credentials,
22764  params_t *params, const char *extra_xml,
22765  cmd_response_data_t* response_data)
22766 {
22767  return edit_resource (connection, "port_list", credentials, params, NULL,
22768  extra_xml, response_data);
22769 }
22770 
22781 char *
22782 edit_port_list_omp (openvas_connection_t *connection,
22783  credentials_t * credentials, params_t *params,
22784  cmd_response_data_t* response_data)
22785 {
22786  return edit_port_list (connection, credentials, params, NULL, response_data);
22787 }
22788 
22799 char *
22800 save_port_list_omp (openvas_connection_t *connection,
22801  credentials_t * credentials, params_t *params,
22802  cmd_response_data_t* response_data)
22803 {
22804  int ret;
22805  gchar *html, *response;
22806  const char *no_redirect, *port_list_id, *name, *comment;
22807  entity_t entity;
22808 
22809  no_redirect = params_value (params, "no_redirect");
22810  port_list_id = params_value (params, "port_list_id");
22811  name = params_value (params, "name");
22812  comment = params_value (params, "comment");
22813 
22814  CHECK_PARAM_INVALID (port_list_id, "Save Port List", "edit_port_list");
22815  CHECK_PARAM_INVALID (name, "Save Port List", "edit_port_list");
22816  CHECK_PARAM_INVALID (comment, "Save Port List", "edit_port_list");
22817 
22818  /* Modify the Port List. */
22819 
22820  response = NULL;
22821  entity = NULL;
22822  ret = ompf (connection, credentials,
22823  &response,
22824  &entity,
22825  response_data,
22826  "<modify_port_list port_list_id=\"%s\">"
22827  "<name>%s</name>"
22828  "<comment>%s</comment>"
22829  "</modify_port_list>",
22830  port_list_id,
22831  name,
22832  comment);
22833 
22834  switch (ret)
22835  {
22836  case 0:
22837  case -1:
22838  break;
22839  case 1:
22840  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22841  return gsad_message (credentials,
22842  "Internal error", __FUNCTION__, __LINE__,
22843  "An internal error occurred while saving a Port List. "
22844  "The Port List was not saved. "
22845  "Diagnostics: Failure to send command to manager daemon.",
22846  "/omp?cmd=get_port_lists", response_data);
22847  case 2:
22848  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22849  return gsad_message (credentials,
22850  "Internal error", __FUNCTION__, __LINE__,
22851  "An internal error occurred while saving a Port List. "
22852  "It is unclear whether the Port List has been saved or not. "
22853  "Diagnostics: Failure to receive response from manager daemon.",
22854  "/omp?cmd=get_port_lists", response_data);
22855  default:
22856  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22857  return gsad_message (credentials,
22858  "Internal error", __FUNCTION__, __LINE__,
22859  "An internal error occurred while saving a Port List. "
22860  "It is unclear whether the Port List has been saved or not. "
22861  "Diagnostics: Internal Error.",
22862  "/omp?cmd=get_port_lists", response_data);
22863  }
22864 
22865  html = response_from_entity (connection, credentials, params, entity,
22866  (no_redirect && strcmp (no_redirect, "0")),
22867  NULL, "get_port_lists",
22868  NULL, "edit_port_list",
22869  "Save Port List", response_data);
22870  free_entity (entity);
22871  g_free (response);
22872  return html;
22873 }
22874 
22885 char *
22886 delete_port_list_omp (openvas_connection_t *connection,
22887  credentials_t * credentials, params_t *params,
22888  cmd_response_data_t* response_data)
22889 {
22890  return delete_resource (connection, "port_list", credentials, params, 0,
22891  "get_port_lists", response_data);
22892 }
22893 
22904 char *
22905 delete_trash_port_list_omp (openvas_connection_t *connection,
22906  credentials_t * credentials, params_t *params,
22907  cmd_response_data_t* response_data)
22908 {
22909  return delete_resource (connection, "port_list", credentials, params, 1,
22910  "get_trash", response_data);
22911 }
22912 
22923 char *
22924 delete_port_range_omp (openvas_connection_t *connection,
22925  credentials_t * credentials, params_t *params,
22926  cmd_response_data_t* response_data)
22927 {
22928  return delete_resource (connection, "port_range", credentials, params, 1,
22929  "edit_port_list", response_data);
22930 }
22931 
22942 char *
22943 import_port_list_omp (openvas_connection_t *connection,
22944  credentials_t * credentials, params_t *params,
22945  cmd_response_data_t* response_data)
22946 {
22947  const char *no_redirect;
22948  gchar *command, *html, *response;
22949  entity_t entity;
22950  int ret;
22951 
22952  no_redirect = params_value (params, "no_redirect");
22953 
22954  /* Create the port list. */
22955 
22956  response = NULL;
22957  entity = NULL;
22958  command = g_strdup_printf ("<create_port_list>"
22959  "%s"
22960  "</create_port_list>",
22961  params_value (params, "xml_file"));
22962  ret = omp (connection, credentials, &response, &entity, response_data,
22963  command);
22964  g_free (command);
22965  switch (ret)
22966  {
22967  case 0:
22968  case -1:
22969  break;
22970  case 1:
22971  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22972  return gsad_message (credentials,
22973  "Internal error", __FUNCTION__, __LINE__,
22974  "An internal error occurred while importing a port_list. "
22975  "The schedule remains the same. "
22976  "Diagnostics: Failure to send command to manager daemon.",
22977  "/omp?cmd=get_port_lists",
22978  response_data);
22979  case 2:
22980  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22981  return gsad_message (credentials,
22982  "Internal error", __FUNCTION__, __LINE__,
22983  "An internal error occurred while importing a port_list. "
22984  "It is unclear whether the schedule has been saved or not. "
22985  "Diagnostics: Failure to receive response from manager daemon.",
22986  "/omp?cmd=get_port_lists",
22987  response_data);
22988  default:
22989  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22990  return gsad_message (credentials,
22991  "Internal error", __FUNCTION__, __LINE__,
22992  "An internal error occurred while importing a port_list. "
22993  "It is unclear whether the schedule has been saved or not. "
22994  "Diagnostics: Internal Error.",
22995  "/omp?cmd=get_port_lists",
22996  response_data);
22997  }
22998 
22999  /* Cleanup, and return transformed XML. */
23000 
23001  html = response_from_entity (connection, credentials, params, entity,
23002  (no_redirect && strcmp (no_redirect, "0")),
23003  NULL, "get_port_lists",
23004  NULL, "new_port_list",
23005  "Import Port List", response_data);
23006  free_entity (entity);
23007  g_free (response);
23008  return html;
23009 }
23010 
23011 
23012 /* Roles. */
23013 
23025 static char *
23026 new_role (openvas_connection_t *connection, credentials_t *credentials,
23027  params_t *params, const char *extra_xml,
23028  cmd_response_data_t* response_data)
23029 {
23030  GString *xml;
23031  xml = g_string_new ("<new_role>");
23032  if (extra_xml)
23033  g_string_append (xml, extra_xml);
23034  g_string_append (xml, "</new_role>");
23035  return xsl_transform_omp (connection, credentials, params,
23036  g_string_free (xml, FALSE), response_data);
23037 }
23038 
23049 char *
23050 new_role_omp (openvas_connection_t *connection, credentials_t *credentials,
23051  params_t *params, cmd_response_data_t* response_data)
23052 {
23053  return new_role (connection, credentials, params, NULL, response_data);
23054 }
23055 
23066 char *
23067 delete_trash_role_omp (openvas_connection_t *connection,
23068  credentials_t * credentials, params_t *params,
23069  cmd_response_data_t* response_data)
23070 {
23071  return delete_resource (connection, "role", credentials, params, 1,
23072  "get_trash", response_data);
23073 }
23074 
23085 char *
23086 delete_role_omp (openvas_connection_t *connection, credentials_t * credentials,
23087  params_t *params, cmd_response_data_t* response_data)
23088 {
23089  return delete_resource (connection, "role", credentials, params, 0,
23090  "get_roles", response_data);
23091 }
23092 
23103 char *
23104 create_role_omp (openvas_connection_t *connection, credentials_t *credentials,
23105  params_t *params, cmd_response_data_t* response_data)
23106 {
23107  char *ret;
23108  gchar *response;
23109  const char *no_redirect, *name, *comment, *users;
23110  entity_t entity;
23111 
23112  no_redirect = params_value (params, "no_redirect");
23113  name = params_value (params, "name");
23114  comment = params_value (params, "comment");
23115  users = params_value (params, "users");
23116 
23117  CHECK_PARAM_INVALID (name, "Create Role", "new_role");
23118  CHECK_PARAM_INVALID (comment, "Create Role", "new_role");
23119  CHECK_PARAM_INVALID (users, "Create Role", "new_role");
23120 
23121  response = NULL;
23122  entity = NULL;
23123  switch (ompf (connection, credentials,
23124  &response,
23125  &entity,
23126  response_data,
23127  "<create_role>"
23128  "<name>%s</name>"
23129  "<comment>%s</comment>"
23130  "<users>%s</users>"
23131  "</create_role>",
23132  name,
23133  comment,
23134  users))
23135  {
23136  case 0:
23137  case -1:
23138  break;
23139  case 1:
23140  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23141  return gsad_message (credentials,
23142  "Internal error", __FUNCTION__, __LINE__,
23143  "An internal error occurred while creating a new role. "
23144  "No new role was created. "
23145  "Diagnostics: Failure to send command to manager daemon.",
23146  "/omp?cmd=get_targets", response_data);
23147  case 2:
23148  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23149  return gsad_message (credentials,
23150  "Internal error", __FUNCTION__, __LINE__,
23151  "An internal error occurred while creating a new role. "
23152  "It is unclear whether the role has been created or not. "
23153  "Diagnostics: Failure to receive response from manager daemon.",
23154  "/omp?cmd=get_roles", response_data);
23155  default:
23156  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23157  return gsad_message (credentials,
23158  "Internal error", __FUNCTION__, __LINE__,
23159  "An internal error occurred while creating a new role. "
23160  "It is unclear whether the role has been created or not. "
23161  "Diagnostics: Internal Error.",
23162  "/omp?cmd=get_roles", response_data);
23163  }
23164 
23165  if (entity_attribute (entity, "id"))
23166  params_add (params, "role_id", entity_attribute (entity, "id"));
23167  ret = response_from_entity (connection, credentials, params, entity,
23168  (no_redirect && strcmp (no_redirect, "0")),
23169  NULL, "get_roles",
23170  NULL, "new_role",
23171  "Create Role", response_data);
23172  free_entity (entity);
23173  g_free (response);
23174  return ret;
23175 }
23176 
23188 char *
23189 edit_role (openvas_connection_t *connection,
23190  credentials_t * credentials, params_t *params,
23191  const char *extra_xml, cmd_response_data_t* response_data)
23192 {
23193  gchar *html;
23194  GString *extra;
23195 
23196  extra = g_string_new ("");
23197 
23198  if (command_enabled (credentials, "GET_PERMISSIONS"))
23199  {
23200  gchar *response;
23201  entity_t entity;
23202 
23203  response = NULL;
23204  entity = NULL;
23205  switch (ompf (connection, credentials, &response, &entity, response_data,
23206  "<get_permissions"
23207  " filter=\"rows=-1 subject_type=role and subject_uuid=%s\"/>",
23208  params_value (params, "role_id")))
23209  {
23210  case 0:
23211  case -1:
23212  break;
23213  case 1:
23214  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23215  return gsad_message (credentials,
23216  "Internal error", __FUNCTION__, __LINE__,
23217  "An internal error occurred getting the permission list. "
23218  "Diagnostics: Failure to send command to manager daemon.",
23219  "/omp?cmd=get_roles", response_data);
23220  case 2:
23221  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23222  return gsad_message (credentials,
23223  "Internal error", __FUNCTION__, __LINE__,
23224  "An internal error occurred getting the permission list. "
23225  "Diagnostics: Failure to receive response from manager daemon.",
23226  "/omp?cmd=get_roles", response_data);
23227  default:
23228  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23229  return gsad_message (credentials,
23230  "Internal error", __FUNCTION__, __LINE__,
23231  "An internal error occurred getting the permission list. "
23232  "Diagnostics: Internal Error.",
23233  "/omp?cmd=get_roles", response_data);
23234  }
23235 
23236  g_string_append (extra, response);
23237 
23238  free_entity (entity);
23239  g_free (response);
23240  }
23241 
23242  if (command_enabled (credentials, "GET_GROUPS"))
23243  {
23244  gchar *response;
23245  entity_t entity;
23246 
23247  response = NULL;
23248  entity = NULL;
23249  switch (ompf (connection, credentials, &response, &entity, response_data,
23250  "<get_groups"
23251  " filter=\"rows=-1 owner=%s\"/>",
23252  credentials->username))
23253  {
23254  case 0:
23255  case -1:
23256  break;
23257  case 1:
23258  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23259  return gsad_message (credentials,
23260  "Internal error", __FUNCTION__, __LINE__,
23261  "An internal error occurred getting the group list. "
23262  "Diagnostics: Failure to send command to manager daemon.",
23263  "/omp?cmd=get_roles", response_data);
23264  case 2:
23265  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23266  return gsad_message (credentials,
23267  "Internal error", __FUNCTION__, __LINE__,
23268  "An internal error occurred getting the group list. "
23269  "Diagnostics: Failure to receive response from manager daemon.",
23270  "/omp?cmd=get_roles", response_data);
23271  default:
23272  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23273  return gsad_message (credentials,
23274  "Internal error", __FUNCTION__, __LINE__,
23275  "An internal error occurred getting the group list. "
23276  "Diagnostics: Internal Error.",
23277  "/omp?cmd=get_roles", response_data);
23278  }
23279 
23280  g_string_append (extra, response);
23281 
23282  free_entity (entity);
23283  g_free (response);
23284  }
23285 
23286  if (extra_xml)
23287  g_string_append (extra, extra_xml);
23288  html = edit_resource (connection, "role", credentials, params, NULL,
23289  extra->str, response_data);
23290  g_string_free (extra, TRUE);
23291  return html;
23292 }
23293 
23304 char *
23305 edit_role_omp (openvas_connection_t *connection,
23306  credentials_t * credentials, params_t *params,
23307  cmd_response_data_t* response_data)
23308 {
23309  return edit_role (connection, credentials, params, NULL, response_data);
23310 }
23311 
23323 static char *
23324 get_role (openvas_connection_t *connection, credentials_t * credentials,
23325  params_t *params, const char *extra_xml,
23326  cmd_response_data_t* response_data)
23327 {
23328  return get_one (connection, "role", credentials, params, extra_xml, NULL,
23329  response_data);
23330 }
23331 
23342 char *
23343 get_role_omp (openvas_connection_t *connection, credentials_t * credentials,
23344  params_t *params, cmd_response_data_t* response_data)
23345 {
23346  return get_role (connection, credentials, params, NULL, response_data);
23347 }
23348 
23360 static char *
23361 get_roles (openvas_connection_t *connection, credentials_t * credentials,
23362  params_t *params, const char *extra_xml,
23363  cmd_response_data_t* response_data)
23364 {
23365  return get_many (connection, "role", credentials, params, extra_xml, NULL,
23366  response_data);
23367 }
23368 
23379 char *
23380 get_roles_omp (openvas_connection_t *connection, credentials_t * credentials,
23381  params_t *params, cmd_response_data_t* response_data)
23382 {
23383  return get_roles (connection, credentials, params, NULL, response_data);
23384 }
23385 
23399 char *
23400 export_role_omp (openvas_connection_t *connection, credentials_t * credentials,
23401  params_t *params, enum content_type * content_type,
23402  char **content_disposition, gsize *content_length,
23403  cmd_response_data_t* response_data)
23404 {
23405  return export_resource (connection, "role", credentials, params, content_type,
23406  content_disposition, content_length, response_data);
23407 }
23408 
23423 char *
23424 export_roles_omp (openvas_connection_t *connection, credentials_t * credentials,
23425  params_t *params, enum content_type * content_type,
23426  char **content_disposition, gsize *content_length,
23427  cmd_response_data_t* response_data)
23428 {
23429  return export_many (connection, "role", credentials, params, content_type,
23430  content_disposition, content_length, response_data);
23431 }
23432 
23443 char *
23444 save_role_omp (openvas_connection_t *connection, credentials_t * credentials,
23445  params_t *params, cmd_response_data_t* response_data)
23446 {
23447  int ret;
23448  gchar *html, *response;
23449  const char *no_redirect, *role_id, *name, *comment, *users;
23450  entity_t entity;
23451 
23452  no_redirect = params_value (params, "no_redirect");
23453  role_id = params_value (params, "role_id");
23454  name = params_value (params, "name");
23455  comment = params_value (params, "comment");
23456  users = params_value (params, "users");
23457 
23458  CHECK_PARAM_INVALID (role_id, "Save Role", "edit_role");
23459  CHECK_PARAM_INVALID (name, "Save Role", "edit_role");
23460  CHECK_PARAM_INVALID (comment, "Save Role", "edit_role");
23461  CHECK_PARAM_INVALID (users, "Save Role", "edit_role");
23462 
23463  /* Modify the Role. */
23464 
23465  response = NULL;
23466  entity = NULL;
23467  ret = ompf (connection, credentials,
23468  &response,
23469  &entity,
23470  response_data,
23471  "<modify_role role_id=\"%s\">"
23472  "<name>%s</name>"
23473  "<comment>%s</comment>"
23474  "<users>%s</users>"
23475  "</modify_role>",
23476  role_id,
23477  name,
23478  comment,
23479  users);
23480 
23481  switch (ret)
23482  {
23483  case 0:
23484  case -1:
23485  break;
23486  case 1:
23487  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23488  return gsad_message (credentials,
23489  "Internal error", __FUNCTION__, __LINE__,
23490  "An internal error occurred while saving a role. "
23491  "The role was not saved. "
23492  "Diagnostics: Failure to send command to manager daemon.",
23493  "/omp?cmd=get_roles", response_data);
23494  case 2:
23495  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23496  return gsad_message (credentials,
23497  "Internal error", __FUNCTION__, __LINE__,
23498  "An internal error occurred while saving a role. "
23499  "It is unclear whether the role has been saved or not. "
23500  "Diagnostics: Failure to receive response from manager daemon.",
23501  "/omp?cmd=get_roles", response_data);
23502  default:
23503  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23504  return gsad_message (credentials,
23505  "Internal error", __FUNCTION__, __LINE__,
23506  "An internal error occurred while saving a role. "
23507  "It is unclear whether the role has been saved or not. "
23508  "Diagnostics: Internal Error.",
23509  "/omp?cmd=get_roles", response_data);
23510  }
23511 
23512  html = response_from_entity (connection, credentials, params, entity,
23513  (no_redirect && strcmp (no_redirect, "0")),
23514  NULL, "get_roles",
23515  NULL, "edit_role",
23516  "Save Role", response_data);
23517  free_entity (entity);
23518  g_free (response);
23519  return html;
23520 }
23521 
23522 
23523 /* Feeds. */
23524 
23535 char *
23536 get_feeds_omp (openvas_connection_t *connection, credentials_t * credentials,
23537  params_t *params, cmd_response_data_t* response_data)
23538 {
23539  entity_t entity;
23540  char *text = NULL;
23541  gchar *response;
23542  time_t now;
23543  struct tm *tm;
23544  gchar current_timestamp[30];
23545 
23546  if (openvas_connection_sendf (connection,
23547  "<commands>"
23548  "<get_feeds/>"
23549  "</commands>")
23550  == -1)
23551  {
23552  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23553  return gsad_message (credentials,
23554  "Internal error", __FUNCTION__, __LINE__,
23555  "An internal error occurred while getting the feed list. "
23556  "The current list of feeds is not available. "
23557  "Diagnostics: Failure to send command to manager daemon.",
23558  "/omp?cmd=get_tasks", response_data);
23559  }
23560 
23561  if (read_entity_and_text_c (connection, &entity, &text))
23562  {
23563  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23564  return gsad_message (credentials,
23565  "Internal error", __FUNCTION__, __LINE__,
23566  "An internal error occurred while getting the feed. "
23567  "The current list of feeds is not available. "
23568  "Diagnostics: Failure to receive response from manager daemon.",
23569  "/omp?cmd=get_tasks", response_data);
23570  }
23571 
23572  time (&now);
23573  tm = gmtime (&now);
23574  if (tm == NULL
23575  || (strftime (current_timestamp,
23576  29,
23577  "%Y-%m-%dT%H:%M:%S",
23578  tm)
23579  == 0))
23580  {
23581  current_timestamp[0] = '\0';
23582  }
23583 
23584  response = g_strdup_printf ("<get_feeds>"
23585  "%s"
23586  "<current_time_utc>%s</current_time_utc>"
23587  "</get_feeds>",
23588  text,
23589  current_timestamp);
23590 
23591  g_free (text);
23592 
23593  return xsl_transform_omp (connection, credentials, params, response,
23594  response_data);
23595 }
23596 
23610 static char*
23611 sync_feed (openvas_connection_t *connection, credentials_t * credentials,
23612  params_t *params, const char *sync_cmd, const char *action,
23613  const char *feed_name,
23614  cmd_response_data_t* response_data)
23615 {
23616  const char *no_redirect;
23617  entity_t entity;
23618  char *text = NULL;
23619  gchar *html, *msg;
23620 
23621  no_redirect = params_value (params, "no_redirect");
23622 
23623  if (openvas_connection_sendf (connection,
23624  "<%s/>",
23625  sync_cmd)
23626  == -1)
23627  {
23628  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23629 
23630  msg = g_strdup_printf
23631  ("An internal error occurred while synchronizing with %s. "
23632  "Feed synchronization is currently not available. "
23633  "Diagnostics: Failure to send command to manager daemon.",
23634  feed_name);
23635  html = gsad_message (credentials,
23636  "Internal error", __FUNCTION__, __LINE__,
23637  msg, "/omp?cmd=get_tasks", response_data);
23638  g_free (msg);
23639  return html;
23640  }
23641 
23642  if (read_entity_and_text_c (connection, &entity, &text))
23643  {
23644  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23645 
23646  msg = g_strdup_printf
23647  ("An internal error occurred while synchronizing with %s. "
23648  "Feed synchronization is currently not available. "
23649  "Diagnostics: Failure to receive response from manager daemon.",
23650  feed_name);
23651  html = gsad_message (credentials,
23652  "Internal error", __FUNCTION__, __LINE__,
23653  msg, "/omp?cmd=get_tasks", response_data);
23654  g_free (msg);
23655  return html;
23656  }
23657 
23658  html = response_from_entity (connection, credentials, params, entity,
23659  (no_redirect && strcmp (no_redirect, "0")),
23660  NULL, "get_feeds",
23661  NULL, "get_feeds",
23662  action, response_data);
23663 
23664  return html;
23665 }
23666 
23677 char *
23678 sync_feed_omp (openvas_connection_t *connection, credentials_t * credentials,
23679  params_t *params, cmd_response_data_t* response_data)
23680 {
23681  return sync_feed (connection, credentials, params,
23682  "sync_feed", "Synchronize Feed", "the NVT feed",
23683  response_data);
23684 }
23685 
23696 char *
23697 sync_scap_omp (openvas_connection_t *connection, credentials_t * credentials,
23698  params_t *params, cmd_response_data_t* response_data)
23699 {
23700  return sync_feed (connection, credentials, params,
23701  "sync_scap", "Synchronize Feed", "the SCAP feed",
23702  response_data);
23703 }
23704 
23715 char *
23716 sync_cert_omp (openvas_connection_t *connection, credentials_t * credentials,
23717  params_t *params, cmd_response_data_t* response_data)
23718 {
23719  return sync_feed (connection, credentials, params,
23720  "sync_cert", "Synchronize CERT Feed", "the CERT feed",
23721  response_data);
23722 }
23723 
23724 
23725 /* Filters. */
23726 
23738 char *
23739 get_filter (openvas_connection_t *connection, credentials_t * credentials,
23740  params_t *params, const char *extra_xml,
23741  cmd_response_data_t* response_data)
23742 {
23743  return get_one (connection, "filter", credentials, params, extra_xml,
23744  "alerts=\"1\"", response_data);
23745 }
23746 
23757 char *
23758 get_filter_omp (openvas_connection_t *connection, credentials_t * credentials,
23759  params_t *params, cmd_response_data_t* response_data)
23760 {
23761  return get_filter (connection, credentials, params, NULL, response_data);
23762 }
23763 
23775 char *
23776 get_filters (openvas_connection_t *connection, credentials_t * credentials,
23777  params_t *params, const char *extra_xml,
23778  cmd_response_data_t* response_data)
23779 {
23780  return get_many (connection, "filter", credentials, params, extra_xml, NULL,
23781  response_data);
23782 }
23783 
23794 char *
23795 get_filters_omp (openvas_connection_t *connection, credentials_t * credentials,
23796  params_t *params, cmd_response_data_t* response_data)
23797 {
23798  return get_filters (connection, credentials, params, NULL, response_data);
23799 }
23800 
23812 static char *
23813 new_filter (openvas_connection_t *connection, credentials_t *credentials,
23814  params_t *params, const char *extra_xml,
23815  cmd_response_data_t* response_data)
23816 {
23817  GString *xml;
23818  xml = g_string_new ("<new_filter>");
23819  if (extra_xml)
23820  g_string_append (xml, extra_xml);
23821  g_string_append (xml, "</new_filter>");
23822  return xsl_transform_omp (connection, credentials, params,
23823  g_string_free (xml, FALSE), response_data);
23824 }
23825 
23836 char *
23837 create_filter_omp (openvas_connection_t *connection, credentials_t *credentials,
23838  params_t *params, cmd_response_data_t* response_data)
23839 {
23840  gchar *html, *response;
23841  const char *no_redirect, *name, *comment, *term, *type;
23842  entity_t entity;
23843 
23844  no_redirect = params_value (params, "no_redirect");
23845  name = params_value (params, "name");
23846  comment = params_value (params, "comment");
23847  term = params_value (params, "term");
23848  type = params_value (params, "optional_resource_type");
23849 
23850  CHECK_PARAM_INVALID (name, "Create Filter", "new_filter");
23851  CHECK_PARAM_INVALID (comment, "Create Filter", "new_filter");
23852  CHECK_PARAM_INVALID (term, "Create Filter", "new_filter");
23853  CHECK_PARAM_INVALID (type, "Create Filter", "new_filter");
23854 
23855  switch (ompf (connection, credentials,
23856  &response,
23857  &entity,
23858  response_data,
23859  "<create_filter>"
23860  "<name>%s</name>"
23861  "<comment>%s</comment>"
23862  "<term>%s</term>"
23863  "<type>%s</type>"
23864  "</create_filter>",
23865  name,
23866  comment,
23867  term,
23868  type))
23869  {
23870  case 0:
23871  case -1:
23872  break;
23873  case 1:
23874  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23875  return gsad_message (credentials,
23876  "Internal error", __FUNCTION__, __LINE__,
23877  "An internal error occurred while creating a new alert. "
23878  "No new alert was created. "
23879  "Diagnostics: Failure to send command to manager daemon.",
23880  "/omp?cmd=get_alerts", response_data);
23881  case 2:
23882  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23883  return gsad_message (credentials,
23884  "Internal error", __FUNCTION__, __LINE__,
23885  "An internal error occurred while creating a new alert. "
23886  "It is unclear whether the alert has been created or not. "
23887  "Diagnostics: Failure to receive response from manager daemon.",
23888  "/omp?cmd=get_alerts", response_data);
23889  default:
23890  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23891  return gsad_message (credentials,
23892  "Internal error", __FUNCTION__, __LINE__,
23893  "An internal error occurred while creating a new alert. "
23894  "It is unclear whether the alert has been created or not. "
23895  "Diagnostics: Internal Error.",
23896  "/omp?cmd=get_alerts", response_data);
23897  }
23898 
23899  if (omp_success (entity))
23900  {
23901  const char *filter_id;
23902 
23903  filter_id = entity_attribute (entity, "id");
23904  if (filter_id && strlen (filter_id))
23905  {
23906  param_t *param;
23907  param = params_add (params, "filt_id", filter_id);
23908  param->valid = 1;
23909  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
23910  }
23911  }
23912 
23913  if (entity_attribute (entity, "id"))
23914  params_add (params, "filter_id", entity_attribute (entity, "id"));
23915  html = response_from_entity (connection, credentials, params, entity,
23916  (no_redirect && strcmp (no_redirect, "0")),
23917  NULL, "get_filters",
23918  NULL, "new_filter",
23919  "Create Filter", response_data);
23920  free_entity (entity);
23921  g_free (response);
23922  return html;
23923 }
23924 
23935 char *
23936 delete_trash_filter_omp (openvas_connection_t *connection,
23937  credentials_t * credentials, params_t *params,
23938  cmd_response_data_t* response_data)
23939 {
23940  return delete_resource (connection, "filter", credentials, params, 1,
23941  "get_trash", response_data);
23942 }
23943 
23954 char *
23955 delete_filter_omp (openvas_connection_t *connection,
23956  credentials_t * credentials, params_t *params,
23957  cmd_response_data_t* response_data)
23958 {
23959  param_t *filt_id, *id;
23960  GList *list;
23961 
23962  filt_id = params_get (params, "filt_id");
23963  id = params_get (params, "filter_id");
23964  if (id && id->value && filt_id && filt_id->value
23965  && (strcmp (id->value, filt_id->value) == 0))
23966  // TODO: Add params_remove.
23967  filt_id->value = NULL;
23968 
23969  /* remove to be deleted key from the user credentials */
23970  if (id && id->value)
23971  {
23972  find_by_value_t find;
23973 
23974  init_find_by_value (&find, id->value);
23975 
23976  g_tree_foreach (credentials->last_filt_ids, (GTraverseFunc)find_by_value,
23977  &find);
23978  if (find.keys != NULL)
23979  {
23980  list = g_list_first (find.keys);
23981 
23982  while (list != NULL)
23983  {
23984  g_debug ("%s removing filter from last filter ids for %s\n",
23985  __FUNCTION__, (char *)list->data);
23986  g_tree_remove (credentials->last_filt_ids, list->data);
23987  list = g_list_next (find.keys);
23988  }
23989  }
23990 
23991  free_find_by_value(&find);
23992  }
23993 
23994  return delete_resource (connection, "filter", credentials, params, 0,
23995  "get_filters", response_data);
23996 }
23997 
24009 char *
24010 edit_filter (openvas_connection_t *connection, credentials_t * credentials,
24011  params_t *params, const char *extra_xml,
24012  cmd_response_data_t* response_data)
24013 {
24014  return edit_resource (connection, "filter", credentials, params, NULL,
24015  extra_xml, response_data);
24016 }
24017 
24028 char *
24029 edit_filter_omp (openvas_connection_t *connection, credentials_t * credentials,
24030  params_t *params, cmd_response_data_t* response_data)
24031 {
24032  return edit_filter (connection, credentials, params, NULL, response_data);
24033 }
24034 
24048 char *
24049 export_filter_omp (openvas_connection_t *connection,
24050  credentials_t * credentials, params_t *params,
24051  enum content_type * content_type, char **content_disposition,
24052  gsize *content_length, cmd_response_data_t* response_data)
24053 {
24054  return export_resource (connection, "filter", credentials, params,
24055  content_type, content_disposition, content_length,
24056  response_data);
24057 }
24058 
24073 char *
24074 export_filters_omp (openvas_connection_t *connection,
24075  credentials_t * credentials, params_t *params,
24076  enum content_type * content_type, char **content_disposition,
24077  gsize *content_length, cmd_response_data_t* response_data)
24078 {
24079  return export_many (connection, "filter", credentials, params, content_type,
24080  content_disposition, content_length, response_data);
24081 }
24082 
24093 char *
24094 new_filter_omp (openvas_connection_t *connection, credentials_t *credentials,
24095  params_t *params, cmd_response_data_t* response_data)
24096 {
24097  return new_filter (connection, credentials, params, NULL, response_data);
24098 }
24099 
24110 char *
24111 save_filter_omp (openvas_connection_t *connection, credentials_t * credentials,
24112  params_t *params, cmd_response_data_t* response_data)
24113 {
24114  entity_t entity;
24115  gchar *html, *response;
24116  const char *no_redirect, *filter_id, *name, *comment, *term, *type;
24117 
24118  no_redirect = params_value (params, "no_redirect");
24119  filter_id = params_value (params, "filter_id");
24120  name = params_value (params, "name");
24121  comment = params_value (params, "comment");
24122  term = params_value (params, "term");
24123  type = params_value (params, "optional_resource_type");
24124 
24125  CHECK_PARAM_INVALID (filter_id, "Save Filter", "edit_filter");
24126  CHECK_PARAM_INVALID (name, "Save Filter", "edit_filter");
24127  CHECK_PARAM_INVALID (comment, "Save Filter", "edit_filter");
24128  CHECK_PARAM_INVALID (term, "Save Filter", "edit_filter");
24129  CHECK_PARAM_INVALID (type, "Save Filter", "edit_filter");
24130 
24131  {
24132  int ret;
24133 
24134  /* Modify the filter. */
24135 
24136  ret = openvas_connection_sendf_xml (connection,
24137  "<modify_filter filter_id=\"%s\">"
24138  "<name>%s</name>"
24139  "<comment>%s</comment>"
24140  "<term>%s</term>"
24141  "<type>%s</type>"
24142  "</modify_filter>",
24143  filter_id,
24144  name,
24145  comment,
24146  term,
24147  type);
24148 
24149  if (ret == -1)
24150  {
24151  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24152  return gsad_message (credentials,
24153  "Internal error", __FUNCTION__, __LINE__,
24154  "An internal error occurred while modifying a filter. "
24155  "The filter was not modified. "
24156  "Diagnostics: Failure to send command to manager daemon.",
24157  "/omp?cmd=get_filters", response_data);
24158  }
24159 
24160  entity = NULL;
24161  if (read_entity_and_text_c (connection, &entity, &response))
24162  {
24163  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24164  return gsad_message (credentials,
24165  "Internal error", __FUNCTION__, __LINE__,
24166  "An internal error occurred while modifying a filter. "
24167  "It is unclear whether the filter has been modified or not. "
24168  "Diagnostics: Failure to receive response from manager daemon.",
24169  "/omp?cmd=get_filters", response_data);
24170  }
24171 
24172  }
24173 
24174  /* Pass response to handler of following page. */
24175 
24176  html = response_from_entity (connection, credentials, params, entity,
24177  (no_redirect && strcmp (no_redirect, "0")),
24178  NULL, "get_filters",
24179  NULL, "edit_filter",
24180  "Save Filter", response_data);
24181 
24182  free_entity (entity);
24183  g_free (response);
24184  return html;
24185 }
24186 
24187 
24188 /* Schedules. */
24189 
24201 char *
24202 edit_schedule (openvas_connection_t *connection, credentials_t * credentials,
24203  params_t *params, const char *extra_xml,
24204  cmd_response_data_t* response_data)
24205 {
24206  return edit_resource (connection, "schedule", credentials, params,
24207  "tasks=\"1\"", extra_xml, response_data);
24208 }
24209 
24220 char *
24221 edit_schedule_omp (openvas_connection_t *connection,
24222  credentials_t * credentials, params_t *params,
24223  cmd_response_data_t* response_data)
24224 {
24225  return edit_schedule (connection, credentials, params, NULL, response_data);
24226 }
24227 
24241 char *
24242 export_schedule_omp (openvas_connection_t *connection,
24243  credentials_t * credentials, params_t *params,
24244  enum content_type * content_type,
24245  char **content_disposition, gsize *content_length,
24246  cmd_response_data_t* response_data)
24247 {
24248  return export_resource (connection, "schedule", credentials, params,
24249  content_type, content_disposition, content_length,
24250  response_data);
24251 }
24252 
24266 char *
24267 export_schedules_omp (openvas_connection_t *connection,
24268  credentials_t * credentials, params_t *params,
24269  enum content_type * content_type,
24270  char **content_disposition, gsize *content_length,
24271  cmd_response_data_t* response_data)
24272 {
24273  return export_many (connection, "schedule", credentials, params, content_type,
24274  content_disposition, content_length, response_data);
24275 }
24276 
24287 char *
24288 save_schedule_omp (openvas_connection_t *connection,
24289  credentials_t * credentials, params_t *params,
24290  cmd_response_data_t* response_data)
24291 {
24292  gchar *response;
24293  entity_t entity;
24294  const char *no_redirect, *schedule_id, *name, *comment;
24295  const char *hour, *minute, *day_of_month, *month, *year, *timezone;
24296  const char *period, *period_unit, *duration, *duration_unit;
24297  char *ret;
24298 
24299  no_redirect = params_value (params, "no_redirect");
24300  schedule_id = params_value (params, "schedule_id");
24301  name = params_value (params, "name");
24302  comment = params_value (params, "comment");
24303  hour = params_value (params, "hour");
24304  minute = params_value (params, "minute");
24305  day_of_month = params_value (params, "day_of_month");
24306  duration = params_value (params, "duration");
24307  duration_unit = params_value (params, "duration_unit");
24308  month = params_value (params, "month");
24309  period = params_value (params, "period");
24310  period_unit = params_value (params, "period_unit");
24311  year = params_value (params, "year");
24312  timezone = params_value (params, "timezone");
24313 
24314  CHECK_PARAM_INVALID (schedule_id, "Save Schedule", "edit_schedule");
24315  CHECK_PARAM_INVALID (name, "Save Schedule", "edit_schedule");
24316  CHECK_PARAM_INVALID (comment, "Save Schedule", "edit_schedule");
24317  CHECK_PARAM_INVALID (hour, "Save Schedule", "edit_schedule");
24318  CHECK_PARAM_INVALID (minute, "Save Schedule", "edit_schedule");
24319  CHECK_PARAM_INVALID (day_of_month, "Save Schedule", "edit_schedule");
24320  CHECK_PARAM_INVALID (duration, "Save Schedule", "edit_schedule");
24321  CHECK_PARAM_INVALID (duration_unit, "Save Schedule", "edit_schedule");
24322  CHECK_PARAM_INVALID (month, "Save Schedule", "edit_schedule");
24323  CHECK_PARAM_INVALID (period, "Save Schedule", "edit_schedule");
24324  CHECK_PARAM_INVALID (period_unit, "Save Schedule", "edit_schedule");
24325  CHECK_PARAM_INVALID (year, "Save Schedule", "edit_schedule");
24326  CHECK_PARAM_INVALID (timezone, "Save Schedule", "edit_schedule");
24327 
24328  response = NULL;
24329  entity = NULL;
24330  switch (ompf (connection, credentials,
24331  &response,
24332  &entity,
24333  response_data,
24334  "<modify_schedule schedule_id=\"%s\">"
24335  "<name>%s</name>"
24336  "<comment>%s</comment>"
24337  "<first_time>"
24338  "<hour>%s</hour>"
24339  "<minute>%s</minute>"
24340  "<day_of_month>%s</day_of_month>"
24341  "<month>%s</month>"
24342  "<year>%s</year>"
24343  "</first_time>"
24344  "<timezone>%s</timezone>"
24345  "<period>"
24346  "<unit>%s</unit>"
24347  "%s"
24348  "</period>"
24349  "<duration>"
24350  "<unit>%s</unit>"
24351  "%s"
24352  "</duration>"
24353  "</modify_schedule>",
24354  schedule_id,
24355  name ? name : "",
24356  comment ? comment : "",
24357  hour,
24358  minute,
24359  day_of_month,
24360  month,
24361  year,
24362  timezone,
24363  (strcmp (period_unit, "")
24364  ? period_unit
24365  : "second"),
24366  period,
24367  (strcmp (duration_unit, "")
24368  ? duration_unit
24369  : "second"),
24370  duration))
24371  {
24372  case 0:
24373  case -1:
24374  break;
24375  case 1:
24376  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24377  return gsad_message (credentials,
24378  "Internal error", __FUNCTION__, __LINE__,
24379  "An internal error occurred while saving a schedule. "
24380  "The schedule remains the same. "
24381  "Diagnostics: Failure to send command to manager daemon.",
24382  "/omp?cmd=get_schedules", response_data);
24383  case 2:
24384  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24385  return gsad_message (credentials,
24386  "Internal error", __FUNCTION__, __LINE__,
24387  "An internal error occurred while saving a schedule. "
24388  "It is unclear whether the schedule has been saved or not. "
24389  "Diagnostics: Failure to receive response from manager daemon.",
24390  "/omp?cmd=get_schedules", response_data);
24391  default:
24392  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24393  return gsad_message (credentials,
24394  "Internal error", __FUNCTION__, __LINE__,
24395  "An internal error occurred while saving a schedule. "
24396  "It is unclear whether the schedule has been saved or not. "
24397  "Diagnostics: Internal Error.",
24398  "/omp?cmd=get_schedules", response_data);
24399  }
24400 
24401  ret = response_from_entity (connection, credentials, params, entity,
24402  (no_redirect && strcmp (no_redirect, "0")),
24403  NULL, "get_schedules",
24404  NULL, "edit_schedule",
24405  "Save Schedule", response_data);
24406  free_entity (entity);
24407  g_free (response);
24408  return ret;
24409 }
24410 
24411 
24412 /* Users. */
24413 
24425 static char *
24426 new_user (openvas_connection_t *connection, credentials_t *credentials,
24427  params_t *params, const char *extra_xml,
24428  cmd_response_data_t* response_data)
24429 {
24430  GString *xml;
24431 
24432  xml = g_string_new ("<new_user>");
24433  if (extra_xml)
24434  g_string_append (xml, extra_xml);
24435 
24436  if (command_enabled (credentials, "DESCRIBE_AUTH"))
24437  {
24438  gchar *response;
24439  entity_t entity;
24440 
24441  response = NULL;
24442  entity = NULL;
24443  switch (omp (connection, credentials, &response, &entity, response_data,
24444  "<describe_auth/>"))
24445  {
24446  case 0:
24447  case -1:
24448  break;
24449  case 1:
24450  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24451  return gsad_message (credentials,
24452  "Internal error", __FUNCTION__, __LINE__,
24453  "An internal error occurred getting the auth list. "
24454  "No new user was created. "
24455  "Diagnostics: Failure to send command to manager daemon.",
24456  "/omp?cmd=get_users", response_data);
24457  case 2:
24458  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24459  return gsad_message (credentials,
24460  "Internal error", __FUNCTION__, __LINE__,
24461  "An internal error occurred getting the auth list. "
24462  "No new user was created. "
24463  "Diagnostics: Failure to receive response from manager daemon.",
24464  "/omp?cmd=get_users", response_data);
24465  default:
24466  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24467  return gsad_message (credentials,
24468  "Internal error", __FUNCTION__, __LINE__,
24469  "An internal error occurred getting the auth list. "
24470  "No new user was created. "
24471  "Diagnostics: Internal Error.",
24472  "/omp?cmd=get_users", response_data);
24473  }
24474 
24475  g_string_append (xml, response);
24476 
24477  free_entity (entity);
24478  g_free (response);
24479  }
24480 
24481  if (command_enabled (credentials, "GET_GROUPS"))
24482  {
24483  gchar *response;
24484  entity_t entity;
24485 
24486  response = NULL;
24487  entity = NULL;
24488  switch (omp (connection, credentials, &response, &entity, response_data,
24489  "<get_groups filter=\"permission=modify_group\"/>"))
24490  {
24491  case 0:
24492  case -1:
24493  break;
24494  case 1:
24495  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24496  return gsad_message (credentials,
24497  "Internal error", __FUNCTION__, __LINE__,
24498  "An internal error occurred getting the group list. "
24499  "No new user was created. "
24500  "Diagnostics: Failure to send command to manager daemon.",
24501  "/omp?cmd=get_users", response_data);
24502  case 2:
24503  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24504  return gsad_message (credentials,
24505  "Internal error", __FUNCTION__, __LINE__,
24506  "An internal error occurred getting the group list. "
24507  "No new user was created. "
24508  "Diagnostics: Failure to receive response from manager daemon.",
24509  "/omp?cmd=get_users", response_data);
24510  default:
24511  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24512  return gsad_message (credentials,
24513  "Internal error", __FUNCTION__, __LINE__,
24514  "An internal error occurred getting the group list. "
24515  "No new user was created. "
24516  "Diagnostics: Internal Error.",
24517  "/omp?cmd=get_users", response_data);
24518  }
24519 
24520  g_string_append (xml, response);
24521 
24522  free_entity (entity);
24523  g_free (response);
24524  }
24525 
24526  if (command_enabled (credentials, "GET_ROLES"))
24527  {
24528  gchar *response;
24529  entity_t entity;
24530 
24531  response = NULL;
24532  entity = NULL;
24533  switch (omp (connection, credentials, &response, &entity, response_data,
24534  "<get_roles/>"))
24535  {
24536  case 0:
24537  case -1:
24538  break;
24539  case 1:
24540  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24541  return gsad_message (credentials,
24542  "Internal error", __FUNCTION__, __LINE__,
24543  "An internal error occurred getting the role list. "
24544  "No new user was created. "
24545  "Diagnostics: Failure to send command to manager daemon.",
24546  "/omp?cmd=get_users", response_data);
24547  case 2:
24548  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24549  return gsad_message (credentials,
24550  "Internal error", __FUNCTION__, __LINE__,
24551  "An internal error occurred getting the role list. "
24552  "No new user was created. "
24553  "Diagnostics: Failure to receive response from manager daemon.",
24554  "/omp?cmd=get_users", response_data);
24555  default:
24556  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24557  return gsad_message (credentials,
24558  "Internal error", __FUNCTION__, __LINE__,
24559  "An internal error occurred getting the role list. "
24560  "No new user was created. "
24561  "Diagnostics: Internal Error.",
24562  "/omp?cmd=get_users", response_data);
24563  }
24564 
24565  g_string_append (xml, response);
24566 
24567  free_entity (entity);
24568  g_free (response);
24569  }
24570 
24571  g_string_append (xml, "</new_user>");
24572  return xsl_transform_omp (connection, credentials, params,
24573  g_string_free (xml, FALSE), response_data);
24574 }
24575 
24586 char *
24587 new_user_omp (openvas_connection_t *connection, credentials_t *credentials,
24588  params_t *params, cmd_response_data_t* response_data)
24589 {
24590  return new_user (connection, credentials, params, NULL, response_data);
24591 }
24592 
24603 char *
24604 delete_user_omp (openvas_connection_t *connection, credentials_t * credentials,
24605  params_t *params, cmd_response_data_t* response_data)
24606 {
24607  return delete_resource (connection, "user", credentials, params, 0,
24608  "get_users", response_data);
24609 }
24610 
24622 static char *
24623 delete_user_confirm (openvas_connection_t *connection,
24624  credentials_t *credentials, params_t *params,
24625  const char *extra_xml, cmd_response_data_t* response_data)
24626 {
24627  GString *xml;
24628  gchar *response = NULL;
24629  entity_t entity = NULL;
24630 
24631  if (command_enabled (credentials, "GET_USERS"))
24632  {
24633  switch (omp (connection, credentials, &response, &entity, response_data,
24634  "<get_users filter=\"first=1 rows=-1\"/>"))
24635  {
24636  case 0:
24637  case -1:
24638  break;
24639  case 1:
24640  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24641  return gsad_message (credentials,
24642  "Internal error", __FUNCTION__, __LINE__,
24643  "An internal error occurred getting the user list. "
24644  "Diagnostics: Failure to send command to manager daemon.",
24645  "/omp?cmd=get_users", response_data);
24646  case 2:
24647  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24648  return gsad_message (credentials,
24649  "Internal error", __FUNCTION__, __LINE__,
24650  "An internal error occurred getting the user list. "
24651  "Diagnostics: Failure to receive response from manager daemon.",
24652  "/omp?cmd=get_users", response_data);
24653  default:
24654  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24655  return gsad_message (credentials,
24656  "Internal error", __FUNCTION__, __LINE__,
24657  "An internal error occurred getting the user list. "
24658  "Diagnostics: Internal Error.",
24659  "/omp?cmd=get_users", response_data);
24660  }
24661  }
24662 
24663  xml = g_string_new ("<delete_user>");
24664  if (extra_xml)
24665  g_string_append (xml, extra_xml);
24666 
24667  if (response)
24668  g_string_append (xml, response);
24669  free_entity (entity);
24670  g_free (response);
24671 
24672  if (extra_xml)
24673  g_string_append (xml, extra_xml);
24674 
24675  g_string_append (xml, "</delete_user>");
24676  return xsl_transform_omp (connection, credentials, params,
24677  g_string_free (xml, FALSE), response_data);
24678 }
24679 
24690 char *
24691 delete_user_confirm_omp (openvas_connection_t *connection,
24692  credentials_t * credentials, params_t *params,
24693  cmd_response_data_t* response_data)
24694 {
24695  return delete_user_confirm (connection, credentials, params, NULL,
24696  response_data);
24697 }
24698 
24710 char *
24711 get_user (openvas_connection_t *connection, credentials_t * credentials,
24712  params_t *params, const char *extra_xml,
24713  cmd_response_data_t* response_data)
24714 {
24715  gchar *html;
24716  GString *extra;
24717 
24718  extra = g_string_new ("");
24719  if (extra_xml)
24720  g_string_append (extra, extra_xml);
24721  if (command_enabled (credentials, "DESCRIBE_AUTH"))
24722  {
24723  gchar *response;
24724  entity_t entity;
24725 
24726  response = NULL;
24727  entity = NULL;
24728  switch (omp (connection, credentials, &response, &entity, response_data,
24729  "<describe_auth/>"))
24730  {
24731  case 0:
24732  case -1:
24733  break;
24734  case 1:
24735  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24736  return gsad_message (credentials,
24737  "Internal error", __FUNCTION__, __LINE__,
24738  "An internal error occurred getting the auth list. "
24739  "Diagnostics: Failure to send command to manager daemon.",
24740  "/omp?cmd=get_users", response_data);
24741  case 2:
24742  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24743  return gsad_message (credentials,
24744  "Internal error", __FUNCTION__, __LINE__,
24745  "An internal error occurred getting the auth list. "
24746  "Diagnostics: Failure to receive response from manager daemon.",
24747  "/omp?cmd=get_users", response_data);
24748  default:
24749  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24750  return gsad_message (credentials,
24751  "Internal error", __FUNCTION__, __LINE__,
24752  "An internal error occurred getting the auth list. "
24753  "Diagnostics: Internal Error.",
24754  "/omp?cmd=get_users", response_data);
24755  }
24756 
24757  g_string_append (extra, response);
24758 
24759  free_entity (entity);
24760  g_free (response);
24761  }
24762  html = get_one (connection, "user", credentials, params, extra->str, NULL,
24763  response_data);
24764  g_string_free (extra, TRUE);
24765  return html;
24766 }
24767 
24778 char *
24779 get_user_omp (openvas_connection_t *connection, credentials_t * credentials,
24780  params_t *params, cmd_response_data_t* response_data)
24781 {
24782  return get_user (connection, credentials, params, NULL, response_data);
24783 }
24784 
24796 static char *
24797 get_users (openvas_connection_t *connection, credentials_t * credentials,
24798  params_t *params, const char *extra_xml,
24799  cmd_response_data_t* response_data)
24800 {
24801  gchar *html;
24802  GString *extra;
24803 
24804  extra = g_string_new ("");
24805  if (command_enabled (credentials, "DESCRIBE_AUTH"))
24806  {
24807  gchar *response;
24808  entity_t entity;
24809 
24810  response = NULL;
24811  entity = NULL;
24812  switch (omp (connection, credentials, &response, &entity, response_data,
24813  "<describe_auth/>"))
24814  {
24815  case 0:
24816  case -1:
24817  break;
24818  case 1:
24819  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24820  return gsad_message (credentials,
24821  "Internal error", __FUNCTION__, __LINE__,
24822  "An internal error occurred getting the auth list. "
24823  "Diagnostics: Failure to send command to manager daemon.",
24824  "/omp?cmd=get_users", response_data);
24825  case 2:
24826  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24827  return gsad_message (credentials,
24828  "Internal error", __FUNCTION__, __LINE__,
24829  "An internal error occurred getting the auth list. "
24830  "Diagnostics: Failure to receive response from manager daemon.",
24831  "/omp?cmd=get_users", response_data);
24832  default:
24833  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24834  return gsad_message (credentials,
24835  "Internal error", __FUNCTION__, __LINE__,
24836  "An internal error occurred getting the auth list. "
24837  "Diagnostics: Internal Error.",
24838  "/omp?cmd=get_users", response_data);
24839  }
24840 
24841  g_string_append (extra, response);
24842 
24843  free_entity (entity);
24844  g_free (response);
24845  }
24846  if (extra_xml)
24847  g_string_append (extra, extra_xml);
24848  html = get_many (connection, "user", credentials, params, extra->str, NULL,
24849  response_data);
24850  g_string_free (extra, TRUE);
24851  return html;
24852 }
24853 
24864 char *
24865 get_users_omp (openvas_connection_t *connection, credentials_t * credentials,
24866  params_t *params, cmd_response_data_t* response_data)
24867 {
24868  return get_users (connection, credentials, params, NULL, response_data);
24869 }
24870 
24881 char *
24882 create_user_omp (openvas_connection_t *connection, credentials_t * credentials,
24883  params_t *params, cmd_response_data_t* response_data)
24884 {
24885  const char *no_redirect;
24886  const char *name, *password, *hosts, *hosts_allow, *ifaces, *ifaces_allow;
24887  const char *auth_method;
24888  int ret;
24889  params_t *groups, *roles;
24890  GString *group_elements, *role_elements, *string;
24891  gchar *buf, *response, *html;
24892  entity_t entity;
24893 
24894  no_redirect = params_value (params, "no_redirect");
24895  name = params_value (params, "login");
24896  password = params_value (params, "password");
24897  hosts = params_value (params, "access_hosts");
24898  hosts_allow = params_value (params, "hosts_allow");
24899  ifaces = params_value (params, "access_ifaces");
24900  ifaces_allow = params_value (params, "ifaces_allow");
24901  auth_method = params_value (params, "auth_method");
24902 
24903  CHECK_PARAM_INVALID (name, "Create User", "new_user");
24904  CHECK_PARAM_INVALID (password, "Create User", "new_user");
24905  CHECK_PARAM_INVALID (hosts, "Create User", "new_user");
24906  CHECK_PARAM_INVALID (hosts_allow, "Create User", "new_user");
24907  CHECK_PARAM_INVALID (ifaces, "Create User", "new_user");
24908  CHECK_PARAM_INVALID (ifaces_allow, "Create User", "new_user");
24909 
24910  /* Create the user. */
24911 
24912  string = g_string_new ("<create_user>");
24913  buf = g_markup_printf_escaped ("<name>%s</name>"
24914  "<password>%s</password>",
24915  name,
24916  password);
24917 
24918  g_string_append (string, buf);
24919  g_free (buf);
24920 
24921  group_elements = g_string_new ("<groups>");
24922  if (params_given (params, "group_id_optional:"))
24923  groups = params_values (params, "group_id_optional:");
24924  else
24925  groups = params_values (params, "group_ids:");
24926 
24927  if (groups)
24928  {
24929  params_iterator_t iter;
24930  char *name;
24931  param_t *param;
24932 
24933  params_iterator_init (&iter, groups);
24934  while (params_iterator_next (&iter, &name, &param))
24935  {
24936  if (param->value && strcmp (param->value, "--"))
24937  g_string_append_printf (group_elements,
24938  "<group id=\"%s\"/>",
24939  param->value ? param->value : "");
24940  }
24941  }
24942  g_string_append (string, group_elements->str);
24943  g_string_free (group_elements, TRUE);
24944  g_string_append (string, "</groups>");
24945 
24946  role_elements = g_string_new ("");
24947  if (params_given (params, "role_id_optional:"))
24948  roles = params_values (params, "role_id_optional:");
24949  else
24950  roles = params_values (params, "role_ids:");
24951 
24952  if (roles)
24953  {
24954  params_iterator_t iter;
24955  char *name;
24956  param_t *param;
24957 
24958  params_iterator_init (&iter, roles);
24959  while (params_iterator_next (&iter, &name, &param))
24960  {
24961  if (param->value && strcmp (param->value, "--"))
24962  g_string_append_printf (role_elements,
24963  "<role id=\"%s\"/>",
24964  param->value ? param->value : "");
24965  }
24966  }
24967  g_string_append (string, role_elements->str);
24968  g_string_free (role_elements, TRUE);
24969 
24970  buf = g_markup_printf_escaped ("<hosts allow=\"%s\">%s</hosts>"
24971  "<ifaces allow=\"%s\">%s</ifaces>",
24972  hosts_allow, hosts, ifaces_allow, ifaces);
24973  g_string_append (string, buf);
24974  g_free (buf);
24975  if (auth_method && !strcmp (auth_method, "1"))
24976  g_string_append
24977  (string, "<sources><source>ldap_connect</source></sources>");
24978  else if (auth_method && !strcmp (auth_method, "2"))
24979  g_string_append
24980  (string, "<sources><source>radius_connect</source></sources>");
24981  g_string_append (string, "</create_user>");
24982 
24983  buf = g_string_free (string, FALSE);
24984 
24985  response = NULL;
24986  entity = NULL;
24987  ret = omp (connection, credentials, &response, &entity, response_data, buf);
24988  g_free (buf);
24989  switch (ret)
24990  {
24991  case 0:
24992  case -1:
24993  break;
24994  case 1:
24995  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24996  return gsad_message (credentials,
24997  "Internal error", __FUNCTION__, __LINE__,
24998  "An internal error occurred while creating a new user. "
24999  "No new user was created. "
25000  "Diagnostics: Failure to send command to manager daemon.",
25001  "/omp?cmd=get_users", response_data);
25002  case 2:
25003  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25004  return gsad_message (credentials,
25005  "Internal error", __FUNCTION__, __LINE__,
25006  "An internal error occurred while creating a new user. "
25007  "It is unclear whether the user has been created or not. "
25008  "Diagnostics: Failure to receive response from manager daemon.",
25009  "/omp?cmd=get_users", response_data);
25010  default:
25011  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25012  return gsad_message (credentials,
25013  "Internal error", __FUNCTION__, __LINE__,
25014  "An internal error occurred while creating a new user. "
25015  "It is unclear whether the user has been created or not. "
25016  "Diagnostics: Internal Error.",
25017  "/omp?cmd=get_users", response_data);
25018  }
25019 
25020  if (entity_attribute (entity, "id"))
25021  params_add (params, "user_id", entity_attribute (entity, "id"));
25022  html = response_from_entity (connection, credentials, params, entity,
25023  (no_redirect && strcmp (no_redirect, "0")),
25024  NULL, "get_users",
25025  NULL, "new_user",
25026  "Create User", response_data);
25027  free_entity (entity);
25028  g_free (response);
25029  return html;
25030 }
25031 
25043 char *
25044 edit_user (openvas_connection_t *connection, credentials_t * credentials,
25045  params_t *params, const char *extra_xml,
25046  cmd_response_data_t* response_data)
25047 {
25048  gchar *html;
25049  GString *extra;
25050 
25051  extra = g_string_new ("");
25052  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25053  {
25054  gchar *response;
25055  entity_t entity;
25056 
25057  response = NULL;
25058  entity = NULL;
25059  switch (omp (connection, credentials, &response, &entity, response_data,
25060  "<describe_auth/>"))
25061  {
25062  case 0:
25063  case -1:
25064  break;
25065  case 1:
25066  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25067  return gsad_message (credentials,
25068  "Internal error", __FUNCTION__, __LINE__,
25069  "An internal error occurred getting the auth list. "
25070  "Diagnostics: Failure to send command to manager daemon.",
25071  "/omp?cmd=get_users", response_data);
25072  case 2:
25073  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25074  return gsad_message (credentials,
25075  "Internal error", __FUNCTION__, __LINE__,
25076  "An internal error occurred getting the auth list. "
25077  "Diagnostics: Failure to receive response from manager daemon.",
25078  "/omp?cmd=get_users", response_data);
25079  default:
25080  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25081  return gsad_message (credentials,
25082  "Internal error", __FUNCTION__, __LINE__,
25083  "An internal error occurred getting the auth list. "
25084  "Diagnostics: Internal Error.",
25085  "/omp?cmd=get_users", response_data);
25086  }
25087 
25088  g_string_append (extra, response);
25089 
25090  free_entity (entity);
25091  g_free (response);
25092  }
25093 
25094  if (command_enabled (credentials, "GET_GROUPS"))
25095  {
25096  gchar *response;
25097  entity_t entity;
25098 
25099  response = NULL;
25100  entity = NULL;
25101  switch (omp (connection, credentials, &response, &entity, response_data,
25102  "<get_groups filter=\"permission=modify_group\"/>"))
25103  {
25104  case 0:
25105  case -1:
25106  break;
25107  case 1:
25108  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25109  return gsad_message (credentials,
25110  "Internal error", __FUNCTION__, __LINE__,
25111  "An internal error occurred getting the group list. "
25112  "Diagnostics: Failure to send command to manager daemon.",
25113  "/omp?cmd=get_users", response_data);
25114  case 2:
25115  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25116  return gsad_message (credentials,
25117  "Internal error", __FUNCTION__, __LINE__,
25118  "An internal error occurred getting the group list. "
25119  "Diagnostics: Failure to receive response from manager daemon.",
25120  "/omp?cmd=get_users", response_data);
25121  default:
25122  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25123  return gsad_message (credentials,
25124  "Internal error", __FUNCTION__, __LINE__,
25125  "An internal error occurred getting the group list. "
25126  "Diagnostics: Internal Error.",
25127  "/omp?cmd=get_users", response_data);
25128  }
25129 
25130  g_string_append (extra, response);
25131 
25132  free_entity (entity);
25133  g_free (response);
25134  }
25135 
25136  if (command_enabled (credentials, "GET_ROLES"))
25137  {
25138  gchar *response;
25139  entity_t entity;
25140 
25141  response = NULL;
25142  entity = NULL;
25143  switch (omp (connection, credentials, &response, &entity, response_data,
25144  "<get_roles/>"))
25145  {
25146  case 0:
25147  case -1:
25148  break;
25149  case 1:
25150  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25151  return gsad_message (credentials,
25152  "Internal error", __FUNCTION__, __LINE__,
25153  "An internal error occurred getting the role list. "
25154  "No new user was created. "
25155  "Diagnostics: Failure to send command to manager daemon.",
25156  "/omp?cmd=get_users", response_data);
25157  case 2:
25158  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25159  return gsad_message (credentials,
25160  "Internal error", __FUNCTION__, __LINE__,
25161  "An internal error occurred getting the role list. "
25162  "No new user was created. "
25163  "Diagnostics: Failure to receive response from manager daemon.",
25164  "/omp?cmd=get_users", response_data);
25165  default:
25166  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25167  return gsad_message (credentials,
25168  "Internal error", __FUNCTION__, __LINE__,
25169  "An internal error occurred getting the role list. "
25170  "No new user was created. "
25171  "Diagnostics: Internal Error.",
25172  "/omp?cmd=get_users", response_data);
25173  }
25174 
25175  g_string_append (extra, response);
25176 
25177  free_entity (entity);
25178  g_free (response);
25179  }
25180 
25181  if (extra_xml)
25182  g_string_append (extra, extra_xml);
25183  html = edit_resource (connection, "user", credentials, params, NULL,
25184  extra->str, response_data);
25185  g_string_free (extra, TRUE);
25186  return html;
25187 }
25188 
25199 char *
25200 edit_user_omp (openvas_connection_t *connection, credentials_t * credentials,
25201  params_t *params, cmd_response_data_t* response_data)
25202 {
25203  return edit_user (connection, credentials, params, NULL, response_data);
25204 }
25205 
25206 char *
25207 auth_settings_omp (openvas_connection_t *connection,
25208  credentials_t * credentials, params_t *params,
25209  cmd_response_data_t* response_data)
25210 {
25211  GString * xml;
25212  gchar * buf;
25213  const char *name;
25214 
25215  name = params_value (params, "name");
25216 
25217  CHECK_PARAM_INVALID (name, "Auth settings", "auth_settings");
25218 
25219  xml = g_string_new ("");
25220  buf = g_markup_printf_escaped ("<auth_settings name=\"%s\">", name);
25221  g_string_append (xml, buf);
25222  g_free (buf);
25223 
25224  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25225  {
25226  gchar * response = NULL;
25227  entity_t entity = NULL;
25228 
25229  switch (omp (connection, credentials, &response, &entity, response_data,
25230  "<describe_auth/>"))
25231  {
25232  case 0:
25233  case -1:
25234  break;
25235  case 1:
25236  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25237  return gsad_message (credentials,
25238  "Internal error", __FUNCTION__, __LINE__,
25239  "An internal error occurred getting the auth list. "
25240  "Diagnostics: Failure to send command to manager daemon.",
25241  "/omp?cmd=get_users", response_data);
25242  case 2:
25243  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25244  return gsad_message (credentials,
25245  "Internal error", __FUNCTION__, __LINE__,
25246  "An internal error occurred getting the auth list. "
25247  "Diagnostics: Failure to receive response from manager daemon.",
25248  "/omp?cmd=get_users", response_data);
25249  default:
25250  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25251  return gsad_message (credentials,
25252  "Internal error", __FUNCTION__, __LINE__,
25253  "An internal error occurred getting the auth list. "
25254  "Diagnostics: Internal Error.",
25255  "/omp?cmd=get_users", response_data);
25256  }
25257 
25258 
25259  g_string_append (xml, response);
25260  free_entity (entity);
25261  g_free (response);
25262  }
25263 
25264  g_string_append (xml, "</auth_settings>");
25265 
25266  return xsl_transform_omp (connection, credentials, params,
25267  g_string_free (xml, FALSE), response_data);
25268 }
25269 
25279 char *
25280 logout (credentials_t *credentials, const gchar *message,
25281  cmd_response_data_t *response_data)
25282 {
25283  time_t now;
25284  gchar *xml;
25285  char *res;
25286  char ctime_now[200];
25287  int ret;
25288  const char* xml_flag;
25289  xml_flag = params_value (credentials->params, "xml");
25290 
25291  if (credentials->token == NULL)
25292  return NULL;
25293 
25294  ret = token_user_remove (credentials->token);
25295  if (ret)
25296  return NULL;
25297 
25298  now = time (NULL);
25299  ctime_r_strip_newline (&now, ctime_now);
25300 
25301  xml = login_xml (message, NULL, ctime_now, NULL, NULL, NULL);
25302 
25303  if (xml_flag && strcmp (xml_flag, "0"))
25304  return xml;
25305 
25306  res = xsl_transform (xml, response_data);
25307  g_free (xml);
25308  return res;
25309 }
25310 
25324 char *
25325 save_user_omp (openvas_connection_t *connection, credentials_t * credentials,
25326  params_t *params, char **password_return, char **modified_user,
25327  int *logout_user, cmd_response_data_t* response_data)
25328 {
25329  int ret;
25330  gchar *html, *response, *buf;
25331  const char *no_redirect;
25332  const char *user_id, *login, *old_login, *modify_password, *password;
25333  const char *hosts, *hosts_allow, *ifaces, *ifaces_allow;
25334  entity_t entity;
25335  GString *command, *group_elements, *role_elements;
25336  params_t *groups, *roles;
25337  const char *status;
25338 
25339  *password_return = NULL;
25340  *modified_user = NULL;
25341  *logout_user = 0;
25342 
25343  no_redirect = params_value (params, "no_redirect");
25344  /* List of hosts user has/lacks access rights. */
25345  hosts = params_value (params, "access_hosts");
25346  /* Whether hosts grants ("1") or forbids ("0") access. "2" for all
25347  * access. */
25348  hosts_allow = params_value (params, "hosts_allow");
25349  ifaces = params_value (params, "access_ifaces");
25350  ifaces_allow = params_value (params, "ifaces_allow");
25351  login = params_value (params, "login");
25352  old_login = params_value (params, "old_login");
25353  modify_password = params_value (params, "modify_password");
25354  password = params_value (params, "password");
25355  user_id = params_value (params, "user_id");
25356 
25357  CHECK_PARAM_INVALID (user_id, "Edit User", "edit_user");
25358  CHECK_PARAM_INVALID (modify_password, "Edit User", "edit_user");
25359  CHECK_PARAM_INVALID (password, "Edit User", "edit_user");
25360  CHECK_PARAM_INVALID (hosts, "Edit User", "edit_user");
25361  CHECK_PARAM_INVALID (hosts_allow, "Edit User", "edit_user");
25362  CHECK_PARAM_INVALID (ifaces, "Save User", "edit_user");
25363  CHECK_PARAM_INVALID (ifaces_allow, "Save User", "edit_user");
25364 
25365  if (params_given (params, "login")
25366  && !(params_given (params, "current_user")))
25367  {
25368  CHECK_PARAM_INVALID (login, "Save User", "edit_user");
25369  CHECK_PARAM_INVALID (old_login, "Save User", "edit_user");
25370  }
25371 
25372  /* Modify the user. */
25373 
25374  command = g_string_new ("");
25375  buf = g_markup_printf_escaped ("<modify_user user_id=\"%s\">"
25376  "<password modify=\"%s\">"
25377  "%s</password>",
25378  user_id,
25379  modify_password,
25380  password);
25381  g_string_append (command, buf);
25382  g_free (buf);
25383 
25384  if (login)
25385  {
25386  buf = g_markup_printf_escaped ("<new_name>%s</new_name>",
25387  login);
25388  g_string_append (command, buf);
25389  g_free (buf);
25390  }
25391 
25392  buf = g_markup_printf_escaped ("<hosts allow=\"%s\">%s</hosts>"
25393  "<ifaces allow=\"%s\">%s</ifaces>",
25394  hosts_allow, hosts, ifaces_allow, ifaces);
25395  g_string_append (command, buf);
25396  g_free (buf);
25397 
25398  if (modify_password && !strcmp (modify_password, "2"))
25399  g_string_append (command, "<sources><source>ldap_connect</source></sources>");
25400  else if (modify_password && !strcmp (modify_password, "3"))
25401  g_string_append (command, "<sources><source>radius_connect</source></sources>");
25402  else
25403  g_string_append (command, "<sources><source>file</source></sources>");
25404 
25405  group_elements = g_string_new ("<groups>");
25406  if (params_given (params, "group_id_optional:"))
25407  groups = params_values (params, "group_id_optional:");
25408  else
25409  groups = params_values (params, "group_ids:");
25410 
25411  if (groups)
25412  {
25413  params_iterator_t iter;
25414  char *name;
25415  param_t *param;
25416 
25417  params_iterator_init (&iter, groups);
25418  while (params_iterator_next (&iter, &name, &param))
25419  {
25420  if (param->value && strcmp (param->value, "--"))
25421  g_string_append_printf (group_elements,
25422  "<group id=\"%s\"/>",
25423  param->value ? param->value : "");
25424  }
25425  }
25426  g_string_append (command, group_elements->str);
25427  g_string_free (group_elements, TRUE);
25428  g_string_append (command, "</groups>");
25429 
25430  role_elements = g_string_new ("");
25431  if (params_given (params, "role_id_optional:"))
25432  roles = params_values (params, "role_id_optional:");
25433  else
25434  roles = params_values (params, "role_ids:");
25435 
25436  if (roles)
25437  {
25438  params_iterator_t iter;
25439  char *name;
25440  param_t *param;
25441 
25442  params_iterator_init (&iter, roles);
25443  while (params_iterator_next (&iter, &name, &param))
25444  {
25445  if (param->value && strcmp (param->value, "--"))
25446  g_string_append_printf (role_elements,
25447  "<role id=\"%s\"/>",
25448  param->value ? param->value : "");
25449  }
25450  }
25451  else
25452  g_string_append_printf (role_elements, "<role id=\"0\"/>");
25453 
25454  g_string_append (command, role_elements->str);
25455  g_string_free (role_elements, TRUE);
25456 
25457  g_string_append (command, "</modify_user>");
25458 
25459  response = NULL;
25460  entity = NULL;
25461  ret = omp (connection, credentials,
25462  &response,
25463  &entity,
25464  response_data,
25465  command->str);
25466  g_string_free (command, TRUE);
25467  switch (ret)
25468  {
25469  case 0:
25470  status = entity_attribute (entity, "status");
25471  if (status && (strlen (status) > 0) && (status[0] == '2'))
25472  {
25473  *modified_user
25474  = g_strdup (old_login ? old_login : credentials->username);
25475 
25476  if (strcmp (modify_password, "0")
25477  || (login && *modified_user && strcmp (*modified_user, login)))
25478  *logout_user = 1;
25479 
25480  if (strcmp (modify_password, "0")
25481  && strcmp (modify_password, "2")
25482  && strcmp (modify_password, "3")
25483  && params_given (params, "current_user"))
25484  {
25485  g_free (credentials->password);
25486  credentials->password = g_strdup (password);
25487  *password_return = g_strdup (password);
25488  }
25489  }
25490  break;
25491  case -1:
25492  break;
25493  case 1:
25494  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25495  return gsad_message (credentials,
25496  "Internal error", __FUNCTION__, __LINE__,
25497  "An internal error occurred while saving a user. "
25498  "The user was not saved. "
25499  "Diagnostics: Failure to send command to manager daemon.",
25500  "/omp?cmd=get_users", response_data);
25501  case 2:
25502  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25503  return gsad_message (credentials,
25504  "Internal error", __FUNCTION__, __LINE__,
25505  "An internal error occurred while saving a user. "
25506  "It is unclear whether the user has been saved or not. "
25507  "Diagnostics: Failure to receive response from manager daemon.",
25508  "/omp?cmd=get_users", response_data);
25509  default:
25510  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25511  return gsad_message (credentials,
25512  "Internal error", __FUNCTION__, __LINE__,
25513  "An internal error occurred while saving a user. "
25514  "It is unclear whether the user has been saved or not. "
25515  "Diagnostics: Internal Error.",
25516  "/omp?cmd=get_users", response_data);
25517  }
25518 
25519  if (omp_success (entity)
25520  && (!strcmp (modify_password, "2")
25521  || !strcmp (modify_password, "3"))
25522  && params_given (params, "current_user"))
25523  {
25524  free_entity (entity);
25525  g_free (response);
25526  html = logout (credentials,
25527  "Authentication method changed."
25528  " Please login with LDAP password.",
25529  response_data);
25530  }
25531  else
25532  html = response_from_entity (connection, credentials, params, entity,
25533  (no_redirect && strcmp (no_redirect, "0")),
25534  NULL, "get_users",
25535  NULL, "edit_user",
25536  "Save User", response_data);
25537  free_entity (entity);
25538  g_free (response);
25539  return html;
25540 }
25541 
25555 char *
25556 export_user_omp (openvas_connection_t *connection, credentials_t * credentials,
25557  params_t *params, enum content_type * content_type,
25558  char **content_disposition, gsize *content_length,
25559  cmd_response_data_t* response_data)
25560 {
25561  return export_resource (connection, "user", credentials, params, content_type,
25562  content_disposition, content_length, response_data);
25563 }
25564 
25579 char *
25580 export_users_omp (openvas_connection_t *connection, credentials_t * credentials,
25581  params_t *params, enum content_type * content_type,
25582  char **content_disposition, gsize *content_length,
25583  cmd_response_data_t* response_data)
25584 {
25585  return export_many (connection, "user", credentials, params, content_type,
25586  content_disposition, content_length, response_data);
25587 }
25588 
25589 char *
25590 cvss_calculator (openvas_connection_t *connection, credentials_t * credentials,
25591  params_t *params, cmd_response_data_t* response_data)
25592 {
25593  GString *xml;
25594  const char *cvss_av, *cvss_au, *cvss_ac, *cvss_c, *cvss_i, *cvss_a;
25595  const char *cvss_vector, *name;
25596 
25597  cvss_av = params_value (params, "cvss_av");
25598  cvss_au = params_value (params, "cvss_au");
25599  cvss_ac = params_value (params, "cvss_ac");
25600  cvss_c = params_value (params, "cvss_c");
25601  cvss_i = params_value (params, "cvss_i");
25602  cvss_a = params_value (params, "cvss_a");
25603  cvss_vector = params_value (params, "cvss_vector");
25604  name = params_value (params, "name");
25605 
25606  xml = g_string_new ("<cvss_calculator>");
25607 
25608  /* Calculate base score */
25609  if (cvss_av && cvss_au && cvss_ac && cvss_c && cvss_i && cvss_a)
25610  {
25611  char *vector = g_strdup_printf
25612  ("AV:%c/AC:%c/Au:%c/C:%c/I:%c/A:%c",
25613  *cvss_av, *cvss_ac, *cvss_au, *cvss_c, *cvss_i, *cvss_a);
25614 
25615  g_string_append_printf (xml,
25616  "<cvss_vector>%s</cvss_vector>"
25617  "<cvss_score>%.1f</cvss_score>",
25618  vector,
25619  get_cvss_score_from_base_metrics (vector));
25620 
25621  g_string_append_printf
25622  (xml,
25623  "<cvss_av>%c</cvss_av><cvss_au>%c</cvss_au>"
25624  "<cvss_ac>%c</cvss_ac><cvss_c>%c</cvss_c>"
25625  "<cvss_i>%c</cvss_i><cvss_a>%c</cvss_a>",
25626  *cvss_av, *cvss_au, *cvss_ac, *cvss_c, *cvss_i, *cvss_a);
25627 
25628  g_free (vector);
25629  }
25630  else if (cvss_vector)
25631  {
25632  double cvss_score = get_cvss_score_from_base_metrics (cvss_vector);
25633 
25634  g_string_append_printf (xml,
25635  "<cvss_vector>%s</cvss_vector>"
25636  "<cvss_score>%.1f</cvss_score>",
25637  cvss_vector,
25638  cvss_score);
25639 
25640  if (cvss_score != -1.0)
25641  {
25642  cvss_av = strstr (cvss_vector, "AV:");
25643  cvss_ac = strstr (cvss_vector, "/AC:");
25644  cvss_au = strstr (cvss_vector, "/Au:");
25645  if (cvss_au == NULL)
25646  cvss_au = strstr (cvss_vector, "/AU:");
25647  cvss_c = strstr (cvss_vector, "/C:");
25648  cvss_i = strstr (cvss_vector, "/I:");
25649  cvss_a = strstr (cvss_vector, "/A:");
25650 
25651  if (cvss_av && cvss_ac && cvss_au && cvss_c
25652  && cvss_i && cvss_a)
25653  g_string_append_printf
25654  (xml,
25655  "<cvss_av>%c</cvss_av><cvss_ac>%c</cvss_ac>"
25656  "<cvss_au>%c</cvss_au><cvss_c>%c</cvss_c>"
25657  "<cvss_i>%c</cvss_i><cvss_a>%c</cvss_a>",
25658  *(cvss_av + 3), *(cvss_ac + 4), *(cvss_au + 4),
25659  *(cvss_c + 3), *(cvss_i + 3), *(cvss_a + 3));
25660  }
25661  }
25662  else if (name && !strcmp ("vector", name))
25663  {
25664  g_string_append_printf (xml,
25665  "<cvss_score>%.1f</cvss_score>",
25666  -1.0);
25667  }
25668 
25669  g_string_append (xml, "</cvss_calculator>");
25670  return xsl_transform_omp (connection, credentials, params,
25671  g_string_free (xml, FALSE), response_data);
25672 }
25673 
25684 char *
25685 dashboard (openvas_connection_t *connection, credentials_t * credentials,
25686  params_t *params, cmd_response_data_t* response_data)
25687 {
25688  GString *xml;
25689  const char *name;
25690  gchar* response;
25691  entity_t entity;
25692  int ret;
25693  int may_get_aggregates, may_get_info, may_get_tasks;
25694 
25695  may_get_aggregates = command_enabled (credentials, "GET_AGGREGATES");
25696  may_get_info = command_enabled (credentials, "GET_INFO");
25697  may_get_tasks = command_enabled (credentials, "GET_TASKS");
25698  name = params_value (params, "dashboard_name");
25699  if (name == NULL)
25700  {
25701  if (credentials->guest
25702  && may_get_aggregates && may_get_info)
25703  name = "secinfo";
25704  else if (may_get_aggregates && may_get_info && may_get_tasks)
25705  name = "main";
25706  else if (may_get_aggregates && may_get_tasks)
25707  name = "scans";
25708  else if (may_get_aggregates && may_get_info)
25709  name = "secinfo";
25710  else if (command_enabled (credentials, "GET_SYSTEM_REPORTS"))
25711  {
25712  gchar *redirect_url;
25713  redirect_url = g_strdup_printf ("/omp?cmd=get_system_reports&token=%s",
25714  credentials->token);
25715  response_data->redirect = redirect_url;
25716  return g_strdup_printf ("redirecting to %s", redirect_url);
25717  }
25718  else
25719  {
25720  gchar *redirect_url;
25721  redirect_url = g_strdup_printf ("/help/about.html?token=%s",
25722  credentials->token);
25723  response_data->redirect = redirect_url;
25724  return g_strdup_printf ("redirecting to %s", redirect_url);
25725  }
25726  }
25727 
25728  xml = g_string_new ("<dashboard>");
25729  g_string_append_printf (xml, "<name>%s</name>", name);
25730 
25731  response = NULL;
25732  entity = NULL;
25733 
25734  // Test if SCAP and CERT databases are available
25735  if (strcasecmp (name, "") == 0
25736  || strcasecmp (name, "Main") == 0
25737  || strcasecmp (name, "SecInfo") == 0)
25738  {
25739  ret = ompf (connection,
25740  credentials,
25741  &response,
25742  &entity,
25743  response_data,
25744  "<get_info type=\"cve\" info_id=\"--\"/>");
25745 
25746  switch (ret)
25747  {
25748  case 0:
25749  case -1:
25750  break;
25751  case 1:
25752  g_string_free (xml, TRUE);
25753  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25754  return gsad_message (credentials,
25755  "Internal error", __FUNCTION__, __LINE__,
25756  "An internal error occurred while "
25757  "testing SecInfo database availability. "
25758  "Diagnostics: Failure to send command to "
25759  "manager daemon.",
25760  "/omp?cmd=dashboard", response_data);
25761  case 2:
25762  g_string_free (xml, TRUE);
25763  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25764  return gsad_message (credentials,
25765  "Internal error", __FUNCTION__, __LINE__,
25766  "An internal error occurred while "
25767  "testing SecInfo database availability. "
25768  "Diagnostics: Failure to receive response from "
25769  "manager daemon.",
25770  "/omp?cmd=dashboard", response_data);
25771  default:
25772  g_string_free (xml, TRUE);
25773  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25774  return gsad_message (credentials,
25775  "Internal error", __FUNCTION__, __LINE__,
25776  "An internal error occurred while "
25777  "testing SecInfo database availability. "
25778  "Diagnostics: Internal Error.",
25779  "/omp?cmd=dashboard", response_data);
25780  }
25781  g_string_append_printf (xml,
25782  "<secinfo_test>%s</secinfo_test>",
25783  response);
25784  g_free (response);
25785  free_entity (entity);
25786  }
25787 
25788  if (strcasecmp (name, "SecInfo") == 0)
25789  ret = ompf (connection,
25790  credentials,
25791  &response,
25792  &entity,
25793  response_data,
25794  "<get_filters filter=\"type=info or type= first=1 rows=-1\"/>");
25795  else
25796  ret = ompf (connection,
25797  credentials,
25798  &response,
25799  &entity,
25800  response_data,
25801  "<get_filters filter=\"first=1 rows=-1\"/>");
25802 
25803  switch (ret)
25804  {
25805  case 0:
25806  case -1:
25807  break;
25808  case 1:
25809  g_string_free (xml, TRUE);
25810  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25811  return gsad_message (credentials,
25812  "Internal error", __FUNCTION__, __LINE__,
25813  "An internal error occurred while getting the "
25814  "filters list. "
25815  "Diagnostics: Failure to send command to "
25816  "manager daemon.",
25817  "/omp?cmd=dashboard", response_data);
25818  case 2:
25819  g_string_free (xml, TRUE);
25820  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25821  return gsad_message (credentials,
25822  "Internal error", __FUNCTION__, __LINE__,
25823  "An internal error occurred while getting the "
25824  "filters list. "
25825  "Diagnostics: Failure to receive response from "
25826  "manager daemon.",
25827  "/omp?cmd=dashboard", response_data);
25828  default:
25829  g_string_free (xml, TRUE);
25830  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25831  return gsad_message (credentials,
25832  "Internal error", __FUNCTION__, __LINE__,
25833  "An internal error occurred while getting the "
25834  "filters list. "
25835  "Diagnostics: Internal Error.",
25836  "/omp?cmd=dashboard", response_data);
25837  }
25838 
25839  g_string_append (xml, response);
25840  g_free (response);
25841  free_entity (entity);
25842 
25843  g_string_append (xml, "</dashboard>");
25844  return xsl_transform_omp (connection, credentials, params,
25845  g_string_free (xml, FALSE), response_data);
25846 }
25847 
25851 #define AUTH_CONF_SETTING(key, value) \
25852  "<auth_conf_setting>" \
25853  "<key>" key "</key>" \
25854  "<value>" value "</value>" \
25855  "</auth_conf_setting>"
25856 
25867 char*
25868 save_auth_omp (openvas_connection_t *connection, credentials_t* credentials,
25869  params_t *params, cmd_response_data_t* response_data)
25870 {
25871  int ret;
25872  entity_t entity = NULL;
25873  char *html, *response = NULL, *truefalse;
25874  const char *no_redirect, *method;
25875 
25876  if (params_value (params, "enable")
25877  && (strcmp (params_value (params, "enable"), "1") == 0))
25878  truefalse = "true";
25879  else
25880  truefalse = "false";
25881 
25882  no_redirect = params_value (params, "no_redirect");
25883  method = params_value (params, "group");
25884  CHECK_PARAM_INVALID (method, "Save Authentication", "get_users");
25885  if (!strcmp (method, "method:ldap_connect"))
25886  {
25887  const char *ldaphost, *authdn, *certificate;
25888  ldaphost = params_value (params, "ldaphost");
25889  authdn = params_value (params, "authdn");
25890  certificate = params_value (params, "certificate");
25891 
25892  CHECK_PARAM_INVALID (ldaphost, "Save Authentication", "get_users");
25893  CHECK_PARAM_INVALID (authdn, "Save Authentication", "get_users");
25894  if (params_given (params, "certificate") && strcmp (certificate, ""))
25895  {
25896  CHECK_PARAM_INVALID (certificate, "Save Authentication", "get_users");
25898  ret = ompf (connection, credentials, &response, &entity,
25899  response_data,
25900  "<modify_auth>"
25901  "<group name=\"%s\">"
25902  AUTH_CONF_SETTING ("enable", "%s")
25903  AUTH_CONF_SETTING ("ldaphost", "%s")
25904  AUTH_CONF_SETTING ("authdn", "%s")
25905  AUTH_CONF_SETTING ("cacert", "%s")
25906  "</group>"
25907  "</modify_auth>", method, truefalse, ldaphost, authdn,
25908  certificate);
25909  }
25910  else
25912  ret = ompf (connection, credentials, &response, &entity, response_data,
25913  "<modify_auth>"
25914  "<group name=\"%s\">"
25915  AUTH_CONF_SETTING ("enable", "%s")
25916  AUTH_CONF_SETTING ("ldaphost", "%s")
25917  AUTH_CONF_SETTING ("authdn", "%s")
25918  "</group>"
25919  "</modify_auth>", method, truefalse, ldaphost, authdn);
25920  }
25921  else if (!strcmp (method, "method:radius_connect"))
25922  {
25923  const char *radiushost, *radiuskey;
25924  radiushost = params_value (params, "radiushost");
25925  radiuskey = params_value (params, "radiuskey");
25926 
25927  CHECK_PARAM_INVALID (radiushost, "Save Authentication", "get_users");
25928  CHECK_PARAM_INVALID (radiuskey, "Save Authentication", "get_users");
25930  ret = ompf (connection, credentials, &response, &entity, response_data,
25931  "<modify_auth>"
25932  "<group name=\"%s\">"
25933  AUTH_CONF_SETTING ("enable", "%s")
25934  AUTH_CONF_SETTING ("radiushost", "%s")
25935  AUTH_CONF_SETTING ("radiuskey", "%s")
25936  "</group>"
25937  "</modify_auth>", method, truefalse, radiushost, radiuskey);
25938  }
25939  else
25940  return get_users (connection, credentials, params,
25942  ("Save Authentication Configuration"),
25943  response_data);
25944 
25945  switch (ret)
25946  {
25947  case 0:
25948  case -1:
25949  break;
25950  case 1:
25951  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25952  return gsad_message (credentials,
25953  "Internal error", __FUNCTION__, __LINE__,
25954  "An internal error occurred while saving the auth settings. "
25955  "The settings were not saved. "
25956  "Diagnostics: Failure to send command to manager daemon.",
25957  "/omp?cmd=get_users", response_data);
25958  case 2:
25959  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25960  return gsad_message (credentials,
25961  "Internal error", __FUNCTION__, __LINE__,
25962  "An internal error occurred while saving the auth settings. "
25963  "It is unclear whether the settings have been saved or not. "
25964  "Diagnostics: Failure to receive response from manager daemon.",
25965  "/omp?cmd=get_users", response_data);
25966  default:
25967  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25968  return gsad_message (credentials,
25969  "Internal error", __FUNCTION__, __LINE__,
25970  "An internal error occurred while saving the auth settings. "
25971  "It is unclear whether the settings have been saved or not. "
25972  "Diagnostics: Internal Error.",
25973  "/omp?cmd=get_users", response_data);
25974  }
25975 
25976  html = response_from_entity (connection, credentials, params, entity,
25977  (no_redirect && strcmp (no_redirect, "0")),
25978  NULL, NULL,
25979  NULL, "modify_auth",
25980  "Save Authentication Configuration",
25981  response_data);
25982  free_entity (entity);
25983  g_free (response);
25984  return html;
25985 }
25986 
25999 char*
26000 save_chart_preference_omp (openvas_connection_t *connection,
26001  credentials_t* credentials, params_t *params,
26002  gchar **pref_id, gchar **pref_value,
26003  cmd_response_data_t* response_data)
26004 {
26005  *pref_id = g_strdup (params_value (params, "chart_preference_id"));
26006  *pref_value = g_strdup (params_value (params, "chart_preference_value"));
26007 
26008  gchar* value_64 = g_base64_encode ((guchar*)*pref_value,
26009  strlen (*pref_value));
26010  gchar* response;
26011  entity_t entity;
26012  int ret;
26013 
26014  if (*pref_id == NULL)
26015  {
26016  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26017  return ("<save_chart_preference_response"
26018  " status=\"400\" status_text=\"Invalid or missing name\"/>");
26019  }
26020  if (*pref_value == NULL)
26021  {
26022  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26023  return ("<save_chart_preference_response"
26024  " status=\"400\" status_text=\"Invalid or missing value\"/>");
26025  }
26026 
26027  response = NULL;
26028  entity = NULL;
26029  ret = ompf (connection, credentials, &response, &entity, response_data,
26030  "<modify_setting setting_id=\"%s\">"
26031  "<value>%s</value>"
26032  "</modify_setting>",
26033  *pref_id, value_64);
26034  g_free (value_64);
26035  switch (ret)
26036  {
26037  case 0:
26038  case -1:
26039  break;
26040  case 1:
26041  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26042  return gsad_message (credentials,
26043  "Internal error", __FUNCTION__, __LINE__,
26044  "An internal error occurred while saving settings. "
26045  "It is unclear whether all the settings were saved. "
26046  "Diagnostics: Failure to send command to manager daemon.",
26047  "/omp?cmd=get_my_settings", response_data);
26048  case 2:
26049  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26050  return gsad_message (credentials,
26051  "Internal error", __FUNCTION__, __LINE__,
26052  "An internal error occurred while saving settings. "
26053  "It is unclear whether all the settings were saved. "
26054  "Diagnostics: Failure to receive response from manager daemon.",
26055  "/omp?cmd=get_my_settings", response_data);
26056  default:
26057  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26058  return gsad_message (credentials,
26059  "Internal error", __FUNCTION__, __LINE__,
26060  "An internal error occurred while saving settings. "
26061  "It is unclear whether all the settings were saved. "
26062  "Diagnostics: Internal Error.",
26063  "/omp?cmd=get_my_settings", response_data);
26064  }
26065 
26066  if (omp_success (entity))
26067  {
26068  free_entity (entity);
26069  g_free (response);
26070  return ("<save_chart_preference_response"
26071  " status=\"200\" status_text=\"OK\"/>");
26072  }
26073  else
26074  {
26075  set_http_status_from_entity (entity, response_data);
26076  gchar* ret_response
26077  = g_strdup_printf("<save_chart_preference_response"
26078  " status=\"%s\" status_text=\"%s\"/>",
26079  entity_attribute (entity, "status"),
26080  entity_attribute (entity, "status_text"));
26081  free_entity (entity);
26082  g_free (response);
26083  return ret_response;
26084  }
26085 }
26086 
26087 
26088 /* Wizards. */
26089 
26101 char *
26102 wizard (openvas_connection_t *connection, credentials_t *credentials,
26103  params_t *params, const char *extra_xml,
26104  cmd_response_data_t* response_data)
26105 {
26106  GString *xml;
26107  const char* name = params_value (params, "name");
26108 
26109  if (name == NULL)
26110  {
26111  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26112  return gsad_message (credentials,
26113  "Internal error", __FUNCTION__, __LINE__,
26114  "An internal error occurred while getting the wizard. "
26115  "Given name was invalid",
26116  "/omp?cmd=get_tasks", response_data);
26117  }
26118 
26119  xml = g_string_new ("");
26120  g_string_append_printf (xml,
26121  "<wizard>%s<%s/>",
26122  extra_xml ? extra_xml : "",
26123  name);
26124 
26125  /* Try to run init mode of the wizard */
26126  if (openvas_connection_sendf_xml (connection,
26127  "<run_wizard read_only=\"1\">"
26128  "<name>%s</name>"
26129  "<mode>init</mode>"
26130  "</run_wizard>",
26131  name)
26132  == -1)
26133  {
26134  g_string_free (xml, TRUE);
26135  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26136  return gsad_message (credentials,
26137  "Internal error", __FUNCTION__, __LINE__,
26138  "An internal error occurred while getting the wizard. "
26139  "Diagnostics: Failure to send command to manager daemon.",
26140  "/omp?cmd=get_tasks", response_data);
26141  }
26142 
26143  if (read_string_c (connection, &xml))
26144  {
26145  g_string_free (xml, TRUE);
26146  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26147  return gsad_message (credentials,
26148  "Internal error", __FUNCTION__, __LINE__,
26149  "An internal error occurred while getting the"
26150  " wizard."
26151  "Diagnostics: Failure to receive response from"
26152  " manager daemon.",
26153  "/omp?cmd=get_tasks", response_data);
26154  }
26155 
26156  /* Get the setting. */
26157 
26158  if (openvas_connection_sendf_xml (connection,
26159  "<get_settings"
26160  " setting_id=\"20f3034c-e709-11e1-87e7-406186ea4fc5\"/>")
26161  == -1)
26162  {
26163  g_string_free (xml, TRUE);
26164  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26165  return gsad_message (credentials,
26166  "Internal error", __FUNCTION__, __LINE__,
26167  "An internal error occurred while getting the wizard. "
26168  "Diagnostics: Failure to send command to manager daemon.",
26169  "/omp?cmd=get_tasks", response_data);
26170  }
26171 
26172  if (read_string_c (connection, &xml))
26173  {
26174  g_string_free (xml, TRUE);
26175  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26176  return gsad_message (credentials,
26177  "Internal error", __FUNCTION__, __LINE__,
26178  "An internal error occurred while the wizard. "
26179  "Diagnostics: Failure to receive response from manager daemon.",
26180  "/omp?cmd=get_tasks", response_data);
26181  }
26182 
26183  /* Cleanup, and return transformed XML. */
26184 
26185  g_string_append_printf (xml, "</wizard>");
26186  return xsl_transform_omp (connection, credentials, params,
26187  g_string_free (xml, FALSE), response_data);
26188 }
26189 
26200 char *
26201 wizard_omp (openvas_connection_t *connection, credentials_t *credentials,
26202  params_t *params, cmd_response_data_t* response_data)
26203 {
26204  return wizard (connection, credentials, params, NULL, response_data);
26205 }
26206 
26218 char *
26219 wizard_get (openvas_connection_t *connection, credentials_t *credentials,
26220  params_t *params, const char *extra_xml,
26221  cmd_response_data_t* response_data)
26222 {
26223  const char *name;
26224  int ret;
26225  GString *run;
26226  param_t *param;
26227  gchar *param_name, *response;
26228  params_iterator_t iter;
26229  params_t *wizard_params;
26230  entity_t entity;
26231  gchar *wizard_xml;
26232 
26233  /* The naming is a bit subtle here, because the HTTP request
26234  * parameters are called "param"s and so are the OMP wizard
26235  * parameters. */
26236 
26237  name = params_value (params, "get_name");
26238  if (name == NULL)
26239  {
26240  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26241  return gsad_message (credentials,
26242  "Internal error", __FUNCTION__, __LINE__,
26243  "An internal error occurred while trying to start a wizard. "
26244  "Diagnostics: Required parameter 'get_name' was NULL.",
26245  "/omp?cmd=get_tasks", response_data);
26246  }
26247 
26248  run = g_string_new ("<run_wizard read_only=\"1\">");
26249 
26250  g_string_append_printf (run,
26251  "<name>%s</name>"
26252  "<params>",
26253  name);
26254 
26255  wizard_params = params_values (params, "event_data:");
26256  if (wizard_params)
26257  {
26258  params_iterator_init (&iter, wizard_params);
26259  while (params_iterator_next (&iter, &param_name, &param))
26260  xml_string_append (run,
26261  "<param>"
26262  "<name>%s</name>"
26263  "<value>%s</value>"
26264  "</param>",
26265  param_name,
26266  param->value);
26267  }
26268 
26269  g_string_append (run, "</params></run_wizard>");
26270 
26271  response = NULL;
26272  entity = NULL;
26273  ret = omp (connection, credentials, &response, &entity, response_data,
26274  run->str);
26275  g_string_free (run, TRUE);
26276  switch (ret)
26277  {
26278  case 0:
26279  case -1:
26280  break;
26281  case 1:
26282  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26283  return gsad_message (credentials,
26284  "Internal error", __FUNCTION__, __LINE__,
26285  "An internal error occurred while running a wizard. "
26286  "The wizard did not start. "
26287  "Diagnostics: Failure to send command to manager daemon.",
26288  "/omp?cmd=get_tasks", response_data);
26289  case 2:
26290  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26291  return gsad_message (credentials,
26292  "Internal error", __FUNCTION__, __LINE__,
26293  "An internal error occurred while running a wizard. "
26294  "It is unclear whether the wizard started or not. "
26295  "Diagnostics: Failure to receive response from manager daemon.",
26296  "/omp?cmd=get_tasks", response_data);
26297  default:
26298  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26299  return gsad_message (credentials,
26300  "Internal error", __FUNCTION__, __LINE__,
26301  "An internal error occurred while running a wizard. "
26302  "It is unclear whether the wizard started or not. "
26303  "Diagnostics: Internal Error.",
26304  "/omp?cmd=get_tasks", response_data);
26305  }
26306 
26307  wizard_xml = g_strdup_printf ("<wizard><%s/>%s%s</wizard>",
26308  name,
26309  extra_xml ? extra_xml : "",
26310  response);
26311 
26312  return xsl_transform_omp (connection, credentials, params, wizard_xml,
26313  response_data);
26314 }
26315 
26326 char *
26327 wizard_get_omp (openvas_connection_t *connection, credentials_t *credentials,
26328  params_t *params, cmd_response_data_t* response_data)
26329 {
26330  return wizard_get (connection, credentials, params, NULL, response_data);
26331 }
26332 
26346 char *
26347 process_bulk_omp (openvas_connection_t *connection, credentials_t *credentials,
26348  params_t *params, enum content_type *content_type,
26349  char **content_disposition, gsize *content_length,
26350  cmd_response_data_t* response_data)
26351 {
26352  GString *bulk_string;
26353  const char *type, *subtype, *action;
26354  char *param_name;
26355  params_t *selected_ids;
26356  param_t *param;
26357  params_iterator_t iter;
26358 
26359  type = params_value (params, "resource_type");
26360  if (type == NULL)
26361  {
26362  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26363  return gsad_message (credentials,
26364  "Internal error", __FUNCTION__, __LINE__,
26365  "An internal error occurred while performing a bulk action. "
26366  "Diagnostics: Required parameter 'resource_type' was NULL.",
26367  "/omp?cmd=get_tasks", response_data);
26368  }
26369  if (strcmp (type, "info") == 0)
26370  {
26371  subtype = params_value (params, "info_type");
26372  if (subtype == NULL)
26373  {
26374  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26375  return gsad_message (credentials,
26376  "Internal error", __FUNCTION__, __LINE__,
26377  "An internal error occurred while performing a bulk action. "
26378  "Diagnostics: Required parameter 'info_type' was NULL.",
26379  "/omp?cmd=get_tasks", response_data);
26380  }
26381  }
26382  else
26383  subtype = NULL;
26384 
26385  if (params_valid (params, "bulk_create.x"))
26386  action = "create";
26387  else if (params_valid (params, "bulk_delete.x"))
26388  action = "delete";
26389  else if (params_valid (params, "bulk_export.x"))
26390  action = "export";
26391  else if (params_value (params, "bulk_trash.x"))
26392  action = "trash";
26393  else
26394  {
26395  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26396  return gsad_message (credentials,
26397  "Internal error", __FUNCTION__, __LINE__,
26398  "An internal error occurred while performing a bulk action. "
26399  "Diagnostics: Could not determine the action.",
26400  "/omp?cmd=get_tasks", response_data);
26401  }
26402 
26403  if (strcmp (action, "create") == 0)
26404  {
26405  if (params_value (params, "bulk_select")
26406  && strcmp (params_value (params, "bulk_select"), "1") == 0)
26407  {
26408  bulk_string = g_string_new ("first=1 rows=-1");
26409 
26410  selected_ids = params_values (params, "bulk_selected:");
26411  if (selected_ids)
26412  {
26413  params_iterator_init (&iter, selected_ids);
26414  while (params_iterator_next (&iter, &param_name, &param))
26415  xml_string_append (bulk_string,
26416  " uuid=%s",
26417  param_name);
26418  }
26419  }
26420  else if (params_value (params, "bulk_select")
26421  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26422  {
26423  bulk_string = g_string_new ("first=1 rows=-1 ");
26424  g_string_append (bulk_string, params_value (params, "filter") ? : "");
26425  }
26426  else
26427  {
26428  bulk_string = g_string_new (params_value (params, "filter") ? : "");
26429  }
26430  param = params_add (params, "hosts_filter", bulk_string->str);
26431  param->valid = 1;
26432  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
26433  g_string_free (bulk_string, TRUE);
26434 
26435  return new_target (connection, credentials, params, NULL, response_data);
26436  }
26437 
26438  if (strcmp (action, "export") == 0)
26439  {
26440  if (params_value (params, "bulk_select")
26441  && strcmp (params_value (params, "bulk_select"), "1") == 0)
26442  {
26443  bulk_string
26444  = g_string_new ("first=1 rows=-1 uuid=");
26445 
26446  selected_ids = params_values (params, "bulk_selected:");
26447  if (selected_ids)
26448  {
26449  params_iterator_init (&iter, selected_ids);
26450  while (params_iterator_next (&iter, &param_name, &param))
26451  xml_string_append (bulk_string,
26452  " uuid=%s",
26453  param_name);
26454  }
26455  }
26456  else if (params_value (params, "bulk_select")
26457  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26458  {
26459  bulk_string
26460  = g_string_new ("first=1 rows=-1 ");
26461  g_string_append (bulk_string, params_value (params, "filter") ? : "");
26462  }
26463  else
26464  {
26465  bulk_string
26466  = g_string_new (params_value (params, "filter") ? : "");
26467  }
26468  params_add (params, "filter", g_string_free (bulk_string, FALSE));
26469 
26470  return export_many (connection, type, credentials, params, content_type,
26471  content_disposition, content_length, response_data);
26472  }
26473 
26474  bulk_string = g_string_new ("<process_bulk>");
26475 
26476  g_string_append_printf (bulk_string,
26477  "<type>%s</type>"
26478  "<action>%s</action>",
26479  type,
26480  action);
26481 
26482  g_string_append (bulk_string, "<selections>");
26483 
26484  if (params_value (params, "bulk_select")
26485  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26486  {
26487  int ret;
26488  entity_t entity;
26489  gchar *response;
26490 
26491  ret = ompf (connection, credentials, &response, &entity, response_data,
26492  "<get_%ss filter=\"first=1 rows=-1 %s\"/>",
26493  type,
26494  params_value (params, "filter") ? : "");
26495 
26496  if (ret)
26497  {
26498  free_entity (entity);
26499  g_free (response);
26500  g_string_free (bulk_string, TRUE);
26501  }
26502  switch (ret)
26503  {
26504  case 0:
26505  case -1:
26506  break;
26507  case 1:
26508  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26509  return gsad_message (credentials,
26510  "Internal error", __FUNCTION__, __LINE__,
26511  "An internal error occurred while getting a"
26512  " resources list. "
26513  "Diagnostics: Failure to send command to"
26514  " manager daemon.",
26515  "/omp?cmd=get_my_settings", response_data);
26516  case 2:
26517  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26518  return gsad_message (credentials,
26519  "Internal error", __FUNCTION__, __LINE__,
26520  "An internal error occurred while getting a"
26521  " resources list. "
26522  "Diagnostics: Failure to receive response from"
26523  " manager daemon.",
26524  "/omp?cmd=get_my_settings", response_data);
26525  default:
26526  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26527  return gsad_message (credentials,
26528  "Internal error", __FUNCTION__, __LINE__,
26529  "An internal error occurred while getting a"
26530  " resources list. "
26531  "Diagnostics: Internal Error.",
26532  "/omp?cmd=get_my_settings", response_data);
26533  }
26534 
26535  entities_t entities = entity->entities;
26536  entity_t child_entity;
26537 
26538  while ((child_entity = first_entity (entities)))
26539  {
26540  if (strcmp (entity_name (child_entity), type) == 0)
26541  {
26542  const char *resource_id
26543  = entity_attribute (child_entity, "id");
26544 
26545  if (resource_id)
26546  xml_string_append (bulk_string,
26547  "<selection id=\"%s\" />",
26548  resource_id);
26549  }
26550  entities = next_entities (entities);
26551  }
26552  }
26553  else
26554  {
26555  selected_ids = params_values (params, "bulk_selected:");
26556  if (selected_ids)
26557  {
26558  params_iterator_init (&iter, selected_ids);
26559  while (params_iterator_next (&iter, &param_name, &param))
26560  xml_string_append (bulk_string,
26561  "<selection id=\"%s\" />",
26562  param_name);
26563  }
26564  }
26565  g_string_append (bulk_string, "</selections>");
26566 
26567  if (strcmp (action, "delete") == 0 && strcmp (type, "user") == 0)
26568  {
26569  int ret;
26570  entity_t entity;
26571  gchar *response;
26572 
26573  ret = ompf (connection, credentials, &response, &entity, response_data,
26574  "<get_users filter=\"first=1 rows=-1\"/>",
26575  type,
26576  params_value (params, "filter") ? : "");
26577 
26578  if (ret)
26579  {
26580  free_entity (entity);
26581  g_free (response);
26582  g_string_free (bulk_string, TRUE);
26583  }
26584  switch (ret)
26585  {
26586  case 0:
26587  case -1:
26588  break;
26589  case 1:
26590  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26591  return gsad_message (credentials,
26592  "Internal error", __FUNCTION__, __LINE__,
26593  "An internal error occurred while getting a"
26594  " resources list. "
26595  "Diagnostics: Failure to send command to"
26596  " manager daemon.",
26597  "/omp?cmd=get_my_settings", response_data);
26598  case 2:
26599  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26600  return gsad_message (credentials,
26601  "Internal error", __FUNCTION__, __LINE__,
26602  "An internal error occurred while getting a"
26603  " resources list. "
26604  "Diagnostics: Failure to receive response from"
26605  " manager daemon.",
26606  "/omp?cmd=get_my_settings", response_data);
26607  default:
26608  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26609  return gsad_message (credentials,
26610  "Internal error", __FUNCTION__, __LINE__,
26611  "An internal error occurred while getting a"
26612  " resources list. "
26613  "Diagnostics: Internal Error.",
26614  "/omp?cmd=get_my_settings", response_data);
26615  }
26616 
26617  g_string_append (bulk_string, response);
26618  free_entity (entity);
26619  g_free (response);
26620  }
26621 
26622  g_string_append (bulk_string, "</process_bulk>");
26623 
26624  return xsl_transform_omp (connection, credentials, params,
26625  g_string_free (bulk_string, FALSE), response_data);
26626 }
26627 
26638 char *
26639 bulk_delete_omp (openvas_connection_t *connection, credentials_t * credentials,
26640  params_t *params, cmd_response_data_t* response_data)
26641 {
26642  const char *no_redirect, *type;
26643  GString *commands_xml;
26644  params_t *selected_ids;
26645  params_iterator_t iter;
26646  param_t *param;
26647  gchar *param_name;
26648  gchar *html, *response;
26649  entity_t entity;
26650  gchar *extra_attribs;
26651 
26652  no_redirect = params_value (params, "no_redirect");
26653  type = params_value (params, "resource_type");
26654  if (type == NULL)
26655  {
26656  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26657  return gsad_message (credentials,
26658  "Internal error", __FUNCTION__, __LINE__,
26659  "An internal error occurred while deleting resources. "
26660  "The resources were not deleted. "
26661  "Diagnostics: Required parameter 'resource_type' was NULL.",
26662  "/omp?cmd=get_tasks", response_data);
26663  }
26664 
26665  /* Extra attributes */
26666  extra_attribs = NULL;
26667 
26668  /* Inheritor of user's resource */
26669  if (strcmp (type, "user") == 0)
26670  {
26671  const char* inheritor_id;
26672  inheritor_id = params_value (params, "inheritor_id");
26673  if (inheritor_id)
26674  extra_attribs = g_strdup_printf ("inheritor_id=\"%s\"", inheritor_id);
26675  }
26676 
26677  commands_xml = g_string_new ("<commands>");
26678 
26679  selected_ids = params_values (params, "bulk_selected:");
26680  if (selected_ids)
26681  {
26682  params_iterator_init (&iter, selected_ids);
26683  while (params_iterator_next (&iter, &param_name, &param))
26684  {
26685  xml_string_append (commands_xml,
26686  "<delete_%s %s_id=\"%s\" ultimate=\"0\"",
26687  type,
26688  type,
26689  param_name);
26690  if (extra_attribs)
26691  g_string_append_printf (commands_xml, " %s/>", extra_attribs);
26692  else
26693  g_string_append (commands_xml, "/>");
26694  }
26695  }
26696 
26697  g_string_append (commands_xml, "</commands>");
26698 
26699  /* Delete the resources and get all resources. */
26700 
26701  if (openvas_connection_sendf_xml (connection,
26702  commands_xml->str)
26703  == -1)
26704  {
26705  g_string_free (commands_xml, TRUE);
26706  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26707  return gsad_message (credentials,
26708  "Internal error", __FUNCTION__, __LINE__,
26709  "An internal error occurred while deleting resources. "
26710  "The resources were not deleted. "
26711  "Diagnostics: Failure to send command to manager daemon.",
26712  "/omp?cmd=get_tasks", response_data);
26713  }
26714  g_string_free (commands_xml, TRUE);
26715 
26716  entity = NULL;
26717  if (read_entity_and_text_c (connection, &entity, &response))
26718  {
26719  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26720  return gsad_message (credentials,
26721  "Internal error", __FUNCTION__, __LINE__,
26722  "An internal error occurred while deleting resources. "
26723  "It is unclear whether the resources have been deleted or not. "
26724  "Diagnostics: Failure to read response from manager daemon.",
26725  "/omp?cmd=get_tasks", response_data);
26726  }
26727 
26728  /* Cleanup, and return transformed XML. */
26729 
26730  if (params_given (params, "next") == 0)
26731  {
26732  gchar *next;
26733  next = g_strdup_printf ("get_%ss", type);
26734  params_add (params, "next", next);
26735  g_free (next);
26736  }
26737  html = response_from_entity (connection, credentials, params, entity,
26738  (no_redirect && strcmp (no_redirect, "0")),
26739  NULL, NULL,
26740  NULL, NULL,
26741  "Bulk Delete", response_data);
26742  g_free (response);
26743  free_entity (entity);
26744 
26745  return html;
26746 }
26747 
26748 
26749 /* Assets. */
26750 
26762 static char *
26763 new_host (openvas_connection_t *connection, credentials_t *credentials,
26764  params_t *params, const char *extra_xml,
26765  cmd_response_data_t* response_data)
26766 {
26767  GString *xml;
26768  xml = g_string_new ("<new_host>");
26769  if (extra_xml)
26770  g_string_append (xml, extra_xml);
26771  g_string_append (xml, "</new_host>");
26772  return xsl_transform_omp (connection, credentials, params,
26773  g_string_free (xml, FALSE), response_data);
26774 }
26775 
26786 char *
26787 new_host_omp (openvas_connection_t *connection, credentials_t *credentials,
26788  params_t *params, cmd_response_data_t* response_data)
26789 {
26790  return new_host (connection, credentials, params, NULL, response_data);
26791 }
26792 
26803 char *
26804 create_host_omp (openvas_connection_t *connection, credentials_t * credentials,
26805  params_t *params, cmd_response_data_t* response_data)
26806 {
26807  int ret;
26808  gchar *html, *response;
26809  const char *no_redirect, *name, *comment;
26810  entity_t entity;
26811  GString *xml;
26812 
26813  no_redirect = params_value (params, "no_redirect");
26814 
26815  name = params_value (params, "name");
26816  CHECK_PARAM_INVALID (name, "Create Host", "new_host");
26817 
26818  comment = params_value (params, "comment");
26819  CHECK_PARAM_INVALID (comment, "Create Host", "new_host");
26820 
26821  /* Create the host. */
26822 
26823  xml = g_string_new ("");
26824 
26825  xml_string_append (xml,
26826  "<create_asset>"
26827  "<asset>"
26828  "<type>host</type>"
26829  "<name>%s</name>"
26830  "<comment>%s</comment>"
26831  "</asset>"
26832  "</create_asset>",
26833  name,
26834  comment);
26835 
26836  ret = omp (connection, credentials,
26837  &response,
26838  &entity,
26839  response_data,
26840  xml->str);
26841  g_string_free (xml, TRUE);
26842  switch (ret)
26843  {
26844  case 0:
26845  case -1:
26846  break;
26847  case 1:
26848  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26849  return gsad_message (credentials,
26850  "Internal error", __FUNCTION__, __LINE__,
26851  "An internal error occurred while creating a new host. "
26852  "No new host was created. "
26853  "Diagnostics: Failure to send command to manager daemon.",
26854  "/omp?cmd=get_assets", response_data);
26855  case 2:
26856  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26857  return gsad_message (credentials,
26858  "Internal error", __FUNCTION__, __LINE__,
26859  "An internal error occurred while creating a new host. "
26860  "It is unclear whether the host has been created or not. "
26861  "Diagnostics: Failure to receive response from manager daemon.",
26862  "/omp?cmd=get_assets", response_data);
26863  default:
26864  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26865  return gsad_message (credentials,
26866  "Internal error", __FUNCTION__, __LINE__,
26867  "An internal error occurred while creating a new host. "
26868  "It is unclear whether the host has been created or not. "
26869  "Diagnostics: Internal Error.",
26870  "/omp?cmd=get_assets", response_data);
26871  }
26872 
26873  if (entity_attribute (entity, "id"))
26874  params_add (params, "asset_id", entity_attribute (entity, "id"));
26875  html = response_from_entity (connection, credentials, params, entity,
26876  (no_redirect && strcmp (no_redirect, "0")),
26877  NULL, "get_assets",
26878  NULL, "new_host",
26879  "Create Host", response_data);
26880  free_entity (entity);
26881  g_free (response);
26882  return html;
26883 }
26884 
26896 char *
26897 get_asset (openvas_connection_t *connection, credentials_t *credentials,
26898  params_t *params, const char *extra_xml,
26899  cmd_response_data_t* response_data)
26900 {
26901  char *ret;
26902  GString *extra_attribs, *extra_response;
26903  const char *asset_type;
26904 
26905  asset_type = params_value (params, "asset_type");
26906  if (asset_type == NULL)
26907  {
26908  param_t *param;
26909  param = params_add (params, "asset_type", "host");
26910  param->valid = 1;
26911  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
26912  asset_type = params_value (params, "asset_type");
26913  }
26914 
26915  if (strcmp (asset_type, "host")
26916  && strcmp (asset_type, "os"))
26917  {
26918  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26919  return gsad_message (credentials,
26920  "Internal error", __FUNCTION__, __LINE__,
26921  "An internal error occurred while getting an asset. "
26922  "Diagnostics: Invalid asset_type parameter value",
26923  "/omp?cmd=get_asset", response_data);
26924  }
26925 
26926  if (params_value (params, "asset_name")
26927  && params_value (params, "asset_id"))
26928  {
26929  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26930  return gsad_message (credentials,
26931  "Internal error", __FUNCTION__, __LINE__,
26932  "An internal error occurred while getting an asset. "
26933  "Diagnostics: Both ID and Name set.",
26934  "/omp?cmd=get_asset", response_data);
26935  }
26936 
26937  extra_response = g_string_new (extra_xml ? extra_xml : "");
26938 
26939  extra_attribs = g_string_new ("");
26940  g_string_append_printf (extra_attribs, "type=\"%s\"",
26941  params_value (params, "asset_type"));
26942  if (params_value (params, "asset_name"))
26943  g_string_append_printf (extra_attribs,
26944  " name=\"%s\"",
26945  params_value (params, "asset_name"));
26946  else if (params_value (params, "asset_id"))
26947  g_string_append_printf (extra_attribs,
26948  " asset_id=\"%s\"",
26949  params_value (params, "asset_id"));
26950  if (params_value (params, "details"))
26951  g_string_append_printf (extra_attribs,
26952  " details=\"%s\"",
26953  params_value (params, "details"));
26954 
26955  ret = get_one (connection, "asset", credentials, params, extra_response->str,
26956  extra_attribs->str, response_data);
26957 
26958  g_string_free (extra_response, TRUE);
26959  g_string_free (extra_attribs, TRUE);
26960 
26961  return ret;
26962 }
26963 
26974 char *
26975 get_asset_omp (openvas_connection_t *connection, credentials_t * credentials,
26976  params_t *params, cmd_response_data_t* response_data)
26977 {
26978  return get_asset (connection, credentials, params, NULL, response_data);
26979 }
26980 
26992 char *
26993 get_assets (openvas_connection_t *connection, credentials_t *credentials,
26994  params_t *params, const char *extra_xml,
26995  cmd_response_data_t* response_data)
26996 {
26997  char *ret;
26998  GString *extra_attribs, *extra_response;
26999  const char *asset_type;
27000 
27001  asset_type = params_value (params, "asset_type");
27002  if (asset_type == NULL)
27003  {
27004  param_t *param;
27005  param = params_add (params, "asset_type", "host");
27006  param->valid = 1;
27007  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
27008  asset_type = params_value (params, "asset_type");
27009  }
27010 
27011  if (strcmp (asset_type, "host")
27012  && strcmp (asset_type, "os"))
27013  {
27014  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27015  return gsad_message (credentials,
27016  "Internal error", __FUNCTION__, __LINE__,
27017  "An internal error occurred while getting Assets. "
27018  "Diagnostics: Invalid asset_type parameter value",
27019  "/omp?cmd=get_assets", response_data);
27020  }
27021 
27022  extra_response = g_string_new (extra_xml ? extra_xml : "");
27023 
27024  extra_attribs = g_string_new("");
27025  g_string_append_printf (extra_attribs, "type=\"%s\" ignore_pagination=\"%s\"",
27026  params_value (params, "asset_type"),
27027  params_value (params, "ignore_pagination")
27028  ? params_value (params, "ignore_pagination")
27029  : "0");
27030  if (params_value (params, "details"))
27031  g_string_append_printf (extra_attribs,
27032  " details=\"%s\"",
27033  params_value (params, "details"));
27034  ret = get_many (connection, "asset", credentials, params, extra_response->str,
27035  extra_attribs->str, response_data);
27036 
27037  g_string_free (extra_response, TRUE);
27038  g_string_free (extra_attribs, TRUE);
27039 
27040  return ret;
27041 }
27042 
27053 char *
27054 get_assets_omp (openvas_connection_t *connection, credentials_t * credentials,
27055  params_t *params, cmd_response_data_t* response_data)
27056 {
27057  return get_assets (connection, credentials, params, NULL, response_data);
27058 }
27059 
27070 char *
27071 create_asset_omp (openvas_connection_t *connection, credentials_t *credentials,
27072  params_t *params, cmd_response_data_t* response_data)
27073 {
27074  char *ret;
27075  gchar *response;
27076  const char *no_redirect, *report_id, *filter;
27077  entity_t entity;
27078 
27079  no_redirect = params_value (params, "no_redirect");
27080  report_id = params_value (params, "report_id");
27081  filter = params_value (params, "filter");
27082 
27083  CHECK_PARAM_INVALID (report_id, "Create Asset", "get_report_section");
27084  CHECK_PARAM_INVALID (filter, "Create Asset", "get_report_section");
27085 
27086  response = NULL;
27087  entity = NULL;
27088  switch (ompf (connection, credentials,
27089  &response,
27090  &entity,
27091  response_data,
27092  "<create_asset>"
27093  "<report id=\"%s\">"
27094  "<filter><term>%s</term></filter>"
27095  "</report>"
27096  "</create_asset>",
27097  report_id,
27098  filter))
27099  {
27100  case 0:
27101  case -1:
27102  break;
27103  case 1:
27104  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27105  return gsad_message (credentials,
27106  "Internal error", __FUNCTION__, __LINE__,
27107  "An internal error occurred while creating an asset. "
27108  "No new asset was created. "
27109  "Diagnostics: Failure to send command to manager daemon.",
27110  "/omp?cmd=get_tasks", response_data);
27111  case 2:
27112  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27113  return gsad_message (credentials,
27114  "Internal error", __FUNCTION__, __LINE__,
27115  "An internal error occurred while creating an asset. "
27116  "It is unclear whether the asset has been created or not. "
27117  "Diagnostics: Failure to receive response from manager daemon.",
27118  "/omp?cmd=get_tasks", response_data);
27119  default:
27120  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27121  return gsad_message (credentials,
27122  "Internal error", __FUNCTION__, __LINE__,
27123  "An internal error occurred while creating an asset. "
27124  "It is unclear whether the asset has been created or not. "
27125  "Diagnostics: Internal Error.",
27126  "/omp?cmd=get_tasks", response_data);
27127  }
27128 
27129  ret = response_from_entity (connection, credentials, params, entity,
27130  (no_redirect && strcmp (no_redirect, "0")),
27131  NULL, "get_report_section",
27132  NULL, "get_report_section",
27133  "Create Asset", response_data);
27134  free_entity (entity);
27135  g_free (response);
27136  return ret;
27137 }
27138 
27149 char *
27150 delete_asset_omp (openvas_connection_t *connection, credentials_t * credentials,
27151  params_t *params, cmd_response_data_t* response_data)
27152 {
27153  gchar *html, *response, *resource_id;
27154  const char *next_id, *no_redirect;
27155  entity_t entity;
27156  gchar *next_url;
27157 
27158  if (params_value (params, "asset_id"))
27159  resource_id = g_strdup (params_value (params, "asset_id"));
27160  else if (params_value (params, "report_id"))
27161  resource_id = g_strdup (params_value (params, "report_id"));
27162  else
27163  {
27164  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27165  return gsad_message (credentials,
27166  "Internal error", __FUNCTION__, __LINE__,
27167  "An internal error occurred while deleting an asset. "
27168  "The asset was not deleted. "
27169  "Diagnostics: Required parameter was NULL.",
27170  "/omp?cmd=get_tasks", response_data);
27171  }
27172 
27173  /* This is a hack, needed because asset_id is the param name used for
27174  * both the asset being deleted and the asset on the next page. */
27175  next_id = params_value (params, "next_id");
27176  if (next_id
27177  && params_value (params, "asset_id"))
27178  {
27179  param_t *param;
27180  param = params_get (params, "asset_id");
27181  g_free (param->value);
27182  param->value = g_strdup (next_id);
27183  param->value_size = strlen (param->value);
27184  }
27185 
27186  /* Delete the resource and get all resources. */
27187 
27188  if (openvas_connection_sendf (connection,
27189  "<delete_asset %s_id=\"%s\"/>",
27190  params_value (params, "asset_id")
27191  ? "asset" : "report",
27192  resource_id)
27193  == -1)
27194  {
27195  g_free (resource_id);
27196  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27197  return gsad_message (credentials,
27198  "Internal error", __FUNCTION__, __LINE__,
27199  "An internal error occurred while deleting an asset. "
27200  "The asset is not deleted. "
27201  "Diagnostics: Failure to send command to manager daemon.",
27202  "/omp?cmd=get_tasks", response_data);
27203  }
27204 
27205  g_free (resource_id);
27206 
27207  entity = NULL;
27208  if (read_entity_and_text_c (connection, &entity, &response))
27209  {
27210  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27211  return gsad_message (credentials,
27212  "Internal error", __FUNCTION__, __LINE__,
27213  "An internal error occurred while deleting an asset. "
27214  "It is unclear whether the asset has been deleted or not. "
27215  "Diagnostics: Failure to read response from manager daemon.",
27216  "/omp?cmd=get_tasks", response_data);
27217  }
27218 
27219  /* Cleanup, and return transformed XML. */
27220 
27221  if (params_given (params, "next") == 0)
27222  params_add (params, "next", "get_asset");
27223 
27224  no_redirect = params_value (params, "no_redirect");
27225  if (no_redirect && strcmp (no_redirect, "0"))
27226  {
27227  html = next_page (connection, credentials, params, response,
27228  response_data);
27229  if (html == NULL)
27230  {
27231  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27232  html = gsad_message (credentials,
27233  "Internal error", __FUNCTION__, __LINE__,
27234  "An internal error occurred while deleting an "
27235  "asset. Diagnostics: Error in parameter next.",
27236  "/omp?cmd=get_tasks", response_data);
27237  }
27238  }
27239  else
27240  {
27241  next_url = next_page_url (credentials, params,
27242  NULL, "get_asset", "delete_asset",
27243  entity_attribute (entity, "status"),
27244  entity_attribute (entity, "status_text"));
27245  response_data->redirect = next_url;
27246 
27247  html = NULL;
27248  }
27249 
27250  g_free (response);
27251  free_entity (entity);
27252  return html;
27253 }
27254 
27268 char *
27269 export_asset_omp (openvas_connection_t *connection, credentials_t * credentials,
27270  params_t *params, enum content_type * content_type,
27271  char **content_disposition, gsize *content_length,
27272  cmd_response_data_t* response_data)
27273 {
27274  return export_resource (connection, "asset", credentials, params,
27275  content_type, content_disposition, content_length,
27276  response_data);
27277 }
27278 
27293 char *
27294 export_assets_omp (openvas_connection_t *connection,
27295  credentials_t * credentials, params_t *params,
27296  enum content_type * content_type, char **content_disposition,
27297  gsize *content_length, cmd_response_data_t* response_data)
27298 {
27299  return export_many (connection, "asset", credentials, params, content_type,
27300  content_disposition, content_length, response_data);
27301 }
27302 
27314 char *
27315 edit_asset (openvas_connection_t *connection, credentials_t *credentials,
27316  params_t *params, const char *extra_xml,
27317  cmd_response_data_t* response_data)
27318 {
27319  GString *xml;
27320  const char *asset_id;
27321  gchar *response;
27322  entity_t entity;
27323 
27324  asset_id = params_value (params, "asset_id");
27325  if (asset_id == NULL)
27326  {
27327  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27328  return gsad_message (credentials,
27329  "Internal error", __FUNCTION__, __LINE__,
27330  "An internal error occurred while editing a asset. "
27331  "The asset remains as it was. "
27332  "Diagnostics: Required ID parameter was NULL.",
27333  "/omp?cmd=get_tasks", response_data);
27334  }
27335 
27336  xml = g_string_new ("");
27337 
27338  g_string_append_printf (xml, "<edit_asset>");
27339 
27340  if (extra_xml)
27341  g_string_append (xml, extra_xml);
27342 
27343  response = NULL;
27344  entity = NULL;
27345  switch (ompf (connection, credentials,
27346  &response,
27347  &entity,
27348  response_data,
27349  "<get_assets"
27350  " type=\"host\""
27351  " asset_id=\"%s\""
27352  " details=\"1\"/>",
27353  asset_id))
27354  {
27355  case 0:
27356  case -1:
27357  break;
27358  case 1:
27359  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27360  g_string_free (xml, TRUE);
27361  return gsad_message (credentials,
27362  "Internal error", __FUNCTION__, __LINE__,
27363  "An internal error occurred while getting the asset. "
27364  "Diagnostics: Failure to send command to manager daemon.",
27365  "/omp?cmd=get_assets", response_data);
27366  case 2:
27367  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27368  g_string_free (xml, TRUE);
27369  return gsad_message (credentials,
27370  "Internal error", __FUNCTION__, __LINE__,
27371  "An internal error occurred while getting the asset. "
27372  "Diagnostics: Failure to receive response from manager daemon.",
27373  "/omp?cmd=get_assets", response_data);
27374  default:
27375  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27376  g_string_free (xml, TRUE);
27377  return gsad_message (credentials,
27378  "Internal error", __FUNCTION__, __LINE__,
27379  "An internal error occurred while getting the asset. "
27380  "Diagnostics: Internal Error.",
27381  "/omp?cmd=get_assets", response_data);
27382  }
27383 
27384  g_string_append (xml, response);
27385  g_string_append_printf (xml, "</edit_asset>");
27386  free_entity (entity);
27387  g_free (response);
27388  return xsl_transform_omp (connection, credentials, params,
27389  g_string_free (xml, FALSE), response_data);
27390 }
27391 
27402 char *
27403 edit_asset_omp (openvas_connection_t *connection,
27404  credentials_t * credentials, params_t *params,
27405  cmd_response_data_t* response_data)
27406 {
27407  return edit_asset (connection, credentials, params, NULL, response_data);
27408 }
27409 
27420 char *
27421 save_asset_omp (openvas_connection_t *connection,
27422  credentials_t * credentials, params_t *params,
27423  cmd_response_data_t* response_data)
27424 {
27425  int ret;
27426  gchar *html, *response;
27427  const char *no_redirect, *asset_id, *comment;
27428  entity_t entity;
27429 
27430  no_redirect = params_value (params, "no_redirect");
27431  asset_id = params_value (params, "asset_id");
27432  comment = params_value (params, "comment");
27433 
27434  CHECK_PARAM_INVALID (asset_id, "Save Asset", "edit_asset");
27435  CHECK_PARAM_INVALID (comment, "Save Asset", "edit_asset");
27436 
27437  /* Modify the asset. */
27438 
27439  response = NULL;
27440  entity = NULL;
27441  ret = ompf (connection, credentials,
27442  &response,
27443  &entity,
27444  response_data,
27445  "<modify_asset asset_id=\"%s\">"
27446  "<comment>%s</comment>"
27447  "</modify_asset>",
27448  asset_id,
27449  comment);
27450 
27451  switch (ret)
27452  {
27453  case 0:
27454  case -1:
27455  break;
27456  case 1:
27457  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27458  return gsad_message (credentials,
27459  "Internal error", __FUNCTION__, __LINE__,
27460  "An internal error occurred while saving an asset. "
27461  "The asset was not saved. "
27462  "Diagnostics: Failure to send command to manager daemon.",
27463  "/omp?cmd=get_assets", response_data);
27464  case 2:
27465  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27466  return gsad_message (credentials,
27467  "Internal error", __FUNCTION__, __LINE__,
27468  "An internal error occurred while saving an asset. "
27469  "It is unclear whether the asset has been saved or not. "
27470  "Diagnostics: Failure to receive response from manager daemon.",
27471  "/omp?cmd=get_assets", response_data);
27472  default:
27473  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27474  return gsad_message (credentials,
27475  "Internal error", __FUNCTION__, __LINE__,
27476  "An internal error occurred while saving an asset. "
27477  "It is unclear whether the asset has been saved or not. "
27478  "Diagnostics: Internal Error.",
27479  "/omp?cmd=get_assets", response_data);
27480  }
27481 
27482  html = response_from_entity (connection, credentials, params, entity,
27483  (no_redirect && strcmp (no_redirect, "0")),
27484  NULL, "get_assets",
27485  NULL, "edit_asset",
27486  "Save Asset", response_data);
27487  free_entity (entity);
27488  g_free (response);
27489  return html;
27490 }
27491 
27503 static char *
27504 get_assets_chart (openvas_connection_t *connection, credentials_t *credentials,
27505  params_t *params, const char *extra_xml,
27506  cmd_response_data_t* response_data)
27507 {
27508  return xsl_transform_omp (connection, credentials, params,
27509  g_strdup ("<get_assets_chart/>"), response_data);
27510 }
27511 
27522 char *
27523 get_assets_chart_omp (openvas_connection_t *connection,
27524  credentials_t * credentials, params_t *params,
27525  cmd_response_data_t* response_data)
27526 {
27527  return get_assets_chart (connection, credentials, params, NULL,
27528  response_data);
27529 }
27530 
27531 
27532 /* Manager communication. */
27533 
27541 int
27542 connect_unix (const gchar *path)
27543 {
27544  struct sockaddr_un address;
27545  int sock;
27546 
27547  /* Make socket. */
27548 
27549  sock = socket (AF_UNIX, SOCK_STREAM, 0);
27550  if (sock == -1)
27551  {
27552  g_warning ("Failed to create server socket");
27553  return -1;
27554  }
27555 
27556  /* Connect to server. */
27557 
27558  address.sun_family = AF_UNIX;
27559  strncpy (address.sun_path, path, sizeof (address.sun_path) - 1);
27560  if (connect (sock, (struct sockaddr *) &address, sizeof (address)) == -1)
27561  {
27562  g_warning ("Failed to connect to server: %s", strerror (errno));
27563  close (sock);
27564  return -1;
27565  }
27566 
27567  return sock;
27568 }
27569 
27579 int
27580 openvas_connection_open (openvas_connection_t *connection,
27581  const gchar *address,
27582  int port)
27583 {
27584  if (address == NULL)
27585  return -1;
27586 
27587  connection->tls = manager_use_tls;
27588 
27589  if (manager_use_tls)
27590  {
27591  connection->socket = openvas_server_open (&connection->session,
27592  address,
27593  port);
27594  connection->credentials = NULL;
27595  }
27596  else
27597  connection->socket = connect_unix (address);
27598 
27599  if (connection->socket == -1)
27600  return -1;
27601 
27602  return 0;
27603 }
27604 
27621 int
27622 authenticate_omp (const gchar * username, const gchar * password,
27623  gchar **role, gchar **timezone, gchar **severity,
27624  gchar **capabilities, gchar **language, gchar **pw_warning,
27625  GTree **chart_prefs, gchar **autorefresh)
27626 {
27627  openvas_connection_t connection;
27628  int auth;
27629  omp_authenticate_info_opts_t auth_opts;
27630 
27631  if (openvas_connection_open (&connection,
27633  manager_port))
27634  {
27635  g_debug ("%s failed to acquire socket!\n", __FUNCTION__);
27636  return 2;
27637  }
27638 
27639 #ifdef DEBUG_AUTH
27640  /* Enable this if you need the CGI to sleep after launch. This can be useful
27641  * if you need to attach to manager process the CGI is talking to for
27642  * debugging purposes.
27643  *
27644  * It's probably easier to run gsad in the foreground under gdb and
27645  * set a break point here.
27646  */
27647  g_debug ("Sleeping!");
27648  sleep (20);
27649 #endif
27650 
27651  auth_opts = omp_authenticate_info_opts_defaults;
27652  auth_opts.username = username;
27653  auth_opts.password = password;
27654  auth_opts.role = role;
27655  auth_opts.severity = severity;
27656  auth_opts.timezone = timezone;
27657  auth_opts.pw_warning = pw_warning;
27658 
27659  auth = omp_authenticate_info_ext_c (&connection, auth_opts);
27660  if (auth == 0)
27661  {
27662  entity_t entity;
27663  const char* status;
27664  char first;
27665  gchar *response;
27666  int ret;
27667 
27668  /* Get language setting. */
27669 
27670  ret = setting_get_value (&connection,
27671  "6765549a-934e-11e3-b358-406186ea4fc5",
27672  language,
27673  NULL);
27674 
27675  switch (ret)
27676  {
27677  case 0:
27678  break;
27679  case 1:
27680  case 2:
27681  openvas_connection_close (&connection);
27682  return 2;
27683  default:
27684  openvas_connection_close (&connection);
27685  return -1;
27686  }
27687 
27688  /* Request help. */
27689 
27690  ret = openvas_connection_sendf (&connection,
27691  "<help format=\"XML\" type=\"brief\"/>");
27692  if (ret)
27693  {
27694  openvas_connection_close (&connection);
27695  return 2;
27696  }
27697 
27698  /* Read the response. */
27699 
27700  entity = NULL;
27701  if (read_entity_and_text_c (&connection, &entity, &response))
27702  {
27703  openvas_connection_close (&connection);
27704  return 2;
27705  }
27706 
27707  /* Check the response. */
27708 
27709  status = entity_attribute (entity, "status");
27710  if (status == NULL
27711  || strlen (status) == 0)
27712  {
27713  g_free (response);
27714  free_entity (entity);
27715  return -1;
27716  }
27717  first = status[0];
27718  free_entity (entity);
27719  if (first == '2')
27720  {
27721  *capabilities = response;
27722  }
27723  else
27724  {
27725  openvas_connection_close (&connection);
27726  g_free (response);
27727  return -1;
27728  }
27729 
27730  /* Get the chart preferences */
27731 
27732  ret = openvas_connection_sendf (&connection,
27733  "<get_settings"
27734  " filter='name~\"Dashboard\"'/>");
27735  if (ret)
27736  {
27737  openvas_connection_close (&connection);
27738  return 2;
27739  }
27740 
27741  /* Read the response */
27742  entity = NULL;
27743  if (read_entity_and_text_c (&connection, &entity, &response))
27744  {
27745  openvas_connection_close (&connection);
27746  return 2;
27747  }
27748 
27749  /* Check the response. */
27750  status = entity_attribute (entity, "status");
27751  if (status == NULL
27752  || strlen (status) == 0)
27753  {
27754  g_free (response);
27755  free_entity (entity);
27756  return -1;
27757  }
27758  first = status[0];
27759  if (first == '2')
27760  {
27761  entities_t entities = entity->entities;
27762  entity_t child_entity;
27763  *chart_prefs = g_tree_new_full ((GCompareDataFunc) g_strcmp0,
27764  NULL, g_free, g_free);
27765 
27766  while ((child_entity = first_entity (entities)))
27767  {
27768  if (strcmp (entity_name (child_entity), "setting") == 0)
27769  {
27770  const char *setting_id
27771  = entity_attribute (child_entity, "id");
27772  const char *setting_value
27773  = entity_text (entity_child (child_entity, "value"));
27774 
27775  if (setting_id && setting_value)
27776  g_tree_insert (*chart_prefs,
27777  g_strdup (setting_id),
27778  g_strdup (setting_value));
27779  }
27780  entities = next_entities (entities);
27781  }
27782  free_entity (entity);
27783  g_free (response);
27784  }
27785  else
27786  {
27787  free_entity (entity);
27788  g_free (response);
27789  openvas_connection_close (&connection);
27790  return -1;
27791  }
27792 
27793  /* Get autorefresh setting. */
27794 
27795  ret = setting_get_value (&connection,
27796  "578a1c14-e2dc-45ef-a591-89d31391d007",
27797  autorefresh,
27798  NULL);
27799 
27800  switch (ret)
27801  {
27802  case 0:
27803  break;
27804  case 1:
27805  case 2:
27806  openvas_connection_close (&connection);
27807  return 2;
27808  default:
27809  openvas_connection_close (&connection);
27810  return -1;
27811  }
27812 
27813  openvas_connection_close (&connection);
27814  return 0;
27815  }
27816  else
27817  {
27818  openvas_connection_close (&connection);
27819  return 1;
27820  }
27821 }
27822 
27834 int
27835 manager_connect (credentials_t *credentials, openvas_connection_t *connection,
27836  cmd_response_data_t *response_data)
27837 {
27838  omp_authenticate_info_opts_t auth_opts;
27839 
27840  if (openvas_connection_open (connection,
27842  manager_port))
27843  {
27844  response_data->http_status_code = MHD_HTTP_SERVICE_UNAVAILABLE;
27845  return -1;
27846  }
27847 
27848 #if 0
27849  g_debug ("in manager_connect: Trying to authenticate with %s/%s\n",
27850  credentials->username,
27851  credentials->password);
27852 #endif
27853 
27854 
27855  auth_opts = omp_authenticate_info_opts_defaults;
27856  auth_opts.username = credentials->username;
27857  auth_opts.password = credentials->password;
27858  if (omp_authenticate_info_ext_c (connection, auth_opts))
27859  {
27860  g_debug ("authenticate failed!\n");
27861  openvas_connection_close (connection);
27862  return -2;
27863  }
27864 
27865 #ifdef DEBUG
27866  /* Enable this if you need the CGI to sleep after launch. This can be useful
27867  * if you need to attach to manager process the CGI is talking to for
27868  * debugging purposes.
27869  *
27870  * An easier method is to run gsad under gdb and set a breakpoint here.
27871  */
27872  g_debug ("Sleeping!");
27873  sleep (10);
27874 #endif
27875  return 0;
27876 }
char * new_container_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new task.
Definition: gsad_omp.c:3773
char * save_container_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save container task, get next page, XSL transform the result.
Definition: gsad_omp.c:4887
char * create_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a permission, get all permissions, XSL transform the result.
Definition: gsad_omp.c:20856
char * delete_port_range_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a port range, get the port list, XSL transform the result.
Definition: gsad_omp.c:22924
char * save_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save details of an NVT for a config and return the next page.
Definition: gsad_omp.c:11952
char * delete_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete report format, get report formats, XSL transform the result.
Definition: gsad_omp.c:18077
int download_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *result_len, char **html, char **login, cmd_response_data_t *response_data)
Export a Credential in a defined format.
Definition: gsad_omp.c:6328
char * new_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new scanner page.
Definition: gsad_omp.c:16775
void set_http_status_from_entity(entity_t entity, cmd_response_data_t *response_data)
Set the HTTP status according to OMP response entity.
Definition: gsad_omp.c:885
char * export_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a filter.
Definition: gsad_omp.c:24049
char * export_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a user.
Definition: gsad_omp.c:25556
char * edit_agent(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_agent XML, XSL transform the result.
Definition: gsad_omp.c:7259
char * import_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Import report format, get all report formats, XSL transform result.
Definition: gsad_omp.c:18153
char * edit_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_alert XML, XSL transform the result.
Definition: gsad_omp.c:8945
char * export_agents_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of agents.
Definition: gsad_omp.c:7579
int params_value_size(params_t *params, const char *name)
Get the size of the value of param.
Definition: gsad_base.c:727
char * edit_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Edit override, get next page, XSL transform the result.
Definition: gsad_omp.c:16440
params_t * params_values(params_t *params, const char *name)
Get values of param.
Definition: gsad_base.c:777
int authenticate_omp(const gchar *username, const gchar *password, gchar **role, gchar **timezone, gchar **severity, gchar **capabilities, gchar **language, gchar **pw_warning, GTree **chart_prefs, gchar **autorefresh)
Check authentication credentials.
Definition: gsad_omp.c:27622
char * edit_my_settings_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page with user&#39;s settings, for editing.
Definition: gsad_omp.c:19125
char * export_report_formats_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of Report Formats.
Definition: gsad_omp.c:13224
char * download_ssl_cert(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get an SSL Certificate.
Definition: gsad_omp.c:14798
char * upload_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload scan config page.
Definition: gsad_omp.c:11362
char * wizard_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns a wizard page.
Definition: gsad_omp.c:26201
gchar * value
Definition: gsad_base.h:148
char * create_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create an asset, get report, XSL transform the result.
Definition: gsad_omp.c:27071
char * export_permissions_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of permissions.
Definition: gsad_omp.c:22236
char * delete_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete agent, get all agents, XSL transform result.
Definition: gsad_omp.c:7073
char * edit_filter(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_filter XML, XSL transform the result.
Definition: gsad_omp.c:24010
char * export_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export an asset.
Definition: gsad_omp.c:27269
char * export_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a permission.
Definition: gsad_omp.c:22210
char * get_permissions_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all permissions, XSL transform the result.
Definition: gsad_omp.c:20618
char * resource_action(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *type, const char *action, cmd_response_data_t *response_data)
Perform action on resource, get next page, XSL transform result.
Definition: gsad_omp.c:3135
char * save_config_family_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get details of an NVT for a config, XSL transform the result.
Definition: gsad_omp.c:12379
char * save_my_settings_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *accept_language, char **timezone, char **password, char **severity, char **language, cmd_response_data_t *response_data)
Returns page with user&#39;s settings, for editing.
Definition: gsad_omp.c:19221
void cmd_response_data_init(cmd_response_data_t *data)
Initializes a cmd_response_data_t struct.
Definition: gsad_omp.c:348
char * create_port_range_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Add a range to a port list, XSL transform the result.
Definition: gsad_omp.c:22578
char * delete_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a scanner, get all scanners, XSL transform the result.
Definition: gsad_omp.c:16987
char * get_filters_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all filters, XSL transform the result.
Definition: gsad_omp.c:23795
char * sync_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Sync config, get configs, XSL transform the result.
Definition: gsad_omp.c:11821
char * new_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new notes page.
Definition: gsad_omp.c:15448
char * delete_trash_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash report format, get all trash, XSL transform the result.
Definition: gsad_omp.c:9858
char * edit_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_port_list XML, XSL transform the result.
Definition: gsad_omp.c:22782
char * capabilities
Capabilites of manager.
Definition: gsad_base.h:76
char * import_report_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Import report, get all reports, XSL transform the result.
Definition: gsad_omp.c:4008
char * create_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a task, get all tasks, XSL transform the result.
Definition: gsad_omp.c:4114
char * get_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one tag, XSL transform the result.
Definition: gsad_omp.c:10546
param_t * params_get(params_t *params, const char *name)
Get param.
Definition: gsad_base.c:679
char * save_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save note, get next page, XSL transform the result.
Definition: gsad_omp.c:15744
char * timezone
User&#39;s timezone.
Definition: gsad_base.h:72
content_type
Content types.
Definition: gsad_base.h:120
char * get_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one target, XSL transform the result.
Definition: gsad_omp.c:10886
char * export_users_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of users.
Definition: gsad_omp.c:25580
char * gsad_message(credentials_t *credentials, const char *title, const char *function, int line, const char *msg, const char *backurl, cmd_response_data_t *response_data)
Handles fatal errors.
Definition: gsad_base.c:467
char * edit_scanner(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_scanner XML, XSL transform the result.
Definition: gsad_omp.c:17026
char * delete_trash_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash config, get all trash, XSL transform the result.
Definition: gsad_omp.c:9801
char * export_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a agent.
Definition: gsad_omp.c:7554
#define CHECK_OMPF_RET
Definition: gsad_omp.c:21140
char * edit_asset(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit XML, XSL transform the result.
Definition: gsad_omp.c:27315
gchar * format_file_name(gchar *fname_format, credentials_t *credentials, const char *type, const char *uuid, entity_t resource_entity)
Generates a file name for exporting.
Definition: gsad_omp.c:2581
char * get_result_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one result, XSL transform the result.
Definition: gsad_omp.c:15139
char * delete_trash_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a filter, get all filters, XSL transform the result.
Definition: gsad_omp.c:23936
int params_given(params_t *params, const char *name)
Get whether a param was given at all.
Definition: gsad_base.c:695
#define CHECK_PARAM(name, op_name, ret_func)
Check a param.
Definition: gsad_omp.c:1174
char * save_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, char **password_return, char **modified_user, int *logout_user, cmd_response_data_t *response_data)
Modify a user, get all users, XSL transform the result.
Definition: gsad_omp.c:25325
char * restore_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Restore a resource, get all trash, XSL transform the result.
Definition: gsad_omp.c:9934
char * create_permissions_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create multiple permission, get next page, XSL transform the result.
Definition: gsad_omp.c:21507
char * export_roles_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of roles.
Definition: gsad_omp.c:23424
char * export_preference_file_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a file preference.
Definition: gsad_omp.c:13097
gchar * manager_address
The address the manager is on.
Definition: gsad_omp.c:96
char * caller
Caller URL, for POST relogin.
Definition: gsad_base.h:74
char * export_tags_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of tags.
Definition: gsad_omp.c:10506
char * export_results_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of results.
Definition: gsad_omp.c:14940
char * get_assets_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get assets, XSL transform the result.
Definition: gsad_omp.c:27054
char * new_permissions_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup new_permission XML, XSL transform the result.
Definition: gsad_omp.c:21489
char * delete_trash_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a permission, get all permissions, XSL transform the result.
Definition: gsad_omp.c:20636
Response information for commands.
Definition: gsad_base.h:92
char * get_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one credential, XSL transform the result.
Definition: gsad_omp.c:6306
void omp_init(const gchar *manager_address_unix, const gchar *manager_address_tls, int port_manager)
Init the GSA OMP library.
Definition: gsad_omp.c:319
int valid_utf8
Definition: gsad_base.h:153
char * logout(credentials_t *credentials, const gchar *message, cmd_response_data_t *response_data)
Setup edit_user XML, XSL transform the result.
Definition: gsad_omp.c:25280
char * save_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a port list, get all port list, XSL transform the result.
Definition: gsad_omp.c:22800
char * run_wizard_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Run a wizard and XSL transform the result.
Definition: gsad_omp.c:18630
const char * params_filename(params_t *params, const char *name)
Get filename of param.
Definition: gsad_base.c:761
char * sync_feed_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Synchronize with an NVT feed and XSL transform the result.
Definition: gsad_omp.c:23678
char * download_key_pub(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get a Scanner&#39;s Certificate.
Definition: gsad_omp.c:14876
char * delete_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a filter, get all filters, XSL transform the result.
Definition: gsad_omp.c:23955
char * dashboard(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Show a dashboard.
Definition: gsad_omp.c:25685
char * resume_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Resume a task, get all tasks, XSL transform the result.
Definition: gsad_omp.c:5055
char * new_permissions(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup new_permissions XML, XSL transform the result.
Definition: gsad_omp.c:21202
char * cvss_calculator(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Definition: gsad_omp.c:25590
char * toggle_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Set tag enabled status.
Definition: gsad_omp.c:10600
char * new_override(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Return the new overrides page.
Definition: gsad_omp.c:15950
char * delete_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a task, get all tasks, XSL transform the result.
Definition: gsad_omp.c:4478
void set_language_code(gchar **lang, const gchar *language)
Set language code of user.
Definition: gsad_base.c:218
char * create_report_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a report, get all tasks, XSL transform the result.
Definition: gsad_omp.c:3854
int token_user_remove(const char *)
Remove a user from the session "database", releasing the user_t too.
Definition: gsad.c:980
char * delete_trash_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash scanner, get all scanners, XSL transform the result.
Definition: gsad_omp.c:17006
char * delete_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a target, get all targets, XSL transform the result.
Definition: gsad_omp.c:9763
char * new_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new target.
Definition: gsad_omp.c:10126
char * get_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one permission, XSL transform the result.
Definition: gsad_omp.c:20580
char * new_port_range_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new Port Range page.
Definition: gsad_omp.c:22560
char * sync_scap_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Synchronize with a SCAP feed and XSL transform the result.
Definition: gsad_omp.c:23697
char * edit_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_credential XML, XSL transform the result.
Definition: gsad_omp.c:6661
char * get_tasks_chart_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a tasks chart, XSL transform the result.
Definition: gsad_omp.c:5578
#define RESULTS_PER_PAGE
Initial filtered results per page on the report summary.
Definition: gsad_base.h:41
char * get_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one port_list, XSL transform the result.
Definition: gsad_omp.c:22689
char * sync_cert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Synchronize with a CERT feed and XSL transform the result.
Definition: gsad_omp.c:23716
char * get_roles_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all roles, XSL transform the result.
Definition: gsad_omp.c:23380
void cmd_response_data_reset(cmd_response_data_t *data)
Clears a cmd_response_data_t struct.
Definition: gsad_omp.c:360
char * edit_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_task XML, XSL transform the result.
Definition: gsad_omp.c:4622
char * auth_settings_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Definition: gsad_omp.c:25207
char * new_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new report format page.
Definition: gsad_omp.c:18058
char * edit_schedule(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_schedule XML, XSL transform the result.
Definition: gsad_omp.c:24202
char * get_system_reports_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all system reports, XSL transform the result.
Definition: gsad_omp.c:17577
char * export_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a task.
Definition: gsad_omp.c:4993
gchar * message_invalid(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data, const char *message, const char *status, const char *op_name, const char *next_cmd)
Check a param using the direct response method.
Definition: gsad_omp.c:1472
char * get_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a task, XSL transform the result.
Definition: gsad_omp.c:5932
Structure of credential related information.
Definition: gsad_base.h:66
int manager_use_tls
Whether to use TLS for Manager connections.
Definition: gsad_omp.c:91
char * delete_trash_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a role from trash, get all roles, XSL transform the result.
Definition: gsad_omp.c:23067
char * export_configs_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of scan configs.
Definition: gsad_omp.c:12925
const char * params_original_value(params_t *params, const char *name)
Get original value of param, before validation.
Definition: gsad_base.c:745
char * get_my_settings_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page with user&#39;s settings.
Definition: gsad_omp.c:18916
char * create_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a schedule, get all schedules, XSL transform the result.
Definition: gsad_omp.c:17425
char * delete_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete an alert, get all alerts, XSL transform the result.
Definition: gsad_omp.c:8376
#define CHECK_PARAM_INVALID(name, op_name, next_cmd)
Check a param using the direct response method.
Definition: gsad_omp.c:1195
char * get_one(openvas_connection_t *connection, const char *type, credentials_t *credentials, params_t *params, const char *extra_xml, const char *extra_attribs, cmd_response_data_t *response_data)
Get one resource, XSL transform the result.
Definition: gsad_omp.c:1823
char * get_trash_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all trash, XSL transform the result.
Definition: gsad_omp.c:18839
char * get_report_formats_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all Report Formats, XSL transform the result.
Definition: gsad_omp.c:18014
char * edit_tag(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_tag XML, XSL transform the result.
Definition: gsad_omp.c:10280
gchar * accept_language_to_env_fmt(const char *accept_language)
Convert an Accept-Language string to the LANGUAGE env variable form.
Definition: xslt_i18n.c:769
char * start_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Start a task, get all tasks, XSL transform the result.
Definition: gsad_omp.c:5073
char * username
Name of user.
Definition: gsad_base.h:69
char * create_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create an agent, get all agents, XSL transform result.
Definition: gsad_omp.c:6928
char * import_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Import port list, get all port_lists, XSL transform the result.
Definition: gsad_omp.c:22943
char * get_system_report_omp(openvas_connection_t *connection, credentials_t *credentials, const char *url, params_t *params, enum content_type *content_type, gsize *content_length, cmd_response_data_t *response_data)
Return system report image.
Definition: gsad_omp.c:17777
char * get_results_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all results, XSL transform the result.
Definition: gsad_omp.c:14988
char * new_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new target.
Definition: gsad_omp.c:9390
char * get_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one scanner, XSL transform the result.
Definition: gsad_omp.c:16658
char * new_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new agent page.
Definition: gsad_omp.c:6911
char * new_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new user.
Definition: gsad_omp.c:24587
char * get_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one agent, XSL transform the result.
Definition: gsad_omp.c:7400
char * export_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a Port List.
Definition: gsad_omp.c:13046
char * edit_user(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_user XML, XSL transform the result.
Definition: gsad_omp.c:25044
char * delete_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete config, get all configs, XSL transform the result.
Definition: gsad_omp.c:12878
char * save_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a tag, get all tags, XSL transform the result.
Definition: gsad_omp.c:10373
char * get_tags_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all tags, XSL transform the result.
Definition: gsad_omp.c:10583
char * export_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a config.
Definition: gsad_omp.c:12900
char * process_bulk_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Returns a process_bulk page.
Definition: gsad_omp.c:26347
char * get_notes_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all notes, XSL transform the result.
Definition: gsad_omp.c:15214
char * export_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a report format.
Definition: gsad_omp.c:13198
int user_set_autorefresh(const gchar *token, const gchar *autorefresh)
Set default autorefresh interval of user.
Definition: gsad.c:852
char * edit_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_tag XML, XSL transform the result.
Definition: gsad_omp.c:10356
char * edit_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Edit note, get next page, XSL transform the result.
Definition: gsad_omp.c:15727
char * edit_alert(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_alert XML, XSL transform the result.
Definition: gsad_omp.c:8723
char * delete_trash_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a override, get all overrides, XSL transform the result.
Definition: gsad_omp.c:16357
char * edit_note(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Edit note, get next page, XSL transform the result.
Definition: gsad_omp.c:15664
char * delete_report_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete report, get task status, XSL transform the result.
Definition: gsad_omp.c:13246
char * autorefresh
Auto-refresh interval.
Definition: gsad_base.h:82
char * get_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one report format, XSL transform the result.
Definition: gsad_omp.c:17975
GTree * last_filt_ids
Last filter ids.
Definition: gsad_base.h:83
char * edit_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_asset XML, XSL transform the result.
Definition: gsad_omp.c:27403
char * client_address
Client&#39;s address.
Definition: gsad_base.h:80
char * save_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a role, return the next page.
Definition: gsad_omp.c:23444
char * get_scanners_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all scanners, XSL transform the result.
Definition: gsad_omp.c:16621
char * get_report_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *report_len, gchar **content_type, char **content_disposition, cmd_response_data_t *response_data)
Get a report and XSL transform the result.
Definition: gsad_omp.c:14595
char * edit_override(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Edit override, get next page, XSL transform the result.
Definition: gsad_omp.c:16377
#define PARAM_OR_SETTING(value, param, setting_id, cleanup)
Get a value from a param or fall back to a setting.
Definition: gsad_omp.c:3330
char * wizard_get_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns a wizard_get page.
Definition: gsad_omp.c:26327
char * delete_trash_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash port list, get all trash, XSL transform the result.
Definition: gsad_omp.c:22905
char * get_credentials_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one or all credentials, XSL transform the result.
Definition: gsad_omp.c:6586
char * export_alerts_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of alerts.
Definition: gsad_omp.c:9210
char * save_auth_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save authentication settings.
Definition: gsad_omp.c:25868
void init_find_by_value(find_by_value_t *find, gchar *value)
Definition: gsad_omp.c:396
char * get_aggregate_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an aggregate of resources.
Definition: gsad_omp.c:7599
char * export_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a Credential.
Definition: gsad_omp.c:6517
Headers/structs used generally in GSA.
int value_size
Definition: gsad_base.h:154
#define CHECK(name)
Check a param.
Definition: gsad_omp.c:9605
char * upload_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload port list page.
Definition: gsad_omp.c:22428
char * export_schedules_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of schedules.
Definition: gsad_omp.c:24267
Structure to search a key by value.
Definition: gsad_omp.c:389
char * edit_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_filter XML, XSL transform the result.
Definition: gsad_omp.c:24029
char * new_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new group.
Definition: gsad_omp.c:20224
char * get_targets_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all targets, XSL transform the result.
Definition: gsad_omp.c:10923
char * new_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new overrides page.
Definition: gsad_omp.c:16131
char * get_protocol_doc_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get OMP doc.
Definition: gsad_omp.c:19965
char * save_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a permission, get all permissions, XSL transform the result.
Definition: gsad_omp.c:22258
struct timeval cmd_start
Seconds since command page handler started.
Definition: gsad_base.h:68
gchar * login_xml(const gchar *message, const gchar *token, const gchar *time, const gchar *url, const gchar *i18n, const gchar *guest)
Generate XML for login page.
Definition: gsad_base.c:584
char * save_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify an asset, get all assets, XSL transform the result.
Definition: gsad_omp.c:27421
char * create_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create an override, get report, XSL transform the result.
Definition: gsad_omp.c:16148
char * create_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create config, get all configs, XSL transform the result.
Definition: gsad_omp.c:11379
char * get_feeds_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get descriptions of the feeds connected to the manager.
Definition: gsad_omp.c:23536
char * save_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify an alert, get all alerts, XSL transform the result.
Definition: gsad_omp.c:8962
char * create_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a credential, get all credentials, XSL transform result.
Definition: gsad_omp.c:5974
int download_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *result_len, char **html, char **filename, cmd_response_data_t *response_data)
Get an agent, XSL transform the result.
Definition: gsad_omp.c:7095
char * get_config_nvt_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get details of an NVT for a config, XSL transform the result.
Definition: gsad_omp.c:12620
char * stop_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Stop a task, get all tasks, XSL transform the result.
Definition: gsad_omp.c:5037
char * export_tasks_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of tasks.
Definition: gsad_omp.c:5017
char * export_credentials_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of Credentials.
Definition: gsad_omp.c:6543
char * get_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one filter, XSL transform the result.
Definition: gsad_omp.c:23758
char * bulk_delete_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete multiple resources, get next page, XSL transform the result.
Definition: gsad_omp.c:26639
char * get_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a config, XSL transform the result.
Definition: gsad_omp.c:11768
char * save_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save task, get next page, XSL transform the result.
Definition: gsad_omp.c:4639
char * delete_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete override, get next page, XSL transform the result.
Definition: gsad_omp.c:16338
char * save_chart_preference_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gchar **pref_id, gchar **pref_value, cmd_response_data_t *response_data)
Save chart preferences.
Definition: gsad_omp.c:26000
char * get_info_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get info, XSL transform the result.
Definition: gsad_omp.c:5410
char * create_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create an alert, get all alerts, XSL transform the result.
Definition: gsad_omp.c:8228
char * export_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a note.
Definition: gsad_omp.c:12948
char * create_host_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a host, serve next page.
Definition: gsad_omp.c:26804
char * delete_trash_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash schedule, get all trash, XSL transform the result.
Definition: gsad_omp.c:9877
#define params_t
Definition: gsad_base.h:61
char * export_port_lists_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of Port Lists.
Definition: gsad_omp.c:13072
char * get_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an override, XSL transform the result.
Definition: gsad_omp.c:15932
char * edit_config_family_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get details of a family for editing a config, XSL transform result.
Definition: gsad_omp.c:12361
char * new_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new schedule page.
Definition: gsad_omp.c:17408
char * new_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new Credential.
Definition: gsad_omp.c:6623
gchar * value
Definition: gsad_omp.c:391
char * get_info(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Requests SecInfo.
Definition: gsad_omp.c:5282
char * export_groups_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of groups.
Definition: gsad_omp.c:20448
char * new_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new Port List page.
Definition: gsad_omp.c:22745
char * delete_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a group, get all groups, XSL transform the result.
Definition: gsad_omp.c:20260
char * create_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a group, get all groups, XSL transform the result.
Definition: gsad_omp.c:20278
char * delete_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete note, get next page, XSL transform the result.
Definition: gsad_omp.c:10242
char * export_targets_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of targets.
Definition: gsad_omp.c:11230
char * edit_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_report_format XML, XSL transform the result.
Definition: gsad_omp.c:18134
Request parameter.
Definition: gsad_base.h:146
char * get_overrides_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all overrides, XSL transform the result.
Definition: gsad_omp.c:15895
char * save_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a agent, get all agents, XSL transform the result.
Definition: gsad_omp.c:7295
#define GET_TRASH_RESOURCE(capability, command, name)
Definition: gsad_omp.c:18726
char * get_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one group, XSL transform the result.
Definition: gsad_omp.c:20145
char * create_container_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a container task, serve next page.
Definition: gsad_omp.c:4029
char * delete_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a role, get all roles, XSL transform the result.
Definition: gsad_omp.c:23086
char * create_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a filter, get all filters, XSL transform the result.
Definition: gsad_omp.c:23837
char * get_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one user, XSL transform the result.
Definition: gsad_omp.c:24779
char * get_port_lists_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all port_lists, XSL transform the result.
Definition: gsad_omp.c:22727
GPtrArray * users
User session data.
Definition: gsad.c:340
char * delete_trash_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash agent, get all agents, XSL transform the result.
Definition: gsad_omp.c:9782
char * delete_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete note, get next page, XSL transform the result.
Definition: gsad_omp.c:15626
int guest
Whether the user is a guest user.
Definition: gsad_base.h:86
char * save_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save credential, get next page, XSL transform the result.
Definition: gsad_omp.c:6679
char * save_config_nvt_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save NVT prefs for a config, get NVT details, XSL transform result.
Definition: gsad_omp.c:12656
char * export_assets_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of assets.
Definition: gsad_omp.c:27294
char * get_users_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all users, XSL transform the result.
Definition: gsad_omp.c:24865
char * import_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Import config, get all configs, XSL transform the result.
Definition: gsad_omp.c:11468
int manager_port
The port the manager is on.
Definition: gsad_omp.c:101
char * verify_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Verify agent, get agents, XSL transform the result.
Definition: gsad_omp.c:7454
char * token
Session token.
Definition: gsad_base.h:73
char * export_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export an override.
Definition: gsad_omp.c:12995
const char * params_value(params_t *params, const char *name)
Get value of param.
Definition: gsad_base.c:711
char * delete_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a user, get all users, XSL transform the result.
Definition: gsad_omp.c:24604
char * get_config_family_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get details of a family for a config, XSL transform the result.
Definition: gsad_omp.c:12343
int member1(params_t *params, const char *string)
Look for param with value 1 and name equal to given string.
Definition: gsad_omp.c:725
char * get_assets_chart_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an assets chart, XSL transform the result.
Definition: gsad_omp.c:27523
char * new_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new filter.
Definition: gsad_omp.c:24094
char * export_scanners_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of scanners.
Definition: gsad_omp.c:16703
char * export_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export an alert.
Definition: gsad_omp.c:9185
char * download_ca_pub(openvas_connection_t *connection, credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get a Scanner&#39;s CA Certificate.
Definition: gsad_omp.c:14841
gboolean params_iterator_next(params_iterator_t *iterator, char **name, param_t **param)
Increment a params iterator.
Definition: gsad_base.c:887
char * delete_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a schedule, get all schedules, XSL transform the result.
Definition: gsad_omp.c:17558
char * edit_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_user XML, XSL transform the result.
Definition: gsad_omp.c:25200
char * role
User&#39;s role.
Definition: gsad_base.h:71
char * delete_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a port list, get all port lists, XSL transform the result.
Definition: gsad_omp.c:22886
#define GSAD_MESSAGE_INVALID
Answer for invalid input.
Definition: gsad_base.h:44
char * delete_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete credential, get all credentials, XSL transform result.
Definition: gsad_omp.c:6604
char * save_filter_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a filter, get all filters, XSL transform the result.
Definition: gsad_omp.c:24111
char * get_reports_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all reports, XSL transform the result.
Definition: gsad_omp.c:14648
char * edit_config_nvt_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Edit details of an NVT for a config, XSL transform the result.
Definition: gsad_omp.c:12638
char * edit_target(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_target XML, XSL transform the result.
Definition: gsad_omp.c:10692
char * export_resource(openvas_connection_t *connection, const char *type, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a resource.
Definition: gsad_omp.c:2671
char * edit_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a config, XSL transform the result.
Definition: gsad_omp.c:11804
char * save_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a target, get all targets, XSL transform the result.
Definition: gsad_omp.c:10940
const gchar * vendor_version_get()
Get the vendor version.
Definition: gsad_base.c:175
char * new_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new role.
Definition: gsad_omp.c:23050
int charts
Whether to show charts for this user.
Definition: gsad_base.h:85
char * edit_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_group XML, XSL transform the result.
Definition: gsad_omp.c:20403
char * delete_trash_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a group from trash, get all groups, XSL transform the result.
Definition: gsad_omp.c:20241
char * delete_trash_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash alert, get all trash, XSL transform the result.
Definition: gsad_omp.c:9820
char * save_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a group, return the next page.
Definition: gsad_omp.c:20468
char * get_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one role, XSL transform the result.
Definition: gsad_omp.c:23343
char * export_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a scanner.
Definition: gsad_omp.c:16678
char * pw_warning
Password policy warning message.
Definition: gsad_base.h:79
char * get_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one alert, XSL transform the result.
Definition: gsad_omp.c:8564
gchar * redirect
HTTP status code.
Definition: gsad_base.h:94
char * save_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save report_format, get next page, XSL transform the result.
Definition: gsad_omp.c:18232
char * export_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a schedule.
Definition: gsad_omp.c:24242
char * export_omp_doc_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Download the OMP doc.
Definition: gsad_omp.c:20023
GList * keys
Definition: gsad_omp.c:392
char * verify_report_format_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Verify report format, get report formats, XSL transform the result.
Definition: gsad_omp.c:18529
char * get_alerts_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all alerts, XSL transform the result.
Definition: gsad_omp.c:8705
char * edit_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_permission XML, XSL transform the result.
Definition: gsad_omp.c:22189
char * export_group_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a group.
Definition: gsad_omp.c:20423
char * xsl_transform(const char *xml_text, cmd_response_data_t *response_data)
XSL Transformation.
Definition: gsad_base.c:445
char * new_host_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new host page.
Definition: gsad_omp.c:26787
GTree * chart_prefs
Chart preferences.
Definition: gsad_base.h:81
char * get_tasks_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all tasks, XSL transform the result.
Definition: gsad_omp.c:5541
char * new_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup new_permission XML, XSL transform the result.
Definition: gsad_omp.c:20838
char * new_note(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Return the new notes page.
Definition: gsad_omp.c:15269
char * delete_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete an asset, go to the next page.
Definition: gsad_omp.c:27150
char * get_report_section_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a report section, XSL transform the result.
Definition: gsad_omp.c:14779
char * empty_trashcan_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Empty the trashcan, get all trash, XSL transform the result.
Definition: gsad_omp.c:10011
char * severity
Severity class.
Definition: gsad_base.h:78
char * get_groups_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all groups, XSL transform the result.
Definition: gsad_omp.c:20182
char * delete_user_confirm_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Show confirmation deleting a user, XSL transform the result.
Definition: gsad_omp.c:24691
char * export_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a target.
Definition: gsad_omp.c:11205
char * edit_agent_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_agent XML, XSL transform the result.
Definition: gsad_omp.c:7278
char * get_asset_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get asset, XSL transform the result.
Definition: gsad_omp.c:26975
void free_find_by_value(find_by_value_t *find)
Definition: gsad_omp.c:403
#define GSAD_MESSAGE_INVALID_PARAM(op)
Answer for invalid input.
Definition: gsad_base.h:53
char * get_configs_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all scan configs, XSL transform the result.
Definition: gsad_omp.c:11564
char * new_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new alert.
Definition: gsad_omp.c:7993
char * create_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a scanner, get all scanners, XSL transform the result.
Definition: gsad_omp.c:16888
char * test_alert_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Test an alert, get all alerts XSL transform the result.
Definition: gsad_omp.c:9112
#define params_iterator_init
Definition: gsad_base.h:189
char * current_page
Current page URL, for refresh.
Definition: gsad_base.h:75
char * move_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Reassign a task to a new OMP slave.
Definition: gsad_omp.c:5091
char * get_schedules_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all schedules, XSL transform the result.
Definition: gsad_omp.c:17343
char * delete_resource(openvas_connection_t *connection, const char *type, credentials_t *credentials, params_t *params, int ultimate, const char *get, cmd_response_data_t *response_data)
Delete a resource, get all resources, XSL transform the result.
Definition: gsad_omp.c:3011
char * get_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a note, XSL transform the result.
Definition: gsad_omp.c:15251
char * new_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new task.
Definition: gsad_omp.c:3722
char * language
Accept-Language browser header.
Definition: gsad_base.h:77
char * new_config_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new scan config page.
Definition: gsad_omp.c:11318
char * save_override_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save override, get next page, XSL transform the result.
Definition: gsad_omp.c:16457
char * save_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save scanner, get next page, XSL transform the result.
Definition: gsad_omp.c:17125
char * edit_resource(openvas_connection_t *connection, const char *type, credentials_t *credentials, params_t *params, const char *extra_get_attribs, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit XML, XSL transform the result.
Definition: gsad_omp.c:2498
char * new_permission(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup new_permission XML, XSL transform the result.
Definition: gsad_omp.c:20675
char * export_filters_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of filters.
Definition: gsad_omp.c:24074
#define AUTH_CONF_SETTING(key, value)
Generate AUTH_CONF_SETTING element for save_auth_omp.
Definition: gsad_omp.c:25851
char * create_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a note, get report, XSL transform the result.
Definition: gsad_omp.c:15465
char * clone_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Clone a resource, XSL transform the result.
Definition: gsad_omp.c:9629
int manager_connect(credentials_t *credentials, openvas_connection_t *connection, cmd_response_data_t *response_data)
Connect to OpenVAS Manager daemon.
Definition: gsad_omp.c:27835
int openvas_connection_open(openvas_connection_t *connection, const gchar *address, int port)
Connect to an address.
Definition: gsad_omp.c:27580
char * delete_trash_note_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a note, get all notes, XSL transform the result.
Definition: gsad_omp.c:15644
int valid
Definition: gsad_base.h:152
char * edit_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_target XML, XSL transform the result.
Definition: gsad_omp.c:10849
params_t * params
Request parameters.
Definition: gsad_base.h:84
char * ctime_r_strip_newline(time_t *time, char *string)
Return string from ctime_r with newline replaces with terminator.
Definition: gsad_base.c:241
char * export_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a role.
Definition: gsad_omp.c:23400
#define params_iterator_t
Definition: gsad_base.h:187
Headers for GSA&#39;s OMP communication module.
char * export_result_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a result.
Definition: gsad_omp.c:14915
void buffer_languages_xml(GString *buffer)
Write the list of installed languages to a buffer as XML.
Definition: xslt_i18n.c:718
char * create_user_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a user, get all users, XSL transform the result.
Definition: gsad_omp.c:24882
char * delete_permission_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a permission, get all permissions, XSL transform the result.
Definition: gsad_omp.c:20655
char * edit_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_schedule XML, XSL transform the result.
Definition: gsad_omp.c:24221
int command_enabled(credentials_t *credentials, const gchar *name)
Init the GSA OMP library.
Definition: gsad_omp.c:305
char * delete_trash_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a note, get all notes, XSL transform the result.
Definition: gsad_omp.c:10260
char * edit_group(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_group XML, XSL transform the result.
Definition: gsad_omp.c:20384
char * edit_permission(openvas_connection_t *connection, credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Setup edit_permission XML, XSL transform the result.
Definition: gsad_omp.c:22026
char * create_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a tag, get report, XSL transform the result.
Definition: gsad_omp.c:10143
char * get_agents_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all agents, XSL transform the result.
Definition: gsad_omp.c:7437
char * verify_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Verify scanner, get scanners, XSL transform the result.
Definition: gsad_omp.c:16792
params_t * values
Definition: gsad_base.h:151
char * save_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save schedule, get next page, XSL transform the result.
Definition: gsad_omp.c:24288
char * password
User&#39;s password.
Definition: gsad_base.h:70
char * upload_report_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload report page.
Definition: gsad_omp.c:3837
char * export_notes_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of notes.
Definition: gsad_omp.c:12972
char * edit_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_role XML, XSL transform the result.
Definition: gsad_omp.c:23305
char * get_nvts_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Requests NVT details, accepting extra commands.
Definition: gsad_omp.c:5428
param_t * params_add(params_t *params, const char *name, const char *value)
Add a param.
Definition: gsad_base.c:808
char * delete_trash_credential_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash credential, get all trash, XSL transform the result.
Definition: gsad_omp.c:9839
char * export_tag_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a tag.
Definition: gsad_omp.c:10482
int params_valid(params_t *params, const char *name)
Get whether a param is valid.
Definition: gsad_base.c:793
char * create_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a target, get all targets, XSL transform the result.
Definition: gsad_omp.c:9407
char * edit_scanner_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_scanner XML, XSL transform the result.
Definition: gsad_omp.c:17108
char * export_overrides_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, enum content_type *content_type, char **content_disposition, gsize *content_length, cmd_response_data_t *response_data)
Export a list of overrides.
Definition: gsad_omp.c:13021
char * get_schedule_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one schedule, XSL transform the result.
Definition: gsad_omp.c:17306
char * create_port_list_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a port list, get all port lists, XSL transform the result.
Definition: gsad_omp.c:22447
char * delete_trash_target_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash target, get all trash, XSL transform the result.
Definition: gsad_omp.c:9896
int connect_unix(const gchar *path)
Connect to OpenVAS Manager daemon.
Definition: gsad_omp.c:27542
char * delete_trash_task_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete a trash task, get all trash, XSL transform the result.
Definition: gsad_omp.c:9915
char * create_role_omp(openvas_connection_t *connection, credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a role, get all roles, XSL transform the result.
Definition: gsad_omp.c:23104