Greenbone Security Assistant  7.0.0
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 (credentials_t *, gchar **, entity_t *, cmd_response_data_t*,
108  const char *);
109 
110 static int
111 ompf (credentials_t *, gchar **, entity_t *, cmd_response_data_t*,
112  const char *, ...);
113 
114 int manager_connect (credentials_t *, openvas_connection_t *, gchar **,
116 
117 static char *edit_role (credentials_t *, params_t *, const char *,
119 
120 static char *edit_task (credentials_t *, params_t *, const char *,
122 
123 static char *get_alert (credentials_t *, params_t *, const char *,
125 
126 static char *get_alerts (credentials_t *, params_t *, const char *,
128 
129 static char *get_agent (credentials_t *, params_t *, const char *,
131 
132 static char *get_agents (credentials_t *, params_t *, const char *,
134 
135 static char *get_asset (credentials_t *, params_t *, const char *,
137 
138 static char *get_assets (credentials_t *, params_t *, const char *,
140 
141 static char *get_assets_chart (credentials_t *, params_t *, const char *,
143 
144 
145 static char *get_task (credentials_t *, params_t *, const char *,
147 
148 static char *get_tasks (credentials_t *, params_t *, const char *,
150 
151 static char *get_tasks_chart (credentials_t *, params_t *, const char *,
153 
154 static char *get_trash (credentials_t *, params_t *, const char *,
156 
157 static char *get_config_family (credentials_t *, params_t *, int,
159 
160 static char *get_config (credentials_t *, params_t *, const char *, int,
162 
163 static char *get_configs (credentials_t *, params_t *, const char *,
165 
166 static char *get_filter (credentials_t *, params_t *, const char *,
168 
169 static char *get_filters (credentials_t *, params_t *, const char *,
171 
172 static char *get_group (credentials_t *, params_t *, const char *,
174 
175 static char *get_groups (credentials_t *, params_t *, const char *,
177 
178 static char *get_credential (credentials_t *, params_t *, const char *,
180 
181 static char *get_credentials (credentials_t *, params_t *, const char *,
183 
184 static char *get_notes (credentials_t *, params_t *, const char *,
186 
187 static char *get_note (credentials_t *, params_t *, const char *,
189 
190 static char *get_nvts (credentials_t *credentials, params_t *,
191  const char *, const char *, cmd_response_data_t*);
192 
193 static char *get_overrides (credentials_t *, params_t *, const char *,
195 
196 static char *get_override (credentials_t *, params_t *, const char *,
198 
199 static char *get_permission (credentials_t *, params_t *, const char *,
201 
202 static char *get_permissions (credentials_t *, params_t *, const char *,
204 
205 static char *get_port_list (credentials_t *, params_t *, const char *,
207 
208 static char *get_port_lists (credentials_t *, params_t *, const char *,
210 
211 static char *edit_port_list (credentials_t *, params_t *, const char *,
213 
214 static char *get_tag (credentials_t *, params_t *, const char *,
216 
217 static char *get_tags (credentials_t *, params_t *, const char *,
219 
220 static char *get_target (credentials_t *, params_t *, const char *,
222 
223 static char *get_targets (credentials_t *, params_t *, const char *,
225 
226 static char *get_report (credentials_t *, params_t *, const char *, gsize *,
227  gchar **, char **, const char *, int *,
229 
230 static char *get_report_format (credentials_t *, params_t *, const char *,
232 
233 static char *get_report_formats (credentials_t *, params_t *, const char *,
235 
236 static char *get_report_section (credentials_t *, params_t *, const char *,
238 
239 static char *get_reports (credentials_t *, params_t *, const char *,
241 
242 static char *get_result_page (credentials_t *, params_t *, const char *,
244 
245 static char *get_results (credentials_t *, params_t *, const char *,
247 
248 static char *get_role (credentials_t *, params_t *, const char *,
250 
251 static char *get_roles (credentials_t *, params_t *, const char *,
253 
254 static char *get_scanner (credentials_t *, params_t *, const char *,
256 
257 static char *get_scanners (credentials_t *, params_t *, const char *,
259 
260 static char *get_schedule (credentials_t *, params_t *, const char *,
262 
263 static char *get_schedules (credentials_t *, params_t *, const char *,
265 
266 static char *get_user (credentials_t *, params_t *, const char *,
268 
269 static char *get_users (credentials_t *, params_t *, const char *,
271 
272 static char *wizard (credentials_t *, params_t *, const char *,
274 
275 static char *wizard_get (credentials_t *, params_t *, const char *,
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 (credentials_t *, cmd_response_data_t *,
287  const char*, const char*, const char*,
288  const char*);
289 
290 static gchar* response_from_entity (credentials_t*, params_t *,
291  entity_t, int, const char*, const char *,
292  const char*, const char*, const char*,
294 
295 /* Helpers. */
296 
303 int
304 command_enabled (credentials_t *credentials, const gchar *name)
305 {
306  /* TODO Hack. Fails if command named in summary of another command. */
307  return strstr (credentials->capabilities, name) ? 1 : 0;
308 }
309 
317 void
318 omp_init (const gchar *manager_address_unix, const gchar *manager_address_tls,
319  int port_manager)
320 {
321  if (manager_address_unix)
322  {
323  manager_address = g_strdup (manager_address_unix);
324  manager_use_tls = 0;
325  }
326  else if (manager_address_tls)
327  {
328  manager_address = g_strdup (manager_address_tls);
329  manager_use_tls = 1;
330  }
331  else
332  {
333  manager_address = g_build_filename (OPENVAS_RUN_DIR,
334  "openvasmd.sock",
335  NULL);
336  manager_use_tls = 0;
337  }
338  manager_port = port_manager;
339 }
340 
346 void
348 {
349  data->http_status_code = MHD_HTTP_OK;
350  data->redirect = NULL;
351 }
352 
358 void
360 {
361  memset (data, 0, sizeof (cmd_response_data_t));
362 }
363 
373 static gboolean
374 print_chart_pref (gchar *id, gchar *value, GString* buffer)
375 {
376  g_string_append_printf (buffer,
377  "<chart_preference id=\"%s\">"
378  "<value>%s</value>"
379  "</chart_preference>",
380  id,
381  value);
382  return 0;
383 }
384 
388 typedef struct
389 {
390  gchar *value;
391  GList *keys;
393 
394 void
396 {
397  find->keys = NULL;
398  find->value = value;
399 }
400 
401 void
403 {
404  g_list_free (find->keys);
405 }
406 
417 static gboolean
418 find_by_value (gchar *key, gchar *value, find_by_value_t *data)
419 {
420  if (strcmp (value, data->value) == 0)
421  {
422  g_debug ("%s found key %s for value %s\n",
423  __FUNCTION__, key, value);
424  data->keys = g_list_append (data->keys, key);
425  }
426  return FALSE;
427 }
428 
437 static int
438 filter_exists (openvas_connection_t *connection, const char *filt_id)
439 {
440  entity_t entity;
441 
442  if (filt_id == NULL
443  || strcmp (filt_id, "0")
444  || strcmp (filt_id, "-2"))
445  return 1;
446 
447  /* check if filter still exists */
448  if (openvas_connection_sendf (connection, "<get_filters filter_id='%s'/>",
449  filt_id))
450  {
451  return -2;
452  }
453 
454  if (read_entity_c (connection, &entity))
455  {
456  return -3;
457  }
458 
459  return omp_success (entity);
460 }
461 
471 static char *
472 xsl_transform_omp (credentials_t * credentials, gchar * xml,
473  cmd_response_data_t *response_data)
474 {
475  time_t now;
476  gchar *res, *name;
477  GString *string;
478  char *html;
479  char ctime_now[200];
480  params_iterator_t iter;
481  param_t *param;
482  const char *refresh_interval, *xml_flag;
483  struct timeval tv;
484 
485  assert (credentials);
486 
487  now = time (NULL);
488  ctime_r_strip_newline (&now, ctime_now);
489 
490  string = g_string_new ("");
491 
492  gettimeofday (&tv, NULL);
493  res = g_markup_printf_escaped ("<envelope>"
494  "<version>%s</version>"
495  "<vendor_version>%s</vendor_version>"
496  "<token>%s</token>"
497  "<caller>%s</caller>"
498  "<current_page>%s</current_page>"
499  "<time>%s</time>"
500  "<timezone>%s</timezone>"
501  "<login>%s</login>"
502  "<role>%s</role>"
503  "<severity>%s</severity>"
504  "<i18n>%s</i18n>"
505  "<charts>%d</charts>"
506  "<guest>%d</guest>"
507  "<client_address>%s</client_address>"
508  "<backend_operation>%.2f</backend_operation>",
509  GSAD_VERSION,
511  credentials->token,
512  credentials->caller ? credentials->caller : "",
513  credentials->current_page
514  ? credentials->current_page
515  : (credentials->caller
516  ? credentials->caller
517  : ""),
518  ctime_now,
519  credentials->timezone
520  ? credentials->timezone : "",
521  credentials->username,
522  credentials->role,
523  credentials->severity,
524  credentials->language,
525  credentials->charts,
526  credentials->guest,
527  credentials->client_address,
528  (double) ((tv.tv_sec
529  - credentials->cmd_start.tv_sec)
530  * 1000000L
531  + tv.tv_usec
532  - credentials->cmd_start.tv_usec)
533  / 1000000.0);
534  g_string_append (string, res);
535  g_free (res);
536 
537  g_string_append (string, "<chart_preferences>");
538  g_tree_foreach (credentials->chart_prefs,
539  (GTraverseFunc)print_chart_pref,
540  string);
541  g_string_append (string, "</chart_preferences>");
542 
543  if (credentials->pw_warning)
544  {
545  gchar *warning_elem;
546  warning_elem = g_markup_printf_escaped ("<password_warning>"
547  "%s"
548  "</password_warning>",
549  credentials->pw_warning);
550  g_string_append (string, warning_elem);
551  g_free (warning_elem);
552  }
553 
554  refresh_interval = params_value (credentials->params, "refresh_interval");
555  if ((refresh_interval == NULL) || (strcmp (refresh_interval, "") == 0))
556  g_string_append_printf (string,
557  "<autorefresh interval=\"%s\"/>",
558  credentials->autorefresh);
559  else
560  {
561  int ret;
562  gchar *interval_64, *response;
563  entity_t entity;
564 
565  interval_64 = (refresh_interval
566  ? g_base64_encode ((guchar*) refresh_interval,
567  strlen (refresh_interval))
568  : g_strdup (""));
569  ret = ompf (credentials, &response, &entity, response_data,
570  "<modify_setting"
571  " setting_id=\"578a1c14-e2dc-45ef-a591-89d31391d007\">"
572  "<value>%s</value>"
573  "</modify_setting>",
574  interval_64);
575  g_free (interval_64);
576  switch (ret)
577  {
578  case 0:
579  case -1:
580  break;
581  case 1:
582  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
583  return gsad_message (credentials,
584  "Internal error", __FUNCTION__, __LINE__,
585  "An internal error occurred while modifying the"
586  " autorefresh setting for the settings. "
587  "Diagnostics: Failure to send command to"
588  " manager daemon.",
589  "/omp?cmd=get_my_settings", response_data);
590  case 2:
591  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
592  return gsad_message (credentials,
593  "Internal error", __FUNCTION__, __LINE__,
594  "An internal error occurred while modifying the"
595  " autorefresh setting for the settings. "
596  "Diagnostics: Failure to receive response from"
597  " manager daemon.",
598  "/omp?cmd=get_my_settings", response_data);
599  default:
600  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
601  return gsad_message (credentials,
602  "Internal error", __FUNCTION__, __LINE__,
603  "An internal error occurred while modifying the"
604  " autorefresh setting for the settings. "
605  "Diagnostics: Internal Error.",
606  "/omp?cmd=get_my_settings", response_data);
607  }
608 
609  free_entity (entity);
610  g_free (credentials->autorefresh);
611  credentials->autorefresh = g_strdup (refresh_interval);
612  user_set_autorefresh (credentials->token, refresh_interval);
613 
614  g_string_append_printf (string,
615  "<autorefresh interval=\"%s\"/>",
616  credentials->autorefresh);
617  }
618 
619  g_string_append (string, "<params>");
620  params_iterator_init (&iter, credentials->params);
621  while (params_iterator_next (&iter, &name, &param))
622  {
623  if (name && strcmp (name, ""))
624  {
625  if ((name[strlen (name) - 1] == ':') && param->values)
626  {
627  gchar *child_name;
628  params_iterator_t children_iter;
629  param_t *child_param;
630 
631  params_iterator_init (&children_iter, param->values);
632  while (params_iterator_next (&children_iter, &child_name,
633  &child_param))
634  if (child_param->value
635  && child_param->valid
636  && child_param->valid_utf8)
637  xml_string_append (string,
638  "<_param>"
639  "<name>%s%s</name><value>%s</value>"
640  "</_param>",
641  name, child_name, child_param->value);
642  }
643 
644  if (param->value && param->valid && param->valid_utf8
645  && strcmp (name, "xml_file") && strcmp (name, "installer"))
646  xml_string_append (string, "<%s>%s</%s>", name, param->value, name);
647  }
648  else
649  g_warning ("%s: Param without name found", __FUNCTION__);
650  }
651  g_string_append (string, "</params>");
652 
653  g_string_append_printf (string,
654  "<capabilities>%s</capabilities>"
655  "%s"
656  "</envelope>",
657  credentials->capabilities,
658  xml);
659  g_free (xml);
660 
661  xml_flag = params_value (credentials->params, "xml");
662  if (xml_flag && strcmp (xml_flag, "0"))
663  return g_string_free (string, FALSE);
664 
665  html = xsl_transform (string->str, response_data);
666  g_string_free (string, TRUE);
667  if (html == NULL)
668  {
669  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
670  res = g_strdup_printf ("<gsad_response>"
671  "<title>Internal Error</title>"
672  "<message>"
673  "An internal server error has occurred during XSL"
674  " transformation."
675  "</message>"
676  "<backurl>/omp?cmd=get_tasks</backurl>"
677  "</gsad_response>");
678  html = xsl_transform (res, response_data);
679  if (html == NULL)
680  html = g_strdup ("<html>"
681  "<body>"
682  "An internal server error has occurred during XSL"
683  " transformation."
684  "</body>"
685  "</html>");
686  g_free (res);
687  }
688  return html;
689 }
690 
699 static int
700 member (params_t *params, const char *string)
701 {
702  params_iterator_t iter;
703  param_t *param;
704  char *name;
705 
706  params_iterator_init (&iter, params);
707  while (params_iterator_next (&iter, &name, &param))
708  if (strcmp (name, string) == 0) return 1;
709  return 0;
710 }
711 
720 int
721 member1 (params_t *params, const char *string)
722 {
723  params_iterator_t iter;
724  param_t *param;
725  char *name;
726 
727  params_iterator_init (&iter, params);
728  while (params_iterator_next (&iter, &name, &param))
729  if (param->value_size
730  && param->value[0] == '1'
731  && strcmp (name, string) == 0)
732  return 1;
733  return 0;
734 }
735 
749 static char *
750 check_modify_config (credentials_t *credentials,
751  openvas_connection_t *connection, params_t *params,
752  const char* next, const char* fail_next, int* success,
753  cmd_response_data_t *response_data)
754 {
755  entity_t entity;
756  gchar *response;
757  const char *no_redirect, *status_text;
758 
759  no_redirect = params_value (params, "no_redirect");
760 
761  if (success)
762  *success = 0;
763 
766  /* Read the response. */
767 
768  if (read_entity_c (connection, &entity))
769  {
770  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
771  return gsad_message (credentials,
772  "Internal error", __FUNCTION__, __LINE__,
773  "An internal error occurred while saving a config. "
774  "It is unclear whether the entire config has been saved. "
775  "Diagnostics: Failure to read command to manager daemon.",
776  "/omp?cmd=get_configs", response_data);
777  }
778 
779  /* Check the response. */
780 
781  status_text = entity_attribute (entity, "status_text");
782  if (status_text == NULL)
783  {
784  free_entity (entity);
785  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
786  return gsad_message (credentials,
787  "Internal error", __FUNCTION__, __LINE__,
788  "An internal error occurred while saving a config. "
789  "It is unclear whether the entire config has been saved. "
790  "Diagnostics: Failure to parse status_text from response.",
791  "/omp?cmd=get_configs", response_data);
792  }
793 
794  if (strcmp (status_text, "Config is in use") == 0)
795  {
796  const char* message = "The config is now in use by a task,"
797  " so only name and comment can be modified.";
798  gchar *next_url = next_page_url (credentials, params,
799  NULL, fail_next,
800  "Save Config",
801  entity_attribute (entity, "status"),
802  message);
803 
804  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
805  response
806  = action_result_page (credentials, response_data, "Save Config",
807  entity_attribute (entity, "status"),
808  message, next_url);
809 
810  g_free (next_url);
811  free_entity (entity);
812  return response;
813  }
814  else if (strcmp (status_text, "MODIFY_CONFIG name must be unique") == 0)
815  {
816  const char* message = "A config with the given name exists already.";
817  gchar *next_url = next_page_url (credentials, params,
818  NULL, fail_next,
819  "Save Config",
820  entity_attribute (entity, "status"),
821  message);
822 
823  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
824  response
825  = action_result_page (credentials, response_data, "Save Config",
826  entity_attribute (entity, "status"),
827  message, next_url);
828 
829  g_free (next_url);
830  free_entity (entity);
831  return response;
832  }
833  else if (success && omp_success (entity))
834  {
835  *success = 1;
836  }
837 
838  response
839  = response_from_entity (credentials, params, entity,
840  (no_redirect && strcmp (no_redirect, "0")),
841  NULL, next,
842  NULL, fail_next,
843  "Save Config", response_data);
844  free_entity (entity);
845 
846  return response;
847 }
848 
856 static int
857 omp_success (entity_t entity)
858 {
859  const char *status;
860 
861  if (entity == NULL)
862  return 0;
863 
864  status = entity_attribute (entity, "status");
865  if ((status == NULL)
866  || (strlen (status) == 0))
867  return -1;
868 
869  return status[0] == '2';
870 }
871 
878 void
880  cmd_response_data_t *response_data)
881 {
882  if (entity == NULL)
883  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
884  else if (strcmp (entity_attribute (entity, "status_text"),
885  "Permission denied")
886  == 0)
887  response_data->http_status_code = MHD_HTTP_FORBIDDEN;
888  else if (strcmp (entity_attribute (entity, "status"), "404") == 0)
889  response_data->http_status_code = MHD_HTTP_NOT_FOUND;
890  else
891  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
892 }
893 
906 static int
907 omp (credentials_t *credentials, gchar **response, entity_t *entity_return,
908  cmd_response_data_t *response_data, const char *command)
909 {
910  openvas_connection_t connection;
911  int ret;
912  entity_t entity;
913 
914  if (entity_return)
915  *entity_return = NULL;
916 
917  switch (manager_connect (credentials, &connection, response, response_data))
918  {
919  case 0:
920  break;
921  case -1:
922  return -1;
923  default:
924  if (response_data)
925  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
926  if (response)
927  *response = gsad_message (credentials,
928  "Internal error", __FUNCTION__, __LINE__,
929  "An internal error occurred. "
930  "Diagnostics: Failure to connect to manager daemon.",
931  "/omp?cmd=get_tasks", response_data);
932  return -1;
933  }
934 
935  ret = openvas_connection_sendf (&connection, "%s", command);
936  if (ret == -1)
937  {
938  openvas_connection_close (&connection);
939  return 1;
940  }
941 
942  entity = NULL;
943  if (read_entity_and_text_c (&connection, &entity, response))
944  {
945  openvas_connection_close (&connection);
946  return 2;
947  }
948  if (entity_return)
949  *entity_return = entity;
950  else
951  free_entity (entity);
952  openvas_connection_close (&connection);
953  return 0;
954 }
955 
969 static int
970 simple_ompf (const gchar *message_operation, credentials_t *credentials,
971  gchar **response, cmd_response_data_t *response_data,
972  const char *format, ...)
973 {
974  int ret;
975  gchar *command;
976  va_list args;
977  entity_t entity;
978 
979  va_start (args, format);
980  command = g_markup_vprintf_escaped (format, args);
981  va_end (args);
982 
983  ret = omp (credentials, response, &entity, response_data, command);
984  g_free (command);
985  switch (ret)
986  {
987  case 0:
988  break;
989  case -1:
990  /* 'omp' set response. */
991  return 4;
992  case 1:
993  if (response_data)
994  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
995  if (response)
996  {
997  gchar *message;
998  message = g_strdup_printf
999  ("An internal error occurred while %s. "
1000  " The operation was not started."
1001  " Diagnostics: Failure to send command to manager"
1002  " daemon.",
1003  message_operation
1004  ? message_operation
1005  : "performing an operation");
1006  *response = gsad_message (credentials, "Internal error",
1007  __FUNCTION__, __LINE__,
1008  message, "/omp?cmd=get_tasks",
1009  response_data);
1010  g_free (message);
1011  }
1012  return 1;
1013  case 2:
1014  if (response_data)
1015  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1016  if (response)
1017  {
1018  gchar *message;
1019  message = g_strdup_printf
1020  ("An internal error occurred while %s."
1021  " It is unclear whether the operation succeeded."
1022  " Diagnostics: Failure to receive response from manager"
1023  " daemon.",
1024  message_operation
1025  ? message_operation
1026  : "performing an operation");
1027  *response = gsad_message (credentials, "Internal error",
1028  __FUNCTION__, __LINE__,
1029  message, "/omp?cmd=get_tasks",
1030  response_data);
1031  g_free (message);
1032  }
1033  return 2;
1034  default:
1035  if (response_data)
1036  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1037  if (response)
1038  {
1039  gchar *message;
1040  message = g_strdup_printf
1041  ("An internal error occurred while %s."
1042  " It is unclear whether the operation succeeded."
1043  " Diagnostics: Internal Error.",
1044  message_operation
1045  ? message_operation
1046  : "performing an operation");
1047  *response = gsad_message (credentials, "Internal error",
1048  __FUNCTION__, __LINE__,
1049  message, "/omp?cmd=get_tasks",
1050  response_data);
1051  g_free (message);
1052  }
1053  return -1;
1054  }
1055 
1056  switch (omp_success (entity))
1057  {
1058  case 0:
1059  set_http_status_from_entity (entity, response_data);
1060  ret = 3;
1061  break;
1062  case 1:
1063  ret = 0;
1064  break;
1065  default:
1066  ret = -1;
1067  break;
1068  }
1069  free_entity (entity);
1070  return ret;
1071 }
1072 
1086 static int
1087 ompf (credentials_t *credentials, gchar **response, entity_t *entity_return,
1088  cmd_response_data_t *response_data, const char *format, ...)
1089 {
1090  int ret;
1091  gchar *command;
1092  va_list args;
1093 
1094  va_start (args, format);
1095  command = g_markup_vprintf_escaped (format, args);
1096  va_end (args);
1097 
1098  ret = omp (credentials, response, entity_return, response_data, command);
1099  g_free (command);
1100  return ret;
1101 }
1102 
1113 static int
1114 setting_get_value (openvas_connection_t *connection, const char *setting_id,
1115  gchar **value, cmd_response_data_t *response_data)
1116 {
1117  int ret;
1118  entity_t entity;
1119  const char *status;
1120  gchar *response;
1121 
1122  *value = NULL;
1123 
1124  ret = openvas_connection_sendf
1125  (connection,
1126  "<get_settings setting_id=\"%s\"/>",
1127  setting_id);
1128  if (ret)
1129  return 1;
1130 
1131  entity = NULL;
1132  if (read_entity_and_text_c (connection, &entity, &response))
1133  return 2;
1134 
1135  status = entity_attribute (entity, "status");
1136  if (status == NULL
1137  || strlen (status) == 0)
1138  {
1139  g_free (response);
1140  free_entity (entity);
1141  return -1;
1142  }
1143 
1144  if (status[0] == '2')
1145  {
1146  entity_t setting;
1147  setting = entity_child (entity, "setting");
1148  if (setting == NULL)
1149  {
1150  free_entity (entity);
1151  g_free (response);
1152  return 0;
1153  }
1154  setting = entity_child (setting, "value");
1155  if (setting == NULL)
1156  {
1157  free_entity (entity);
1158  g_free (response);
1159  return -1;
1160  }
1161  *value = g_strdup (entity_text (setting));
1162  g_free (response);
1163  free_entity (entity);
1164  }
1165  else
1166  {
1167  if (response_data)
1168  set_http_status_from_entity (entity, response_data);
1169  free_entity (entity);
1170  g_free (response);
1171  return -1;
1172  }
1173 
1174  return 0;
1175 }
1176 
1184 #define CHECK_PARAM(name, op_name, ret_func) \
1185  if (name == NULL) \
1186  { \
1187  gchar *msg, *ret_html; \
1188  msg = g_strdup_printf (GSAD_MESSAGE_INVALID, \
1189  "Given " G_STRINGIFY (name) " was invalid", \
1190  op_name); \
1191  ret_html = ret_func (credentials, params, msg, response_data); \
1192  g_free (msg); \
1193  response_data->http_status_code = MHD_HTTP_BAD_REQUEST; \
1194  return ret_html; \
1195  }
1196 
1204 #define CHECK_PARAM_INVALID(name, op_name, next_cmd) \
1205  if (name == NULL) \
1206  { \
1207  return message_invalid (credentials, params, response_data, \
1208  "Given " G_STRINGIFY (name) " was invalid", \
1209  G_STRINGIFY (MHD_HTTP_BAD_REQUEST), \
1210  op_name, next_cmd); \
1211  }
1212 
1221 static char *
1222 page_url (credentials_t *credentials, const gchar *cmd)
1223 {
1224  GString *url;
1225 
1226  assert (cmd);
1227 
1228  url = g_string_new ("");
1229 
1230  if (credentials->caller && strlen (credentials->caller))
1231  {
1232  gchar **split_question, *page, *params;
1233 
1234  split_question = g_strsplit (credentials->caller, "?", 2);
1235 
1236  page = *split_question;
1237 
1238  if (page)
1239  {
1240  g_string_append_printf (url, "%s?cmd=%s", page, cmd);
1241  params = split_question[1];
1242  }
1243  else
1244  {
1245  g_string_append_printf (url, "cmd=%s", cmd);
1246  params = credentials->caller;
1247  }
1248  if (params)
1249  {
1250  gchar **split_amp, **point;
1251 
1252  point = split_amp = g_strsplit (params, "&", 0);
1253  while (*point)
1254  {
1255  gchar *param;
1256 
1257  param = *point;
1258 
1259  g_strstrip (param);
1260 
1261  if (strstr (param, "cmd=") == param)
1262  {
1263  point++;
1264  continue;
1265  }
1266 
1267  g_string_append_printf (url, "&%s", param);
1268 
1269  point++;
1270  }
1271  g_strfreev (split_amp);
1272  }
1273  g_strfreev (split_question);
1274  }
1275  else
1276  g_string_append_printf (url, "?cmd=%s", cmd);
1277 
1278  return g_string_free (url, FALSE);
1279 }
1280 
1288 static gchar*
1289 capitalize (const char* input)
1290 {
1291  gchar *output;
1292  if (input == NULL)
1293  return NULL;
1294  else
1295  {
1296  int first_letter = 1;
1297  int pos = 0;
1298  output = g_strdup (input);
1299 
1300  while (output[pos])
1301  {
1302  if (g_ascii_isalpha (output[pos]) && first_letter)
1303  {
1304  output[pos] = g_ascii_toupper (output[pos]);
1305  first_letter = 0;
1306  }
1307  else if (output[pos] == '_')
1308  {
1309  output[pos] = ' ';
1310  first_letter = 1;
1311  }
1312  pos++;
1313  }
1314  return output;
1315  }
1316 }
1317 
1318 
1319 /* Generic page handlers. */
1320 
1334 static gchar *
1335 next_page_url (credentials_t *credentials, params_t *params,
1336  const char* override_next, const char *default_next,
1337  const char* prev_action, const char* action_status,
1338  const char* action_message)
1339 {
1340  GString *url;
1341  const char *next_cmd, *xml_param, *next_xml_param;
1342  params_iterator_t iter;
1343  gchar *param_name;
1344  param_t *current_param;
1345  int error;
1346  if (action_status && strcmp (action_status, "") && action_status[0] != '2')
1347  error = 1;
1348  else
1349  error = 0;
1350 
1351  url = g_string_new ("/omp?cmd=");
1352 
1353  if (override_next)
1354  next_cmd = override_next;
1355  else if (error && params_valid (params, "next_error"))
1356  next_cmd = params_value (params, "next_error");
1357  else if (!error && params_valid (params, "next"))
1358  next_cmd = params_value (params, "next");
1359  else if (default_next)
1360  next_cmd = default_next;
1361  else
1362  next_cmd = "get_tasks";
1363 
1364  xml_param = params_value (params, "xml");
1365  next_xml_param = params_value (params, "next_xml");
1366 
1367  g_string_append (url, next_cmd);
1368 
1369  params_iterator_init (&iter, params);
1370  while (params_iterator_next (&iter, &param_name, &current_param))
1371  {
1372  if (strcmp (param_name, "asset_type") == 0
1373  || strcmp (param_name, "filter") == 0
1374  || strcmp (param_name, "filt_id") == 0
1375  || (strstr (param_name, "_id")
1376  == param_name + strlen (param_name) - strlen ("_id"))
1377  || (strcmp (param_name, "name") == 0
1378  && strcasecmp (prev_action, "Run Wizard") == 0)
1379  || (strcmp (param_name, "get_name") == 0
1380  && strcasecmp (next_cmd, "wizard_get") == 0))
1381  {
1382  g_string_append_printf (url, "&%s=%s",
1383  param_name,
1384  current_param->value
1385  ? current_param->value
1386  : "");
1387  }
1388  }
1389 
1390  if (next_xml_param)
1391  {
1392  g_string_append_printf (url, "&xml=%s", next_xml_param);
1393  }
1394  else if (xml_param)
1395  {
1396  g_string_append_printf (url, "&xml=%s", xml_param);
1397  }
1398 
1399  if (action_status)
1400  {
1401  gchar *escaped = g_uri_escape_string (action_status, NULL, FALSE);
1402  g_string_append_printf (url, "&action_status=%s", escaped);
1403  g_free (escaped);
1404  }
1405 
1406  if (action_message)
1407  {
1408  gchar *escaped = g_uri_escape_string (action_message, NULL, FALSE);
1409  g_string_append_printf (url, "&action_message=%s", escaped);
1410  g_free (escaped);
1411  }
1412 
1413  if (prev_action)
1414  {
1415  gchar *escaped = g_uri_escape_string (prev_action, NULL, FALSE);
1416  g_string_append_printf (url, "&prev_action=%s", escaped);
1417  g_free (escaped);
1418  }
1419 
1420 
1421  g_string_append_printf (url, "&token=%s", credentials->token);
1422 
1423  return g_string_free (url, FALSE);
1424 }
1425 
1438 static gchar *
1439 action_result_page (credentials_t *credentials,
1440  cmd_response_data_t *response_data,
1441  const char* action, const char* status,
1442  const char* message, const char* next_url)
1443 {
1444  gchar *xml;
1445  xml = g_markup_printf_escaped ("<action_result>"
1446  "<action>%s</action>"
1447  "<status>%s</status>"
1448  "<message>%s</message>"
1449  "<next>%s</next>"
1450  "</action_result>",
1451  action ? action : "",
1452  status ? status : "",
1453  message ? message : "",
1454  next_url ? next_url : "");
1455  return xsl_transform_omp (credentials, xml, response_data);
1456 }
1457 
1471 gchar *
1472 message_invalid (credentials_t *credentials, params_t *params,
1473  cmd_response_data_t *response_data, const char *message,
1474  const char *status, const char *op_name, const char *next_cmd)
1475 {
1476  gchar *ret;
1477  gchar *next_url;
1478 
1479  next_url = next_page_url (credentials, params, next_cmd, NULL, op_name,
1480  status, message);
1481  ret = action_result_page (credentials, response_data, op_name,
1482  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
1483  message,
1484  next_url);
1485  g_free (next_url);
1486  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
1487  return ret;
1488 }
1489 
1493 static gchar*
1494 response_from_entity (credentials_t* credentials, params_t *params,
1495  entity_t entity, int no_redirect,
1496  const char* override_next, const char *default_next,
1497  const char* override_fail_next,
1498  const char* default_fail_next,
1499  const char* action, cmd_response_data_t *response_data)
1500 {
1501  gchar *res, *next_url;
1502  int success;
1503  success = omp_success (entity);
1504 
1505  if (success)
1506  {
1507  next_url = next_page_url (credentials, params,
1508  override_next, default_next,
1509  action,
1510  entity_attribute (entity, "status"),
1511  entity_attribute (entity, "status_text"));
1512  }
1513  else
1514  {
1515  set_http_status_from_entity (entity, response_data);
1516  next_url = next_page_url (credentials, params,
1517  override_fail_next, default_fail_next,
1518  action,
1519  entity_attribute (entity, "status"),
1520  entity_attribute (entity, "status_text"));
1521  }
1522 
1523  if (no_redirect || success == 0)
1524  {
1525  res = action_result_page (credentials, response_data, action,
1526  entity_attribute (entity, "status"),
1527  entity_attribute (entity, "status_text"),
1528  next_url);
1529  g_free (next_url);
1530  }
1531  else
1532  {
1533  res = NULL;
1534  response_data->redirect = next_url;
1535  }
1536  return res;
1537 }
1538 
1550 static char *
1551 generate_page (credentials_t *credentials, params_t *params, gchar *response,
1552  const gchar *next, cmd_response_data_t* response_data)
1553 {
1554  credentials->current_page = page_url (credentials, next);
1555  if (g_utf8_validate (credentials->current_page, -1, NULL) == FALSE)
1556  {
1557  g_free (credentials->current_page);
1558  g_warning ("%s - current_page is not valid UTF-8", __FUNCTION__);
1559  credentials->current_page = NULL;
1560  }
1561 
1562  if (strcmp (next, "edit_role") == 0)
1563  return edit_role (credentials, params, response, response_data);
1564 
1565  if (strcmp (next, "edit_task") == 0)
1566  return edit_task (credentials, params, response, response_data);
1567 
1568  if (strcmp (next, "get_alerts") == 0)
1569  return get_alerts (credentials, params, response, response_data);
1570 
1571  if (strcmp (next, "get_alert") == 0)
1572  return get_alert (credentials, params, response, response_data);
1573 
1574  if (strcmp (next, "edit_port_list") == 0)
1575  return edit_port_list (credentials, params, response, response_data);
1576 
1577  if (strcmp (next, "get_agents") == 0)
1578  return get_agents (credentials, params, response, response_data);
1579 
1580  if (strcmp (next, "get_agent") == 0)
1581  return get_agent (credentials, params, response, response_data);
1582 
1583  if (strcmp (next, "get_asset") == 0)
1584  return get_asset (credentials, params, response, response_data);
1585 
1586  if (strcmp (next, "get_assets") == 0)
1587  return get_assets (credentials, params, response, response_data);
1588 
1589  if (strcmp (next, "get_assets_chart") == 0)
1590  return get_assets_chart (credentials, params, response, response_data);
1591 
1592  if (strcmp (next, "get_config") == 0)
1593  return get_config (credentials, params, response, 0, response_data);
1594 
1595  if (strcmp (next, "get_configs") == 0)
1596  return get_configs (credentials, params, response, response_data);
1597 
1598  if (strcmp (next, "get_filter") == 0)
1599  return get_filter (credentials, params, response, response_data);
1600 
1601  if (strcmp (next, "get_filters") == 0)
1602  return get_filters (credentials, params, response, response_data);
1603 
1604  if (strcmp (next, "get_group") == 0)
1605  return get_group (credentials, params, response, response_data);
1606 
1607  if (strcmp (next, "get_groups") == 0)
1608  return get_groups (credentials, params, response, response_data);
1609 
1610  if (strcmp (next, "get_credential") == 0)
1611  return get_credential (credentials, params, response, response_data);
1612 
1613  if (strcmp (next, "get_credentials") == 0)
1614  return get_credentials (credentials, params, response, response_data);
1615 
1616  if (strcmp (next, "get_note") == 0)
1617  return get_note (credentials, params, response, response_data);
1618 
1619  if (strcmp (next, "get_notes") == 0)
1620  return get_notes (credentials, params, response, response_data);
1621 
1622  if (strcmp (next, "get_nvts") == 0)
1623  return get_nvts (credentials, params, NULL, response, response_data);
1624 
1625  if (strcmp (next, "get_override") == 0)
1626  return get_override (credentials, params, response, response_data);
1627 
1628  if (strcmp (next, "get_overrides") == 0)
1629  return get_overrides (credentials, params, response, response_data);
1630 
1631  if (strcmp (next, "get_permission") == 0)
1632  return get_permission (credentials, params, response, response_data);
1633 
1634  if (strcmp (next, "get_permissions") == 0)
1635  return get_permissions (credentials, params, response, response_data);
1636 
1637  if (strcmp (next, "get_port_list") == 0)
1638  return get_port_list (credentials, params, response, response_data);
1639 
1640  if (strcmp (next, "get_port_lists") == 0)
1641  return get_port_lists (credentials, params, response, response_data);
1642 
1643  if (strcmp (next, "get_tag") == 0)
1644  return get_tag (credentials, params, response, response_data);
1645 
1646  if (strcmp (next, "get_tags") == 0)
1647  return get_tags (credentials, params, response, response_data);
1648 
1649  if (strcmp (next, "get_target") == 0)
1650  return get_target (credentials, params, response, response_data);
1651 
1652  if (strcmp (next, "get_targets") == 0)
1653  return get_targets (credentials, params, response, response_data);
1654 
1655  if (strcmp (next, "get_task") == 0)
1656  return get_task (credentials, params, response, response_data);
1657 
1658  if (strcmp (next, "get_tasks") == 0)
1659  return get_tasks (credentials, params, response, response_data);
1660 
1661  if (strcmp (next, "get_tasks_chart") == 0)
1662  return get_tasks_chart (credentials, params, response, response_data);
1663 
1664  if (strcmp (next, "get_report") == 0)
1665  {
1666  char *result;
1667  int error = 0;
1668 
1669  result = get_report (credentials, params, NULL, NULL, NULL,
1670  NULL, response, &error, response_data);
1671 
1672  return error ? result : xsl_transform_omp (credentials, result,
1673  response_data);
1674  }
1675 
1676  if (strcmp (next, "get_report_format") == 0)
1677  return get_report_format (credentials, params, response, response_data);
1678 
1679  if (strcmp (next, "get_report_formats") == 0)
1680  return get_report_formats (credentials, params, response, response_data);
1681 
1682  if (strcmp (next, "get_report_section") == 0)
1683  return get_report_section (credentials, params, response, response_data);
1684 
1685  if (strcmp (next, "get_reports") == 0)
1686  return get_reports (credentials, params, response, response_data);
1687 
1688  if (strcmp (next, "get_results") == 0)
1689  return get_results (credentials, params, response, response_data);
1690 
1691  if (strcmp (next, "get_result") == 0)
1692  return get_result_page (credentials, params, response, response_data);
1693 
1694  if (strcmp (next, "get_role") == 0)
1695  return get_role (credentials, params, response, response_data);
1696 
1697  if (strcmp (next, "get_roles") == 0)
1698  return get_roles (credentials, params, response, response_data);
1699 
1700  if (strcmp (next, "get_scanner") == 0)
1701  return get_scanner (credentials, params, response, response_data);
1702 
1703  if (strcmp (next, "get_scanners") == 0)
1704  return get_scanners (credentials, params, response, response_data);
1705 
1706  if (strcmp (next, "get_schedule") == 0)
1707  return get_schedule (credentials, params, response, response_data);
1708 
1709  if (strcmp (next, "get_schedules") == 0)
1710  return get_schedules (credentials, params, response, response_data);
1711 
1712  if (strcmp (next, "get_user") == 0)
1713  return get_user (credentials, params, response, response_data);
1714 
1715  if (strcmp (next, "get_users") == 0)
1716  return get_users (credentials, params, response, response_data);
1717 
1718  if (strcmp (next, "get_info") == 0)
1719  return get_info (credentials, params, response, response_data);
1720 
1721  if (strcmp (next, "wizard") == 0)
1722  return wizard (credentials, params, response, response_data);
1723 
1724  if (strcmp (next, "wizard_get") == 0)
1725  return wizard_get (credentials, params, response, response_data);
1726 
1727  return NULL;
1728 }
1729 
1740 static char *
1741 next_page (credentials_t *credentials, params_t *params, gchar *response,
1742  cmd_response_data_t* response_data)
1743 {
1744  const char *next;
1745 
1746  next = params_value (params, "next");
1747  if (next == NULL)
1748  return NULL;
1749 
1750  return generate_page (credentials, params, response, next, response_data);
1751 }
1752 
1765 char *
1766 get_one (const char *type, credentials_t * credentials, params_t *params,
1767  const char *extra_xml, const char *extra_attribs,
1768  cmd_response_data_t* response_data)
1769 {
1770  GString *xml;
1771  openvas_connection_t connection;
1772  int ret;
1773  gchar *html, *end, *id_name;
1774  const char *id, *sort_field, *sort_order, *filter, *first, *max;
1775 
1776  id_name = g_strdup_printf ("%s_id", type);
1777  id = params_value (params, id_name);
1778  g_free (id_name);
1779  sort_field = params_value (params, "sort_field");
1780  sort_order = params_value (params, "sort_order");
1781 
1782  if (id == NULL)
1783  {
1784  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1785  return gsad_message (credentials,
1786  "Internal error", __FUNCTION__, __LINE__,
1787  "An internal error occurred while getting a resource. "
1788  "Diagnostics: missing ID.",
1789  "/omp?cmd=get_tasks", response_data);
1790  }
1791 
1792  switch (manager_connect (credentials, &connection, &html, response_data))
1793  {
1794  case 0:
1795  break;
1796  case -1:
1797  if (html)
1798  return html;
1799  /* Fall through. */
1800  default:
1801  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1802  return gsad_message (credentials,
1803  "Internal error", __FUNCTION__, __LINE__,
1804  "An internal error occurred while getting a resource. "
1805  "The resource is currently not available. "
1806  "Diagnostics: Failure to connect to manager daemon.",
1807  "/omp?cmd=get_tasks", response_data);
1808  }
1809 
1810  xml = g_string_new ("");
1811  g_string_append_printf (xml, "<get_%s>", type);
1812 
1813  if (strcmp (type, "role") == 0
1814  && command_enabled (credentials, "GET_PERMISSIONS")
1815  && params_value (params, "role_id"))
1816  {
1817  gchar *response;
1818  entity_t entity;
1819 
1820  response = NULL;
1821  entity = NULL;
1822  switch (ompf (credentials, &response, &entity, response_data,
1823  "<get_permissions"
1824  " filter=\"rows=-1 subject_type=role and subject_uuid=%s\"/>",
1825  params_value (params, "role_id")))
1826  {
1827  case 0:
1828  case -1:
1829  break;
1830  case 1:
1831  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1832  return gsad_message (credentials,
1833  "Internal error", __FUNCTION__, __LINE__,
1834  "An internal error occurred getting permissions. "
1835  "Diagnostics: Failure to send command to manager daemon.",
1836  "/omp?cmd=get_roles", response_data);
1837  case 2:
1838  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1839  return gsad_message (credentials,
1840  "Internal error", __FUNCTION__, __LINE__,
1841  "An internal error occurred getting permissions. "
1842  "Diagnostics: Failure to receive response from manager daemon.",
1843  "/omp?cmd=get_roles", response_data);
1844  default:
1845  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1846  return gsad_message (credentials,
1847  "Internal error", __FUNCTION__, __LINE__,
1848  "An internal error occurred getting permissins. "
1849  "Diagnostics: Internal Error.",
1850  "/omp?cmd=get_roles", response_data);
1851  }
1852 
1853  g_string_append (xml, response);
1854 
1855  if (!omp_success (entity))
1856  set_http_status_from_entity (entity, response_data);
1857 
1858  free_entity (entity);
1859  g_free (response);
1860  }
1861 
1862  /* Pass through params for get_resources. */
1863  filter = params_value (params, "filter");
1864  first = params_value (params, "first");
1865  max = params_value (params, "max");
1866  end = g_markup_printf_escaped ("<filters><term>%s</term></filters>"
1867  "<%ss start=\"%s\" max=\"%s\"/>",
1868  filter ? filter : "",
1869  type,
1870  first ? first : "",
1871  max ? max : "");
1872  g_string_append (xml, end);
1873  g_free (end);
1874 
1875  if (extra_xml)
1876  g_string_append (xml, extra_xml);
1877 
1878  /* Get the resource. */
1879 
1880  if (openvas_connection_sendf (&connection,
1881  "<get_%ss"
1882  " %s_id=\"%s\""
1883  " sort_field=\"%s\""
1884  " sort_order=\"%s\""
1885  " details=\"1\""
1886  " %s/>",
1887  type,
1888  type,
1889  id,
1890  sort_field ? sort_field : "name",
1891  sort_order ? sort_order : "ascending",
1892  extra_attribs ? extra_attribs : "")
1893  == -1)
1894  {
1895  g_string_free (xml, TRUE);
1896  openvas_connection_close (&connection);
1897  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1898  return gsad_message (credentials,
1899  "Internal error", __FUNCTION__, __LINE__,
1900  "An internal error occurred while getting resources list. "
1901  "The current list of resources is not available. "
1902  "Diagnostics: Failure to send command to manager daemon.",
1903  "/omp?cmd=get_resources", response_data);
1904  }
1905 
1906  if (read_string_c (&connection, &xml))
1907  {
1908  g_string_free (xml, TRUE);
1909  openvas_connection_close (&connection);
1910  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1911  return gsad_message (credentials,
1912  "Internal error", __FUNCTION__, __LINE__,
1913  "An internal error occurred while getting resources list. "
1914  "The current list of resources is not available. "
1915  "Diagnostics: Failure to receive response from manager daemon.",
1916  "/omp?cmd=get_resources", response_data);
1917  }
1918 
1919  /* Get tag names */
1920 
1921  if (openvas_connection_sendf (&connection,
1922  "<get_tags"
1923  " filter=\"resource_type=%s"
1924  " first=1"
1925  " rows=-1\""
1926  " names_only=\"1\""
1927  "/>",
1928  type)
1929  == -1)
1930  {
1931  g_string_free (xml, TRUE);
1932  openvas_connection_close (&connection);
1933  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1934  return gsad_message (credentials,
1935  "Internal error", __FUNCTION__, __LINE__,
1936  "An internal error occurred while getting tag names list. "
1937  "The current list of resources is not available. "
1938  "Diagnostics: Failure to send command to manager daemon.",
1939  "/omp?cmd=get_resources", response_data);
1940  }
1941 
1942  if (read_string_c (&connection, &xml))
1943  {
1944  g_string_free (xml, TRUE);
1945  openvas_connection_close (&connection);
1946  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1947  return gsad_message (credentials,
1948  "Internal error", __FUNCTION__, __LINE__,
1949  "An internal error occurred while getting tag names list. "
1950  "The current list of resources is not available. "
1951  "Diagnostics: Failure to receive response from manager daemon.",
1952  "/omp?cmd=get_resources", response_data);
1953  }
1954 
1955  /* Get permissions */
1956 
1957  g_string_append (xml, "<permissions>");
1958 
1959  if ((strcmp (type, "user") == 0)
1960  || (strcmp (type, "group") == 0)
1961  || (strcmp (type, "role") == 0))
1962  ret = openvas_connection_sendf (&connection,
1963  "<get_permissions"
1964  " filter=\"subject_uuid=%s"
1965  " and not resource_uuid=&quot;&quot;"
1966  " or resource_uuid=%s"
1967  " first=1 rows=-1\"/>",
1968  id,
1969  id);
1970  else
1971  ret = openvas_connection_sendf (&connection,
1972  "<get_permissions"
1973  " filter=\"resource_uuid=%s"
1974  " first=1 rows=-1\"/>",
1975  id);
1976  if (ret == -1)
1977  {
1978  g_string_free (xml, TRUE);
1979  openvas_connection_close (&connection);
1980  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1981  return gsad_message (credentials,
1982  "Internal error", __FUNCTION__, __LINE__,
1983  "An internal error occurred while getting permissions list. "
1984  "The current list of resources is not available. "
1985  "Diagnostics: Failure to send command to manager daemon.",
1986  "/omp?cmd=get_resources", response_data);
1987  }
1988 
1989  if (read_string_c (&connection, &xml))
1990  {
1991  g_string_free (xml, TRUE);
1992  openvas_connection_close (&connection);
1993  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
1994  return gsad_message (credentials,
1995  "Internal error", __FUNCTION__, __LINE__,
1996  "An internal error occurred while getting permissions list. "
1997  "The current list of resources is not available. "
1998  "Diagnostics: Failure to receive response from manager daemon.",
1999  "/omp?cmd=get_resources", response_data);
2000  }
2001 
2002  g_string_append (xml, "</permissions>");
2003 
2004  /* Cleanup, and return transformed XML. */
2005 
2006  g_string_append_printf (xml, "</get_%s>", type);
2007  openvas_connection_close (&connection);
2008  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
2009  response_data);
2010 }
2011 
2024 static char *
2025 get_many (const char *type, credentials_t * credentials, params_t *params,
2026  const char *extra_xml, const char *extra_attribs,
2027  cmd_response_data_t* response_data)
2028 {
2029  GString *xml;
2030  GString *type_many; /* The plural form of type */
2031  openvas_connection_t connection;
2032  gchar *filter_type, *html, *request, *built_filter;
2033  int no_filter_history;
2034  const char *build_filter, *given_filt_id, *filt_id, *filter, *filter_extra;
2035  const char *first, *max, *sort_field, *sort_order, *owner, *permission;
2036  const char *replace_task_id;
2037  const char *overrides, *autofp, *autofp_value, *min_qod;
2038  const char *level_high, *level_medium, *level_low, *level_log;
2039  const char *level_false_positive;
2040 
2041  no_filter_history = params_value(params, "no_filter_history")
2042  ? atoi (params_value(params, "no_filter_history"))
2043  : 0;
2044  build_filter = params_value(params, "build_filter");
2045  given_filt_id = params_value (params, "filt_id");
2046  filter = params_value (params, "filter");
2047  filter_extra = params_value (params, "filter_extra");
2048  first = params_value (params, "first");
2049  max = params_value (params, "max");
2050  replace_task_id = params_value (params, "replace_task_id");
2051  sort_field = params_value (params, "sort_field");
2052  sort_order = params_value (params, "sort_order");
2053  owner = params_value (params, "owner");
2054  permission = params_value (params, "permission");
2055  overrides = params_value (params, "overrides");
2056  autofp = params_value (params, "autofp");
2057  autofp_value = params_value (params, "autofp_value");
2058  min_qod = params_value (params, "min_qod");
2059  level_high = params_value (params, "level_high");
2060  level_medium = params_value (params, "level_medium");
2061  level_low = params_value (params, "level_low");
2062  level_log = params_value (params, "level_log");
2063  level_false_positive = params_value (params, "level_false_positive");
2064 
2065  if (strcasecmp (type, "info") == 0)
2066  filter_type = g_strdup (params_value (params, "info_type"));
2067  else
2068  filter_type = g_strdup (type);
2069 
2070  if (given_filt_id)
2071  {
2072  if (no_filter_history == 0 && strcmp (given_filt_id, "0"))
2073  g_tree_replace (credentials->last_filt_ids, filter_type,
2074  g_strdup (given_filt_id));
2075  filt_id = given_filt_id;
2076  }
2077  else
2078  {
2079  if (no_filter_history == 0
2080  && (filter == NULL || strcmp (filter, "") == 0))
2081  filt_id = g_tree_lookup (credentials->last_filt_ids, filter_type);
2082  else
2083  filt_id = NULL;
2084  g_free (filter_type);
2085  }
2086 
2087  switch (manager_connect (credentials, &connection, &html, response_data))
2088  {
2089  case 0:
2090  break;
2091  case -1:
2092  if (html)
2093  return html;
2094  /* Fall through. */
2095  default:
2096  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2097  return gsad_message (credentials,
2098  "Internal error", __FUNCTION__, __LINE__,
2099  "An internal error occurred while getting a resource list. "
2100  "The current list of resources is not available. "
2101  "Diagnostics: Failure to connect to manager daemon.",
2102  "/omp?cmd=get_tasks", response_data);
2103  }
2104 
2105  /* check if filter still exists */
2106  switch (filter_exists (&connection, filt_id))
2107  {
2108  case 1:
2109  break;
2110  case 0:
2111  g_debug ("%s filter doesn't exist anymore %s!\n", __FUNCTION__,
2112  filt_id);
2113  filt_id = NULL;
2114  break;
2115  case -1:
2116  g_debug ("%s filter response didn't contain a status!\n", __FUNCTION__);
2117  filt_id = NULL;
2118  break;
2119  case -2:
2120  g_debug ("%s could not send filter request!\n", __FUNCTION__);
2121  filt_id = NULL;
2122  break;
2123  case -3:
2124  g_debug ("%s could not read entity from filter response!\n",
2125  __FUNCTION__);
2126  filt_id = NULL;
2127  break;
2128  default:
2129  filt_id = NULL;
2130  }
2131 
2132  xml = g_string_new ("");
2133  type_many = g_string_new (type);
2134 
2135  /* Workaround the fact that info is a non countable noun */
2136  if (strcmp (type, "info") != 0)
2137  g_string_append (type_many, "s");
2138 
2139  g_string_append_printf (xml, "<get_%s>", type_many->str);
2140 
2141  if (extra_xml)
2142  g_string_append (xml, extra_xml);
2143 
2144  built_filter = NULL;
2145  if (filt_id == NULL
2146  || (strcmp (filt_id, "") == 0)
2147  || (strcmp (filt_id, "--") == 0))
2148  {
2149  if ((build_filter && (strcmp (build_filter, "1") == 0))
2150  || ((filter == NULL || strcmp (filter, "") == 0)
2151  && (filter_extra == NULL || strcmp (filter_extra, "") == 0)))
2152  {
2153  if (build_filter && (strcmp (build_filter, "1") == 0))
2154  {
2155  gchar *task;
2156  const char *search_phrase, *task_id;
2157 
2158  if (strcmp (type, "report") == 0
2159  || strcmp (type, "task") == 0)
2160  {
2161  task = g_strdup_printf ("apply_overrides=%i min_qod=%s ",
2162  overrides
2163  && strcmp (overrides, "0"),
2164  min_qod ? min_qod : "");
2165  }
2166  else if (strcmp (type, "result") == 0)
2167  {
2168  gchar *levels
2169  = g_strdup_printf ("%s%s%s%s%s",
2170  level_high ? "h" : "",
2171  level_medium ? "m" : "",
2172  level_low ? "l" : "",
2173  level_log ? "g" : "",
2174  level_false_positive ? "f" : "");
2175  task = g_strdup_printf ("apply_overrides=%i min_qod=%s"
2176  " autofp=%s levels=%s ",
2177  (overrides
2178  && strcmp (overrides, "0")),
2179  min_qod ? min_qod : "",
2180  (autofp && autofp_value)
2181  ? autofp_value : "0",
2182  levels);
2183  g_free (levels);
2184  }
2185  else
2186  task = NULL;
2187 
2188  search_phrase = params_value (params, "search_phrase");
2189  task_id = params_value (params, "task_id");
2190  built_filter = g_strdup_printf
2191  ("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
2192  task ? task : "",
2193  task_id ? "task_id=" : "",
2194  task_id ? task_id : "",
2195  task_id ? " " : "",
2196  first ? "first=" : "",
2197  first ? first : "",
2198  first ? " " : "",
2199  max ? "rows=" : "",
2200  max ? max : "",
2201  max ? " " : "",
2202  sort_field
2203  ? ((sort_order && strcmp (sort_order,
2204  "ascending"))
2205  ? "sort-reverse="
2206  : "sort=")
2207  : "",
2208  sort_field ? sort_field : "",
2209  sort_field ? " " : "",
2210  permission ? "permission=" : "",
2211  permission ? permission : "",
2212  permission ? " " : "",
2213  owner ? "owner=" : "",
2214  owner ? owner : "",
2215  owner ? " " : "",
2216  (filter && search_phrase) ? " " : "",
2217  filter ? filter : "",
2218  search_phrase ? " " : "",
2219  search_phrase
2220  ? search_phrase
2221  : "");
2222  filt_id = "-2";
2223  g_free (task);
2224  }
2225  else if (strcmp (type, "info") == 0
2226  && params_value (params, "info_type"))
2227  {
2228  if (strcmp (params_value (params, "info_type"), "cve") == 0)
2229  filter = "sort-reverse=published rows=-2";
2230  else
2231  filter = "sort-reverse=created rows=-2";
2232  }
2233  else if (strcmp (type, "user") == 0)
2234  filter = "sort=roles rows=-2";
2235  else if (strcmp (type, "report") == 0)
2236  {
2237  const char *task_id;
2238  task_id = params_value (params, "task_id");
2239  if (task_id)
2240  built_filter = g_strdup_printf ("task_id=%s apply_overrides=1"
2241  " rows=-2 sort-reverse=date",
2242  task_id);
2243  else
2244  filter = "apply_overrides=1 rows=-2 sort-reverse=date";
2245  }
2246  else if (strcmp (type, "result") == 0)
2247  {
2248  built_filter
2249  = g_strdup_printf("apply_overrides=%d autofp=%s rows=-2"
2250  " sort-reverse=created",
2251  (overrides == NULL
2252  || strcmp (overrides, "0")),
2253  (autofp && strcmp (autofp, "0"))
2254  ? autofp_value
2255  : "0");
2256  }
2257  else if (strcmp (type, "task"))
2258  filter = "rows=-2";
2259  else
2260  filter = "apply_overrides=1 rows=-2";
2261  if (filt_id && strcmp (filt_id, ""))
2262  /* Request to use "filter" instead. */
2263  filt_id = "0";
2264  else
2265  filt_id = "-2";
2266  }
2267  else if ((strcmp (filter, "sort=nvt") == 0)
2268  && ((strcmp (type, "note") == 0)
2269  || (strcmp (type, "override") == 0)))
2270  filt_id = "-2";
2271  else if ((strcmp (filter, "apply_overrides=1") == 0)
2272  && (strcmp (type, "task") == 0))
2273  filt_id = "-2";
2274  }
2275  else if (replace_task_id)
2276  {
2277  const char *task_id;
2278  task_id = params_value (params, "task_id");
2279  if (task_id)
2280  built_filter = g_strdup_printf ("task_id=%s %s",
2281  task_id,
2282  filter ? filter : "");
2283  }
2284 
2285  /* Get the list. */
2286 
2287  request = g_markup_printf_escaped (" %sfilt_id=\"%s\""
2288  " %sfilter=\"%s%s%s%s\""
2289  " filter_replace=\"%s\""
2290  " first=\"%s\""
2291  " max=\"%s\""
2292  " sort_field=\"%s\""
2293  " sort_order=\"%s\"",
2294  strcmp (type, "report") ? "" : "report_",
2295  filt_id ? filt_id : "0",
2296  strcmp (type, "report") ? "" : "report_",
2297  built_filter
2298  ? built_filter
2299  : (filter ? filter : ""),
2300  filter_extra ? " " : "",
2301  filter_extra ? filter_extra : "",
2302  filter_extra ? " " : "",
2303  replace_task_id ? "task_id" : "",
2304  first ? first : "1",
2305  max ? max : "-2",
2306  sort_field ? sort_field : "name",
2307  sort_order ? sort_order : "ascending");
2308 
2309  g_free (built_filter);
2310  if (openvas_connection_sendf (&connection,
2311  "<get_%s%s%s %s/>",
2312  type_many->str,
2313  strcmp (type, "report") ? "" : " details=\"0\"",
2314  request,
2315  extra_attribs ? extra_attribs : "")
2316  == -1)
2317  {
2318  g_free(request);
2319  g_string_free (xml, TRUE);
2320  g_string_free (type_many, TRUE);
2321  openvas_connection_close (&connection);
2322  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2323  return gsad_message (credentials,
2324  "Internal error", __FUNCTION__, __LINE__,
2325  "An internal error occurred while getting a resource list. "
2326  "The current list of resources is not available. "
2327  "Diagnostics: Failure to send command to manager daemon.",
2328  "/omp?cmd=get_tasks", response_data);
2329  }
2330  g_free(request);
2331  if (read_string_c (&connection, &xml))
2332  {
2333  g_string_free (xml, TRUE);
2334  g_string_free (type_many, TRUE);
2335  openvas_connection_close (&connection);
2336  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2337  return gsad_message (credentials,
2338  "Internal error", __FUNCTION__, __LINE__,
2339  "An internal error occurred while getting resources list. "
2340  "The current list of resources is not available. "
2341  "Diagnostics: Failure to receive response from manager daemon.",
2342  "/omp?cmd=get_tasks", response_data);
2343  }
2344 
2345  // TODO: Test response
2346 
2347  if (command_enabled (credentials, "GET_FILTERS"))
2348  {
2349  /* Get the filters. */
2350 
2351  g_string_append (xml, "<filters>");
2352 
2353  if (openvas_connection_sendf_xml
2354  (&connection,
2355  "<get_filters"
2356  " filter=\"rows=-1 type=%s or type=\"/>",
2357  type)
2358  == -1)
2359  {
2360  g_string_free (xml, TRUE);
2361  g_string_free (type_many, TRUE);
2362  openvas_connection_close (&connection);
2363  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2364  return gsad_message (credentials,
2365  "Internal error", __FUNCTION__, __LINE__,
2366  "An internal error occurred while getting the filter list. "
2367  "The current list of filters is not available. "
2368  "Diagnostics: Failure to send command to manager daemon.",
2369  "/omp?cmd=get_tasks", response_data);
2370  }
2371 
2372  if (read_string_c (&connection, &xml))
2373  {
2374  g_string_free (xml, TRUE);
2375  g_string_free (type_many, TRUE);
2376  openvas_connection_close (&connection);
2377  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2378  return gsad_message (credentials,
2379  "Internal error", __FUNCTION__, __LINE__,
2380  "An internal error occurred while getting the filter list. "
2381  "The current list of filters is not available. "
2382  "Diagnostics: Failure to receive response from manager daemon.",
2383  "/omp?cmd=get_tasks", response_data);
2384  }
2385 
2386  g_string_append (xml, "</filters>");
2387  }
2388 
2389  if (command_enabled (credentials, "GET_SETTINGS"))
2390  {
2391  /* Get the Wizard Rows setting. */
2392 
2393  if (openvas_connection_sendf_xml
2394  (&connection,
2395  "<get_settings"
2396  " setting_id=\"20f3034c-e709-11e1-87e7-406186ea4fc5\"/>",
2397  type)
2398  == -1)
2399  {
2400  g_string_free (xml, TRUE);
2401  g_string_free (type_many, TRUE);
2402  openvas_connection_close (&connection);
2403  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2404  return gsad_message (credentials,
2405  "Internal error", __FUNCTION__, __LINE__,
2406  "An internal error occurred while getting the filter list. "
2407  "The current list of filters is not available. "
2408  "Diagnostics: Failure to send command to manager daemon.",
2409  "/omp?cmd=get_tasks", response_data);
2410  }
2411 
2412  if (read_string_c (&connection, &xml))
2413  {
2414  g_string_free (xml, TRUE);
2415  g_string_free (type_many, TRUE);
2416  openvas_connection_close (&connection);
2417  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2418  return gsad_message (credentials,
2419  "Internal error", __FUNCTION__, __LINE__,
2420  "An internal error occurred while getting the filter list. "
2421  "The current list of filters is not available. "
2422  "Diagnostics: Failure to receive response from manager daemon.",
2423  "/omp?cmd=get_tasks", response_data);
2424  }
2425  }
2426 
2427  if (command_enabled (credentials, "GET_TAGS")
2428  && params_value (params, "details"))
2429  {
2430  /* Get tag names */
2431 
2432  if (openvas_connection_sendf (&connection,
2433  "<get_tags"
2434  " filter=\"resource_type=%s"
2435  " first=1"
2436  " rows=-1\""
2437  " names_only=\"1\""
2438  "/>",
2439  strcmp (type, "info")
2440  ? type
2441  : params_value (params, "info_type"))
2442  == -1)
2443  {
2444  g_string_free (xml, TRUE);
2445  openvas_connection_close (&connection);
2446  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2447  return gsad_message (credentials,
2448  "Internal error", __FUNCTION__, __LINE__,
2449  "An internal error occurred while getting tag names list. "
2450  "The current list of resources is not available. "
2451  "Diagnostics: Failure to send command to manager daemon.",
2452  "/omp?cmd=get_resources", response_data);
2453  }
2454 
2455  if (read_string_c (&connection, &xml))
2456  {
2457  g_string_free (xml, TRUE);
2458  openvas_connection_close (&connection);
2459  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2460  return gsad_message (credentials,
2461  "Internal error", __FUNCTION__, __LINE__,
2462  "An internal error occurred while getting tag names list. "
2463  "The current list of resources is not available. "
2464  "Diagnostics: Failure to receive response from manager daemon.",
2465  "/omp?cmd=get_resources", response_data);
2466  }
2467  }
2468 
2469  /* Cleanup, and return transformed XML. */
2470  g_string_append_printf (xml, "</get_%s>", type_many->str);
2471  openvas_connection_close (&connection);
2472  g_string_free (type_many, TRUE);
2473  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
2474  response_data);
2475 }
2476 
2489 char *
2490 edit_resource (const char *type, credentials_t *credentials, params_t *params,
2491  const char *extra_get_attribs, const char *extra_xml,
2492  cmd_response_data_t* response_data)
2493 {
2494  GString *xml;
2495  openvas_connection_t connection;
2496  gchar *html, *id_name;
2497  const char *resource_id;
2498 
2499  id_name = g_strdup_printf ("%s_id", type);
2500  resource_id = params_value (params, id_name);
2501  g_free (id_name);
2502 
2503  if (resource_id == NULL)
2504  {
2505  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
2506  return gsad_message (credentials,
2507  "Internal error", __FUNCTION__, __LINE__,
2508  "An internal error occurred while editing a resource. "
2509  "The resource remains as it was. "
2510  "Diagnostics: Required ID parameter was NULL.",
2511  "/omp?cmd=get_tasks", response_data);
2512  }
2513 
2514  switch (manager_connect (credentials, &connection, &html,
2515  response_data))
2516  {
2517  case 0:
2518  break;
2519  case -1:
2520  if (html)
2521  return html;
2522  /* Fall through. */
2523  default:
2524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2525  return gsad_message (credentials,
2526  "Internal error", __FUNCTION__, __LINE__,
2527  "An internal error occurred while editing a resource. "
2528  "The resource remains as it was. "
2529  "Diagnostics: Failure to connect to manager daemon.",
2530  "/omp?cmd=get_tasks", response_data);
2531  }
2532 
2533  if (openvas_connection_sendf (&connection,
2534  /* TODO: Remove redundant COMMANDS. */
2535  "<commands>"
2536  "<get_%ss"
2537  " %s"
2538  " %s_id=\"%s\""
2539  " details=\"1\"/>"
2540  "</commands>",
2541  type,
2542  extra_get_attribs ? extra_get_attribs : "",
2543  type,
2544  resource_id)
2545  == -1)
2546  {
2547  openvas_connection_close (&connection);
2548  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2549  return gsad_message (credentials,
2550  "Internal error", __FUNCTION__, __LINE__,
2551  "An internal error occurred while getting a resource. "
2552  "Diagnostics: Failure to send command to manager daemon.",
2553  "/omp?cmd=get_tasks", response_data);
2554  }
2555 
2556  xml = g_string_new ("");
2557 
2558  g_string_append_printf (xml, "<edit_%s>", type);
2559 
2560  if (extra_xml)
2561  g_string_append (xml, extra_xml);
2562 
2563  if (read_string_c (&connection, &xml))
2564  {
2565  g_string_free (xml, TRUE);
2566  openvas_connection_close (&connection);
2567  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2568  return gsad_message (credentials,
2569  "Internal error", __FUNCTION__, __LINE__,
2570  "An internal error occurred while getting a resource. "
2571  "Diagnostics: Failure to receive response from manager daemon.",
2572  "/omp?cmd=get_tasks", response_data);
2573  }
2574 
2575  /* Cleanup, and return transformed XML. */
2576 
2577  g_string_append_printf (xml, "</edit_%s>", type);
2578  openvas_connection_close (&connection);
2579  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
2580  response_data);
2581 }
2582 
2594 gchar *
2595 format_file_name (gchar* fname_format, credentials_t* credentials,
2596  const char* type, const char* uuid,
2597  entity_t resource_entity)
2598 {
2599  gchar *creation_time, *modification_time, *name, *format_name;
2600  gchar *ret;
2601 
2602  if (resource_entity)
2603  {
2604  entity_t creation_time_entity, modification_time_entity;
2605  entity_t task_entity, format_entity, format_name_entity, name_entity;
2606 
2607  creation_time_entity = entity_child (resource_entity,
2608  "creation_time");
2609 
2610  if (creation_time_entity)
2611  creation_time = entity_text (creation_time_entity);
2612  else
2613  creation_time = NULL;
2614 
2615  modification_time_entity = entity_child (resource_entity,
2616  "modification_time");
2617 
2618  if (modification_time_entity)
2619  modification_time = entity_text (modification_time_entity);
2620  else
2621  modification_time = NULL;
2622 
2623  if (strcasecmp (type, "report") == 0)
2624  {
2625  task_entity = entity_child (resource_entity, "task");
2626  if (task_entity)
2627  name_entity = entity_child (task_entity, "name");
2628  else
2629  name_entity = NULL;
2630 
2631  format_entity = entity_child (resource_entity, "report_format");
2632  if (format_entity)
2633  {
2634  format_name_entity = entity_child (format_entity, "name");
2635  }
2636  else
2637  format_name_entity = NULL;
2638 
2639  if (format_name_entity && strlen (entity_text (format_name_entity)))
2640  format_name = entity_text (format_name_entity);
2641  else
2642  format_name = NULL;
2643  }
2644  else
2645  {
2646  name_entity = entity_child (resource_entity, "name");
2647  format_name = NULL;
2648  }
2649 
2650  if (name_entity)
2651  name = entity_text (name_entity);
2652  else
2653  name = NULL;
2654  }
2655  else
2656  {
2657  creation_time = NULL;
2658  modification_time = NULL;
2659  name = NULL;
2660  format_name = NULL;
2661  }
2662 
2663  ret = openvas_export_file_name (fname_format, credentials->username,
2664  type, uuid, creation_time, modification_time,
2665  name, format_name);
2666  return ret;
2667 }
2668 
2669 
2683 char *
2684 export_resource (const char *type, credentials_t * credentials,
2685  params_t *params, enum content_type * content_type,
2686  char **content_disposition, gsize *content_length,
2687  cmd_response_data_t* response_data)
2688 {
2689  GString *xml;
2690  entity_t entity;
2691  entity_t resource_entity;
2692  openvas_connection_t connection;
2693  char *content = NULL;
2694  gchar *html, *id_name;
2695  gchar *fname_format, *file_name;
2696  int ret;
2697  const char *resource_id, *subtype;
2698 
2699  *content_length = 0;
2700 
2701  switch (manager_connect (credentials, &connection, &html,
2702  response_data))
2703  {
2704  case 0:
2705  break;
2706  case -1:
2707  if (html)
2708  return html;
2709  /* Fall through. */
2710  default:
2711  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2712  return gsad_message (credentials,
2713  "Internal error", __FUNCTION__, __LINE__,
2714  "An internal error occurred while getting a resource. "
2715  "The resource could not be delivered. "
2716  "Diagnostics: Failure to connect to manager daemon.",
2717  "/omp?cmd=get_tasks", response_data);
2718  }
2719 
2720  xml = g_string_new ("");
2721 
2722  id_name = g_strdup_printf ("%s_id", type);
2723  resource_id = params_value (params, id_name);
2724  g_free (id_name);
2725 
2726  if (resource_id == NULL)
2727  {
2728  g_string_append (xml, GSAD_MESSAGE_INVALID_PARAM ("Export Resource"));
2729  openvas_connection_close (&connection);
2730  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
2731  response_data);
2732  }
2733 
2734  subtype = params_value (params, "subtype");
2735 
2736  if (openvas_connection_sendf (&connection,
2737  "<get_%ss"
2738  " %s_id=\"%s\""
2739  "%s%s%s"
2740  " export=\"1\""
2741  " details=\"1\"/>",
2742  type,
2743  type,
2744  resource_id,
2745  subtype ? " type=\"" : "",
2746  subtype ? subtype : "",
2747  subtype ? "\"" : "")
2748  == -1)
2749  {
2750  g_string_free (xml, TRUE);
2751  openvas_connection_close (&connection);
2752  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2753  return gsad_message (credentials,
2754  "Internal error", __FUNCTION__, __LINE__,
2755  "An internal error occurred while getting a resource. "
2756  "The resource could not be delivered. "
2757  "Diagnostics: Failure to send command to manager daemon.",
2758  "/omp?cmd=get_tasks", response_data);
2759  }
2760 
2761  entity = NULL;
2762  if (read_entity_and_text_c (&connection, &entity, &content))
2763  {
2764  g_string_free (xml, TRUE);
2765  openvas_connection_close (&connection);
2766  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2767  return gsad_message (credentials,
2768  "Internal error", __FUNCTION__, __LINE__,
2769  "An internal error occurred while getting a resource. "
2770  "The resource could not be delivered. "
2771  "Diagnostics: Failure to receive response from manager daemon.",
2772  "/omp?cmd=get_tasks", response_data);
2773  }
2774 
2775  if (!omp_success (entity))
2776  set_http_status_from_entity (entity, response_data);
2777 
2778  resource_entity = entity_child (entity, type);
2779 
2780  if (resource_entity == NULL)
2781  {
2782  g_free (content);
2783  free_entity (entity);
2784  g_string_free (xml, TRUE);
2785  openvas_connection_close (&connection);
2786  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2787  return gsad_message (credentials,
2788  "Internal error", __FUNCTION__, __LINE__,
2789  "An internal error occurred while getting a resource. "
2790  "The resource could not be delivered. "
2791  "Diagnostics: Failure to receive resource from manager daemon.",
2792  "/omp?cmd=get_tasks", response_data);
2793  }
2794 
2795  ret = setting_get_value (&connection,
2796  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2",
2797  &fname_format,
2798  response_data);
2799  if (ret)
2800  {
2801  g_free (content);
2802  free_entity (entity);
2803  g_string_free (xml, TRUE);
2804  openvas_connection_close (&connection);
2805  switch (ret)
2806  {
2807  case 1:
2808  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2809  return gsad_message (credentials,
2810  "Internal error", __FUNCTION__, __LINE__,
2811  "An internal error occurred while getting a setting. "
2812  "The setting could not be delivered. "
2813  "Diagnostics: Failure to send command to manager daemon.",
2814  "/omp?cmd=get_tasks", response_data);
2815  case 2:
2816  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2817  return gsad_message (credentials,
2818  "Internal error", __FUNCTION__, __LINE__,
2819  "An internal error occurred while getting a setting. "
2820  "The setting could not be delivered. "
2821  "Diagnostics: Failure to receive response from manager daemon.",
2822  "/omp?cmd=get_tasks", response_data);
2823  default:
2824  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2825  return gsad_message (credentials,
2826  "Internal error", __FUNCTION__, __LINE__,
2827  "An internal error occurred while getting a setting. "
2828  "The setting could not be delivered. "
2829  "Diagnostics: Internal error.",
2830  "/omp?cmd=get_tasks", response_data);
2831  }
2832  }
2833 
2834  if (fname_format == NULL)
2835  {
2836  g_warning ("%s : File name format setting not found.", __FUNCTION__);
2837  fname_format = "%T-%U";
2838  }
2839 
2840  file_name = format_file_name (fname_format, credentials, type, resource_id,
2841  resource_entity);
2842  if (file_name == NULL)
2843  file_name = g_strdup_printf ("%s-%s", type, resource_id);
2844 
2845  *content_type = GSAD_CONTENT_TYPE_APP_XML;
2846  *content_disposition = g_strdup_printf ("attachment; filename=\"%s.xml\"",
2847  file_name);
2848  *content_length = strlen (content);
2849  free_entity (entity);
2850  g_free (file_name);
2851  g_string_free (xml, TRUE);
2852  openvas_connection_close (&connection);
2853  return content;
2854 }
2855 
2868 static char *
2869 export_many (const char *type, credentials_t * credentials, params_t *params,
2870  enum content_type * content_type, char **content_disposition,
2871  gsize *content_length, cmd_response_data_t* response_data)
2872 {
2873  entity_t entity;
2874  openvas_connection_t connection;
2875  char *content = NULL;
2876  gchar *html;
2877  const char *filter;
2878  gchar *filter_escaped;
2879  gchar *type_many;
2880  gchar *fname_format, *file_name;
2881  int ret;
2882 
2883  *content_length = 0;
2884 
2885  switch (manager_connect (credentials, &connection, &html,
2886  response_data))
2887  {
2888  case 0:
2889  break;
2890  case -1:
2891  if (html)
2892  return html;
2893  /* Fall through. */
2894  default:
2895  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2896  return gsad_message (credentials,
2897  "Internal error", __FUNCTION__, __LINE__,
2898  "An internal error occurred while getting a list. "
2899  "The list could not be delivered. "
2900  "Diagnostics: Failure to connect to manager daemon.",
2901  "/omp?cmd=get_tasks", response_data);
2902  }
2903 
2904  filter = params_value (params, "filter");
2905 
2906  filter_escaped = g_markup_escape_text (filter, -1);
2907 
2908  if (strcmp (type, "info") == 0)
2909  {
2910  if (openvas_connection_sendf (&connection,
2911  "<get_info"
2912  " type=\"%s\""
2913  " export=\"1\""
2914  " details=\"1\""
2915  " filter=\"%s\"/>",
2916  params_value (params, "info_type"),
2917  filter_escaped ? filter_escaped : "")
2918  == -1)
2919  {
2920  openvas_connection_close (&connection);
2921  g_free (filter_escaped);
2922  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2923  return gsad_message (credentials,
2924  "Internal error", __FUNCTION__, __LINE__,
2925  "An internal error occurred while getting a list. "
2926  "The list could not be delivered. "
2927  "Diagnostics: Failure to send command to manager daemon.",
2928  "/omp?cmd=get_tasks", response_data);
2929  }
2930  }
2931  else if (strcmp (type, "asset") == 0)
2932  {
2933  if (openvas_connection_sendf (&connection,
2934  "<get_assets"
2935  " type=\"%s\""
2936  " export=\"1\""
2937  " details=\"1\""
2938  " filter=\"%s\"/>",
2939  params_value (params, "asset_type"),
2940  filter_escaped ? filter_escaped : "")
2941  == -1)
2942  {
2943  openvas_connection_close (&connection);
2944  g_free (filter_escaped);
2945  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2946  return gsad_message (credentials,
2947  "Internal error", __FUNCTION__, __LINE__,
2948  "An internal error occurred while getting a list. "
2949  "The list could not be delivered. "
2950  "Diagnostics: Failure to send command to manager daemon.",
2951  "/omp?cmd=get_tasks", response_data);
2952  }
2953  }
2954  else
2955  {
2956  if (openvas_connection_sendf (&connection,
2957  "<get_%ss"
2958  " export=\"1\""
2959  " details=\"1\""
2960  " filter=\"%s\"/>",
2961  type,
2962  filter_escaped ? filter_escaped : "")
2963  == -1)
2964  {
2965  openvas_connection_close (&connection);
2966  g_free (filter_escaped);
2967  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2968  return gsad_message (credentials,
2969  "Internal error", __FUNCTION__, __LINE__,
2970  "An internal error occurred while getting a list. "
2971  "The list could not be delivered. "
2972  "Diagnostics: Failure to send command to manager daemon.",
2973  "/omp?cmd=get_tasks", response_data);
2974  }
2975  }
2976  g_free (filter_escaped);
2977 
2978  entity = NULL;
2979  if (read_entity_and_text_c (&connection, &entity, &content))
2980  {
2981  openvas_connection_close (&connection);
2982  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
2983  return gsad_message (credentials,
2984  "Internal error", __FUNCTION__, __LINE__,
2985  "An internal error occurred while getting a list. "
2986  "The list could not be delivered. "
2987  "Diagnostics: Failure to receive response from manager daemon.",
2988  "/omp?cmd=get_tasks", response_data);
2989  }
2990 
2991  if (!omp_success (entity))
2992  set_http_status_from_entity (entity, response_data);
2993 
2994  ret = setting_get_value (&connection,
2995  "0872a6ed-4f85-48c5-ac3f-a5ef5e006745",
2996  &fname_format,
2997  response_data);
2998  if (ret)
2999  {
3000  g_free (content);
3001  free_entity (entity);
3002  openvas_connection_close (&connection);
3003  switch (ret)
3004  {
3005  case 1:
3006  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3007  return gsad_message (credentials,
3008  "Internal error", __FUNCTION__, __LINE__,
3009  "An internal error occurred while getting a setting. "
3010  "The setting could not be delivered. "
3011  "Diagnostics: Failure to send command to manager daemon.",
3012  "/omp?cmd=get_tasks", response_data);
3013  case 2:
3014  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3015  return gsad_message (credentials,
3016  "Internal error", __FUNCTION__, __LINE__,
3017  "An internal error occurred while getting a setting. "
3018  "The setting could not be delivered. "
3019  "Diagnostics: Failure to receive response from manager daemon.",
3020  "/omp?cmd=get_tasks", response_data);
3021  default:
3022  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3023  return gsad_message (credentials,
3024  "Internal error", __FUNCTION__, __LINE__,
3025  "An internal error occurred while getting a setting. "
3026  "The setting could not be delivered. "
3027  "Diagnostics: Internal error.",
3028  "/omp?cmd=get_tasks", response_data);
3029  }
3030  }
3031 
3032  if (fname_format == NULL)
3033  {
3034  g_warning ("%s : File name format setting not found.", __FUNCTION__);
3035  fname_format = "%T-%D";
3036  }
3037 
3038  if (strcmp (type, "info") == 0)
3039  type_many = g_strdup (type);
3040  else
3041  type_many = g_strdup_printf ("%ss", type);
3042 
3043  file_name = format_file_name (fname_format, credentials, type_many, "list",
3044  NULL);
3045  if (file_name == NULL)
3046  file_name = g_strdup_printf ("%s-%s", type_many, "list");
3047 
3048  g_free (type_many);
3049 
3050  *content_type = GSAD_CONTENT_TYPE_APP_XML;
3051  *content_disposition = g_strdup_printf ("attachment; filename=\"%s.xml\"",
3052  file_name);
3053  *content_length = strlen (content);
3054  free_entity (entity);
3055  g_free (file_name);
3056  openvas_connection_close (&connection);
3057  return content;
3058 }
3059 
3072 char *
3073 delete_resource (const char *type, credentials_t * credentials,
3074  params_t *params, int ultimate,
3075  const char *get, cmd_response_data_t* response_data)
3076 {
3077  openvas_connection_t connection;
3078  gchar *html, *response, *id_name, *resource_id, *extra_attribs;
3079  const char *no_redirect, *next_id;
3080  entity_t entity;
3081  gchar *cap_type, *default_next, *prev_action;
3082 
3083  no_redirect = params_value (params, "no_redirect");
3084  id_name = g_strdup_printf ("%s_id", type);
3085  if (params_value (params, id_name))
3086  resource_id = g_strdup (params_value (params, id_name));
3087  else
3088  {
3089  g_free (id_name);
3090  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
3091  return gsad_message (credentials,
3092  "Internal error", __FUNCTION__, __LINE__,
3093  "An internal error occurred while deleting a resource. "
3094  "The resource was not deleted. "
3095  "Diagnostics: Required parameter resource_id was NULL.",
3096  "/omp?cmd=get_tasks", response_data);
3097  }
3098 
3099  /* This is a hack for assets, because asset_id is the param name used for
3100  * both the asset being deleted and the asset on the next page. */
3101  next_id = params_value (params, "next_id");
3102  if (next_id)
3103  {
3104  param_t *param;
3105  param = params_get (params, id_name);
3106  g_free (param->value);
3107  param->value = g_strdup (next_id);
3108  param->value_size = strlen (param->value);
3109  }
3110 
3111  g_free (id_name);
3112 
3113  switch (manager_connect (credentials, &connection, &html,
3114  response_data))
3115  {
3116  case 0:
3117  break;
3118  case -1:
3119  if (html)
3120  {
3121  response_data->http_status_code = MHD_HTTP_SERVICE_UNAVAILABLE;
3122  g_free (resource_id);
3123  return html;
3124  }
3125  /* Fall through. */
3126  default:
3127  g_free (resource_id);
3128  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3129  return gsad_message (credentials,
3130  "Internal error", __FUNCTION__, __LINE__,
3131  "An internal error occurred while deleting a resource. "
3132  "The resource is not deleted. "
3133  "Diagnostics: Failure to connect to manager daemon.",
3134  "/omp?cmd=get_tasks", response_data);
3135  }
3136 
3137  /* Extra attributes */
3138  extra_attribs = NULL;
3139 
3140  /* Inheritor of user's resource */
3141  if (strcmp (type, "user") == 0)
3142  {
3143  const char* inheritor_id;
3144  inheritor_id = params_value (params, "inheritor_id");
3145  if (inheritor_id)
3146  extra_attribs = g_strdup_printf ("inheritor_id=\"%s\"", inheritor_id);
3147  }
3148 
3149  /* Delete the resource and get all resources. */
3150 
3151  if (openvas_connection_sendf (&connection,
3152  "<delete_%s %s_id=\"%s\" ultimate=\"%i\"%s%s/>",
3153  type,
3154  type,
3155  resource_id,
3156  !!ultimate,
3157  extra_attribs ? " " : "",
3158  extra_attribs ? extra_attribs : "")
3159  == -1)
3160  {
3161  openvas_connection_close (&connection);
3162  g_free (resource_id);
3163  g_free (extra_attribs);
3164  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3165  return gsad_message (credentials,
3166  "Internal error", __FUNCTION__, __LINE__,
3167  "An internal error occurred while deleting a resource. "
3168  "The resource is not deleted. "
3169  "Diagnostics: Failure to send command to manager daemon.",
3170  "/omp?cmd=get_tasks", response_data);
3171  }
3172 
3173  g_free (resource_id);
3174  g_free (extra_attribs);
3175 
3176  entity = NULL;
3177  if (read_entity_and_text_c (&connection, &entity, &response))
3178  {
3179  openvas_connection_close (&connection);
3180  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3181  return gsad_message (credentials,
3182  "Internal error", __FUNCTION__, __LINE__,
3183  "An internal error occurred while deleting a resource. "
3184  "It is unclear whether the resource has been deleted or not. "
3185  "Diagnostics: Failure to read response from manager daemon.",
3186  "/omp?cmd=get_tasks", response_data);
3187  }
3188 
3189  if (!omp_success (entity))
3190  set_http_status_from_entity (entity, response_data);
3191  openvas_connection_close (&connection);
3192 
3193  cap_type = capitalize (type);
3194  default_next = g_strdup_printf ("get_%ss", type);
3195  prev_action = g_strdup_printf ("Delete %s", cap_type);
3196 
3197  html = response_from_entity (credentials, params, entity,
3198  (no_redirect && strcmp (no_redirect, "0")),
3199  NULL, default_next,
3200  NULL, default_next,
3201  prev_action, response_data);
3202 
3203  g_free (response);
3204  free_entity (entity);
3205  g_free (cap_type);
3206  g_free (default_next);
3207  g_free (prev_action);
3208 
3209  return html;
3210 }
3211 
3223 char *
3224 resource_action (credentials_t *credentials, params_t *params, const char *type,
3225  const char *action, cmd_response_data_t* response_data)
3226 {
3227  gchar *html, *response, *param_name;
3228  const char *no_redirect, *resource_id;
3229  gchar *cap_action, *cap_type, *get_cmd, *prev_action;
3230 
3231  int ret;
3232  entity_t entity;
3233 
3234  assert (type);
3235 
3236  param_name = g_strdup_printf ("%s_id", type);
3237  no_redirect = params_value (params, "no_redirect");
3238  resource_id = params_value (params, param_name);
3239 
3240  if (resource_id == NULL)
3241  {
3242  gchar *message;
3243  message = g_strdup_printf
3244  ("An internal error occurred while performing an action. "
3245  "The resource remains the same. "
3246  "Diagnostics: Required parameter %s was NULL.",
3247  param_name);
3248  g_free (param_name);
3249  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
3250  html = gsad_message (credentials,
3251  "Internal error", __FUNCTION__, __LINE__,
3252  message,
3253  "/omp?cmd=get_tasks", response_data);
3254  g_free (message);
3255  return html;
3256  }
3257  g_free (param_name);
3258 
3259  response = NULL;
3260  entity = NULL;
3261  ret = ompf (credentials, &response, &entity, response_data,
3262  "<%s_%s %s_id=\"%s\"/>",
3263  action,
3264  type,
3265  type,
3266  resource_id);
3267  switch (ret)
3268  {
3269  case 0:
3270  case -1:
3271  break;
3272  case 1:
3273  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3274  return gsad_message (credentials,
3275  "Internal error", __FUNCTION__, __LINE__,
3276  "An internal error occurred while performing an action. "
3277  "The resource remains the same. "
3278  "Diagnostics: Failure to send command to manager daemon.",
3279  "/omp?cmd=get_tasks", response_data);
3280  case 2:
3281  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3282  return gsad_message (credentials,
3283  "Internal error", __FUNCTION__, __LINE__,
3284  "An internal error occurred while performing an action. "
3285  "It is unclear whether the resource has been affected. "
3286  "Diagnostics: Failure to receive response from manager daemon.",
3287  "/omp?cmd=get_tasks", response_data);
3288  default:
3289  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3290  return gsad_message (credentials,
3291  "Internal error", __FUNCTION__, __LINE__,
3292  "An internal error occurred while performing an action. "
3293  "It is unclear whether the resource has been affected. "
3294  "Diagnostics: Internal Error.",
3295  "/omp?cmd=get_tasks", response_data);
3296  }
3297 
3298  if (!omp_success (entity))
3299  set_http_status_from_entity (entity, response_data);
3300 
3301  cap_action = capitalize (action);
3302  cap_type = capitalize (type);
3303  get_cmd = g_strdup_printf ("get_%ss", type);
3304  prev_action = g_strdup_printf ("%s %s", cap_action, cap_type);
3305  html = response_from_entity (credentials, params, entity,
3306  (no_redirect && strcmp (no_redirect, "0")),
3307  NULL, get_cmd,
3308  NULL, get_cmd,
3309  prev_action, response_data);
3310 
3311  g_free (response);
3312  free_entity (entity);
3313  g_free (cap_action);
3314  g_free (cap_type);
3315  g_free (get_cmd);
3316  g_free (prev_action);
3317 
3318  return html;
3319 }
3320 
3321 
3322 /* Page handlers. */
3323 
3372 static char *
3373 setting_get_value_error (credentials_t *credentials,
3374  openvas_connection_t *connection,
3375  const gchar *setting_id,
3376  gchar **value,
3377  cmd_response_data_t* response_data)
3378 {
3379  switch (setting_get_value (connection, setting_id, value, response_data))
3380  {
3381  case 0:
3382  return NULL;
3383  case 1:
3384  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3385  return gsad_message (credentials,
3386  "Internal error", __FUNCTION__, __LINE__,
3387  "An internal error occurred while getting a setting. "
3388  "The setting could not be delivered. "
3389  "Diagnostics: Failure to send command to manager daemon.",
3390  "/omp?cmd=get_tasks", response_data);
3391  case 2:
3392  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3393  return gsad_message (credentials,
3394  "Internal error", __FUNCTION__, __LINE__,
3395  "An internal error occurred while getting a setting. "
3396  "The setting could not be delivered. "
3397  "Diagnostics: Failure to receive response from manager daemon.",
3398  "/omp?cmd=get_tasks", response_data);
3399  default:
3400  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3401  return gsad_message (credentials,
3402  "Internal error", __FUNCTION__, __LINE__,
3403  "An internal error occurred while getting a setting. "
3404  "The setting could not be delivered. "
3405  "Diagnostics: Internal error.",
3406  "/omp?cmd=get_tasks", response_data);
3407  }
3408 }
3409 
3418 #define PARAM_OR_SETTING(value, param, setting_id, cleanup) \
3419  if (params_valid (params, param)) \
3420  value = g_strdup (params_value (params, param)); \
3421  else \
3422  { \
3423  char *message; \
3424  message = setting_get_value_error (credentials, &connection, setting_id,\
3425  &value, response_data); \
3426  if (message) \
3427  { \
3428  cleanup; \
3429  return message; \
3430  } \
3431  }
3432 
3444 static char *
3445 new_task (credentials_t * credentials, const char *message, params_t *params,
3446  const char *extra_xml, cmd_response_data_t* response_data)
3447 {
3448  GString *xml;
3449  openvas_connection_t connection;
3450  gchar *html;
3451  int apply_overrides;
3452  const char *alerts, *overrides;
3453  int ret;
3454  gchar *alert, *schedule, *target;
3455  gchar *openvas_config, *osp_config, *openvas_scanner, *osp_scanner;
3456 
3457  alerts = params_value (params, "alerts");
3458  overrides = params_value (params, "overrides");
3459 
3460  apply_overrides = overrides ? strcmp (overrides, "0") : 0;
3461 
3462  switch (manager_connect (credentials, &connection, &html,
3463  response_data))
3464  {
3465  case 0:
3466  break;
3467  case -1:
3468  if (html)
3469  return html;
3470  /* Fall through. */
3471  default:
3472  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3473  return gsad_message (credentials,
3474  "Internal error", __FUNCTION__, __LINE__,
3475  "An internal error occurred while getting targets list. "
3476  "The current list of targets is not available. "
3477  "Diagnostics: Failure to connect to manager daemon.",
3478  "/omp?cmd=get_tasks", response_data);
3479  }
3480 
3481  ret = setting_get_value (&connection,
3482  "f9f5a546-8018-48d0-bef5-5ad4926ea899",
3483  &alert,
3484  response_data);
3485  if (ret)
3486  {
3487  openvas_connection_close (&connection);
3488  switch (ret)
3489  {
3490  case 1:
3491  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3492  return gsad_message (credentials,
3493  "Internal error", __FUNCTION__, __LINE__,
3494  "An internal error occurred while getting a setting. "
3495  "The setting could not be delivered. "
3496  "Diagnostics: Failure to send command to manager daemon.",
3497  "/omp?cmd=get_tasks", response_data);
3498  case 2:
3499  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3500  return gsad_message (credentials,
3501  "Internal error", __FUNCTION__, __LINE__,
3502  "An internal error occurred while getting a setting. "
3503  "The setting could not be delivered. "
3504  "Diagnostics: Failure to receive response from manager daemon.",
3505  "/omp?cmd=get_tasks", response_data);
3506  default:
3507  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3508  return gsad_message (credentials,
3509  "Internal error", __FUNCTION__, __LINE__,
3510  "An internal error occurred while getting a setting. "
3511  "The setting could not be delivered. "
3512  "Diagnostics: Internal error.",
3513  "/omp?cmd=get_tasks", response_data);
3514  }
3515  }
3516 
3517  PARAM_OR_SETTING (schedule, "schedule_id",
3518  "778eedad-5550-4de0-abb6-1320d13b5e18",
3519  openvas_connection_close (&connection);
3520  g_free (alert));
3521 
3522  PARAM_OR_SETTING (target, "target_id",
3523  "23409203-940a-4b4a-b70c-447475f18323",
3524  openvas_connection_close (&connection);
3525  g_free (alert);
3526  g_free (schedule));
3527 
3528  PARAM_OR_SETTING (openvas_config, "config_id",
3529  "fe7ea321-e3e3-4cc6-9952-da836aae83ce",
3530  openvas_connection_close (&connection);
3531  g_free (alert);
3532  g_free (schedule);
3533  g_free (target));
3534 
3535  PARAM_OR_SETTING (osp_config, "osp_config_id",
3536  "fb19ac4b-614c-424c-b046-0bc32bf1be73",
3537  openvas_connection_close (&connection);
3538  g_free (alert);
3539  g_free (schedule);
3540  g_free (target);
3541  g_free (openvas_config));
3542 
3543  PARAM_OR_SETTING (openvas_scanner, "scanner_id",
3544  "f7d0f6ed-6f9e-45dc-8bd9-05cced84e80d",
3545  openvas_connection_close (&connection);
3546  g_free (alert);
3547  g_free (schedule);
3548  g_free (target);
3549  g_free (openvas_config);
3550  g_free (osp_config));
3551 
3552  PARAM_OR_SETTING (osp_scanner, "osp_scanner_id",
3553  "b20697c9-be0a-4cd4-8b4d-5fe7841ebb03",
3554  openvas_connection_close (&connection);
3555  g_free (alert);
3556  g_free (schedule);
3557  g_free (target);
3558  g_free (openvas_config);
3559  g_free (osp_config);
3560  g_free (openvas_scanner));
3561 
3562  xml = g_string_new ("<new_task>");
3563 
3564  g_string_append_printf (xml,
3565  "<alert_id>%s</alert_id>"
3566  "<config_id>%s</config_id>"
3567  "<osp_config_id>%s</osp_config_id>"
3568  "<scanner_id>%s</scanner_id>"
3569  "<osp_scanner_id>%s</osp_scanner_id>"
3570  "<schedule_id>%s</schedule_id>"
3571  "<target_id>%s</target_id>",
3572  alert ? alert : "",
3573  (openvas_config && strlen (openvas_config))
3574  ? openvas_config
3575  : "daba56c8-73ec-11df-a475-002264764cea",
3576  osp_config ? osp_config : "",
3577  openvas_scanner ? openvas_scanner : "",
3578  osp_scanner ? osp_scanner : "",
3579  schedule ? schedule : "",
3580  (target && strlen (target))
3581  ? target
3582  : "b493b7a8-7489-11df-a3ec-002264764cea");
3583 
3584  g_free (schedule);
3585  g_free (target);
3586  g_free (openvas_config);
3587  g_free (osp_config);
3588  g_free (openvas_scanner);
3589  g_free (osp_scanner);
3590 
3591  /* Get list of targets. */
3592  if (openvas_connection_sendf (&connection,
3593  "<get_targets"
3594  " filter=\"rows=-1 sort=name\"/>")
3595  == -1)
3596  {
3597  g_string_free (xml, TRUE);
3598  openvas_connection_close (&connection);
3599  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3600  return gsad_message (credentials,
3601  "Internal error", __FUNCTION__, __LINE__,
3602  "An internal error occurred while getting targets list. "
3603  "The current list of targets is not available. "
3604  "Diagnostics: Failure to send command to manager daemon.",
3605  "/omp?cmd=get_tasks", response_data);
3606  }
3607 
3608  if (read_string_c (&connection, &xml))
3609  {
3610  g_string_free (xml, TRUE);
3611  openvas_connection_close (&connection);
3612  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3613  return gsad_message (credentials,
3614  "Internal error", __FUNCTION__, __LINE__,
3615  "An internal error occurred while getting targets list. "
3616  "The current list of targets is not available. "
3617  "Diagnostics: Failure to receive response from manager daemon.",
3618  "/omp?cmd=get_tasks", response_data);
3619  }
3620 
3621  /* Get configs to select in new task UI. */
3622  if (openvas_connection_sendf (&connection,
3623  "<get_configs"
3624  " filter=\"rows=-1 sort=name\"/>")
3625  == -1)
3626  {
3627  g_string_free (xml, TRUE);
3628  openvas_connection_close (&connection);
3629  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3630  return gsad_message (credentials,
3631  "Internal error", __FUNCTION__, __LINE__,
3632  "An internal error occurred while getting config list. "
3633  "The current list of configs is not available. "
3634  "Diagnostics: Failure to send command to manager daemon.",
3635  "/omp?cmd=get_tasks", response_data);
3636  }
3637 
3638  if (read_string_c (&connection, &xml))
3639  {
3640  g_string_free (xml, TRUE);
3641  openvas_connection_close (&connection);
3642  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3643  return gsad_message (credentials,
3644  "Internal error", __FUNCTION__, __LINE__,
3645  "An internal error occurred while getting config list. "
3646  "The current list of configs is not available. "
3647  "Diagnostics: Failure to receive response from manager daemon.",
3648  "/omp?cmd=get_tasks", response_data);
3649  }
3650 
3651  if (command_enabled (credentials, "GET_ALERTS"))
3652  {
3653  /* Get alerts to select in new task UI. */
3654  if (openvas_connection_sendf (&connection,
3655  "<get_alerts"
3656  " filter=\"rows=-1 sort=name\"/>")
3657  == -1)
3658  {
3659  g_string_free (xml, TRUE);
3660  openvas_connection_close (&connection);
3661  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3662  return gsad_message (credentials,
3663  "Internal error", __FUNCTION__, __LINE__,
3664  "An internal error occurred while getting alert list. "
3665  "The current list of alerts is not available. "
3666  "Diagnostics: Failure to send command to manager daemon.",
3667  "/omp?cmd=get_tasks", response_data);
3668  }
3669 
3670  if (read_string_c (&connection, &xml))
3671  {
3672  g_string_free (xml, TRUE);
3673  openvas_connection_close (&connection);
3674  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3675  return gsad_message (credentials,
3676  "Internal error", __FUNCTION__, __LINE__,
3677  "An internal error occurred while getting alert list. "
3678  "The current list of alerts is not available. "
3679  "Diagnostics: Failure to receive response from manager daemon.",
3680  "/omp?cmd=get_tasks", response_data);
3681  }
3682  }
3683 
3684  if (command_enabled (credentials, "GET_SCHEDULES"))
3685  {
3686  /* Get schedules to select in new task UI. */
3687  if (openvas_connection_sendf
3688  (&connection,
3689  "<get_schedules"
3690  " filter=\"rows=-1 sort=name\"/>")
3691  == -1)
3692  {
3693  g_string_free (xml, TRUE);
3694  openvas_connection_close (&connection);
3695  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3696  return gsad_message (credentials,
3697  "Internal error", __FUNCTION__, __LINE__,
3698  "An internal error occurred while getting the schedule list. "
3699  "The current list of schedules is not available. "
3700  "Diagnostics: Failure to send command to manager daemon.",
3701  "/omp?cmd=get_tasks", response_data);
3702  }
3703 
3704  if (read_string_c (&connection, &xml))
3705  {
3706  g_string_free (xml, TRUE);
3707  openvas_connection_close (&connection);
3708  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3709  return gsad_message (credentials,
3710  "Internal error", __FUNCTION__, __LINE__,
3711  "An internal error occurred while getting the schedule list. "
3712  "The current list of schedules is not available. "
3713  "Diagnostics: Failure to receive response from manager daemon.",
3714  "/omp?cmd=get_tasks", response_data);
3715  }
3716  }
3717 
3718  if (command_enabled (credentials, "GET_SCANNERS"))
3719  {
3720  /* Get scanners to select in new task UI. */
3721  if (openvas_connection_sendf (&connection,
3722  "<get_scanners"
3723  " filter=\"rows=-1\"/>")
3724  == -1)
3725  {
3726  g_string_free (xml, TRUE);
3727  openvas_connection_close (&connection);
3728  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3729  return gsad_message (credentials,
3730  "Internal error", __FUNCTION__, __LINE__,
3731  "An internal error occurred while getting the"
3732  " scanner list. "
3733  "The current list of scanners is not available. "
3734  "Diagnostics: Failure to send command to manager"
3735  " daemon.",
3736  "/omp?cmd=get_tasks", response_data);
3737  }
3738 
3739  if (read_string_c (&connection, &xml))
3740  {
3741  g_string_free (xml, TRUE);
3742  openvas_connection_close (&connection);
3743  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3744  return gsad_message (credentials,
3745  "Internal error", __FUNCTION__, __LINE__,
3746  "An internal error occurred while getting"
3747  " the scanner list."
3748  "The current list of scanners is not available. "
3749  "Diagnostics: Failure to receive response from"
3750  " manager daemon.",
3751  "/omp?cmd=get_tasks", response_data);
3752  }
3753  }
3754 
3755  if (command_enabled (credentials, "GET_GROUPS"))
3756  {
3757  /* Get groups for Observer Groups. */
3758 
3759  if (openvas_connection_sendf (&connection,
3760  "<get_groups/>")
3761  == -1)
3762  {
3763  g_string_free (xml, TRUE);
3764  openvas_connection_close (&connection);
3765  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3766  return gsad_message (credentials,
3767  "Internal error", __FUNCTION__, __LINE__,
3768  "An internal error occurred while getting group list. "
3769  "The current list of groups is not available. "
3770  "Diagnostics: Failure to send command to manager daemon.",
3771  "/omp?cmd=get_tasks", response_data);
3772  }
3773 
3774  if (read_string_c (&connection, &xml))
3775  {
3776  g_string_free (xml, TRUE);
3777  openvas_connection_close (&connection);
3778  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3779  return gsad_message (credentials,
3780  "Internal error", __FUNCTION__, __LINE__,
3781  "An internal error occurred while getting group list. "
3782  "The current list of groups is not available. "
3783  "Diagnostics: Failure to receive response from manager daemon.",
3784  "/omp?cmd=get_tasks", response_data);
3785  }
3786  }
3787 
3788  if (command_enabled (credentials, "GET_TAGS"))
3789  {
3790  /* Get tag names. */
3791 
3792  if (openvas_connection_sendf (&connection,
3793  "<get_tags names_only=\"1\""
3794  " filter=\"resource_type=task rows=-1\"/>")
3795  == -1)
3796  {
3797  g_string_free (xml, TRUE);
3798  openvas_connection_close (&connection);
3799  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3800  return gsad_message (credentials,
3801  "Internal error", __FUNCTION__, __LINE__,
3802  "An internal error occurred while getting tag list. "
3803  "The current list of tags is not available. "
3804  "Diagnostics: Failure to send command to manager daemon.",
3805  "/omp?cmd=get_tasks", response_data);
3806  }
3807 
3808  if (read_string_c (&connection, &xml))
3809  {
3810  g_string_free (xml, TRUE);
3811  openvas_connection_close (&connection);
3812  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
3813  return gsad_message (credentials,
3814  "Internal error", __FUNCTION__, __LINE__,
3815  "An internal error occurred while getting tag list. "
3816  "The current list of tags is not available. "
3817  "Diagnostics: Failure to receive response from manager daemon.",
3818  "/omp?cmd=get_tasks", response_data);
3819  }
3820  }
3821 
3822  if (extra_xml)
3823  g_string_append (xml, extra_xml);
3824  if (message)
3825  g_string_append_printf (xml, GSAD_MESSAGE_INVALID, message, "Create Task");
3826  g_string_append_printf (xml,
3827  "<user>%s</user>"
3828  "<apply_overrides>%i</apply_overrides>"
3829  "<alerts>%s</alerts>"
3830  "</new_task>",
3831  credentials->username,
3832  apply_overrides,
3833  alerts ? alerts : "1");
3834 
3835  openvas_connection_close (&connection);
3836  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
3837  response_data);
3838 }
3839 
3849 char *
3850 new_task_omp (credentials_t * credentials, params_t *params,
3851  cmd_response_data_t* response_data)
3852 {
3853  return new_task (credentials, NULL, params, NULL, response_data);
3854 }
3855 
3867 static char *
3868 new_container_task (credentials_t * credentials, const char *message,
3869  params_t *params, const char *extra_xml,
3870  cmd_response_data_t* response_data)
3871 {
3872  GString *xml;
3873 
3874  xml = g_string_new ("<new_container_task>");
3875 
3876  if (extra_xml)
3877  g_string_append (xml, extra_xml);
3878 
3879  if (message)
3880  g_string_append_printf (xml, GSAD_MESSAGE_INVALID, message, "Create Task");
3881 
3882  g_string_append_printf (xml, "</new_container_task>");
3883 
3884  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
3885  response_data);
3886 }
3887 
3897 char *
3899  cmd_response_data_t* response_data)
3900 {
3901  return new_container_task (credentials, NULL, params, NULL, response_data);
3902 }
3903 
3914 static char *
3915 upload_report (credentials_t *credentials, params_t *params,
3916  const char *extra_xml, cmd_response_data_t* response_data)
3917 {
3918  GString *xml;
3919 
3920  xml = g_string_new ("<upload_report>");
3921  if (extra_xml)
3922  g_string_append (xml, extra_xml);
3923 
3924  if (command_enabled (credentials, "GET_TASKS"))
3925  {
3926  gchar *response;
3927 
3928  if (simple_ompf ("getting Tasks", credentials, &response, response_data,
3929  "<get_tasks"
3930  /* All container tasks. */
3931  " filter=\"target= rows=-1 owner=any permission=any\"/>"))
3932  {
3933  g_string_free (xml, TRUE);
3934  return response;
3935  }
3936 
3937  g_string_append (xml, response);
3938  }
3939 
3940  g_string_append (xml, "</upload_report>");
3941 
3942  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
3943  response_data);
3944 }
3945 
3955 char *
3957  cmd_response_data_t* response_data)
3958 {
3959  return upload_report (credentials, params, NULL, response_data);
3960 }
3961 
3971 char *
3972 create_report_omp (credentials_t * credentials, params_t *params,
3973  cmd_response_data_t* response_data)
3974 {
3975  entity_t entity;
3976  int ret;
3977  gchar *command, *html, *response;
3978  const char *no_redirect, *cmd, *task_id, *name, *comment, *xml_file;
3979  const char *in_assets;
3980 
3981  no_redirect = params_value (params, "no_redirect");
3982  task_id = params_value (params, "task_id");
3983  xml_file = params_value (params, "xml_file");
3984  name = params_value (params, "name");
3985  comment = params_value (params, "comment");
3986  in_assets = params_value (params, "in_assets");
3987 
3988  if (task_id == NULL)
3989  {
3990  CHECK_PARAM_INVALID (name, "Create Report", "new_container_task");
3991  CHECK_PARAM_INVALID (comment, "Create Report", "new_container_task");
3992  }
3993  CHECK_PARAM_INVALID (xml_file, "Create Report", "new_container_task");
3994 
3995  if (params_given (params, "in_assets"))
3996  CHECK_PARAM_INVALID (xml_file, "Create Report", "new_container_task");
3997 
3998  if (strlen (xml_file) == 0)
3999  {
4000  if (task_id)
4001  return message_invalid (credentials, params, response_data,
4002  "Report required",
4003  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
4004  "Create Report", "new_container_task");
4005 
4006  /* Create only the container task. */
4007 
4008  command = g_strdup_printf ("<create_task>"
4009  "<target id=\"0\"/>"
4010  "<name>%s</name>"
4011  "<comment>%s</comment>"
4012  "</create_task>",
4013  name,
4014  comment);
4015  }
4016  else
4017  {
4018  gchar **xml_file_array, *xml_file_escaped;
4019 
4020  xml_file_array = g_strsplit (xml_file, "%", -1);
4021  if (xml_file_array != NULL && xml_file_array[0] != NULL)
4022  xml_file_escaped = g_strjoinv ("%%", xml_file_array);
4023  else
4024  xml_file_escaped = g_strdup (xml_file);
4025  g_strfreev (xml_file_array);
4026 
4027  if (task_id)
4028  command = g_strdup_printf ("<create_report>"
4029  "<in_assets>%s</in_assets>"
4030  "<task id=\"%s\"/>"
4031  "%s"
4032  "</create_report>",
4033  in_assets ? in_assets : "0",
4034  task_id ? task_id : "0",
4035  xml_file_escaped ? xml_file_escaped : "");
4036  else
4037  command = g_strdup_printf ("<create_report>"
4038  "<in_assets>%s</in_assets>"
4039  "<task>"
4040  "<name>%s</name>"
4041  "<comment>%s</comment>"
4042  "</task>"
4043  "%s"
4044  "</create_report>",
4045  in_assets ? in_assets : "",
4046  name,
4047  comment,
4048  xml_file_escaped);
4049  g_free (xml_file_escaped);
4050  }
4051 
4052  ret = omp (credentials,
4053  &response,
4054  &entity,
4055  response_data,
4056  command);
4057  g_free (command);
4058 
4059  switch (ret)
4060  {
4061  case 0:
4062  break;
4063  case -1:
4064  /* 'omp' set response. */
4065  return response;
4066  case 1:
4067  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4068  return gsad_message (credentials,
4069  "Internal error", __FUNCTION__, __LINE__,
4070  "An internal error occurred while creating a new report. "
4071  "No new report was created. "
4072  "Diagnostics: Failure to send command to manager daemon.",
4073  "/omp?cmd=get_tasks", response_data);
4074  case 2:
4075  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4076  return gsad_message (credentials,
4077  "Internal error", __FUNCTION__, __LINE__,
4078  "An internal error occurred while creating a new report. "
4079  "It is unclear whether the report has been created or not. "
4080  "Diagnostics: Failure to receive response from manager daemon.",
4081  "/omp?cmd=get_tasks", response_data);
4082  default:
4083  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4084  return gsad_message (credentials,
4085  "Internal error", __FUNCTION__, __LINE__,
4086  "An internal error occurred while creating a new report. "
4087  "It is unclear whether the report has been created or not. "
4088  "Diagnostics: Internal Error.",
4089  "/omp?cmd=get_tasks", response_data);
4090  }
4091 
4092  cmd = params_value (params, "cmd");
4093  html = response_from_entity (credentials, params, entity,
4094  (no_redirect && strcmp (no_redirect, "0")),
4095  NULL, "get_tasks",
4096  NULL,
4097  (cmd && strcmp (cmd, "import_report"))
4098  ? "new_container_task" : "upload_report",
4099  "Import Report", response_data);
4100  free_entity (entity);
4101  g_free (response);
4102  return html;
4103 }
4104 
4114 char *
4115 import_report_omp (credentials_t * credentials, params_t *params,
4116  cmd_response_data_t* response_data)
4117 {
4118  return create_report_omp (credentials, params, response_data);
4119 }
4120 
4121 #define CHECK(name) \
4122 CHECK_PARAM_INVALID (name, "Create Task", "new_task")
4123 
4133 char *
4135  cmd_response_data_t* response_data)
4136 {
4137  entity_t entity;
4138  int ret;
4139  gchar *command, *html, *response;
4140  const char *no_redirect, *name, *comment;
4141 
4142  no_redirect = params_value (params, "no_redirect");
4143  name = params_value (params, "name");
4144  comment = params_value (params, "comment");
4145  CHECK_PARAM_INVALID (name, "Create Container Task", "new_container_task");
4146  CHECK_PARAM_INVALID (comment, "Create Container Task", "new_container_task");
4147 
4148  command = g_strdup_printf ("<create_task>"
4149  "<target id=\"0\"/>"
4150  "<name>%s</name>"
4151  "<comment>%s</comment>"
4152  "</create_task>",
4153  name,
4154  comment);
4155  ret = omp (credentials,
4156  &response,
4157  &entity,
4158  response_data,
4159  command);
4160  g_free (command);
4161 
4162  switch (ret)
4163  {
4164  case 0:
4165  break;
4166  case -1:
4167  /* 'omp' set response. */
4168  return response;
4169  case 1:
4170  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4171  return gsad_message (credentials,
4172  "Internal error", __FUNCTION__, __LINE__,
4173  "An internal error occurred while creating a container task. "
4174  "No task was created. "
4175  "Diagnostics: Failure to send command to manager daemon.",
4176  "/omp?cmd=get_tasks", response_data);
4177  case 2:
4178  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4179  return gsad_message (credentials,
4180  "Internal error", __FUNCTION__, __LINE__,
4181  "An internal error occurred while creating a container task. "
4182  "It is unclear whether the task has been created or not. "
4183  "Diagnostics: Failure to receive response from manager daemon.",
4184  "/omp?cmd=get_tasks", response_data);
4185  default:
4186  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4187  return gsad_message (credentials,
4188  "Internal error", __FUNCTION__, __LINE__,
4189  "An internal error occurred while creating a container task. "
4190  "It is unclear whether the task has been created or not. "
4191  "Diagnostics: Internal Error.",
4192  "/omp?cmd=get_tasks", response_data);
4193  }
4194 
4195  if (entity_attribute (entity, "id"))
4196  params_add (params, "task_id", entity_attribute (entity, "id"));
4197  html = response_from_entity (credentials, params, entity,
4198  (no_redirect && strcmp (no_redirect, "0")),
4199  NULL, "get_tasks",
4200  NULL, "new_container_task",
4201  "Create Container Task", response_data);
4202  free_entity (entity);
4203  g_free (response);
4204  return html;
4205 }
4206 
4216 char *
4217 create_task_omp (credentials_t * credentials, params_t *params,
4218  cmd_response_data_t* response_data)
4219 {
4220  entity_t entity;
4221  int ret;
4222  gchar *schedule_element, *command;
4223  gchar *response, *html;
4224  const char *no_redirect;
4225  const char *name, *comment, *config_id, *target_id, *scanner_type;
4226  const char *scanner_id, *schedule_id, *schedule_periods;
4227  const char *max_checks, *max_hosts;
4228  const char *in_assets, *hosts_ordering, *alterable, *source_iface;
4229  const char *add_tag, *tag_name, *tag_value, *auto_delete, *auto_delete_data;
4230  const char *apply_overrides, *min_qod;
4231  params_t *alerts;
4232  GString *alert_element;
4233 
4234  no_redirect = params_value (params, "no_redirect");
4235  name = params_value (params, "name");
4236  comment = params_value (params, "comment");
4237  target_id = params_value (params, "target_id");
4238  scanner_type = params_value (params, "scanner_type");
4239  hosts_ordering = params_value (params, "hosts_ordering");
4240  schedule_id = params_value (params, "schedule_id");
4241  schedule_periods = params_value (params, "schedule_periods");
4242  scanner_id = params_value (params, "scanner_id");
4243  config_id = params_value (params, "config_id");
4244  in_assets = params_value (params, "in_assets");
4245  apply_overrides = params_value (params, "apply_overrides");
4246  min_qod = params_value (params, "min_qod");
4247  max_checks = params_value (params, "max_checks");
4248  source_iface = params_value (params, "source_iface");
4249  auto_delete = params_value (params, "auto_delete");
4250  auto_delete_data = params_value (params, "auto_delete_data");
4251  max_hosts = params_value (params, "max_hosts");
4252  alterable = params_value (params, "alterable");
4253  add_tag = params_value (params, "add_tag");
4254  tag_name = params_value (params, "tag_name");
4255  tag_value = params_value (params, "tag_value");
4256  CHECK (scanner_type);
4257  if (!strcmp (scanner_type, "1"))
4258  {
4259  hosts_ordering = "";
4260  max_checks = "";
4261  source_iface = "";
4262  max_hosts = "";
4263  }
4264  else if (!strcmp (scanner_type, "3"))
4265  {
4266  config_id = "";
4267  hosts_ordering = "";
4268  max_checks = "";
4269  source_iface = "";
4270  max_hosts = "";
4271  }
4272 
4273  CHECK (name);
4274  CHECK (comment);
4275  CHECK (config_id);
4276  CHECK (target_id);
4277  CHECK (hosts_ordering);
4278  CHECK (scanner_id);
4279  CHECK (schedule_id);
4280  if (params_given (params, "schedule_periods"))
4281  {
4282  CHECK (schedule_periods);
4283  }
4284  else
4285  schedule_periods = "0";
4286  CHECK (in_assets);
4287  if (!strcmp (in_assets, "1"))
4288  {
4289  CHECK (apply_overrides);
4290  CHECK (min_qod);
4291  }
4292  else
4293  {
4294  if (!params_given (params, "apply_overrides")
4295  || !params_valid (params, "apply_overrides"))
4296  apply_overrides = "";
4297 
4298  if (!params_given (params, "min_qod")
4299  || !params_valid (params, "min_qod"))
4300  min_qod = "";
4301  }
4302  CHECK (max_checks);
4303  CHECK (source_iface);
4304  CHECK (auto_delete);
4305  CHECK (auto_delete_data);
4306  CHECK (max_hosts);
4307  CHECK (alterable);
4308  if (add_tag)
4309  {
4310  CHECK (add_tag);
4311  CHECK (tag_name);
4312  CHECK (tag_value);
4313  }
4314 
4315  if (schedule_id == NULL || strcmp (schedule_id, "0") == 0)
4316  schedule_element = g_strdup ("");
4317  else
4318  schedule_element = g_strdup_printf ("<schedule id=\"%s\"/>", schedule_id);
4319 
4320  alert_element = g_string_new ("");
4321  if (params_given (params, "alert_id_optional:"))
4322  alerts = params_values (params, "alert_id_optional:");
4323  else
4324  alerts = params_values (params, "alert_ids:");
4325 
4326  if (alerts)
4327  {
4328  params_iterator_t iter;
4329  char *name;
4330  param_t *param;
4331 
4332  params_iterator_init (&iter, alerts);
4333  while (params_iterator_next (&iter, &name, &param))
4334  if (param->value && strcmp (param->value, "0"))
4335  g_string_append_printf (alert_element,
4336  "<alert id=\"%s\"/>",
4337  param->value ? param->value : "");
4338  }
4339 
4340  command = g_strdup_printf ("<create_task>"
4341  "<config id=\"%s\"/>"
4342  "<schedule_periods>%s</schedule_periods>"
4343  "%s%s"
4344  "<target id=\"%s\"/>"
4345  "<scanner id=\"%s\"/>"
4346  "<hosts_ordering>%s</hosts_ordering>"
4347  "<name>%s</name>"
4348  "<comment>%s</comment>"
4349  "<preferences>"
4350  "<preference>"
4351  "<scanner_name>max_checks</scanner_name>"
4352  "<value>%s</value>"
4353  "</preference>"
4354  "<preference>"
4355  "<scanner_name>max_hosts</scanner_name>"
4356  "<value>%s</value>"
4357  "</preference>"
4358  "<preference>"
4359  "<scanner_name>in_assets</scanner_name>"
4360  "<value>%s</value>"
4361  "</preference>"
4362  "<preference>"
4363  "<scanner_name>"
4364  "assets_apply_overrides"
4365  "</scanner_name>"
4366  "<value>%s</value>"
4367  "</preference>"
4368  "<preference>"
4369  "<scanner_name>assets_min_qod</scanner_name>"
4370  "<value>%s</value>"
4371  "</preference>"
4372  "<preference>"
4373  "<scanner_name>source_iface</scanner_name>"
4374  "<value>%s</value>"
4375  "</preference>"
4376  "<preference>"
4377  "<scanner_name>auto_delete</scanner_name>"
4378  "<value>%s</value>"
4379  "</preference>"
4380  "<preference>"
4381  "<scanner_name>auto_delete_data</scanner_name>"
4382  "<value>%s</value>"
4383  "</preference>"
4384  "</preferences>"
4385  "<alterable>%i</alterable>"
4386  "</create_task>",
4387  config_id,
4388  schedule_periods,
4389  schedule_element,
4390  alert_element->str,
4391  target_id,
4392  scanner_id,
4393  hosts_ordering,
4394  name,
4395  comment,
4396  max_checks,
4397  max_hosts,
4398  strcmp (in_assets, "0") ? "yes" : "no",
4399  strcmp (apply_overrides, "0") ? "yes" : "no",
4400  min_qod,
4401  source_iface,
4402  auto_delete,
4403  auto_delete_data,
4404  alterable ? strcmp (alterable, "0") : 0);
4405 
4406  ret = omp (credentials,
4407  &response,
4408  &entity,
4409  response_data,
4410  command);
4411  g_free (command);
4412 
4413  g_free (schedule_element);
4414  g_string_free (alert_element, TRUE);
4415 
4416  switch (ret)
4417  {
4418  case 0:
4419  break;
4420  case -1:
4421  /* 'omp' set response. */
4422  return response;
4423  case 1:
4424  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4425  return gsad_message (credentials,
4426  "Internal error", __FUNCTION__, __LINE__,
4427  "An internal error occurred while creating a new task. "
4428  "No new task was created. "
4429  "Diagnostics: Failure to send command to manager daemon.",
4430  "/omp?cmd=get_tasks", response_data);
4431  case 2:
4432  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4433  return gsad_message (credentials,
4434  "Internal error", __FUNCTION__, __LINE__,
4435  "An internal error occurred while creating a new task. "
4436  "It is unclear whether the task has been created or not. "
4437  "Diagnostics: Failure to receive response from manager daemon.",
4438  "/omp?cmd=get_tasks", response_data);
4439  default:
4440  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4441  return gsad_message (credentials,
4442  "Internal error", __FUNCTION__, __LINE__,
4443  "An internal error occurred while creating a new task. "
4444  "It is unclear whether the task has been created or not. "
4445  "Diagnostics: Internal Error.",
4446  "/omp?cmd=get_tasks", response_data);
4447  }
4448 
4449  if (omp_success (entity))
4450  {
4451  if (add_tag && strcmp (add_tag, "0"))
4452  {
4453  const char *new_task_id = entity_attribute (entity, "id");
4454  gchar *tag_command, *tag_response;
4455  entity_t tag_entity;
4456 
4457  if (tag_value && strcmp (tag_value, ""))
4458  tag_command
4459  = g_markup_printf_escaped ("<create_tag>"
4460  "<name>%s</name>"
4461  "<resource id=\"%s\">"
4462  "<type>task</type>"
4463  "</resource>"
4464  "<value>%s</value>"
4465  "</create_tag>",
4466  tag_name,
4467  new_task_id,
4468  tag_value);
4469  else
4470  tag_command
4471  = g_markup_printf_escaped ("<create_tag>"
4472  "<name>%s</name>"
4473  "<resource id=\"%s\">"
4474  "<type>task</type>"
4475  "</resource>"
4476  "</create_tag>",
4477  tag_name,
4478  new_task_id);
4479 
4480  ret = omp (credentials,
4481  &tag_response,
4482  &tag_entity,
4483  response_data,
4484  tag_command);
4485 
4486  switch (ret)
4487  {
4488  case 0:
4489  case -1:
4490  break;
4491  case 1:
4492  free_entity (entity);
4493  g_free (response);
4494  response_data->http_status_code
4495  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4496  return gsad_message (credentials,
4497  "Internal error", __FUNCTION__, __LINE__,
4498  "An internal error occurred while creating a new tag. "
4499  "No new tag was created. "
4500  "Diagnostics: Failure to send command to manager daemon.",
4501  "/omp?cmd=get_tasks", response_data);
4502  case 2:
4503  free_entity (entity);
4504  g_free (response);
4505  response_data->http_status_code
4506  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4507  return gsad_message (credentials,
4508  "Internal error", __FUNCTION__, __LINE__,
4509  "An internal error occurred while creating a new tag. "
4510  "It is unclear whether the tag has been created or not. "
4511  "Diagnostics: Failure to receive response from manager daemon.",
4512  "/omp?cmd=get_tasks", response_data);
4513  default:
4514  free_entity (entity);
4515  g_free (response);
4516  response_data->http_status_code
4517  = MHD_HTTP_INTERNAL_SERVER_ERROR;
4518  return gsad_message (credentials,
4519  "Internal error", __FUNCTION__, __LINE__,
4520  "An internal error occurred while creating a new task. "
4521  "It is unclear whether the tag has been created or not. "
4522  "Diagnostics: Internal Error.",
4523  "/omp?cmd=get_tasks", response_data);
4524  }
4525 
4526  if (entity_attribute (entity, "id"))
4527  params_add (params, "task_id", entity_attribute (entity, "id"));
4528  html
4529  = response_from_entity (credentials, params, tag_entity,
4530  (no_redirect && strcmp (no_redirect, "0")),
4531  NULL, "get_tasks",
4532  NULL, "new_tasks",
4533  "Create Task and Tag", response_data);
4534  free_entity (tag_entity);
4535  g_free (tag_response);
4536  }
4537  else
4538  {
4539  if (entity_attribute (entity, "id"))
4540  params_add (params, "task_id", entity_attribute (entity, "id"));
4541  html
4542  = response_from_entity (credentials, params, entity,
4543  (no_redirect && strcmp (no_redirect, "0")),
4544  NULL, "get_tasks",
4545  NULL, "new_task",
4546  "Create Task", response_data);
4547  }
4548  }
4549  else
4550  {
4551  html = response_from_entity (credentials, params, entity,
4552  (no_redirect && strcmp (no_redirect, "0")),
4553  NULL, "get_tasks",
4554  NULL, "new_task",
4555  "Create Task", response_data);
4556  }
4557  free_entity (entity);
4558  g_free (response);
4559  return html;
4560 }
4561 
4562 
4572 char *
4573 delete_task_omp (credentials_t * credentials, params_t *params,
4574  cmd_response_data_t* response_data)
4575 {
4576  return delete_resource ("task", credentials, params, 0, NULL, response_data);
4577 }
4578 
4589 char *
4590 edit_task (credentials_t * credentials, params_t *params, const char *extra_xml,
4591  cmd_response_data_t* response_data)
4592 {
4593  GString *xml;
4594  openvas_connection_t connection;
4595  gchar *html;
4596  const char *task_id, *next, *refresh_interval, *sort_field, *sort_order;
4597  const char *overrides, *alerts;
4598  int apply_overrides;
4599 
4600  task_id = params_value (params, "task_id");
4601  next = params_value (params, "next");
4602  refresh_interval = params_value (params, "refresh_interval");
4603  sort_field = params_value (params, "sort_field");
4604  sort_order = params_value (params, "sort_order");
4605  overrides = params_value (params, "overrides");
4606  alerts = params_value (params, "alerts");
4607 
4608  apply_overrides = overrides ? strcmp (overrides, "0") : 1;
4609 
4610  if (task_id == NULL)
4611  {
4612  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
4613  return gsad_message (credentials,
4614  "Internal error", __FUNCTION__, __LINE__,
4615  "An internal error occurred while editing a task. "
4616  "The task remains as it was. "
4617  "Diagnostics: Required parameter was NULL.",
4618  "/omp?cmd=get_tasks", response_data);
4619  }
4620 
4621  if (next == NULL)
4622  next = "get_task";
4623 
4624  switch (manager_connect (credentials, &connection, &html,
4625  response_data))
4626  {
4627  case 0:
4628  break;
4629  case -1:
4630  if (html)
4631  return html;
4632  /* Fall through. */
4633  default:
4634  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4635  return gsad_message (credentials,
4636  "Internal error", __FUNCTION__, __LINE__,
4637  "An internal error occurred while editing a task. "
4638  "The task remains as it was. "
4639  "Diagnostics: Failure to connect to manager daemon.",
4640  "/omp?cmd=get_tasks", response_data);
4641  }
4642 
4643  if (openvas_connection_sendf (&connection,
4644  "<commands>"
4645  "<get_tasks task_id=\"%s\" details=\"1\" />"
4646  "<get_targets"
4647  " filter=\"rows=-1 sort=name\"/>"
4648  "<get_configs"
4649  " filter=\"rows=-1 sort=name\"/>"
4650  "%s"
4651  "%s"
4652  "%s"
4653  "%s"
4654  "</commands>",
4655  task_id,
4656  command_enabled (credentials, "GET_ALERTS")
4657  ? "<get_alerts"
4658  " filter=\"rows=-1 sort=name\"/>"
4659  : "",
4660  command_enabled (credentials, "GET_SCHEDULES")
4661  ? "<get_schedules"
4662  " filter=\"rows=-1 sort=name\"/>"
4663  : "",
4664  command_enabled (credentials, "GET_SCANNERS")
4665  ? "<get_scanners"
4666  " filter=\"rows=-1\"/>"
4667  : "",
4668  command_enabled (credentials, "GET_GROUPS")
4669  ? "<get_groups/>"
4670  : "")
4671  == -1)
4672  {
4673  openvas_connection_close (&connection);
4674  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4675  return gsad_message (credentials,
4676  "Internal error", __FUNCTION__, __LINE__,
4677  "An internal error occurred while getting task info. "
4678  "Diagnostics: Failure to send command to manager daemon.",
4679  "/omp?cmd=get_tasks", response_data);
4680  }
4681 
4682  xml = g_string_new ("");
4683 
4684  if (extra_xml)
4685  g_string_append (xml, extra_xml);
4686 
4687  g_string_append_printf (xml,
4688  "<edit_task>"
4689  "<task id=\"%s\"/>"
4690  "<user>%s</user>"
4691  "<alerts>%s</alerts>"
4692  /* Page that follows. */
4693  "<next>%s</next>"
4694  /* Passthroughs. */
4695  "<refresh_interval>%s</refresh_interval>"
4696  "<sort_field>%s</sort_field>"
4697  "<sort_order>%s</sort_order>"
4698  "<apply_overrides>%i</apply_overrides>",
4699  task_id,
4700  credentials->username,
4701  alerts ? alerts : "1",
4702  next,
4703  refresh_interval ? refresh_interval : "",
4704  sort_field,
4705  sort_order,
4706  apply_overrides);
4707 
4708  if (read_string_c (&connection, &xml))
4709  {
4710  g_string_free (xml, TRUE);
4711  openvas_connection_close (&connection);
4712  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4713  return gsad_message (credentials,
4714  "Internal error", __FUNCTION__, __LINE__,
4715  "An internal error occurred while getting task info. "
4716  "Diagnostics: Failure to receive response from manager daemon.",
4717  "/omp?cmd=get_tasks", response_data);
4718  }
4719 
4720  /* Cleanup, and return transformed XML. */
4721 
4722  g_string_append (xml, "</edit_task>");
4723  openvas_connection_close (&connection);
4724  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
4725  response_data);
4726 }
4727 
4737 char *
4738 edit_task_omp (credentials_t * credentials, params_t *params,
4739  cmd_response_data_t* response_data)
4740 {
4741  return edit_task (credentials, params, NULL, response_data);
4742 }
4743 
4753 char *
4754 save_task_omp (credentials_t * credentials, params_t *params,
4755  cmd_response_data_t* response_data)
4756 {
4757  gchar *html, *response, *format;
4758  const char *no_redirect;
4759  const char *comment, *name, *schedule_id, *in_assets;
4760  const char *scanner_id, *task_id, *max_checks, *max_hosts;
4761  const char *config_id, *target_id, *hosts_ordering, *alterable, *source_iface;
4762  const char *scanner_type, *schedule_periods, *auto_delete, *auto_delete_data;
4763  const char *apply_overrides, *min_qod;
4764  int ret;
4765  params_t *alerts;
4766  GString *alert_element;
4767  entity_t entity;
4768 
4769  no_redirect = params_value (params, "no_redirect");
4770  comment = params_value (params, "comment");
4771  name = params_value (params, "name");
4772  task_id = params_value (params, "task_id");
4773  in_assets = params_value (params, "in_assets");
4774  apply_overrides = params_value (params, "apply_overrides");
4775  min_qod = params_value (params, "min_qod");
4776  target_id = params_value (params, "target_id");
4777  scanner_type = params_value (params, "scanner_type");
4778  hosts_ordering = params_value (params, "hosts_ordering");
4779  config_id = params_value (params, "config_id");
4780  schedule_id = params_value (params, "schedule_id");
4781  schedule_periods = params_value (params, "schedule_periods");
4782  scanner_id = params_value (params, "scanner_id");
4783  max_checks = params_value (params, "max_checks");
4784  source_iface = params_value (params, "source_iface");
4785  auto_delete = params_value (params, "auto_delete");
4786  auto_delete_data = params_value (params, "auto_delete_data");
4787  max_hosts = params_value (params, "max_hosts");
4788  alterable = params_value (params, "alterable");
4789  CHECK_PARAM_INVALID (scanner_type, "Save Task", "edit_task");
4790  if (!strcmp (scanner_type, "1"))
4791  {
4792  hosts_ordering = "";
4793  max_checks = "";
4794  source_iface = "";
4795  max_hosts = "";
4796  }
4797  else if (!strcmp (scanner_type, "3"))
4798  {
4799  config_id = "0";
4800  hosts_ordering = "";
4801  max_checks = "";
4802  source_iface = "";
4803  max_hosts = "";
4804  }
4805 
4806  CHECK_PARAM_INVALID (name, "Save Task", "edit_task");
4807  CHECK_PARAM_INVALID (comment, "Save Task", "edit_task");
4808  CHECK_PARAM_INVALID (target_id, "Save Task", "edit_task");
4809  CHECK_PARAM_INVALID (hosts_ordering, "Save Task", "edit_task");
4810  CHECK_PARAM_INVALID (config_id, "Save Task", "edit_task");
4811  CHECK_PARAM_INVALID (schedule_id, "Save Task", "edit_task");
4812  if (params_given (params, "schedule_periods"))
4813  {
4814  CHECK (schedule_periods);
4815  }
4816  else
4817  schedule_periods = "0";
4818  CHECK_PARAM_INVALID (scanner_id, "Save Task", "edit_task");
4819  CHECK_PARAM_INVALID (task_id, "Save Task", "edit_task");
4820  CHECK_PARAM_INVALID (max_checks, "Save Task", "edit_task");
4821  CHECK_PARAM_INVALID (source_iface, "Save Task", "edit_task");
4822  CHECK_PARAM_INVALID (auto_delete, "Save Task", "edit_task");
4823  CHECK_PARAM_INVALID (auto_delete_data, "Save Task", "edit_task");
4824  CHECK_PARAM_INVALID (max_hosts, "Save Task", "edit_task");
4825  CHECK_PARAM_INVALID (in_assets, "Save Task", "edit_task");
4826 
4827  if (!strcmp (in_assets, "1"))
4828  {
4829  CHECK_PARAM_INVALID (apply_overrides, "Save Task", "edit_task");
4830  CHECK_PARAM_INVALID (min_qod, "Save Task", "edit_task");
4831  }
4832  else
4833  {
4834  if (!params_given (params, "apply_overrides")
4835  || !params_valid (params, "apply_overrides"))
4836  apply_overrides = "";
4837 
4838  if (!params_given (params, "min_qod")
4839  || !params_valid (params, "min_qod"))
4840  min_qod = "";
4841  }
4842 
4843  alert_element = g_string_new ("");
4844  if (params_given (params, "alert_id_optional:"))
4845  alerts = params_values (params, "alert_id_optional:");
4846  else
4847  alerts = params_values (params, "alert_ids:");
4848 
4849  if (alerts)
4850  {
4851  params_iterator_t iter;
4852  char *name;
4853  param_t *param;
4854 
4855  params_iterator_init (&iter, alerts);
4856  while (params_iterator_next (&iter, &name, &param))
4857  {
4858  if (param->value && strcmp (param->value, "0"))
4859  g_string_append_printf (alert_element,
4860  "<alert id=\"%s\"/>",
4861  param->value ? param->value : "");
4862  }
4863  }
4864 
4865  // Remove Alerts from Task if none are given.
4866  if (strcmp (alert_element->str, "") == 0)
4867  g_string_append_printf (alert_element, "<alert id=\"0\"/>");
4868 
4869  format = g_strdup_printf ("<modify_task task_id=\"%%s\">"
4870  "<name>%%s</name>"
4871  "<comment>%%s</comment>"
4872  "<hosts_ordering>%s</hosts_ordering>"
4873  "%s"
4874  "<target id=\"%%s\"/>"
4875  "<config id=\"%%s\"/>"
4876  "<schedule id=\"%%s\"/>"
4877  "<schedule_periods>%%s</schedule_periods>"
4878  "<scanner id=\"%%s\"/>"
4879  "<preferences>"
4880  "<preference>"
4881  "<scanner_name>max_checks</scanner_name>"
4882  "<value>%%s</value>"
4883  "</preference>"
4884  "<preference>"
4885  "<scanner_name>max_hosts</scanner_name>"
4886  "<value>%%s</value>"
4887  "</preference>"
4888  "<preference>"
4889  "<scanner_name>in_assets</scanner_name>"
4890  "<value>%%s</value>"
4891  "</preference>"
4892  "<preference>"
4893  "<scanner_name>assets_apply_overrides</scanner_name>"
4894  "<value>%%s</value>"
4895  "</preference>"
4896  "<preference>"
4897  "<scanner_name>assets_min_qod</scanner_name>"
4898  "<value>%%s</value>"
4899  "</preference>"
4900  "<preference>"
4901  "<scanner_name>source_iface</scanner_name>"
4902  "<value>%%s</value>"
4903  "</preference>"
4904  "<preference>"
4905  "<scanner_name>auto_delete</scanner_name>"
4906  "<value>%%s</value>"
4907  "</preference>"
4908  "<preference>"
4909  "<scanner_name>auto_delete_data</scanner_name>"
4910  "<value>%%s</value>"
4911  "</preference>"
4912  "</preferences>"
4913  "%s%i%s"
4914  "</modify_task>",
4915  hosts_ordering,
4916  alert_element->str,
4917  alterable ? "<alterable>" : "",
4918  alterable ? strcmp (alterable, "0") : 0,
4919  alterable ? "</alterable>" : "");
4920  response = NULL;
4921  entity = NULL;
4922  ret = ompf (credentials,
4923  &response,
4924  &entity,
4925  response_data,
4926  format,
4927  task_id,
4928  name,
4929  comment,
4930  target_id,
4931  config_id,
4932  schedule_id,
4933  schedule_periods,
4934  scanner_id,
4935  max_checks,
4936  max_hosts,
4937  strcmp (in_assets, "0") ? "yes" : "no",
4938  strcmp (apply_overrides, "0") ? "yes" : "no",
4939  min_qod,
4940  source_iface,
4941  auto_delete,
4942  auto_delete_data);
4943  g_free (format);
4944 
4945  g_string_free (alert_element, TRUE);
4946 
4947  switch (ret)
4948  {
4949  case 0:
4950  case -1:
4951  break;
4952  case 1:
4953  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4954  return gsad_message (credentials,
4955  "Internal error", __FUNCTION__, __LINE__,
4956  "An internal error occurred while saving a task. "
4957  "The task was not saved. "
4958  "Diagnostics: Failure to send command to manager daemon.",
4959  "/omp?cmd=get_tasks", response_data);
4960  case 2:
4961  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4962  return gsad_message (credentials,
4963  "Internal error", __FUNCTION__, __LINE__,
4964  "An internal error occurred while saving a task. "
4965  "It is unclear whether the task has been saved or not. "
4966  "Diagnostics: Failure to receive response from manager daemon.",
4967  "/omp?cmd=get_tasks", response_data);
4968  default:
4969  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
4970  return gsad_message (credentials,
4971  "Internal error", __FUNCTION__, __LINE__,
4972  "An internal error occurred while saving a task. "
4973  "It is unclear whether the task has been saved or not. "
4974  "Diagnostics: Internal Error.",
4975  "/omp?cmd=get_tasks", response_data);
4976  }
4977 
4978  html = response_from_entity (credentials, params, entity,
4979  (no_redirect && strcmp (no_redirect, "0")),
4980  NULL, "get_tasks",
4981  NULL, "edit_task",
4982  "Save Task", response_data);
4983  free_entity (entity);
4984  g_free (response);
4985  return html;
4986 }
4987 
4988 #undef CHECK
4989 
4999 char *
5001  cmd_response_data_t* response_data)
5002 {
5003  gchar *format, *response, *html;
5004  const char *no_redirect, *comment, *name, *task_id;
5005  const char *in_assets, *auto_delete, *auto_delete_data;
5006  int ret;
5007  entity_t entity;
5008 
5009  no_redirect = params_value (params, "no_redirect");
5010  comment = params_value (params, "comment");
5011  in_assets = params_value (params, "in_assets");
5012  name = params_value (params, "name");
5013  task_id = params_value (params, "task_id");
5014  auto_delete = params_value (params, "auto_delete");
5015  auto_delete_data = params_value (params, "auto_delete_data");
5016  CHECK_PARAM_INVALID (name, "Save Task", "edit_task")
5017  CHECK_PARAM_INVALID (comment, "Save Task", "edit_task")
5018  CHECK_PARAM_INVALID (task_id, "Save Task", "edit_task")
5019  CHECK_PARAM_INVALID (in_assets, "Save Task", "edit_task")
5020  CHECK_PARAM_INVALID (auto_delete, "Save Task", "edit_task");
5021  CHECK_PARAM_INVALID (auto_delete_data, "Save Task", "edit_task");
5022 
5023  format = g_strdup_printf ("<modify_task task_id=\"%%s\">"
5024  "<name>%%s</name>"
5025  "<comment>%%s</comment>"
5026  "<preferences>"
5027  "<preference>"
5028  "<scanner_name>in_assets</scanner_name>"
5029  "<value>%%s</value>"
5030  "</preference>"
5031  "<preference>"
5032  "<scanner_name>auto_delete</scanner_name>"
5033  "<value>%%s</value>"
5034  "</preference>"
5035  "<preference>"
5036  "<scanner_name>auto_delete_data</scanner_name>"
5037  "<value>%%s</value>"
5038  "</preference>"
5039  "</preferences>"
5040  "</modify_task>");
5041 
5042  response = NULL;
5043  entity = NULL;
5044  ret = ompf (credentials, &response, &entity, response_data,
5045  format, task_id, name, comment,
5046  strcmp (in_assets, "0") ? "yes" : "no",
5047  auto_delete,
5048  auto_delete_data);
5049  g_free (format);
5050  switch (ret)
5051  {
5052  case 0:
5053  case -1:
5054  break;
5055  case 1:
5056  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5057  return gsad_message (credentials,
5058  "Internal error", __FUNCTION__, __LINE__,
5059  "An internal error occurred while saving a task. "
5060  "No new task was created. "
5061  "Diagnostics: Failure to send command to manager daemon.",
5062  "/omp?cmd=get_tasks", response_data);
5063  case 2:
5064  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5065  return gsad_message (credentials,
5066  "Internal error", __FUNCTION__, __LINE__,
5067  "An internal error occurred while saving a task. "
5068  "It is unclear whether the task has been created or not. "
5069  "Diagnostics: Failure to receive response from manager daemon.",
5070  "/omp?cmd=get_tasks", response_data);
5071  default:
5072  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5073  return gsad_message (credentials,
5074  "Internal error", __FUNCTION__, __LINE__,
5075  "An internal error occurred while saving a task. "
5076  "It is unclear whether the task has been created or not. "
5077  "Diagnostics: Internal Error.",
5078  "/omp?cmd=get_tasks", response_data);
5079  }
5080 
5081  html = response_from_entity (credentials, params, entity,
5082  (no_redirect && strcmp (no_redirect, "0")),
5083  NULL, "get_tasks",
5084  NULL, "edit_task",
5085  "Save Container Task", response_data);
5086  free_entity (entity);
5087  g_free (response);
5088  return html;
5089 }
5090 
5103 char *
5104 export_task_omp (credentials_t * credentials, params_t *params,
5105  enum content_type * content_type, char **content_disposition,
5106  gsize *content_length, cmd_response_data_t* response_data)
5107 {
5108  return export_resource ("task", credentials, params, content_type,
5109  content_disposition, content_length, response_data);
5110 }
5111 
5125 char *
5126 export_tasks_omp (credentials_t * credentials, params_t *params,
5127  enum content_type * content_type, char **content_disposition,
5128  gsize *content_length, cmd_response_data_t* response_data)
5129 {
5130  return export_many ("task", credentials, params, content_type,
5131  content_disposition, content_length, response_data);
5132 }
5133 
5143 char *
5144 stop_task_omp (credentials_t * credentials, params_t *params,
5145  cmd_response_data_t* response_data)
5146 {
5147  return resource_action (credentials, params, "task", "stop", response_data);
5148 }
5149 
5159 char *
5160 resume_task_omp (credentials_t * credentials, params_t *params,
5161  cmd_response_data_t* response_data)
5162 {
5163  return resource_action (credentials, params, "task", "resume", response_data);
5164 }
5165 
5175 char *
5176 start_task_omp (credentials_t * credentials, params_t *params,
5177  cmd_response_data_t* response_data)
5178 {
5179  return resource_action (credentials, params, "task", "start", response_data);
5180 }
5181 
5191 char *
5192 move_task_omp (credentials_t * credentials, params_t *params,
5193  cmd_response_data_t* response_data)
5194 {
5195  gchar *command, *response, *html;
5196  const char *no_redirect, *task_id, *slave_id;
5197  int ret;
5198  entity_t entity;
5199 
5200  no_redirect = params_value (params, "no_redirect");
5201  slave_id = params_value (params, "slave_id");
5202  task_id = params_value (params, "task_id");
5203 
5204  command = g_strdup_printf ("<move_task task_id=\"%s\" slave_id=\"%s\"/>",
5205  task_id ? task_id : "",
5206  slave_id ? slave_id : "");
5207 
5208  response = NULL;
5209  entity = NULL;
5210  ret = omp (credentials, &response, &entity, response_data, command);
5211  g_free (command);
5212  switch (ret)
5213  {
5214  case 0:
5215  break;
5216  case -1:
5217  /* 'omp' set response. */
5218  return response;
5219  case 1:
5220  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5221  return gsad_message (credentials,
5222  "Internal error", __FUNCTION__, __LINE__,
5223  "An internal error occurred while moving a task. "
5224  "The task was not moved. "
5225  "Diagnostics: Failure to send command to manager daemon.",
5226  "/omp?cmd=get_tasks", response_data);
5227  case 2:
5228  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5229  return gsad_message (credentials,
5230  "Internal error", __FUNCTION__, __LINE__,
5231  "An internal error occurred while moving a task. "
5232  "It is unclear whether the task has been moved or not. "
5233  "Diagnostics: Failure to receive response from manager daemon.",
5234  "/omp?cmd=get_tasks", response_data);
5235  default:
5236  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5237  return gsad_message (credentials,
5238  "Internal error", __FUNCTION__, __LINE__,
5239  "An internal error occurred while moving a task. "
5240  "It is unclear whether the task has been moved or not. "
5241  "Diagnostics: Internal Error.",
5242  "/omp?cmd=get_tasks", response_data);
5243  }
5244 
5245  html = response_from_entity (credentials, params, entity,
5246  (no_redirect && strcmp (no_redirect, "0")),
5247  NULL, "get_tasks",
5248  NULL, "get_tasks",
5249  "Move Task", response_data);
5250 
5251  free_entity (entity);
5252  g_free (response);
5253  return html;
5254 }
5255 
5267 static char*
5268 get_nvts (credentials_t *credentials, params_t *params, const char *commands,
5269  const char *extra_xml, cmd_response_data_t* response_data)
5270 {
5271  GString *xml = NULL;
5272  openvas_connection_t connection;
5273  gchar *html;
5274  const char *oid;
5275 
5276  oid = params_value (params, "oid");
5277  if (oid == NULL)
5278  {
5279  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5280  return gsad_message (credentials,
5281  "Internal error", __FUNCTION__, __LINE__,
5282  "An internal error occurred while getting an NVT. "
5283  "Diagnostics: Required parameter was NULL.",
5284  "/omp?cmd=get_tasks", response_data);
5285  }
5286  switch (manager_connect (credentials, &connection, &html,
5287  response_data))
5288  {
5289  case 0:
5290  break;
5291  case -1:
5292  if (html)
5293  return html;
5294  /* Fall through. */
5295  default:
5296  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5297  return gsad_message (credentials,
5298  "Internal error", __FUNCTION__, __LINE__,
5299  "An internal error occurred while getting nvt details. "
5300  "Diagnostics: Failure to connect to manager daemon.",
5301  "/omp?cmd=get_tasks", response_data);
5302  }
5303 
5304  if (openvas_connection_sendf (&connection,
5305  "<commands>"
5306  "%s"
5307  "<get_nvts"
5308  " nvt_oid=\"%s\""
5309  " details=\"1\""
5310  " preferences=\"1\"/>"
5311  "<get_notes"
5312  " nvt_oid=\"%s\""
5313  " sort_field=\"notes.text\"/>"
5314  "<get_overrides"
5315  " nvt_oid=\"%s\""
5316  " sort_field=\"overrides.text\"/>"
5317  "</commands>",
5318  commands ? commands : "",
5319  oid,
5320  oid,
5321  oid)
5322  == -1)
5323  {
5324  openvas_connection_close (&connection);
5325  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5326  return gsad_message (credentials,
5327  "Internal error", __FUNCTION__, __LINE__,
5328  "An internal error occurred while getting nvt details. "
5329  "Diagnostics: Failure to send command to manager daemon.",
5330  "/omp?cmd=get_tasks", response_data);
5331  }
5332 
5333  xml = g_string_new ("<get_nvts>");
5334  if (read_string_c (&connection, &xml))
5335  {
5336  openvas_connection_close (&connection);
5337  g_string_free (xml, TRUE);
5338  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5339  return gsad_message (credentials,
5340  "Internal error", __FUNCTION__, __LINE__,
5341  "An internal error occurred while getting nvt details. "
5342  "Diagnostics: Failure to receive response from manager daemon.",
5343  "/omp?cmd=get_tasks", response_data);
5344  }
5345 
5346  /* Append extra_xml */
5347  if (extra_xml)
5348  g_string_append (xml, extra_xml);
5349 
5350  /* Get tag names */
5351 
5352  if (openvas_connection_sendf (&connection,
5353  "<get_tags"
5354  " filter=\"resource_type=nvt"
5355  " first=1"
5356  " rows=-1\""
5357  " names_only=\"1\""
5358  "/>")
5359  == -1)
5360  {
5361  g_string_free (xml, TRUE);
5362  openvas_connection_close (&connection);
5363  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5364  return gsad_message (credentials,
5365  "Internal error", __FUNCTION__, __LINE__,
5366  "An internal error occurred while getting tag names list. "
5367  "The current list of resources is not available. "
5368  "Diagnostics: Failure to send command to manager daemon.",
5369  "/omp?cmd=get_resources", response_data);
5370  }
5371 
5372  if (read_string_c (&connection, &xml))
5373  {
5374  g_string_free (xml, TRUE);
5375  openvas_connection_close (&connection);
5376  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5377  return gsad_message (credentials,
5378  "Internal error", __FUNCTION__, __LINE__,
5379  "An internal error occurred while getting tag names list. "
5380  "The current list of resources is not available. "
5381  "Diagnostics: Failure to receive response from manager daemon.",
5382  "/omp?cmd=get_resources", response_data);
5383  }
5384 
5385  g_string_append (xml, "</get_nvts>");
5386 
5387  openvas_connection_close (&connection);
5388  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
5389  response_data);
5390 }
5391 
5402 char *
5403 get_info (credentials_t *credentials, params_t *params, const char *extra_xml,
5404  cmd_response_data_t* response_data)
5405 {
5406  char *ret;
5407  GString *extra_attribs, *extra_response;
5408  const char *info_type;
5409 
5410  info_type = params_value (params, "info_type");
5411  if (info_type == NULL)
5412  {
5413  param_t *param;
5414  param = params_add (params, "info_type", "nvt");
5415  param->valid = 1;
5416  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
5417  info_type = params_value (params, "info_type");
5418  }
5419 
5420  if (strcmp (info_type, "nvt")
5421  && strcmp (info_type, "cve")
5422  && strcmp (info_type, "cpe")
5423  && strcmp (info_type, "ovaldef")
5424  && strcmp (info_type, "cert_bund_adv")
5425  && strcmp (info_type, "dfn_cert_adv")
5426  && strcmp (info_type, "allinfo")
5427  && strcmp (info_type, "NVT")
5428  && strcmp (info_type, "CVE")
5429  && strcmp (info_type, "CPE")
5430  && strcmp (info_type, "OVALDEF")
5431  && strcmp (info_type, "CERT_BUND_ADV")
5432  && strcmp (info_type, "DFN_CERT_ADV")
5433  && strcmp (info_type, "ALLINFO"))
5434  {
5435  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5436  return gsad_message (credentials,
5437  "Internal error", __FUNCTION__, __LINE__,
5438  "An internal error occurred while getting SecInfo. "
5439  "Diagnostics: Invalid info_type parameter value",
5440  "/omp?cmd=get_info", response_data);
5441  }
5442 
5443  if (params_value (params, "info_name")
5444  && params_value (params, "info_id"))
5445  {
5446  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
5447  return gsad_message (credentials,
5448  "Internal error", __FUNCTION__, __LINE__,
5449  "An internal error occurred while getting SecInfo. "
5450  "Diagnostics: Both ID and Name set.",
5451  "/omp?cmd=get_info", response_data);
5452  }
5453  extra_response = g_string_new (extra_xml ? extra_xml : "");
5454 
5455  if (command_enabled (credentials, "GET_NOTES")
5456  && (strcasecmp (info_type, "NVT") == 0)
5457  && params_value (params, "info_id"))
5458  {
5459  gchar *response;
5460 
5461  if (simple_ompf ("getting SecInfo", credentials, &response, response_data,
5462  "<get_notes"
5463  " nvt_oid=\"%s\""
5464  " sort_field=\"notes.text\"/>",
5465  params_value (params, "info_id")))
5466  {
5467  g_string_free (extra_response, TRUE);
5468  return response;
5469  }
5470 
5471  g_string_append (extra_response, response);
5472  }
5473 
5474  if (command_enabled (credentials, "GET_OVERRIDES")
5475  && (strcasecmp (info_type, "NVT") == 0)
5476  && params_value (params, "info_id"))
5477  {
5478  gchar *response;
5479 
5480  if (simple_ompf ("getting SecInfo", credentials, &response, response_data,
5481  "<get_overrides"
5482  " nvt_oid=\"%s\""
5483  " sort_field=\"overrides.text\"/>",
5484  params_value (params, "info_id")))
5485  {
5486  g_string_free (extra_response, TRUE);
5487  return response;
5488  }
5489 
5490  g_string_append (extra_response, response);
5491  }
5492 
5493  extra_attribs = g_string_new("");
5494  g_string_append_printf (extra_attribs, "type=\"%s\"",
5495  params_value (params, "info_type"));
5496  if (params_value (params, "info_name"))
5497  g_string_append_printf (extra_attribs,
5498  " name=\"%s\"",
5499  params_value (params, "info_name"));
5500  else if (params_value (params, "info_id"))
5501  g_string_append_printf (extra_attribs,
5502  " info_id=\"%s\"",
5503  params_value (params, "info_id"));
5504  if (params_value (params, "details"))
5505  g_string_append_printf (extra_attribs,
5506  " details=\"%s\"",
5507  params_value (params, "details"));
5508  ret = get_many ("info", credentials, params, extra_response->str,
5509  extra_attribs->str, response_data);
5510 
5511  g_string_free (extra_response, TRUE);
5512  g_string_free (extra_attribs, TRUE);
5513 
5514  return ret;
5515 }
5516 
5526 char *
5527 get_info_omp (credentials_t * credentials, params_t *params,
5528  cmd_response_data_t* response_data)
5529 {
5530  return get_info (credentials, params, NULL, response_data);
5531 }
5532 
5542 char*
5543 get_nvts_omp (credentials_t *credentials, params_t *params,
5544  cmd_response_data_t* response_data)
5545 {
5546  return get_nvts (credentials, params, NULL, NULL, response_data);
5547 }
5548 
5555 static void
5556 params_toggle_overrides (params_t *params, const char *overrides)
5557 {
5558  param_t *filt_id, *build_filter;
5559  const char *new_filt_id;
5560 
5561  build_filter = params_get (params, "build_filter");
5562 
5563  if (build_filter)
5564  new_filt_id = "";
5565  else
5566  new_filt_id = "0";
5567 
5568  filt_id = params_get (params, "filt_id");
5569  if (filt_id)
5570  {
5571  filt_id->value = g_strdup (new_filt_id);
5572  filt_id->value_size = strlen (filt_id->value);
5573  filt_id->valid = 1;
5574  filt_id->valid_utf8 = 1;
5575  }
5576  else
5577  params_add (params, "filt_id", new_filt_id);
5578 
5579  if (build_filter == NULL)
5580  {
5581  param_t *filter;
5582  filter = params_get (params, "filter");
5583  if (filter && filter->value)
5584  {
5585  gchar *old;
5586  old = filter->value;
5587  filter->value = g_strdup_printf ("apply_overrides=%s %s",
5588  overrides,
5589  old);
5590  g_free (old);
5591  }
5592  else if (strcmp (overrides, "0"))
5593  params_add (params, "filter",
5594  "apply_overrides=1 rows=-2");
5595  else
5596  params_add (params, "filter",
5597  "apply_overrides=0 rows=-2");
5598  }
5599 }
5600 
5611 static char *
5612 get_tasks (credentials_t *credentials, params_t *params, const char *extra_xml,
5613  cmd_response_data_t* response_data)
5614 {
5615  const char *overrides, *schedules_only, *ignore_pagination;
5616  gchar *extra_attribs, *ret;
5617 
5618  overrides = params_value (params, "overrides");
5619  schedules_only = params_value (params, "schedules_only");
5620  ignore_pagination = params_value (params, "ignore_pagination");
5621 
5622  if (overrides)
5623  /* User toggled overrides. Set the overrides value in the filter. */
5624  params_toggle_overrides (params, overrides);
5625 
5626  extra_attribs = g_strdup_printf ("%s%s%s"
5627  "%s%s%s",
5628  schedules_only ? "schedules_only=\"" : "",
5629  schedules_only ? schedules_only : "",
5630  schedules_only ? "\" " : "",
5631  ignore_pagination
5632  ? "ignore_pagination=\""
5633  : "",
5634  ignore_pagination ? ignore_pagination : "",
5635  ignore_pagination ? "\" " : "");
5636 
5637  ret = get_many ("task", credentials, params, extra_xml, extra_attribs,
5638  response_data);
5639  g_free (extra_attribs);
5640  return ret;
5641 }
5642 
5652 char *
5653 get_tasks_omp (credentials_t * credentials, params_t *params,
5654  cmd_response_data_t* response_data)
5655 {
5656  return get_tasks (credentials, params, NULL, response_data);
5657 }
5658 
5669 static char *
5670 get_tasks_chart (credentials_t *credentials, params_t *params,
5671  const char *extra_xml, cmd_response_data_t* response_data)
5672 {
5673  return xsl_transform_omp (credentials, g_strdup ("<get_tasks_chart/>"),
5674  response_data);
5675 }
5676 
5686 char *
5688  cmd_response_data_t* response_data)
5689 {
5690  return get_tasks_chart (credentials, params, NULL, response_data);
5691 }
5692 
5693 
5704 static char *
5705 get_task (credentials_t *credentials, params_t *params, const char *extra_xml,
5706  cmd_response_data_t* response_data)
5707 {
5708  GString *xml = NULL;
5709  GString *commands_xml = NULL;
5710  entity_t commands_entity = NULL;
5711  entity_t task_entity = NULL;
5712  openvas_connection_t connection;
5713  int notes, get_overrides, apply_overrides;
5714  int get_target, get_alerts;
5715  const char *overrides, *task_id;
5716  gchar *html;
5717 
5718  task_id = params_value (params, "task_id");
5719  if (task_id == NULL)
5720  return get_tasks (credentials, params, extra_xml, response_data);
5721 
5722  overrides = params_value (params, "overrides");
5723  apply_overrides = overrides ? strcmp (overrides, "0") : 1;
5724 
5725  switch (manager_connect (credentials, &connection, &html,
5726  response_data))
5727  {
5728  case 0:
5729  break;
5730  case -1:
5731  if (html)
5732  return html;
5733  /* Fall through. */
5734  default:
5735  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5736  return gsad_message (credentials,
5737  "Internal error", __FUNCTION__, __LINE__,
5738  "An internal error occurred while getting the status. "
5739  "No update on status can be retrieved. "
5740  "Diagnostics: Failure to connect to manager daemon.",
5741  "/omp?cmd=get_tasks", response_data);
5742  }
5743 
5744  notes = command_enabled (credentials, "GET_NOTES");
5745  get_overrides = command_enabled (credentials, "GET_OVERRIDES");
5746  if (openvas_connection_sendf
5747  (&connection,
5748  "<commands>"
5749  "<get_tasks"
5750  " task_id=\"%s\""
5751  " filter=\"apply_overrides=%i\""
5752  " details=\"0\"/>"
5753  "%s%s%s"
5754  "%s%s%s"
5755  "</commands>",
5756  task_id,
5757  apply_overrides,
5758  notes
5759  ? "<get_notes"
5760  " sort_field=\"notes_nvt_name, notes.text\""
5761  " task_id=\""
5762  : "",
5763  notes ? task_id : "",
5764  notes ? "\"/>" : "",
5765  get_overrides
5766  ? "<get_overrides"
5767  " sort_field=\"overrides_nvt_name, overrides.text\""
5768  " task_id=\""
5769  : "",
5770  get_overrides ? task_id : "",
5771  get_overrides ? "\"/>" : "")
5772  == -1)
5773  {
5774  openvas_connection_close (&connection);
5775  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5776  return gsad_message (credentials,
5777  "Internal error", __FUNCTION__, __LINE__,
5778  "An internal error occurred while getting the status. "
5779  "No update on the requested task can be retrieved. "
5780  "Diagnostics: Failure to send command to manager daemon.",
5781  "/omp?cmd=get_tasks", response_data);
5782  }
5783 
5784  commands_xml = g_string_new ("");
5785  xml = g_string_new ("<get_task>");
5786 
5787  if (extra_xml)
5788  g_string_append (xml, extra_xml);
5789 
5790  g_string_append_printf (xml,
5791  "<apply_overrides>%i</apply_overrides>"
5792  "<delta>%s</delta>",
5793  apply_overrides,
5794  params_value (params, "delta_report_id")
5795  ? params_value (params, "delta_report_id")
5796  : "");
5797  if (read_string_c (&connection, &commands_xml))
5798  {
5799  openvas_connection_close (&connection);
5800  g_string_free (commands_xml, TRUE);
5801  g_string_free (xml, TRUE);
5802  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5803  return gsad_message (credentials,
5804  "Internal error", __FUNCTION__, __LINE__,
5805  "An internal error occurred while getting the status. "
5806  "No update of the status can be retrieved. "
5807  "Diagnostics: Failure to receive response from manager daemon.",
5808  "/omp?cmd=get_tasks", response_data);
5809  }
5810  g_string_append (xml, commands_xml->str);
5811 
5812  if (parse_entity (commands_xml->str, &commands_entity))
5813  {
5814  openvas_connection_close (&connection);
5815  g_string_free (commands_xml, TRUE);
5816  g_string_free (xml, TRUE);
5817  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5818  return gsad_message (credentials,
5819  "Internal error", __FUNCTION__, __LINE__,
5820  "An internal error occurred while getting the status. "
5821  "No update of the status can be retrieved. "
5822  "Diagnostics: Failure to parse response from manager daemon.",
5823  "/omp?cmd=get_tasks", response_data);
5824  }
5825 
5826  get_target = command_enabled (credentials, "GET_TARGETS");
5827  get_alerts = command_enabled (credentials, "GET_ALERTS");
5828  task_entity = entity_child (commands_entity, "get_tasks_response");
5829  if (task_entity == NULL)
5830  {
5831  g_warning ("%s: No get_tasks_response found in manager response.",
5832  __FUNCTION__);
5833  }
5834  else
5835  {
5836  task_entity = entity_child (task_entity, "task");
5837  if (task_entity == NULL)
5838  g_message ("%s: No task found in manager response.", __FUNCTION__);
5839  else if (get_target || get_alerts)
5840  {
5841  entities_t child_entities;
5842  entity_t child_entity;
5843  child_entities = task_entity->entities;
5844 
5845  while ((child_entity = first_entity (child_entities)))
5846  {
5847  if (get_alerts &&
5848  strcmp (entity_name (child_entity), "alert") == 0)
5849  {
5850  const char *resource_id
5851  = entity_attribute (child_entity, "id");
5852 
5853  if (resource_id != NULL && strcmp (resource_id, ""))
5854  {
5855  if (openvas_connection_sendf (&connection,
5856  "<get_alerts"
5857  " alert_id=\"%s\"/>",
5858  resource_id))
5859  {
5860  openvas_connection_close (&connection);
5861  g_string_free (xml, TRUE);
5862  g_string_free (commands_xml, TRUE);
5863  free_entity (commands_entity);
5864  response_data->http_status_code
5865  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5866  return gsad_message (credentials,
5867  "Internal error",
5868  __FUNCTION__, __LINE__,
5869  "An internal error occurred while getting an alert of a task. "
5870  "Diagnostics: Failure to send command to manager daemon.",
5871  "/omp?cmd=get_tasks",
5872  response_data);
5873  }
5874  if (read_string_c (&connection, &xml))
5875  {
5876  openvas_connection_close (&connection);
5877  g_string_free (commands_xml, TRUE);
5878  g_string_free (xml, TRUE);
5879  free_entity (commands_entity);
5880  response_data->http_status_code
5881  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5882  return gsad_message (credentials,
5883  "Internal error",
5884  __FUNCTION__, __LINE__,
5885  "An internal error occurred while getting an alert of a task. "
5886  "Diagnostics: Failure to receive response from manager daemon.",
5887  "/omp?cmd=get_tasks",
5888  response_data);
5889  }
5890  }
5891  }
5892 
5893  if (get_target &&
5894  strcmp (entity_name (child_entity), "target") == 0)
5895  {
5896  const char *resource_id
5897  = entity_attribute (child_entity, "id");
5898 
5899  if (resource_id != NULL && strcmp (resource_id, ""))
5900  {
5901  if (openvas_connection_sendf (&connection,
5902  "<get_targets"
5903  " target_id=\"%s\"/>",
5904  resource_id))
5905  {
5906  openvas_connection_close (&connection);
5907  g_string_free (xml, TRUE);
5908  g_string_free (commands_xml, TRUE);
5909  free_entity (commands_entity);
5910  response_data->http_status_code
5911  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5912  return gsad_message (credentials,
5913  "Internal error",
5914  __FUNCTION__, __LINE__,
5915  "An internal error occurred while getting the target of a task. "
5916  "Diagnostics: Failure to send command to manager daemon.",
5917  "/omp?cmd=get_tasks",
5918  response_data);
5919  }
5920  if (read_string_c (&connection, &xml))
5921  {
5922  openvas_connection_close (&connection);
5923  g_string_free (commands_xml, TRUE);
5924  g_string_free (xml, TRUE);
5925  free_entity (commands_entity);
5926  response_data->http_status_code
5927  = MHD_HTTP_INTERNAL_SERVER_ERROR;
5928  return gsad_message (credentials,
5929  "Internal error",
5930  __FUNCTION__, __LINE__,
5931  "An internal error occurred while getting the target of a task. "
5932  "Diagnostics: Failure to receive response from manager daemon.",
5933  "/omp?cmd=get_tasks",
5934  response_data);
5935  }
5936  }
5937  }
5938 
5939  child_entities = next_entities (child_entities);
5940  }
5941  }
5942  }
5943 
5944  g_string_free (commands_xml, TRUE);
5945  free_entity (commands_entity);
5946 
5947  /* Get slave scanners. */
5948 
5949  if (command_enabled (credentials, "GET_SCANNERS"))
5950  {
5951  if (openvas_connection_sendf (&connection,
5952  "<get_scanners"
5953  " filter=\"first=1 rows=-1 type=4\"/>")
5954  == -1)
5955  {
5956  g_string_free (xml, TRUE);
5957  openvas_connection_close (&connection);
5958  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5959  return gsad_message (credentials,
5960  "Internal error", __FUNCTION__, __LINE__,
5961  "An internal error occurred while getting slaves list. "
5962  "The current list of resources is not available. "
5963  "Diagnostics: Failure to send command to manager daemon.",
5964  "/omp?cmd=get_tasks", response_data);
5965  }
5966 
5967  if (read_string_c (&connection, &xml))
5968  {
5969  g_string_free (xml, TRUE);
5970  openvas_connection_close (&connection);
5971  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5972  return gsad_message (credentials,
5973  "Internal error", __FUNCTION__, __LINE__,
5974  "An internal error occurred while getting slaves list. "
5975  "The current list of resources is not available. "
5976  "Diagnostics: Failure to receive response from manager daemon.",
5977  "/omp?cmd=get_tasks", response_data);
5978  }
5979  }
5980 
5981  /* Get tag names */
5982 
5983  if (openvas_connection_sendf (&connection,
5984  "<get_tags"
5985  " filter=\"resource_type=task"
5986  " first=1"
5987  " rows=-1\""
5988  " names_only=\"1\""
5989  "/>")
5990  == -1)
5991  {
5992  g_string_free (xml, TRUE);
5993  openvas_connection_close (&connection);
5994  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
5995  return gsad_message (credentials,
5996  "Internal error", __FUNCTION__, __LINE__,
5997  "An internal error occurred while getting tag names list. "
5998  "The current list of resources is not available. "
5999  "Diagnostics: Failure to send command to manager daemon.",
6000  "/omp?cmd=get_resources", response_data);
6001  }
6002 
6003  if (read_string_c (&connection, &xml))
6004  {
6005  g_string_free (xml, TRUE);
6006  openvas_connection_close (&connection);
6007  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6008  return gsad_message (credentials,
6009  "Internal error", __FUNCTION__, __LINE__,
6010  "An internal error occurred while getting tag names list. "
6011  "The current list of resources is not available. "
6012  "Diagnostics: Failure to receive response from manager daemon.",
6013  "/omp?cmd=get_resources", response_data);
6014  }
6015 
6016  /* Get permissions */
6017 
6018  g_string_append (xml, "<permissions>");
6019 
6020  if (openvas_connection_sendf (&connection,
6021  "<get_permissions"
6022  " filter=\"name:^.*(task)s?$"
6023  " and resource_uuid=%s"
6024  " first=1 rows=-1\"/>",
6025  task_id)
6026  == -1)
6027  {
6028  g_string_free (xml, TRUE);
6029  openvas_connection_close (&connection);
6030  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6031  return gsad_message (credentials,
6032  "Internal error", __FUNCTION__, __LINE__,
6033  "An internal error occurred while getting permissions list. "
6034  "The current list of resources is not available. "
6035  "Diagnostics: Failure to send command to manager daemon.",
6036  "/omp?cmd=get_resources", response_data);
6037  }
6038 
6039  if (read_string_c (&connection, &xml))
6040  {
6041  g_string_free (xml, TRUE);
6042  openvas_connection_close (&connection);
6043  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6044  return gsad_message (credentials,
6045  "Internal error", __FUNCTION__, __LINE__,
6046  "An internal error occurred while getting permissions list. "
6047  "The current list of resources is not available. "
6048  "Diagnostics: Failure to receive response from manager daemon.",
6049  "/omp?cmd=get_resources", response_data);
6050  }
6051 
6052  g_string_append (xml, "</permissions>");
6053 
6054  g_string_append (xml, "</get_task>");
6055 
6056  openvas_connection_close (&connection);
6057  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
6058  response_data);
6059 }
6060 
6070 char *
6071 get_task_omp (credentials_t * credentials, params_t *params,
6072  cmd_response_data_t* response_data)
6073 {
6074  return get_task (credentials, params, NULL, response_data);
6075 }
6076 
6087 static char *
6088 new_credential (credentials_t *credentials, params_t *params,
6089  const char *extra_xml, cmd_response_data_t* response_data)
6090 {
6091  GString *xml;
6092  xml = g_string_new ("<new_credential>");
6093  if (extra_xml)
6094  g_string_append (xml, extra_xml);
6095  g_string_append (xml, "</new_credential>");
6096  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
6097  response_data);
6098 }
6099 
6109 char *
6111  cmd_response_data_t* response_data)
6112 {
6113  int ret;
6114  gchar *html, *response;
6115  const char *no_redirect;
6116  const char *name, *comment, *login, *type, *password, *passphrase;
6117  const char *private_key, *certificate, *community, *privacy_password;
6118  const char *auth_algorithm, *privacy_algorithm, *allow_insecure;
6119  int autogenerate;
6120  entity_t entity;
6121 
6122  no_redirect = params_value (params, "no_redirect");
6123  name = params_value (params, "name");
6124  comment = params_value (params, "comment");
6125  login = params_value (params, "credential_login");
6126  type = params_value (params, "base");
6127  password = params_value (params, "lsc_password");
6128  passphrase = params_value (params, "passphrase");
6129  private_key = params_value (params, "private_key");
6130  certificate = params_value (params, "certificate");
6131  community = params_value (params, "community");
6132  privacy_password = params_value (params, "privacy_password");
6133  auth_algorithm = params_value (params, "auth_algorithm");
6134  privacy_algorithm = params_value (params, "privacy_algorithm");
6135  allow_insecure = params_value (params, "allow_insecure");
6136 
6137  if (params_value (params, "autogenerate"))
6138  autogenerate = strcmp (params_value (params, "autogenerate"), "0");
6139  else
6140  return message_invalid (credentials, params, response_data,
6141  "Given autogenerate was invalid",
6142  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
6143  "Create Credential", "new_credential");
6144 
6145  CHECK_PARAM_INVALID (name, "Create Credential", "new_credential");
6146  CHECK_PARAM_INVALID (comment, "Create Credential", "new_credential");
6147  CHECK_PARAM_INVALID (type, "Create Credential", "new_credential");
6148  CHECK_PARAM_INVALID (allow_insecure, "Create Credential", "new_credential");
6149 
6150  if (autogenerate)
6151  {
6152  if (type && (strcmp (type, "cc") == 0))
6153  {
6154  // Auto-generate types without username
6155  ret = ompf (credentials,
6156  &response,
6157  &entity,
6158  response_data,
6159  "<create_credential>"
6160  "<name>%s</name>"
6161  "<comment>%s</comment>"
6162  "<type>%s</type>"
6163  "<allow_insecure>%s</allow_insecure>"
6164  "</create_credential>",
6165  name,
6166  comment ? comment : "",
6167  type,
6168  allow_insecure);
6169  }
6170  else
6171  {
6172  // Auto-generate types with username
6173  CHECK_PARAM_INVALID (login, "Create Credential", "new_credential");
6174 
6175  ret = ompf (credentials,
6176  &response,
6177  &entity,
6178  response_data,
6179  "<create_credential>"
6180  "<name>%s</name>"
6181  "<comment>%s</comment>"
6182  "<type>%s</type>"
6183  "<login>%s</login>"
6184  "<allow_insecure>%s</allow_insecure>"
6185  "</create_credential>",
6186  name,
6187  comment ? comment : "",
6188  type,
6189  login,
6190  allow_insecure);
6191  }
6192  }
6193  else
6194  {
6195  if (type && (strcmp (type, "up") == 0))
6196  {
6197  CHECK_PARAM_INVALID (login,
6198  "Create Credential", "new_credential");
6199  CHECK_PARAM_INVALID (password,
6200  "Create Credential", "new_credential");
6201 
6202  ret = ompf (credentials,
6203  &response,
6204  &entity,
6205  response_data,
6206  "<create_credential>"
6207  "<name>%s</name>"
6208  "<comment>%s</comment>"
6209  "<type>%s</type>"
6210  "<login>%s</login>"
6211  "<password>%s</password>"
6212  "<allow_insecure>%s</allow_insecure>"
6213  "</create_credential>",
6214  name,
6215  comment ? comment : "",
6216  type,
6217  login ? login : "",
6218  password ? password : "",
6219  allow_insecure);
6220  }
6221  else if (type && (strcmp (type, "usk") == 0))
6222  {
6223  CHECK_PARAM_INVALID (login,
6224  "Create Credential", "new_credential");
6225  CHECK_PARAM_INVALID (passphrase,
6226  "Create Credential", "new_credential");
6227  CHECK_PARAM_INVALID (private_key,
6228  "Create Credential", "new_credential");
6229 
6230  ret = ompf (credentials,
6231  &response,
6232  &entity,
6233  response_data,
6234  "<create_credential>"
6235  "<name>%s</name>"
6236  "<comment>%s</comment>"
6237  "<type>%s</type>"
6238  "<login>%s</login>"
6239  "<key>"
6240  "<private>%s</private>"
6241  "<phrase>%s</phrase>"
6242  "</key>"
6243  "<allow_insecure>%s</allow_insecure>"
6244  "</create_credential>",
6245  name,
6246  comment ? comment : "",
6247  type,
6248  login ? login : "",
6249  private_key ? private_key : "",
6250  passphrase ? passphrase : "",
6251  allow_insecure);
6252  }
6253  else if (type && (strcmp (type, "cc") == 0))
6254  {
6255  CHECK_PARAM_INVALID (certificate,
6256  "Create Credential", "new_credential");
6257  CHECK_PARAM_INVALID (private_key,
6258  "Create Credential", "new_credential");
6259 
6260  ret = ompf (credentials,
6261  &response,
6262  &entity,
6263  response_data,
6264  "<create_credential>"
6265  "<name>%s</name>"
6266  "<comment>%s</comment>"
6267  "<type>%s</type>"
6268  "<certificate>%s</certificate>"
6269  "<key>"
6270  "<private>%s</private>"
6271  "</key>"
6272  "<allow_insecure>%s</allow_insecure>"
6273  "</create_credential>",
6274  name,
6275  comment ? comment : "",
6276  type,
6277  certificate ? certificate : "",
6278  private_key ? private_key : "",
6279  allow_insecure);
6280 
6281  }
6282  else if (type && (strcmp (type, "snmp") == 0))
6283  {
6284  CHECK_PARAM_INVALID (community,
6285  "Create Credential", "new_credential");
6286  CHECK_PARAM_INVALID (login,
6287  "Create Credential", "new_credential");
6288  CHECK_PARAM_INVALID (password,
6289  "Create Credential", "new_credential");
6290  CHECK_PARAM_INVALID (privacy_password,
6291  "Create Credential", "new_credential");
6292  CHECK_PARAM_INVALID (auth_algorithm,
6293  "Create Credential", "new_credential");
6294  CHECK_PARAM_INVALID (privacy_algorithm,
6295  "Create Credential", "new_credential");
6296 
6297  if (privacy_password && strcmp (privacy_password, ""))
6298  ret = ompf (credentials,
6299  &response,
6300  &entity,
6301  response_data,
6302  "<create_credential>"
6303  "<name>%s</name>"
6304  "<comment>%s</comment>"
6305  "<type>%s</type>"
6306  "<community>%s</community>"
6307  "<login>%s</login>"
6308  "<password>%s</password>"
6309  "<privacy>"
6310  "<password>%s</password>"
6311  "<algorithm>%s</algorithm>"
6312  "</privacy>"
6313  "<auth_algorithm>%s</auth_algorithm>"
6314  "<allow_insecure>%s</allow_insecure>"
6315  "</create_credential>",
6316  name,
6317  comment ? comment : "",
6318  type,
6319  community ? community : "",
6320  login ? login : "",
6321  password ? password : "",
6322  privacy_password ? privacy_password : "",
6323  privacy_algorithm ? privacy_algorithm : "",
6324  auth_algorithm ? auth_algorithm : "",
6325  allow_insecure);
6326  else
6327  ret = ompf (credentials,
6328  &response,
6329  &entity,
6330  response_data,
6331  "<create_credential>"
6332  "<name>%s</name>"
6333  "<comment>%s</comment>"
6334  "<type>%s</type>"
6335  "<community>%s</community>"
6336  "<login>%s</login>"
6337  "<password>%s</password>"
6338  "<auth_algorithm>%s</auth_algorithm>"
6339  "<allow_insecure>%s</allow_insecure>"
6340  "</create_credential>",
6341  name,
6342  comment ? comment : "",
6343  type,
6344  community ? community : "",
6345  login ? login : "",
6346  password ? password : "",
6347  auth_algorithm ? auth_algorithm : "",
6348  allow_insecure);
6349  }
6350  else
6351  {
6352  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6353  return gsad_message (credentials,
6354  "Internal error", __FUNCTION__, __LINE__,
6355  "An internal error occurred while creating a new credential. "
6356  "The credential could not be created. "
6357  "Diagnostics: Unrecognized credential type.",
6358  "/omp?cmd=get_credentials", response_data);
6359  }
6360  }
6361 
6362  /* Create the credential. */
6363  switch (ret)
6364  {
6365  case 0:
6366  case -1:
6367  break;
6368  case 1:
6369  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6370  return gsad_message (credentials,
6371  "Internal error", __FUNCTION__, __LINE__,
6372  "An internal error occurred while creating a new credential. "
6373  "It is unclear whether the credential has been created or not. "
6374  "Diagnostics: Failure to send command to manager daemon.",
6375  "/omp?cmd=get_credentials", response_data);
6376  case 2:
6377  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6378  return gsad_message (credentials,
6379  "Internal error", __FUNCTION__, __LINE__,
6380  "An internal error occurred while creating a new credential. "
6381  "It is unclear whether the credential has been created or not. "
6382  "Diagnostics: Failure to receive response from manager daemon.",
6383  "/omp?cmd=get_credentials", response_data);
6384  default:
6385  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6386  return gsad_message (credentials,
6387  "Internal error", __FUNCTION__, __LINE__,
6388  "An internal error occurred while creating a new credential. "
6389  "It is unclear whether the credential has been created or not. "
6390  "Diagnostics: Internal Error.",
6391  "/omp?cmd=get_credentials", response_data);
6392  }
6393 
6394  if (entity_attribute (entity, "id"))
6395  params_add (params, "credential_id", entity_attribute (entity, "id"));
6396  html = response_from_entity (credentials, params, entity,
6397  (no_redirect && strcmp (no_redirect, "0")),
6398  NULL, "get_credentials",
6399  NULL, "new_credential",
6400  "Create Credential",
6401  response_data);
6402  free_entity (entity);
6403  g_free (response);
6404  return html;
6405 }
6406 
6417 static char *
6418 get_credential (credentials_t * credentials, params_t *params,
6419  const char *extra_xml, cmd_response_data_t* response_data)
6420 {
6421  return get_one ("credential", credentials, params, extra_xml,
6422  "targets=\"1\" scanners=\"1\"", response_data);
6423 }
6424 
6434 char *
6436  cmd_response_data_t* response_data)
6437 {
6438  return get_credential (credentials, params, NULL, response_data);
6439 }
6440 
6454 int
6456  params_t *params,
6457  gsize *result_len,
6458  char ** html,
6459  char ** login,
6460  cmd_response_data_t* response_data)
6461 {
6462  entity_t entity;
6463  openvas_connection_t connection;
6464  gchar *connect_html;
6465  const char *credential_id, *format;
6466 
6467  assert (html);
6468 
6469  if (result_len) *result_len = 0;
6470 
6471  switch (manager_connect (credentials, &connection, &connect_html,
6472  response_data))
6473  {
6474  case 0:
6475  break;
6476  case -1:
6477  if (connect_html)
6478  {
6479  *html = connect_html;
6480  return 1;
6481  }
6482  /* Fall through. */
6483  default:
6484  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6485  *html = gsad_message (credentials,
6486  "Internal error", __FUNCTION__, __LINE__,
6487  "An internal error occurred while getting a credential. "
6488  "Diagnostics: Failure to connect to manager daemon.",
6489  "/omp?cmd=get_credentials", response_data);
6490  return 1;
6491  }
6492 
6493  /* Send the request. */
6494 
6495  credential_id = params_value (params, "credential_id");
6496  format = params_value (params, "package_format");
6497 
6498  if ((credential_id == NULL) || (format == NULL))
6499  {
6500  openvas_connection_close (&connection);
6501  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
6502  *html = gsad_message (credentials,
6503  "Internal error", __FUNCTION__, __LINE__,
6504  "An internal error occurred while getting a credential. "
6505  "Diagnostics: Required parameter was NULL.",
6506  "/omp?cmd=get_credentials", response_data);
6507  return 1;
6508  }
6509 
6510  if (openvas_connection_sendf (&connection,
6511  "<get_credentials"
6512  " credential_id=\"%s\""
6513  " format=\"%s\"/>",
6514  credential_id,
6515  format)
6516  == -1)
6517  {
6518  openvas_connection_close (&connection);
6519  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6520  *html = gsad_message (credentials,
6521  "Internal error", __FUNCTION__, __LINE__,
6522  "An internal error occurred while getting a credential. "
6523  "Diagnostics: Failure to send command to manager daemon.",
6524  "/omp?cmd=get_credentials", response_data);
6525  return 1;
6526  }
6527 
6528  /* Read and handle the response. */
6529 
6530  if (strcmp (format, "rpm") == 0
6531  || strcmp (format, "deb") == 0
6532  || strcmp (format, "exe") == 0)
6533  {
6534  gchar *package_decoded = NULL;
6535  entity_t package_entity = NULL, credential_entity;
6536 
6537  /* A base64 encoded package. */
6538 
6539  entity = NULL;
6540  if (read_entity_c (&connection, &entity))
6541  {
6542  openvas_connection_close (&connection);
6543  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6544  *html = gsad_message (credentials,
6545  "Internal error", __FUNCTION__, __LINE__,
6546  "An internal error occurred while getting a credential. "
6547  "The credential is not available. "
6548  "Diagnostics: Failure to receive response from manager daemon.",
6549  "/omp?cmd=get_credentials", response_data);
6550  return 1;
6551  }
6552 
6553  credential_entity = entity_child (entity, "credential");
6554  if (credential_entity)
6555  package_entity = entity_child (credential_entity, "package");
6556  if (package_entity != NULL)
6557  {
6558  gsize len;
6559  char *package_encoded = entity_text (package_entity);
6560  if (strlen (package_encoded))
6561  {
6562  package_decoded = (gchar *) g_base64_decode (package_encoded,
6563  &len);
6564  if (package_decoded == NULL)
6565  {
6566  package_decoded = (gchar *) g_strdup ("");
6567  len = 0;
6568  }
6569  }
6570  else
6571  {
6572  package_decoded = (gchar *) g_strdup ("");
6573  len = 0;
6574  }
6575  if (result_len) *result_len = len;
6576  openvas_connection_close (&connection);
6577  *html = package_decoded;
6578  if (login)
6579  {
6580  entity_t login_entity;
6581  login_entity = entity_child (credential_entity, "login");
6582  if (login_entity)
6583  *login = g_strdup (entity_text (login_entity));
6584  else
6585  *login = NULL;
6586  }
6587  free_entity (entity);
6588  return 0;
6589  }
6590  else
6591  {
6592  free_entity (entity);
6593  openvas_connection_close (&connection);
6594  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6595  *html = gsad_message (credentials,
6596  "Internal error", __FUNCTION__, __LINE__,
6597  "An internal error occurred while getting a credential. "
6598  "The credential could not be delivered. "
6599  "Diagnostics: Failure to receive credential from manager daemon.",
6600  "/omp?cmd=get_credentials", response_data);
6601  return 1;
6602  }
6603  }
6604  else
6605  {
6606  entity_t credential_entity, key_entity = NULL;
6607 
6608  /* A key or certificate. */
6609 
6610  entity = NULL;
6611  if (read_entity_c (&connection, &entity))
6612  {
6613  openvas_connection_close (&connection);
6614  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6615  *html = gsad_message (credentials,
6616  "Internal error", __FUNCTION__, __LINE__,
6617  "An internal error occurred while getting a credential. "
6618  "The credential could not be delivered. "
6619  "Diagnostics: Failure to receive credential from manager daemon.",
6620  "/omp?cmd=get_credentials", response_data);
6621  return 1;
6622  }
6623  openvas_connection_close (&connection);
6624 
6625  credential_entity = entity_child (entity, "credential");
6626  if (credential_entity)
6627  {
6628  if (strcmp (format, "pem") == 0)
6629  key_entity = entity_child (credential_entity, "certificate");
6630  else
6631  key_entity = entity_child (credential_entity, "public_key");
6632  }
6633  if (key_entity != NULL)
6634  {
6635  *html = g_strdup (entity_text (key_entity));
6636  if (login)
6637  {
6638  entity_t login_entity = entity_child (credential_entity, "login");
6639  if (login_entity)
6640  *login = g_strdup (entity_text (login_entity));
6641  else
6642  *login = NULL;
6643  }
6644  free_entity (entity);
6645  return 0;
6646  }
6647  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6648  *html = gsad_message (credentials,
6649  "Internal error", __FUNCTION__, __LINE__,
6650  "An internal error occurred while getting a credential. "
6651  "The credential could not be delivered. "
6652  "Diagnostics: Failure to parse credential from manager daemon.",
6653  "/omp?cmd=get_credentials", response_data);
6654  free_entity (entity);
6655  return 1;
6656  }
6657 }
6658 
6672 char *
6674  enum content_type * content_type,
6675  char **content_disposition, gsize *content_length,
6676  cmd_response_data_t* response_data)
6677 {
6678  return export_resource ("credential", credentials, params, content_type,
6679  content_disposition, content_length, response_data);
6680 }
6681 
6695 char *
6697  enum content_type * content_type,
6698  char **content_disposition, gsize *content_length,
6699  cmd_response_data_t* response_data)
6700 {
6701  return export_many ("credential", credentials, params, content_type,
6702  content_disposition, content_length, response_data);
6703 }
6704 
6716 static char *
6717 get_credentials (credentials_t * credentials, params_t *params,
6718  const char *extra_xml, cmd_response_data_t* response_data)
6719 {
6720  return get_many ("credential", credentials, params, extra_xml, NULL,
6721  response_data);
6722 }
6723 
6733 char *
6735  cmd_response_data_t* response_data)
6736 {
6737  return get_credentials (credentials, params, NULL, response_data);
6738 }
6739 
6749 char *
6751  cmd_response_data_t* response_data)
6752 {
6753  return delete_resource ("credential", credentials, params, 0,
6754  "get_credentials", response_data);
6755 }
6756 
6766 char *
6768  cmd_response_data_t* response_data)
6769 {
6770  return new_credential (credentials, params, NULL, response_data);
6771 }
6772 
6783 static char *
6784 edit_credential (credentials_t * credentials, params_t *params,
6785  const char *extra_xml, cmd_response_data_t* response_data)
6786 {
6787  return edit_resource ("credential", credentials, params, NULL, extra_xml,
6788  response_data);
6789 }
6790 
6800 char *
6802  cmd_response_data_t* response_data)
6803 {
6804  return edit_credential (credentials, params, NULL, response_data);
6805 }
6806 
6816 char *
6818  cmd_response_data_t* response_data)
6819 {
6820  int ret, change_password, change_passphrase;
6821  int change_community, change_privacy_password;
6822  gchar *html, *response;
6823  const char *no_redirect, *credential_id;
6824  const char *name, *comment, *login, *password, *passphrase;
6825  const char *private_key, *certificate, *community, *privacy_password;
6826  const char *auth_algorithm, *privacy_algorithm, *allow_insecure;
6827  GString *command;
6828  entity_t entity;
6829 
6830  no_redirect = params_value (params, "no_redirect");
6831  credential_id = params_value (params, "credential_id");
6832  name = params_value (params, "name");
6833  comment = params_value (params, "comment");
6834  login = params_value (params, "credential_login");
6835  password = params_value (params, "password");
6836  passphrase = params_value (params, "passphrase");
6837  private_key = params_value (params, "private_key");
6838  certificate = params_value (params, "certificate");
6839  community = params_value (params, "community");
6840  privacy_password = params_value (params, "privacy_password");
6841  auth_algorithm = params_value (params, "auth_algorithm");
6842  privacy_algorithm = params_value (params, "privacy_algorithm");
6843  allow_insecure = params_value (params, "allow_insecure");
6844 
6845  CHECK_PARAM_INVALID (credential_id, "Save Credential", "edit_credential");
6846  CHECK_PARAM_INVALID (name, "Save Credential", "edit_credential");
6847  CHECK_PARAM_INVALID (comment, "Save Credential", "edit_credential");
6848  CHECK_PARAM_INVALID (allow_insecure, "Save Credential", "edit_credential");
6849  if (params_given (params, "certificate"))
6850  CHECK_PARAM_INVALID (certificate, "Save Credential", "edit_credential");
6851  if (params_given (params, "private_key"))
6852  CHECK_PARAM_INVALID (private_key, "Save Credential", "edit_credential");
6853  if (params_given (params, "login"))
6854  CHECK_PARAM_INVALID (login, "Save Credential", "edit_credential");
6855  if (params_given (params, "auth_algorithm"))
6856  CHECK_PARAM_INVALID (auth_algorithm, "Save Credential", "edit_credential");
6857  if (params_given (params, "privacy_algorithm"))
6858  CHECK_PARAM_INVALID (privacy_algorithm,
6859  "Save Credential", "edit_credential");
6860 
6861  change_community = (params_value (params, "change_community") ? 1 : 0);
6862  if (change_community)
6863  CHECK_PARAM_INVALID (community, "Save Credential", "edit_credential");
6864  change_passphrase = (params_value (params, "change_passphrase") ? 1 : 0);
6865  if (change_passphrase)
6866  CHECK_PARAM_INVALID (passphrase, "Save Credential", "edit_credential");
6867  change_password = (params_value (params, "change_password") ? 1 : 0);
6868  if (change_password)
6869  CHECK_PARAM_INVALID (password, "Save Credential", "edit_credential");
6870  change_privacy_password
6871  = (params_value (params, "change_privacy_password") ? 1 : 0);
6872  if (change_privacy_password)
6873  CHECK_PARAM_INVALID (privacy_password,
6874  "Save Credential", "edit_credential");
6875 
6876  /* Prepare command */
6877  command = g_string_new ("");
6878 
6879  xml_string_append (command,
6880  "<modify_credential credential_id=\"%s\">"
6881  "<name>%s</name>"
6882  "<comment>%s</comment>"
6883  "<allow_insecure>%s</allow_insecure>",
6884  credential_id,
6885  name,
6886  comment,
6887  allow_insecure);
6888 
6889  if (auth_algorithm)
6890  xml_string_append (command,
6891  "<auth_algorithm>%s</auth_algorithm>",
6892  auth_algorithm);
6893 
6894  if (certificate && strcmp (certificate, ""))
6895  xml_string_append (command,
6896  "<certificate>%s</certificate>",
6897  certificate);
6898 
6899  if (change_community)
6900  xml_string_append (command,
6901  "<community>%s</community>",
6902  community);
6903 
6904  if (login && strcmp (login, ""))
6905  xml_string_append (command,
6906  "<login>%s</login>",
6907  login);
6908 
6909  if (change_password)
6910  xml_string_append (command,
6911  "<password>%s</password>",
6912  password);
6913 
6914  if (privacy_algorithm || change_privacy_password)
6915  {
6916  xml_string_append (command,
6917  "<privacy>");
6918  if (privacy_algorithm && strcmp (privacy_algorithm, ""))
6919  {
6920  xml_string_append (command,
6921  "<algorithm>%s</algorithm>",
6922  privacy_algorithm);
6923  if (change_privacy_password)
6924  xml_string_append (command,
6925  "<password>%s</password>",
6926  privacy_password);
6927  }
6928  else if (privacy_algorithm)
6929  {
6930  xml_string_append (command,
6931  "<algorithm></algorithm>");
6932  xml_string_append (command,
6933  "<password></password>");
6934  }
6935 
6936  xml_string_append (command,
6937  "</privacy>");
6938  }
6939 
6940  if ((private_key && strcmp (private_key, "")) || change_passphrase)
6941  {
6942  xml_string_append (command,
6943  "<key>");
6944  if (change_passphrase)
6945  xml_string_append (command,
6946  "<phrase>%s</phrase>",
6947  passphrase);
6948  if (private_key)
6949  xml_string_append (command,
6950  "<private>%s</private>",
6951  private_key);
6952  xml_string_append (command,
6953  "</key>");
6954  }
6955 
6956  xml_string_append (command,
6957  "</modify_credential>");
6958 
6959  /* Modify the credential. */
6960  response = NULL;
6961  entity = NULL;
6962  ret = omp (credentials,
6963  &response,
6964  &entity,
6965  response_data,
6966  command->str);
6967  g_string_free (command, TRUE);
6968 
6969  switch (ret)
6970  {
6971  case 0:
6972  break;
6973  case -1:
6974  /* 'omp' set response. */
6975  return response;
6976  case 1:
6977  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6978  return gsad_message (credentials,
6979  "Internal error", __FUNCTION__, __LINE__,
6980  "An internal error occurred while saving a Credential. "
6981  "The Credential was not saved. "
6982  "Diagnostics: Failure to send command to manager daemon.",
6983  "/omp?cmd=get_credentials", response_data);
6984  case 2:
6985  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6986  return gsad_message (credentials,
6987  "Internal error", __FUNCTION__, __LINE__,
6988  "An internal error occurred while saving a Credential. "
6989  "It is unclear whether the Credential has been saved or not. "
6990  "Diagnostics: Failure to receive response from manager daemon.",
6991  "/omp?cmd=get_credentials", response_data);
6992  default:
6993  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
6994  return gsad_message (credentials,
6995  "Internal error", __FUNCTION__, __LINE__,
6996  "An internal error occurred while saving a Credential. "
6997  "It is unclear whether the Credential has been saved or not. "
6998  "Diagnostics: Internal Error.",
6999  "/omp?cmd=get_credentials", response_data);
7000  }
7001 
7002  html = response_from_entity (credentials, params, entity,
7003  (no_redirect && strcmp (no_redirect, "0")),
7004  NULL, "get_credentials",
7005  NULL, "edit_credential",
7006  "Save Credential", response_data);
7007  free_entity (entity);
7008  g_free (response);
7009  return html;
7010 }
7011 
7022 static char *
7023 new_agent (credentials_t *credentials, params_t *params,
7024  const char *extra_xml, cmd_response_data_t* response_data)
7025 {
7026  GString *xml;
7027  xml = g_string_new ("<new_agent>");
7028  if (extra_xml)
7029  g_string_append (xml, extra_xml);
7030  g_string_append (xml, "</new_agent>");
7031  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
7032  response_data);
7033 }
7034 
7044 char *
7045 new_agent_omp (credentials_t *credentials, params_t *params,
7046  cmd_response_data_t* response_data)
7047 {
7048  return new_agent (credentials, params, NULL, response_data);
7049 }
7050 
7060 char *
7061 create_agent_omp (credentials_t * credentials, params_t *params,
7062  cmd_response_data_t* response_data)
7063 {
7064  entity_t entity;
7065  gchar *response, *html;
7066  const char *no_redirect;
7067  const char *name, *comment, *installer, *installer_filename, *installer_sig;
7068  const char *howto_install, *howto_use;
7069  int installer_size, installer_sig_size, howto_install_size, howto_use_size;
7070  int ret;
7071  gchar *installer_64, *installer_sig_64, *howto_install_64, *howto_use_64;
7072  gchar *command;
7073 
7074  no_redirect = params_value (params, "no_redirect");
7075  name = params_value (params, "name");
7076  comment = params_value (params, "comment");
7077  installer = params_value (params, "installer");
7078  installer_filename = params_filename (params, "installer");
7079  installer_size = params_value_size (params, "installer");
7080  installer_sig = params_value (params, "installer_sig");
7081  installer_sig_size = params_value_size (params, "installer_sig");
7082  howto_install = params_value (params, "howto_install");
7083  howto_install_size = params_value_size (params, "howto_install");
7084  howto_use = params_value (params, "howto_use");
7085  howto_use_size = params_value_size (params, "howto_use");
7086 
7087  CHECK_PARAM_INVALID (name, "Create Agent", "new_agent");
7088  CHECK_PARAM_INVALID (comment, "Create Agent", "new_agent");
7089 
7090  /* Create the agent. */
7091 
7092  installer_64 = (installer_size > 0)
7093  ? g_base64_encode ((guchar *) installer,
7094  installer_size)
7095  : g_strdup ("");
7096 
7097  installer_sig_64 = (installer_sig_size > 0)
7098  ? g_base64_encode ((guchar *) installer_sig,
7099  installer_sig_size)
7100  : g_strdup ("");
7101 
7102  howto_install_64 = (howto_install_size > 0)
7103  ? g_base64_encode ((guchar *) howto_install,
7104  howto_install_size)
7105  : g_strdup ("");
7106 
7107  howto_use_64 = (howto_use_size > 0)
7108  ? g_base64_encode ((guchar *) howto_use,
7109  howto_use_size)
7110  : g_strdup ("");
7111 
7112  command = g_strdup_printf ("<create_agent>"
7113  "<name>%s</name>"
7114  "%s%s%s"
7115  "<installer>"
7116  "%s"
7117  "<signature>%s</signature>"
7118  "<filename>%s</filename>"
7119  "</installer>"
7120  "<howto_install>%s</howto_install>"
7121  "<howto_use>%s</howto_use>"
7122  "</create_agent>",
7123  name, comment ? "<comment>" : "",
7124  comment ? comment : "",
7125  comment ? "</comment>" : "",
7126  installer_64,
7127  installer_sig_64,
7128  installer_filename ? installer_filename : "",
7129  howto_install_64,
7130  howto_use_64);
7131 
7132  ret = omp (credentials,
7133  &response,
7134  &entity,
7135  response_data,
7136  command);
7137  g_free (command);
7138 
7139  g_free (installer_64);
7140  g_free (howto_install_64);
7141  g_free (howto_use_64);
7142 
7143  switch (ret)
7144  {
7145  case 0:
7146  break;
7147  case -1:
7148  /* 'omp' set response. */
7149  return response;
7150  case 1:
7151  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7152  return gsad_message (credentials,
7153  "Internal error", __FUNCTION__, __LINE__,
7154  "An internal error occurred while creating a new agent. "
7155  "No new agent was created. "
7156  "Diagnostics: Failure to send command to manager daemon.",
7157  "/omp?cmd=get_agents", response_data);
7158  case 2:
7159  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7160  return gsad_message (credentials,
7161  "Internal error", __FUNCTION__, __LINE__,
7162  "An internal error occurred while creating a new agent. "
7163  "It is unclear whether the agent has been created or not. "
7164  "Diagnostics: Failure to receive response from manager daemon.",
7165  "/omp?cmd=get_agents", response_data);
7166  default:
7167  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7168  return gsad_message (credentials,
7169  "Internal error", __FUNCTION__, __LINE__,
7170  "An internal error occurred while creating a new agent. "
7171  "It is unclear whether the agent has been created or not. "
7172  "Diagnostics: Internal Error.",
7173  "/omp?cmd=get_agents", response_data);
7174  }
7175 
7176  if (entity_attribute (entity, "id"))
7177  params_add (params, "agent_id", entity_attribute (entity, "id"));
7178  html = response_from_entity (credentials, params, entity,
7179  (no_redirect && strcmp (no_redirect, "0")),
7180  NULL, "get_agents",
7181  NULL, "new_agent",
7182  "Create Agent", response_data);
7183  free_entity (entity);
7184  g_free (response);
7185  return html;
7186 }
7187 
7197 char *
7198 delete_agent_omp (credentials_t * credentials, params_t *params,
7199  cmd_response_data_t* response_data)
7200 {
7201  return delete_resource ("agent", credentials, params, 0, "get_agents",
7202  response_data);
7203 }
7204 
7218 int
7220  params_t *params,
7221  gsize *result_len,
7222  char ** html,
7223  char ** filename,
7224  cmd_response_data_t* response_data)
7225 {
7226  entity_t entity;
7227  openvas_connection_t connection;
7228  gchar *connect_html;
7229  const char *agent_id, *format;
7230 
7231  agent_id = params_value (params, "agent_id");
7232  format = params_value (params, "agent_format");
7233 
7234  if ((agent_id == NULL) || (format == NULL))
7235  {
7236  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
7237  *html = gsad_message (credentials,
7238  "Internal error", __FUNCTION__, __LINE__,
7239  "An internal error occurred while downloading "
7240  "an agent. "
7241  "Diagnostics: Required parameter was NULL.",
7242  "/omp?cmd=get_agents", response_data);
7243  return 1;
7244  }
7245 
7246  *result_len = 0;
7247 
7248  switch (manager_connect (credentials, &connection, &connect_html,
7249  response_data))
7250  {
7251  case 0:
7252  break;
7253  case -1:
7254  if (connect_html)
7255  {
7256  *html = connect_html;
7257  return 1;
7258  }
7259  /* Fall through. */
7260  default:
7261  {
7262  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7263  *html = gsad_message (credentials,
7264  "Internal error", __FUNCTION__, __LINE__,
7265  "An internal error occurred while getting the agent list. "
7266  "The current list of agents is not available. "
7267  "Diagnostics: Failure to connect to manager daemon.",
7268  "/omp?cmd=get_agents", response_data);
7269  return 1;
7270  }
7271  }
7272 
7273  /* Send the request. */
7274 
7275  if (openvas_connection_sendf (&connection,
7276  "<get_agents agent_id=\"%s\" format=\"%s\"/>",
7277  agent_id,
7278  format)
7279  == -1)
7280  {
7281  openvas_connection_close (&connection);
7282  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7283  *html = gsad_message (credentials,
7284  "Internal error", __FUNCTION__, __LINE__,
7285  "An internal error occurred while getting agent list. "
7286  "The current list of agents is not available. "
7287  "Diagnostics: Failure to send command to manager daemon.",
7288  "/omp?cmd=get_agents", response_data);
7289  return 1;
7290  }
7291 
7292  /* Read and handle the response. */
7293 
7294  if (strcmp (format, "installer") == 0
7295  || strcmp (format, "howto_install") == 0
7296  || strcmp (format, "howto_use") == 0)
7297  {
7298  gchar *package_decoded = NULL;
7299  entity_t package_entity = NULL, agent_entity;
7300 
7301  /* A base64 encoded package. */
7302 
7303  entity = NULL;
7304  if (read_entity_c (&connection, &entity))
7305  {
7306  openvas_connection_close (&connection);
7307  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7308  *html = gsad_message (credentials,
7309  "Internal error", __FUNCTION__, __LINE__,
7310  "An internal error occurred while getting a agent. "
7311  "The agent is not available. "
7312  "Diagnostics: Failure to receive response from manager daemon.",
7313  "/omp?cmd=get_agents", response_data);
7314  return 1;
7315  }
7316 
7317  agent_entity = entity_child (entity, "agent");
7318  if (agent_entity)
7319  package_entity = entity_child (agent_entity, "package");
7320  if (package_entity != NULL)
7321  {
7322  char *package_encoded = entity_text (package_entity);
7323  if (strlen (package_encoded))
7324  {
7325  package_decoded = (gchar *) g_base64_decode (package_encoded,
7326  result_len);
7327  if (package_decoded == NULL)
7328  {
7329  package_decoded = (gchar *) g_strdup ("");
7330  *result_len = 0;
7331  }
7332  }
7333  else
7334  {
7335  package_decoded = (gchar *) g_strdup ("");
7336  *result_len = 0;
7337  }
7338  openvas_connection_close (&connection);
7339  *html = package_decoded;
7340  if (filename)
7341  {
7342  entity_t filename_entity;
7343  filename_entity = entity_child (package_entity,
7344  "filename");
7345  if (filename_entity)
7346  *filename = g_strdup (entity_text (filename_entity));
7347  else
7348  *filename = NULL;
7349  if (!(*filename && strlen (*filename)))
7350  {
7351  g_free(*filename);
7352  *filename = g_strdup_printf ("agent-%s-%s", agent_id, format);
7353  }
7354  }
7355  free_entity (entity);
7356  return 0;
7357  }
7358  else
7359  {
7360  free_entity (entity);
7361  openvas_connection_close (&connection);
7362  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7363  *html = gsad_message (credentials,
7364  "Internal error", __FUNCTION__, __LINE__,
7365  "An internal error occurred while getting a agent. "
7366  "The agent could not be delivered. "
7367  "Diagnostics: Failure to receive agent from manager daemon.",
7368  "/omp?cmd=get_tasks", response_data);
7369  return 1;
7370  }
7371  }
7372  else
7373  {
7374  /* An error. */
7375 
7376  entity = NULL;
7377  if (read_entity_c (&connection, &entity))
7378  {
7379  openvas_connection_close (&connection);
7380  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7381  *html = gsad_message (credentials,
7382  "Internal error", __FUNCTION__, __LINE__,
7383  "An internal error occurred while getting a agent. "
7384  "The agent could not be delivered. "
7385  "Diagnostics: Failure to receive agent from manager daemon.",
7386  "/omp?cmd=get_tasks", response_data);
7387  return 1;
7388  }
7389  openvas_connection_close (&connection);
7390 
7391  free_entity (entity);
7392  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7393  *html = gsad_message (credentials,
7394  "Internal error", __FUNCTION__, __LINE__,
7395  "An internal error occurred while getting a agent. "
7396  "The agent could not be delivered. "
7397  "Diagnostics: Failure to parse agent from manager daemon.",
7398  "/omp?cmd=get_tasks", response_data);
7399  return 1;
7400  }
7401 }
7402 
7413 char *
7414 edit_agent (credentials_t * credentials, params_t *params,
7415  const char *extra_xml, cmd_response_data_t* response_data)
7416 {
7417  return edit_resource ("agent", credentials, params, NULL, extra_xml,
7418  response_data);
7419 }
7420 
7430 char *
7431 edit_agent_omp (credentials_t * credentials, params_t *params,
7432  cmd_response_data_t* response_data)
7433 {
7434  return edit_agent (credentials, params, NULL, response_data);
7435 }
7436 
7446 char *
7447 save_agent_omp (credentials_t * credentials, params_t *params,
7448  cmd_response_data_t* response_data)
7449 {
7450  int ret;
7451  gchar *html, *response;
7452  const char *no_redirect, *agent_id, *name, *comment;
7453  entity_t entity;
7454 
7455  no_redirect = params_value (params, "no_redirect");
7456  agent_id = params_value (params, "agent_id");
7457  name = params_value (params, "name");
7458  comment = params_value (params, "comment");
7459 
7460  CHECK_PARAM_INVALID (agent_id, "Save Agent", "edit_agent");
7461  CHECK_PARAM_INVALID (name, "Save Agent", "edit_agent");
7462  CHECK_PARAM_INVALID (comment, "Save Agent", "edit_agent");
7463 
7464  /* Modify the agent. */
7465 
7466  response = NULL;
7467  entity = NULL;
7468  ret = ompf (credentials,
7469  &response,
7470  &entity,
7471  response_data,
7472  "<modify_agent agent_id=\"%s\">"
7473  "<name>%s</name>"
7474  "<comment>%s</comment>"
7475  "</modify_agent>",
7476  agent_id,
7477  name,
7478  comment);
7479 
7480  switch (ret)
7481  {
7482  case 0:
7483  case -1:
7484  break;
7485  case 1:
7486  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7487  return gsad_message (credentials,
7488  "Internal error", __FUNCTION__, __LINE__,
7489  "An internal error occurred while saving a agent. "
7490  "The agent was not saved. "
7491  "Diagnostics: Failure to send command to manager daemon.",
7492  "/omp?cmd=get_agents", response_data);
7493  case 2:
7494  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7495  return gsad_message (credentials,
7496  "Internal error", __FUNCTION__, __LINE__,
7497  "An internal error occurred while saving a agent. "
7498  "It is unclear whether the agent has been saved or not. "
7499  "Diagnostics: Failure to receive response from manager daemon.",
7500  "/omp?cmd=get_agents", response_data);
7501  default:
7502  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7503  return gsad_message (credentials,
7504  "Internal error", __FUNCTION__, __LINE__,
7505  "An internal error occurred while saving a agent. "
7506  "It is unclear whether the agent has been saved or not. "
7507  "Diagnostics: Internal Error.",
7508  "/omp?cmd=get_agents", response_data);
7509  }
7510 
7511  html = response_from_entity (credentials, params, entity,
7512  (no_redirect && strcmp (no_redirect, "0")),
7513  NULL, "get_agents",
7514  NULL, "edit_agent",
7515  "Save Agent", response_data);
7516  free_entity (entity);
7517  g_free (response);
7518  return html;
7519 }
7520 
7531 char *
7532 get_agent (credentials_t * credentials, params_t *params,
7533  const char *extra_xml, cmd_response_data_t* response_data)
7534 {
7535  return get_one ("agent", credentials, params, extra_xml, NULL, response_data);
7536 }
7537 
7547 char *
7548 get_agent_omp (credentials_t * credentials, params_t *params,
7549  cmd_response_data_t* response_data)
7550 {
7551  return get_agent (credentials, params, NULL, response_data);
7552 }
7553 
7564 char *
7565 get_agents (credentials_t * credentials, params_t *params,
7566  const char *extra_xml, cmd_response_data_t* response_data)
7567 {
7568  return get_many ("agent", credentials, params, extra_xml, NULL,
7569  response_data);
7570 }
7571 
7581 char *
7582 get_agents_omp (credentials_t * credentials, params_t *params,
7583  cmd_response_data_t* response_data)
7584 {
7585  return get_agents (credentials, params, NULL, response_data);
7586 }
7587 
7597 char *
7598 verify_agent_omp (credentials_t * credentials, params_t *params,
7599  cmd_response_data_t* response_data)
7600 {
7601  gchar *html, *response;
7602  const char *agent_id;
7603  int ret;
7604  entity_t entity;
7605 
7606  agent_id = params_value (params, "agent_id");
7607  if (agent_id == NULL)
7608  {
7609  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
7610  return gsad_message (credentials,
7611  "Internal error", __FUNCTION__, __LINE__,
7612  "An internal error occurred while verifying an agent. "
7613  "Diagnostics: Required parameter was NULL.",
7614  "/omp?cmd=get_agents", response_data);
7615  }
7616  response = NULL;
7617  entity = NULL;
7618  ret = ompf (credentials,
7619  &response,
7620  &entity,
7621  response_data,
7622  "<verify_agent agent_id=\"%s\" />",
7623  agent_id);
7624 
7625  switch (ret)
7626  {
7627  case 0:
7628  case -1:
7629  break;
7630  case 1:
7631  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7632  return gsad_message (credentials,
7633  "Internal error", __FUNCTION__, __LINE__,
7634  "An internal error occurred while verifying a agent. "
7635  "The agent was not verified. "
7636  "Diagnostics: Failure to send command to manager daemon.",
7637  "/omp?cmd=get_agents", response_data);
7638  case 2:
7639  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7640  return gsad_message (credentials,
7641  "Internal error", __FUNCTION__, __LINE__,
7642  "An internal error occurred while verifying a agent. "
7643  "It is unclear whether the agent was verified or not. "
7644  "Diagnostics: Failure to send command to manager daemon.",
7645  "/omp?cmd=get_agents", response_data);
7646  default:
7647  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7648  return gsad_message (credentials,
7649  "Internal error", __FUNCTION__, __LINE__,
7650  "An internal error occurred while verifying a agent. "
7651  "It is unclear whether the agent was verified or not. "
7652  "Diagnostics: Failure to send command to manager daemon.",
7653  "/omp?cmd=get_agents", response_data);
7654  }
7655 
7656  if (omp_success (entity))
7657  {
7658  html = next_page (credentials, params, response, response_data);
7659  if (html == NULL)
7660  {
7661  free_entity (entity);
7662  g_free (response);
7663  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7664  return gsad_message (credentials,
7665  "Internal error", __FUNCTION__, __LINE__,
7666  "An internal error occurred while verifying a agent. "
7667  "It is unclear whether the agent was verified or not. "
7668  "Diagnostics: Failure to receive response from manager daemon.",
7669  "/omp?cmd=get_agents", response_data);
7670  }
7671  }
7672  else
7673  {
7674  set_http_status_from_entity (entity, response_data);
7675  html = get_agents (credentials, params, response, response_data);
7676  }
7677  free_entity (entity);
7678  g_free (response);
7679  return html;
7680 }
7681 
7694 char *
7695 export_agent_omp (credentials_t * credentials, params_t *params,
7696  enum content_type * content_type, char **content_disposition,
7697  gsize *content_length, cmd_response_data_t* response_data)
7698 {
7699  return export_resource ("agent", credentials, params, content_type,
7700  content_disposition, content_length, response_data);
7701 }
7702 
7716 char *
7717 export_agents_omp (credentials_t * credentials, params_t *params,
7718  enum content_type * content_type, char **content_disposition,
7719  gsize *content_length, cmd_response_data_t* response_data)
7720 {
7721  return export_many ("agent", credentials, params, content_type,
7722  content_disposition, content_length, response_data);
7723 }
7724 
7734 char *
7735 get_aggregate_omp (credentials_t * credentials, params_t *params,
7736  cmd_response_data_t* response_data)
7737 {
7738  params_t *data_columns, *text_columns;
7739  params_t *sort_fields, *sort_stats, *sort_orders;
7740  params_iterator_t data_columns_iterator, text_columns_iterator;
7741  params_iterator_t sort_fields_iterator, sort_stats_iterator;
7742  params_iterator_t sort_orders_iterator;
7743  char *param_name;
7744  param_t *param;
7745 
7746  const char *data_column, *group_column, *subgroup_column, *type;
7747  const char *filter, *filt_id, *xml_param;
7748  const char *first_group, *max_groups;
7749  const char *mode;
7750  gchar *filter_escaped, *command_escaped, *response;
7751  entity_t entity;
7752  GString *xml, *command;
7753  int ret;
7754 
7755  data_columns = params_values (params, "data_columns:");
7756  data_column = params_value (params, "data_column");
7757  text_columns = params_values (params, "text_columns:");
7758  group_column = params_value (params, "group_column");
7759  subgroup_column = params_value (params, "subgroup_column");
7760  type = params_value (params, "aggregate_type");
7761  filter = params_value (params, "filter");
7762  filt_id = params_value (params, "filt_id");
7763  sort_fields = params_values (params, "sort_fields:");
7764  sort_stats = params_values (params, "sort_stats:");
7765  sort_orders = params_values (params, "sort_orders:");
7766  first_group = params_value (params, "first_group");
7767  max_groups = params_value (params, "max_groups");
7768  mode = params_value (params, "aggregate_mode");
7769  if (filter)
7770  filter_escaped = g_markup_escape_text (filter, -1);
7771  else
7772  {
7773  if (filt_id == NULL
7774  || strcmp (filt_id, "") == 0
7775  || strcmp (filt_id, "0") == 0)
7776  filter_escaped = g_strdup ("rows=-2");
7777  else
7778  filter_escaped = NULL;
7779  }
7780  xml_param = params_value (params, "xml");
7781 
7782  if (xml_param == NULL || atoi (xml_param) == 0)
7783  {
7784  return xsl_transform_omp (credentials, g_strdup ("<get_aggregate/>"),
7785  response_data);
7786  }
7787  xml = g_string_new ("<get_aggregate>");
7788 
7789  command = g_string_new ("<get_aggregates");
7790  g_string_append_printf (command, " type=\"%s\"", type);
7791  if (data_column)
7792  g_string_append_printf (command, " data_column=\"%s\"", data_column);
7793  if (group_column)
7794  g_string_append_printf (command, " group_column=\"%s\"", group_column);
7795  if (subgroup_column)
7796  g_string_append_printf (command,
7797  " subgroup_column=\"%s\"", subgroup_column);
7798  if (filter_escaped && strcmp (filter_escaped, ""))
7799  g_string_append_printf (command, " filter=\"%s\"", filter_escaped);
7800  if (filt_id && strcmp (filt_id, ""))
7801  g_string_append_printf (command, " filt_id=\"%s\"", filt_id);
7802  if (first_group && strcmp (first_group, ""))
7803  g_string_append_printf (command, " first_group=\"%s\"", first_group);
7804  if (max_groups && strcmp (max_groups, ""))
7805  g_string_append_printf (command, " max_groups=\"%s\"", max_groups);
7806  if (mode && strcmp (mode, ""))
7807  g_string_append_printf (command, " mode=\"%s\"", mode);
7808  g_string_append (command, ">");
7809 
7810  if (sort_fields && sort_stats && sort_orders)
7811  {
7812  param_t *field_param, *stat_param, *order_param;
7813  gchar *field_i, *stat_i, *order_i;
7814 
7815  params_iterator_init (&sort_fields_iterator, sort_fields);
7816  params_iterator_init (&sort_stats_iterator, sort_stats);
7817  params_iterator_init (&sort_orders_iterator, sort_orders);
7818 
7819  while (params_iterator_next (&sort_fields_iterator,
7820  &field_i, &field_param)
7821  && params_iterator_next (&sort_stats_iterator,
7822  &stat_i, &stat_param)
7823  && params_iterator_next (&sort_orders_iterator,
7824  &order_i, &order_param))
7825  {
7826  if (field_param->valid && stat_param->valid && order_param->valid)
7827  {
7828  xml_string_append (command,
7829  "<sort field=\"%s\""
7830  " stat=\"%s\""
7831  " order=\"%s\"/>",
7832  field_param->value ? field_param->value : "",
7833  stat_param->value ? stat_param->value : "",
7834  order_param->value ? order_param->value : "");
7835  }
7836  }
7837  }
7838 
7839  if (data_columns)
7840  {
7841  params_iterator_init (&data_columns_iterator, data_columns);
7842  while (params_iterator_next (&data_columns_iterator, &param_name, &param))
7843  {
7844  if (param->valid)
7845  {
7846  xml_string_append (command,
7847  "<data_column>%s</data_column>",
7848  param->value);
7849  }
7850  }
7851  }
7852 
7853  if (text_columns)
7854  {
7855  params_iterator_init (&text_columns_iterator, text_columns);
7856  while (params_iterator_next (&text_columns_iterator, &param_name, &param))
7857  {
7858  if (param->valid)
7859  {
7860  xml_string_append (command,
7861  "<text_column>%s</text_column>",
7862  param->value);
7863  }
7864  }
7865  }
7866 
7867  g_string_append (command, "</get_aggregates>");
7868 
7869  g_free (filter_escaped);
7870 
7871  command_escaped = g_markup_escape_text (command->str, -1);
7872  g_string_append (xml, command_escaped);
7873  g_free (command_escaped);
7874 
7875  ret = omp (credentials, &response, &entity, response_data, command->str);
7876  g_string_free (command, TRUE);
7877  switch (ret)
7878  {
7879  case 0:
7880  break;
7881  case -1:
7882  /* 'omp' set response. */
7883  return response;
7884  case 1:
7885  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7886  return gsad_message (credentials,
7887  "Internal error", __FUNCTION__, __LINE__,
7888  "An internal error occurred while getting aggregates. "
7889  "Diagnostics: Failure to send command to manager daemon.",
7890  "/omp?cmd=get_tasks", response_data);
7891  case 2:
7892  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7893  return gsad_message (credentials,
7894  "Internal error", __FUNCTION__, __LINE__,
7895  "An internal error occurred while getting aggregates. "
7896  "Diagnostics: Failure to receive response from manager daemon.",
7897  "/omp?cmd=get_tasks", response_data);
7898  default:
7899  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7900  return gsad_message (credentials,
7901  "Internal error", __FUNCTION__, __LINE__,
7902  "An internal error occurred while getting aggregates. "
7903  "Diagnostics: Internal Error.",
7904  "/omp?cmd=get_tasks", response_data);
7905  }
7906 
7907  g_string_append (xml, response);
7908 
7909  g_string_append (xml, "</get_aggregate>");
7910 
7911  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
7912  response_data);
7913 }
7914 
7925 static char *
7926 new_alert (credentials_t *credentials, params_t *params, const char *extra_xml,
7927  cmd_response_data_t* response_data)
7928 {
7929  GString *xml;
7930  int ret;
7931  entity_t entity;
7932  gchar *response;
7933 
7934  xml = g_string_new ("<new_alert>");
7935  if (extra_xml)
7936  g_string_append (xml, extra_xml);
7937 
7938  /* Get Report Formats. */
7939 
7940  response = NULL;
7941  entity = NULL;
7942  ret = omp (credentials, &response, &entity, response_data,
7943  "<get_report_formats filter=\"rows=-1\"/>");
7944  switch (ret)
7945  {
7946  case 0:
7947  case -1:
7948  break;
7949  case 1:
7950  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7951  return gsad_message (credentials,
7952  "Internal error", __FUNCTION__, __LINE__,
7953  "An internal error occurred while getting Report "
7954  "Formats for new alert. "
7955  "Diagnostics: Failure to send command to manager daemon.",
7956  "/omp?cmd=get_alerts", response_data);
7957  case 2:
7958  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7959  return gsad_message (credentials,
7960  "Internal error", __FUNCTION__, __LINE__,
7961  "An internal error occurred while getting Report "
7962  "Formats for new alert. "
7963  "Diagnostics: Failure to receive response from manager daemon.",
7964  "/omp?cmd=get_alerts", response_data);
7965  default:
7966  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7967  return gsad_message (credentials,
7968  "Internal error", __FUNCTION__, __LINE__,
7969  "An internal error occurred while getting Report "
7970  "Formats for new alert. It is unclear whether"
7971  " the alert has been saved or not. "
7972  "Diagnostics: Internal Error.",
7973  "/omp?cmd=get_alerts", response_data);
7974  }
7975  g_string_append (xml, response);
7976  g_free (response);
7977  free_entity (entity);
7978 
7979  /* Get Report Filters. */
7980 
7981  ret = omp (credentials, &response, &entity, response_data,
7982  "<get_filters filter=\"rows=-1\"/>");
7983 
7984  switch (ret)
7985  {
7986  case 0:
7987  case -1:
7988  break;
7989  case 1:
7990  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
7991  return gsad_message (credentials,
7992  "Internal error", __FUNCTION__, __LINE__,
7993  "An internal error occurred while getting Report "
7994  "Filters for new alert. "
7995  "The task was not saved. "
7996  "Diagnostics: Failure to send command to manager daemon.",
7997  "/omp?cmd=get_alerts", response_data);
7998  case 2:
7999  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8000  return gsad_message (credentials,
8001  "Internal error", __FUNCTION__, __LINE__,
8002  "An internal error occurred while getting Report "
8003  "Filters for new alert. "
8004  "Diagnostics: Failure to receive response from manager daemon.",
8005  "/omp?cmd=get_alerts", response_data);
8006  default:
8007  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8008  return gsad_message (credentials,
8009  "Internal error", __FUNCTION__, __LINE__,
8010  "An internal error occurred while getting Report "
8011  "Filters for new alert. "
8012  "Diagnostics: Internal Error.",
8013  "/omp?cmd=get_alerts", response_data);
8014  }
8015  g_string_append (xml, response);
8016  g_free (response);
8017  free_entity (entity);
8018 
8019  /* Get Tasks. */
8020 
8021  ret = omp (credentials, &response, &entity, response_data,
8022  "<get_tasks"
8023  " schedules_only=\"1\""
8024  " filter=\"owner=any permission=start_task rows=-1\"/>");
8025 
8026  switch (ret)
8027  {
8028  case 0:
8029  case -1:
8030  break;
8031  case 1:
8032  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8033  return gsad_message (credentials,
8034  "Internal error", __FUNCTION__, __LINE__,
8035  "An internal error occurred while getting Tasks"
8036  " for new alert. "
8037  "The task was not saved. "
8038  "Diagnostics: Failure to send command to manager daemon.",
8039  "/omp?cmd=get_alerts",
8040  response_data);
8041  case 2:
8042  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8043  return gsad_message (credentials,
8044  "Internal error", __FUNCTION__, __LINE__,
8045  "An internal error occurred while getting Tasks"
8046  " for new alert. "
8047  "Diagnostics: Failure to receive response from manager daemon.",
8048  "/omp?cmd=get_alerts",
8049  response_data);
8050  default:
8051  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8052  return gsad_message (credentials,
8053  "Internal error", __FUNCTION__, __LINE__,
8054  "An internal error occurred while getting Tasks"
8055  " for new alert. "
8056  "Diagnostics: Internal Error.",
8057  "/omp?cmd=get_alerts",
8058  response_data);
8059  }
8060  g_string_append (xml, response);
8061  g_free (response);
8062  free_entity (entity);
8063 
8064  /* Get Credentials. */
8065 
8066  ret = omp (credentials, &response, &entity, response_data,
8067  "<get_credentials"
8068  " filter=\"type=up owner=any permission=any rows=-1\"/>");
8069 
8070  switch (ret)
8071  {
8072  case 0:
8073  case -1:
8074  break;
8075  case 1:
8076  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8077  return gsad_message (credentials,
8078  "Internal error", __FUNCTION__, __LINE__,
8079  "An internal error occurred while getting"
8080  " Credentials for new alert. "
8081  "The task was not saved. "
8082  "Diagnostics: Failure to send command to manager daemon.",
8083  "/omp?cmd=get_alerts",
8084  response_data);
8085  case 2:
8086  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8087  return gsad_message (credentials,
8088  "Internal error", __FUNCTION__, __LINE__,
8089  "An internal error occurred while getting"
8090  " Credentials for new alert. "
8091  "Diagnostics: Failure to receive response from manager daemon.",
8092  "/omp?cmd=get_alerts",
8093  response_data);
8094  default:
8095  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8096  return gsad_message (credentials,
8097  "Internal error", __FUNCTION__, __LINE__,
8098  "An internal error occurred while getting"
8099  " Credentials for new alert. "
8100  "Diagnostics: Internal Error.",
8101  "/omp?cmd=get_alerts",
8102  response_data);
8103  }
8104 
8105  g_string_append (xml, response);
8106  g_free (response);
8107  free_entity (entity);
8108 
8109  g_string_append (xml, "</new_alert>");
8110  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
8111  response_data);
8112 }
8113 
8123 char *
8124 new_alert_omp (credentials_t *credentials, params_t *params,
8125  cmd_response_data_t* response_data)
8126 {
8127  return new_alert (credentials, params, NULL, response_data);
8128 }
8129 
8130 char *
8131 get_alerts (credentials_t *, params_t *, const char *, cmd_response_data_t*);
8132 
8142 static void
8143 append_alert_event_data (GString *xml, params_t *data, const char *event)
8144 {
8145  if (data)
8146  {
8147  params_iterator_t iter;
8148  char *name;
8149  param_t *param;
8150 
8151  params_iterator_init (&iter, data);
8152  while (params_iterator_next (&iter, &name, &param))
8153  if ((strcmp (event, "Task run status changed") == 0
8154  && strcmp (name, "status") == 0)
8155  || ((strcmp (event, "New SecInfo arrived") == 0
8156  || strcmp (event, "Updated SecInfo arrived") == 0)
8157  && strcmp (name, "secinfo_type") == 0))
8158  xml_string_append (xml,
8159  "<data><name>%s</name>%s</data>",
8160  name,
8161  param->value ? param->value : "");
8162  }
8163 }
8164 
8172 static void
8173 append_alert_condition_data (GString *xml, params_t *data,
8174  const char * condition)
8175 {
8176  if (data)
8177  {
8178  params_iterator_t iter;
8179  char *name;
8180  param_t *param;
8181 
8182  params_iterator_init (&iter, data);
8183  while (params_iterator_next (&iter, &name, &param))
8184  {
8185  if (strcmp (condition, "Filter count at least") == 0)
8186  {
8187  if (strcmp (name, "at_least_count") == 0)
8188  xml_string_append (xml,
8189  "<data><name>count</name>%s</data>",
8190  param->value ? param->value : "");
8191  if (strcmp (name, "at_least_filter_id") == 0)
8192  xml_string_append (xml,
8193  "<data><name>filter_id</name>%s</data>",
8194  param->value ? param->value : "");
8195  }
8196  else if (strcmp (condition, "Filter count changed") == 0)
8197  {
8198  if (strcmp (name, "count") == 0
8199  || strcmp (name, "filter_id") == 0)
8200  xml_string_append (xml,
8201  "<data><name>%s</name>%s</data>",
8202  name,
8203  param->value ? param->value : "");
8204  else if (strcmp (name, "filter_direction") == 0)
8205  xml_string_append (xml,
8206  "<data><name>direction</name>%s</data>",
8207  param->value ? param->value : "");
8208  }
8209  else if ((strcmp (condition, "Severity at least") == 0
8210  && strcmp (name, "severity") == 0)
8211  || (strcmp (condition, "Severity changed") == 0
8212  && strcmp (name, "direction") == 0))
8213  xml_string_append (xml,
8214  "<data><name>%s</name>%s</data>",
8215  name,
8216  param->value ? param->value : "");
8217  }
8218  }
8219 }
8220 
8228 static void
8229 append_alert_method_data (GString *xml, params_t *data, const char *method)
8230 {
8231  params_iterator_t iter;
8232  char *name;
8233  param_t *param;
8234  int notice;
8235 
8236  if (data == NULL)
8237  return;
8238 
8239  params_iterator_init (&iter, data);
8240  /* Used to check email notice type before sending report formats values */
8241  notice = 1;
8242  while (params_iterator_next (&iter, &name, &param))
8243  if (strcmp (name, "notice") == 0)
8244  {
8245  notice = atoi (param->value);
8246  break;
8247  }
8248 
8249  if (strcmp (method, "Sourcefire Connector"))
8250  {
8251  params_iterator_init (&iter, data);
8252 
8253  while (params_iterator_next (&iter, &name, &param))
8254  if ((strcmp (method, "HTTP Get") == 0
8255  && strcmp (name, "URL") == 0)
8256  || (strcmp (method, "Send") == 0
8257  && (strcmp (name, "send_host") == 0
8258  || strcmp (name, "send_port") == 0
8259  || strcmp (name, "send_report_format") == 0))
8260  || (strcmp (method, "SCP") == 0
8261  && (strcmp (name, "scp_credential") == 0
8262  || strcmp (name, "scp_host") == 0
8263  || strcmp (name, "scp_known_hosts") == 0
8264  || strcmp (name, "scp_path") == 0
8265  || strcmp (name, "scp_report_format") == 0))
8266  || (strcmp (method, "SNMP") == 0
8267  && (strcmp (name, "snmp_community") == 0
8268  || strcmp (name, "snmp_agent") == 0
8269  || strcmp (name, "snmp_message") == 0))
8270  || (strcmp (method, "verinice Connector") == 0
8271  && (strcmp (name, "verinice_server_credential") == 0
8272  || strcmp (name, "verinice_server_url") == 0
8273  || strcmp (name, "verinice_server_report_format") == 0))
8274  || (strcmp (method, "Email") == 0
8275  && (strcmp (name, "to_address") == 0
8276  || strcmp (name, "from_address") == 0
8277  || strcmp (name, "subject") == 0
8278  || strcmp (name, "notice") == 0
8279  || (strcmp (name, "notice_report_format") == 0
8280  && notice == 0)
8281  || (strcmp (name, "notice_attach_format") == 0
8282  && notice == 2)))
8283  || (strcmp (method, "Syslog") == 0
8284  && strcmp (name, "submethod") == 0)
8285  || (strcmp (method, "Start Task") == 0
8286  && strcmp (name, "start_task_task") == 0)
8287  || strcmp (name, "details_url") == 0)
8288  xml_string_append (xml,
8289  "<data><name>%s</name>%s</data>",
8290  name,
8291  param->value ? param->value : "");
8292  else if (strcmp (method, "Email") == 0
8293  && notice == 0
8294  && strcmp (name, "message") == 0)
8295  xml_string_append (xml,
8296  "<data><name>message</name>%s</data>",
8297  param->value ? param->value : "");
8298  else if (strcmp (method, "Email") == 0
8299  && notice == 2
8300  && strcmp (name, "message_attach") == 0)
8301  xml_string_append (xml,
8302  "<data><name>message</name>%s</data>",
8303  param->value ? param->value : "");
8304 
8305  return;
8306  }
8307 
8308  params_iterator_init (&iter, data);
8309  while (params_iterator_next (&iter, &name, &param))
8310  if (strcmp (name, "pkcs12"))
8311  {
8312  if (strcmp (name, "defense_center_ip") == 0
8313  || strcmp (name, "defense_center_port") == 0)
8314  xml_string_append (xml,
8315  "<data><name>%s</name>%s</data>",
8316  name,
8317  param->value ? param->value : "");
8318  }
8319  else
8320  {
8321  gchar *base64;
8322 
8323  /* Special case the pkcs12 file, which is binary. */
8324 
8325  base64 = (param->value && param->value_size)
8326  ? g_base64_encode ((guchar*) param->value,
8327  param->value_size)
8328  : g_strdup ("");
8329  xml_string_append (xml,
8330  "<data><name>%s</name>%s</data>",
8331  name,
8332  base64);
8333  g_free (base64);
8334  }
8335 }
8336 
8346 char *
8347 create_alert_omp (credentials_t * credentials, params_t *params,
8348  cmd_response_data_t* response_data)
8349 {
8350  int ret;
8351  gchar *html, *response;
8352  const char *no_redirect;
8353  const char *name, *comment, *condition, *event, *method, *filter_id;
8354  params_t *method_data, *event_data, *condition_data;
8355  entity_t entity;
8356  GString *xml;
8357 
8358  no_redirect = params_value (params, "no_redirect");
8359  name = params_value (params, "name");
8360  comment = params_value (params, "comment");
8361  condition = params_value (params, "condition");
8362  event = params_value (params, "event");
8363  method = params_value (params, "method");
8364  filter_id = params_value (params, "filter_id");
8365 
8366  CHECK_PARAM_INVALID (name, "Create Alert", "new_alert");
8367  CHECK_PARAM_INVALID (comment, "Create Alert", "new_alert");
8368  CHECK_PARAM_INVALID (condition, "Create Alert", "new_alert");
8369  CHECK_PARAM_INVALID (event, "Create Alert", "new_alert");
8370  CHECK_PARAM_INVALID (method, "Create Alert", "new_alert");
8371  CHECK_PARAM_INVALID (filter_id, "Create Alert", "new_alert");
8372 
8373  /* Create the alert. */
8374 
8375  method_data = params_values (params, "method_data:");
8376  event_data = params_values (params, "event_data:");
8377  condition_data = params_values (params, "condition_data:");
8378 
8379  xml = g_string_new ("");
8380 
8381  if ((strcmp (event, "New SecInfo arrived") == 0) && event_data)
8382  {
8383  params_iterator_t iter;
8384  char *name;
8385  param_t *param;
8386 
8387  params_iterator_init (&iter, event_data);
8388  while (params_iterator_next (&iter, &name, &param))
8389  if ((strcmp (name, "feed_event") == 0)
8390  && param->value
8391  && (strcmp (param->value, "updated") == 0))
8392  {
8393  event = "Updated SecInfo arrived";
8394  break;
8395  }
8396  }
8397 
8398  xml_string_append (xml,
8399  "<create_alert>"
8400  "<name>%s</name>"
8401  "<filter id=\"%s\"/>"
8402  "<comment>%s</comment>"
8403  "<event>%s",
8404  name,
8405  filter_id,
8406  comment ? comment : "",
8407  event);
8408 
8409  append_alert_event_data (xml, event_data, event);
8410 
8411  xml_string_append (xml,
8412  "</event>"
8413  "<method>%s",
8414  method);
8415 
8416  append_alert_method_data (xml, method_data, method);
8417 
8418  xml_string_append (xml,
8419  "</method>"
8420  "<condition>%s",
8421  condition);
8422 
8423  append_alert_condition_data (xml, condition_data, condition);
8424 
8425  xml_string_append (xml,
8426  "</condition>"
8427  "</create_alert>");
8428 
8429  ret = omp (credentials,
8430  &response,
8431  &entity,
8432  response_data,
8433  xml->str);
8434  g_string_free (xml, TRUE);
8435  switch (ret)
8436  {
8437  case 0:
8438  case -1:
8439  break;
8440  case 1:
8441  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8442  return gsad_message (credentials,
8443  "Internal error", __FUNCTION__, __LINE__,
8444  "An internal error occurred while creating a new alert. "
8445  "No new alert was created. "
8446  "Diagnostics: Failure to send command to manager daemon.",
8447  "/omp?cmd=get_alerts", response_data);
8448  case 2:
8449  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8450  return gsad_message (credentials,
8451  "Internal error", __FUNCTION__, __LINE__,
8452  "An internal error occurred while creating a new alert. "
8453  "It is unclear whether the alert has been created or not. "
8454  "Diagnostics: Failure to receive response from manager daemon.",
8455  "/omp?cmd=get_alerts", response_data);
8456  default:
8457  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8458  return gsad_message (credentials,
8459  "Internal error", __FUNCTION__, __LINE__,
8460  "An internal error occurred while creating a new alert. "
8461  "It is unclear whether the alert has been created or not. "
8462  "Diagnostics: Internal Error.",
8463  "/omp?cmd=get_alerts", response_data);
8464  }
8465 
8466  if (entity_attribute (entity, "id"))
8467  params_add (params, "alert_id", entity_attribute (entity, "id"));
8468  html = response_from_entity (credentials, params, entity,
8469  (no_redirect && strcmp (no_redirect, "0")),
8470  NULL, "get_alerts",
8471  NULL, "new_alert",
8472  "Create Alert", response_data);
8473  free_entity (entity);
8474  g_free (response);
8475  return html;
8476 }
8477 
8487 char *
8488 delete_alert_omp (credentials_t * credentials, params_t *params,
8489  cmd_response_data_t* response_data)
8490 {
8491  return delete_resource ("alert", credentials, params, 0, "get_alerts",
8492  response_data);
8493 }
8494 
8505 char *
8506 get_alert (credentials_t * credentials, params_t *params,
8507  const char *extra_xml, cmd_response_data_t* response_data)
8508 {
8509  gchar *html;
8510  GString *extra;
8511 
8512  extra = g_string_new ("");
8513  if (extra_xml)
8514  g_string_append (extra, extra_xml);
8515  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
8516  {
8517  gchar *response;
8518  entity_t entity;
8519 
8520  response = NULL;
8521  entity = NULL;
8522  switch (omp (credentials, &response, &entity, response_data,
8523  "<get_report_formats"
8524  " filter=\"rows=-1\"/>"))
8525  {
8526  case 0:
8527  case -1:
8528  break;
8529  case 1:
8530  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8531  return gsad_message (credentials,
8532  "Internal error", __FUNCTION__, __LINE__,
8533  "An internal error occurred while getting Report "
8534  "Formats for the alert. "
8535  "Diagnostics: Failure to send command to manager daemon.",
8536  "/omp?cmd=get_alerts", response_data);
8537  case 2:
8538  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8539  return gsad_message (credentials,
8540  "Internal error", __FUNCTION__, __LINE__,
8541  "An internal error occurred while getting Report "
8542  "Formats for the alert. "
8543  "Diagnostics: Failure to receive response from manager daemon.",
8544  "/omp?cmd=get_alerts", response_data);
8545  default:
8546  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8547  return gsad_message (credentials,
8548  "Internal error", __FUNCTION__, __LINE__,
8549  "An internal error occurred while getting Report "
8550  "Formats for the alert. "
8551  "It is unclear whether the task has been saved or not. "
8552  "Diagnostics: Internal Error.",
8553  "/omp?cmd=get_alerts", response_data);
8554  }
8555 
8556  g_string_append (extra, response);
8557 
8558  free_entity (entity);
8559  g_free (response);
8560  }
8561 
8562  if (command_enabled (credentials, "GET_TASKS"))
8563  {
8564  gchar *response;
8565  entity_t entity;
8566 
8567  response = NULL;
8568  entity = NULL;
8569  switch (omp (credentials, &response, &entity, response_data,
8570  "<get_tasks"
8571  " schedules_only=\"1\""
8572  " filter=\"owner=any permission=start_task rows=-1\"/>"))
8573  {
8574  case 0:
8575  case -1:
8576  break;
8577  case 1:
8578  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8579  return gsad_message (credentials,
8580  "Internal error", __FUNCTION__, __LINE__,
8581  "An internal error occurred while getting Tasks "
8582  "for the alert. "
8583  "Diagnostics: Failure to send command to manager daemon.",
8584  "/omp?cmd=get_alerts", response_data);
8585  case 2:
8586  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8587  return gsad_message (credentials,
8588  "Internal error", __FUNCTION__, __LINE__,
8589  "An internal error occurred while getting Tasks "
8590  "for the alert. "
8591  "Diagnostics: Failure to receive response from manager daemon.",
8592  "/omp?cmd=get_alerts", response_data);
8593  default:
8594  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8595  return gsad_message (credentials,
8596  "Internal error", __FUNCTION__, __LINE__,
8597  "An internal error occurred while getting Tasks "
8598  "for the alert. "
8599  "Diagnostics: Internal Error.",
8600  "/omp?cmd=get_alerts", response_data);
8601  }
8602 
8603  g_string_append (extra, response);
8604 
8605  free_entity (entity);
8606  g_free (response);
8607  }
8608 
8609  if (command_enabled (credentials, "GET_FILTERS"))
8610  {
8611  gchar *response;
8612  entity_t entity;
8613 
8614  /* Get result filters for condition link. */
8615 
8616  response = NULL;
8617  entity = NULL;
8618  switch (omp (credentials, &response, &entity, response_data,
8619  "<get_filters"
8620  " filter=\"type=result rows=-1\"/>"))
8621  {
8622  case 0:
8623  case -1:
8624  break;
8625  case 1:
8626  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8627  return gsad_message (credentials,
8628  "Internal error", __FUNCTION__, __LINE__,
8629  "An internal error occurred while getting filters "
8630  "for the alert. "
8631  "Diagnostics: Failure to send command to manager daemon.",
8632  "/omp?cmd=get_alerts", response_data);
8633  case 2:
8634  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8635  return gsad_message (credentials,
8636  "Internal error", __FUNCTION__, __LINE__,
8637  "An internal error occurred while getting filters "
8638  "for the alert. "
8639  "Diagnostics: Failure to receive response from manager daemon.",
8640  "/omp?cmd=get_alerts", response_data);
8641  default:
8642  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8643  return gsad_message (credentials,
8644  "Internal error", __FUNCTION__, __LINE__,
8645  "An internal error occurred while getting filters "
8646  "for the alert. "
8647  "Diagnostics: Internal Error.",
8648  "/omp?cmd=get_alerts", response_data);
8649  }
8650 
8651  g_string_append (extra, response);
8652 
8653  free_entity (entity);
8654  g_free (response);
8655  }
8656 
8657  html = get_one ("alert", credentials, params, extra->str, "tasks=\"1\"",
8658  response_data);
8659  g_string_free (extra, TRUE);
8660  return html;
8661 }
8662 
8672 char *
8673 get_alert_omp (credentials_t * credentials, params_t *params,
8674  cmd_response_data_t* response_data)
8675 {
8676  return get_alert (credentials, params, NULL, response_data);
8677 }
8678 
8689 char *
8690 get_alerts (credentials_t * credentials, params_t *params,
8691  const char *extra_xml, cmd_response_data_t* response_data)
8692 {
8693  gchar *html;
8694  GString *extra;
8695 
8696  extra = g_string_new ("");
8697  if (command_enabled (credentials, "GET_TASKS"))
8698  {
8699  gchar *response;
8700  entity_t entity;
8701 
8702  response = NULL;
8703  entity = NULL;
8704  switch (omp (credentials, &response, &entity, response_data,
8705  "<get_tasks"
8706  " schedules_only=\"1\""
8707  " filter=\"owner=any permission=start_task rows=-1\"/>"))
8708  {
8709  case 0:
8710  case -1:
8711  break;
8712  case 1:
8713  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8714  return gsad_message (credentials,
8715  "Internal error", __FUNCTION__, __LINE__,
8716  "An internal error occurred getting the tasks. "
8717  "Diagnostics: Failure to send command to manager daemon.",
8718  "/omp?cmd=get_tasks",
8719  response_data);
8720  case 2:
8721  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8722  return gsad_message (credentials,
8723  "Internal error", __FUNCTION__, __LINE__,
8724  "An internal error occurred getting the tasks. "
8725  "Diagnostics: Failure to receive response from manager daemon.",
8726  "/omp?cmd=get_tasks",
8727  response_data);
8728  default:
8729  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8730  return gsad_message (credentials,
8731  "Internal error", __FUNCTION__, __LINE__,
8732  "An internal error occurred getting the reports. "
8733  "Diagnostics: Internal Error.",
8734  "/omp?cmd=get_tasks",
8735  response_data);
8736  }
8737 
8738  g_string_append (extra, response);
8739 
8740  free_entity (entity);
8741  g_free (response);
8742  }
8743 
8744 
8745  if (command_enabled (credentials, "GET_FILTERS"))
8746  {
8747  gchar *response;
8748  entity_t entity;
8749 
8750  /* Get result filters for condition link. */
8751 
8752  response = NULL;
8753  entity = NULL;
8754  switch (omp (credentials, &response, &entity, response_data,
8755  "<get_filters"
8756  " filter=\"type=result rows=-1\"/>"))
8757  {
8758  case 0:
8759  case -1:
8760  break;
8761  case 1:
8762  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8763  return gsad_message (credentials,
8764  "Internal error", __FUNCTION__, __LINE__,
8765  "An internal error occurred while getting filters "
8766  "for the alerts. "
8767  "Diagnostics: Failure to send command to manager daemon.",
8768  "/omp?cmd=get_tasks", response_data);
8769  case 2:
8770  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8771  return gsad_message (credentials,
8772  "Internal error", __FUNCTION__, __LINE__,
8773  "An internal error occurred while getting filters "
8774  "for the alerts. "
8775  "Diagnostics: Failure to receive response from manager daemon.",
8776  "/omp?cmd=get_tasks", response_data);
8777  default:
8778  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8779  return gsad_message (credentials,
8780  "Internal error", __FUNCTION__, __LINE__,
8781  "An internal error occurred while getting filters "
8782  "for the alerts. "
8783  "Diagnostics: Internal Error.",
8784  "/omp?cmd=get_tasks", response_data);
8785  }
8786 
8787  g_string_append (extra, response);
8788 
8789  free_entity (entity);
8790  g_free (response);
8791  }
8792 
8793  if (extra_xml)
8794  g_string_append (extra, extra_xml);
8795  html = get_many ("alert", credentials, params, extra->str, NULL,
8796  response_data);
8797  g_string_free (extra, TRUE);
8798  return html;
8799 }
8800 
8810 char *
8811 get_alerts_omp (credentials_t * credentials, params_t *params,
8812  cmd_response_data_t* response_data)
8813 {
8814  return get_alerts (credentials, params, NULL, response_data);
8815 }
8816 
8827 char *
8828 edit_alert (credentials_t * credentials, params_t *params,
8829  const char *extra_xml, cmd_response_data_t* response_data)
8830 {
8831  GString *xml;
8832  openvas_connection_t connection;
8833  gchar *html, *edit;
8834  const char *alert_id, *next, *filter;
8835 
8836  alert_id = params_value (params, "alert_id");
8837  next = params_value (params, "next");
8838  filter = params_value (params, "filter");
8839 
8840  if (alert_id == NULL)
8841  {
8842  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
8843  return gsad_message (credentials,
8844  "Internal error", __FUNCTION__, __LINE__,
8845  "An internal error occurred while editing an alert. "
8846  "The alert remains as it was. "
8847  "Diagnostics: Required parameter alert_id was NULL.",
8848  "/omp?cmd=get_alerts", response_data);
8849  }
8850 
8851  if (next == NULL)
8852  next = "get_alerts";
8853 
8854  switch (manager_connect (credentials, &connection, &html,
8855  response_data))
8856  {
8857  case 0:
8858  break;
8859  case -1:
8860  if (html)
8861  return html;
8862  /* Fall through. */
8863  default:
8864  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8865  return gsad_message (credentials,
8866  "Internal error", __FUNCTION__, __LINE__,
8867  "An internal error occurred while editing an alert. "
8868  "The alert remains as it was. "
8869  "Diagnostics: Failure to connect to manager daemon.",
8870  "/omp?cmd=get_alerts", response_data);
8871  }
8872 
8873  if (openvas_connection_sendf (&connection,
8874  "<get_alerts"
8875  " alert_id=\"%s\""
8876  " details=\"1\"/>",
8877  alert_id)
8878  == -1)
8879  {
8880  openvas_connection_close (&connection);
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 alert info. "
8885  "Diagnostics: Failure to send command to manager daemon.",
8886  "/omp?cmd=get_alerts", response_data);
8887  }
8888 
8889  xml = g_string_new ("<edit_alert>");
8890 
8891  if (extra_xml)
8892  g_string_append (xml, extra_xml);
8893 
8894  edit = g_markup_printf_escaped ("<alert id=\"%s\"/>"
8895  /* Page that follows. */
8896  "<next>%s</next>"
8897  /* Passthroughs. */
8898  "<filters><term>%s</term></filters>",
8899  alert_id,
8900  next,
8901  filter);
8902  g_string_append (xml, edit);
8903  g_free (edit);
8904 
8905  if (read_string_c (&connection, &xml))
8906  {
8907  g_string_free (xml, TRUE);
8908  openvas_connection_close (&connection);
8909  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8910  return gsad_message (credentials,
8911  "Internal error", __FUNCTION__, __LINE__,
8912  "An internal error occurred while getting alert info. "
8913  "Diagnostics: Failure to receive response from manager daemon.",
8914  "/omp?cmd=get_alerts", response_data);
8915  }
8916 
8917  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
8918  {
8919  /* Get the report formats. */
8920 
8921  if (openvas_connection_sendf (&connection,
8922  "<get_report_formats"
8923  " filter=\"rows=-1\"/>")
8924  == -1)
8925  {
8926  g_string_free (xml, TRUE);
8927  openvas_connection_close (&connection);
8928  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8929  return gsad_message (credentials,
8930  "Internal error", __FUNCTION__, __LINE__,
8931  "An internal error occurred while getting report formats. "
8932  "The current list of report formats is not available. "
8933  "Diagnostics: Failure to send command to manager daemon.",
8934  "/omp?cmd=get_alerts", response_data);
8935  }
8936 
8937  if (read_string_c (&connection, &xml))
8938  {
8939  g_string_free (xml, TRUE);
8940  openvas_connection_close (&connection);
8941  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8942  return gsad_message (credentials,
8943  "Internal error", __FUNCTION__, __LINE__,
8944  "An internal error occurred while getting report formats. "
8945  "The current list of report formats is not available. "
8946  "Diagnostics: Failure to receive response from manager daemon.",
8947  "/omp?cmd=get_alerts", response_data);
8948  }
8949  }
8950 
8951  if (command_enabled (credentials, "GET_FILTERS"))
8952  {
8953  /* Get filters. */
8954 
8955  if (openvas_connection_sendf (&connection,
8956  "<get_filters filter=\"rows=-1\"/>")
8957  == -1)
8958  {
8959  g_string_free (xml, TRUE);
8960  openvas_connection_close (&connection);
8961  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8962  return gsad_message (credentials,
8963  "Internal error", __FUNCTION__, __LINE__,
8964  "An internal error occurred while getting the list "
8965  "of filters. "
8966  "The current list of filters is not available. "
8967  "Diagnostics: Failure to send command to manager daemon.",
8968  "/omp?cmd=get_alerts", response_data);
8969  }
8970 
8971  if (read_string_c (&connection, &xml))
8972  {
8973  g_string_free (xml, TRUE);
8974  openvas_connection_close (&connection);
8975  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
8976  return gsad_message (credentials,
8977  "Internal error", __FUNCTION__, __LINE__,
8978  "An internal error occurred while getting the list "
8979  "of filters. "
8980  "The current list of filters is not available. "
8981  "Diagnostics: Failure to receive response from manager daemon.",
8982  "/omp?cmd=get_filters", response_data);
8983  }
8984  }
8985 
8986  if (command_enabled (credentials, "GET_TASKS"))
8987  {
8988  /* Get tasks. */
8989 
8990  if (openvas_connection_sendf (&connection,
8991  "<get_tasks"
8992  " schedules_only=\"1\""
8993  " filter=\"owner=any permission=start_task"
8994  " rows=-1\"/>")
8995  == -1)
8996  {
8997  g_string_free (xml, TRUE);
8998  openvas_connection_close (&connection);
8999  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9000  return gsad_message (credentials,
9001  "Internal error", __FUNCTION__, __LINE__,
9002  "An internal error occurred while getting the list "
9003  "of tasks. "
9004  "The current list of tasks is not available. "
9005  "Diagnostics: Failure to send command to manager daemon.",
9006  "/omp?cmd=get_alerts", response_data);
9007  }
9008 
9009  if (read_string_c (&connection, &xml))
9010  {
9011  g_string_free (xml, TRUE);
9012  openvas_connection_close (&connection);
9013  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9014  return gsad_message (credentials,
9015  "Internal error", __FUNCTION__, __LINE__,
9016  "An internal error occurred while getting the list "
9017  "of tasks. "
9018  "The current list of tasks is not available. "
9019  "Diagnostics: Failure to receive response from manager daemon.",
9020  "/omp?cmd=get_tasks", response_data);
9021  }
9022  }
9023 
9024  /* Get Credentials. */
9025 
9026  if (command_enabled (credentials, "GET_CREDENTIALS"))
9027  {
9028  if (openvas_connection_sendf (&connection,
9029  "<get_credentials"
9030  " filter=\"type=up owner=any permission=any"
9031  " rows=-1\"/>")
9032  == -1)
9033  {
9034  g_string_free (xml, TRUE);
9035  openvas_connection_close (&connection);
9036  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9037  return gsad_message (credentials,
9038  "Internal error", __FUNCTION__, __LINE__,
9039  "An internal error occurred while getting the list "
9040  "of credentials. "
9041  "The current list of tasks is not available. "
9042  "Diagnostics: Failure to send command to manager daemon.",
9043  "/omp?cmd=get_alerts", response_data);
9044  }
9045 
9046  if (read_string_c (&connection, &xml))
9047  {
9048  g_string_free (xml, TRUE);
9049  openvas_connection_close (&connection);
9050  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9051  return gsad_message (credentials,
9052  "Internal error", __FUNCTION__, __LINE__,
9053  "An internal error occurred while getting the list "
9054  "of credentials. "
9055  "The current list of tasks is not available. "
9056  "Diagnostics: Failure to receive response from manager daemon.",
9057  "/omp?cmd=get_tasks", response_data);
9058  }
9059  }
9060 
9061  /* Cleanup, and return transformed XML. */
9062 
9063  g_string_append (xml, "</edit_alert>");
9064  openvas_connection_close (&connection);
9065  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
9066  response_data);
9067 }
9068 
9078 char *
9079 edit_alert_omp (credentials_t * credentials, params_t *params,
9080  cmd_response_data_t* response_data)
9081 {
9082  return edit_alert (credentials, params, NULL, response_data);
9083 }
9084 
9094 char *
9095 save_alert_omp (credentials_t * credentials, params_t *params,
9096  cmd_response_data_t* response_data)
9097 {
9098  GString *xml;
9099  int ret;
9100  gchar *html, *response;
9101  const char *no_redirect, *name, *comment, *alert_id;
9102  const char *event, *condition, *method;
9103  const char *filter_id;
9104  params_t *event_data, *condition_data, *method_data;
9105  entity_t entity;
9106 
9107  no_redirect = params_value (params, "no_redirect");
9108  name = params_value (params, "name");
9109  comment = params_value (params, "comment");
9110  condition = params_value (params, "condition");
9111  event = params_value (params, "event");
9112  method = params_value (params, "method");
9113  alert_id = params_value (params, "alert_id");
9114  filter_id = params_value (params, "filter_id");
9115 
9116  CHECK_PARAM_INVALID (name, "Save Alert", "edit_alert");
9117  CHECK_PARAM_INVALID (comment, "Save Alert", "edit_alert");
9118  CHECK_PARAM_INVALID (alert_id, "Save Alert", "edit_alert");
9119  CHECK_PARAM_INVALID (condition, "Save Alert", "edit_alert");
9120  CHECK_PARAM_INVALID (event, "Save Alert", "edit_alert");
9121  CHECK_PARAM_INVALID (method, "Save Alert", "edit_alert");
9122  CHECK_PARAM_INVALID (filter_id, "Save Alert", "edit_alert");
9123 
9124  xml = g_string_new ("");
9125 
9126  /* Modify the alert. */
9127 
9128  event_data = params_values (params, "event_data:");
9129  condition_data = params_values (params, "condition_data:");
9130  method_data = params_values (params, "method_data:");
9131 
9132  if ((strcmp (event, "New SecInfo arrived") == 0) && event_data)
9133  {
9134  params_iterator_t iter;
9135  char *name;
9136  param_t *param;
9137 
9138  params_iterator_init (&iter, event_data);
9139  while (params_iterator_next (&iter, &name, &param))
9140  if ((strcmp (name, "feed_event") == 0)
9141  && param->value
9142  && (strcmp (param->value, "updated") == 0))
9143  {
9144  event = "Updated SecInfo arrived";
9145  break;
9146  }
9147  }
9148 
9149  xml_string_append (xml,
9150  "<modify_alert alert_id=\"%s\">"
9151  "<name>%s</name>"
9152  "<filter id=\"%s\"/>"
9153  "<comment>%s</comment>"
9154  "<event>%s",
9155  alert_id,
9156  name,
9157  filter_id,
9158  comment ? comment : "",
9159  event);
9160 
9161  append_alert_event_data (xml, event_data, event);
9162 
9163  xml_string_append (xml,
9164  "</event>"
9165  "<method>%s",
9166  method);
9167 
9168  append_alert_method_data (xml, method_data, method);
9169 
9170  xml_string_append (xml,
9171  "</method>"
9172  "<condition>%s",
9173  condition);
9174 
9175  append_alert_condition_data (xml, condition_data, condition);
9176 
9177  xml_string_append (xml,
9178  "</condition>"
9179  "</modify_alert>");
9180 
9181  ret = omp (credentials,
9182  &response,
9183  &entity,
9184  response_data,
9185  xml->str);
9186  g_string_free (xml, TRUE);
9187  switch (ret)
9188  {
9189  case 0:
9190  case -1:
9191  break;
9192  case 1:
9193  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9194  return gsad_message (credentials,
9195  "Internal error", __FUNCTION__, __LINE__,
9196  "An internal error occurred while saving a new alert. "
9197  "No new alert was created. "
9198  "Diagnostics: Failure to send command to manager daemon.",
9199  "/omp?cmd=get_alerts", response_data);
9200  case 2:
9201  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9202  return gsad_message (credentials,
9203  "Internal error", __FUNCTION__, __LINE__,
9204  "An internal error occurred while saving a new alert. "
9205  "It is unclear whether the alert has been created or not. "
9206  "Diagnostics: Failure to receive response from manager daemon.",
9207  "/omp?cmd=get_alerts", response_data);
9208  default:
9209  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9210  return gsad_message (credentials,
9211  "Internal error", __FUNCTION__, __LINE__,
9212  "An internal error occurred while saving a new alert. "
9213  "It is unclear whether the alert has been created or not. "
9214  "Diagnostics: Internal Error.",
9215  "/omp?cmd=get_alerts", response_data);
9216  }
9217 
9218  html = response_from_entity (credentials, params, entity,
9219  (no_redirect && strcmp (no_redirect, "0")),
9220  NULL, "get_alerts",
9221  NULL, "edit_alert",
9222  "Save Alert", response_data);
9223  free_entity (entity);
9224  g_free (response);
9225  return html;
9226 }
9227 
9237 char *
9238 test_alert_omp (credentials_t * credentials, params_t *params,
9239  cmd_response_data_t* response_data)
9240 {
9241  openvas_connection_t connection;
9242  gchar *html, *response;
9243  const char *no_redirect, *alert_id;
9244  entity_t entity;
9245 
9246  no_redirect = params_value (params, "no_redirect");
9247  alert_id = params_value (params, "alert_id");
9248 
9249  if (alert_id == NULL)
9250  {
9251  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
9252  return gsad_message (credentials,
9253  "Internal error", __FUNCTION__, __LINE__,
9254  "An internal error occurred while testing an alert. "
9255  "Diagnostics: Required parameter was NULL.",
9256  "/omp?cmd=get_alerts", response_data);
9257  }
9258  switch (manager_connect (credentials, &connection, &html,
9259  response_data))
9260  {
9261  case 0:
9262  break;
9263  case -1:
9264  if (html)
9265  return html;
9266  /* Fall through. */
9267  default:
9268  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9269  return gsad_message (credentials,
9270  "Internal error", __FUNCTION__, __LINE__,
9271  "An internal error occurred while testing an alert. "
9272  "Diagnostics: Failure to connect to manager daemon.",
9273  "/omp?cmd=get_alerts", response_data);
9274  }
9275 
9276  /* Test the alert. */
9277 
9278  if (openvas_connection_sendf (&connection,
9279  "<test_alert alert_id=\"%s\"/>",
9280  alert_id)
9281  == -1)
9282  {
9283  openvas_connection_close (&connection);
9284  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9285  return gsad_message (credentials,
9286  "Internal error", __FUNCTION__, __LINE__,
9287  "An internal error occurred while testing an alert. "
9288  "Diagnostics: Failure to send command to manager daemon.",
9289  "/omp?cmd=get_alerts", response_data);
9290  }
9291 
9292  entity = NULL;
9293  if (read_entity_and_text_c (&connection, &entity, &response))
9294  {
9295  openvas_connection_close (&connection);
9296  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9297  return gsad_message (credentials,
9298  "Internal error", __FUNCTION__, __LINE__,
9299  "An internal error occurred while testing an alert. "
9300  "Diagnostics: Failure to receive response from manager daemon.",
9301  "/omp?cmd=get_alerts", response_data);
9302  }
9303 
9304  /* Cleanup, and return transformed XML. */
9305 
9306  openvas_connection_close (&connection);
9307  html = response_from_entity (credentials, params, entity,
9308  (no_redirect && strcmp (no_redirect, "0")),
9309  NULL, "get_alerts",
9310  NULL, "get_alerts",
9311  "Test Alert", response_data);
9312 
9313  free_entity (entity);
9314  g_free (response);
9315  return html;
9316 }
9317 
9330 char *
9331 export_alert_omp (credentials_t * credentials, params_t *params,
9332  enum content_type * content_type, char **content_disposition,
9333  gsize *content_length, cmd_response_data_t* response_data)
9334 {
9335  return export_resource ("alert", credentials, params, content_type,
9336  content_disposition, content_length, response_data);
9337 }
9338 
9352 char *
9353 export_alerts_omp (credentials_t * credentials, params_t *params,
9354  enum content_type * content_type, char **content_disposition,
9355  gsize *content_length, cmd_response_data_t* response_data)
9356 {
9357  return export_many ("alert", credentials, params, content_type,
9358  content_disposition, content_length, response_data);
9359 }
9360 
9371 static char *
9372 new_target (credentials_t *credentials, params_t *params, const char *extra_xml,
9373  cmd_response_data_t* response_data)
9374 {
9375  GString *xml;
9376  openvas_connection_t connection;
9377  gchar *port_list;
9378  gchar *ssh_credential, *smb_credential, *esxi_credential, *snmp_credential;
9379  gchar *html, *end;
9380  const char *filter, *first, *max;
9381 
9382  filter = params_value (params, "filter");
9383  if (filter == NULL)
9384  filter = "";
9385 
9386  first = params_value (params, "first");
9387  if (first == NULL)
9388  first = "";
9389 
9390  max = params_value (params, "max");
9391  if (max == NULL)
9392  max = "";
9393 
9394  switch (manager_connect (credentials, &connection, &html,
9395  response_data))
9396  {
9397  case 0:
9398  break;
9399  case -1:
9400  if (html)
9401  return html;
9402  /* Fall through. */
9403  default:
9404  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9405  return gsad_message (credentials,
9406  "Internal error", __FUNCTION__, __LINE__,
9407  "An internal error occurred while getting targets list. "
9408  "The current list of targets is not available. "
9409  "Diagnostics: Failure to connect to manager daemon.",
9410  "/omp?cmd=get_targets", response_data);
9411  }
9412 
9413  PARAM_OR_SETTING (port_list, "port_list_id",
9414  "d74a9ee8-7d35-4879-9485-ab23f1bd45bc",
9415  openvas_connection_close (&connection));
9416 
9417  PARAM_OR_SETTING (ssh_credential, "ssh_credential_id",
9418  "6fc56b72-c1cf-451c-a4c4-3a9dc784c3bd",
9419  openvas_connection_close (&connection);
9420  g_free (port_list));
9421 
9422  PARAM_OR_SETTING (smb_credential, "smb_credential_id",
9423  "a25c0cfe-f977-417b-b1da-47da370c03e8",
9424  openvas_connection_close (&connection);
9425  g_free (port_list);
9426  g_free (ssh_credential));
9427 
9428  PARAM_OR_SETTING (esxi_credential, "esxi_credential_id",
9429  "83545bcf-0c49-4b4c-abbf-63baf82cc2a7",
9430  openvas_connection_close (&connection);
9431  g_free (port_list);
9432  g_free (ssh_credential);
9433  g_free (smb_credential));
9434 
9435  PARAM_OR_SETTING (snmp_credential, "snmp_credential_id",
9436  "024550b8-868e-4b3c-98bf-99bb732f6a0d",
9437  openvas_connection_close (&connection);
9438  g_free (port_list);
9439  g_free (ssh_credential);
9440  g_free (smb_credential);
9441  g_free (esxi_credential));
9442 
9443  xml = g_string_new ("<new_target>");
9444 
9445  g_string_append_printf (xml,
9446  "<port_list_id>%s</port_list_id>"
9447  "<ssh_credential_id>%s</ssh_credential_id>"
9448  "<smb_credential_id>%s</smb_credential_id>"
9449  "<esxi_credential_id>%s</esxi_credential_id>"
9450  "<snmp_credential_id>%s</snmp_credential_id>",
9451  (port_list && strlen (port_list))
9452  ? port_list
9453  : "33d0cd82-57c6-11e1-8ed1-406186ea4fc5",
9454  ssh_credential ? ssh_credential : "",
9455  smb_credential ? smb_credential : "",
9456  esxi_credential ? esxi_credential : "",
9457  snmp_credential ? snmp_credential : "");
9458 
9459  g_free (port_list);
9460  g_free (ssh_credential);
9461  g_free (smb_credential);
9462  g_free (esxi_credential);
9463 
9464  if (extra_xml)
9465  g_string_append (xml, extra_xml);
9466 
9467  if (command_enabled (credentials, "GET_CREDENTIALS"))
9468  {
9469  /* Get the credentials. */
9470 
9471  if (openvas_connection_sendf (&connection,
9472  "<get_credentials"
9473  " filter=\"rows=-1 sort=name\"/>")
9474  == -1)
9475  {
9476  g_string_free (xml, TRUE);
9477  openvas_connection_close (&connection);
9478  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9479  return gsad_message (credentials,
9480  "Internal error", __FUNCTION__, __LINE__,
9481  "An internal error occurred while getting targets list. "
9482  "The current list of targets is not available. "
9483  "Diagnostics: Failure to send command to manager daemon.",
9484  "/omp?cmd=get_targets", response_data);
9485  }
9486 
9487  if (read_string_c (&connection, &xml))
9488  {
9489  g_string_free (xml, TRUE);
9490  openvas_connection_close (&connection);
9491  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9492  return gsad_message (credentials,
9493  "Internal error", __FUNCTION__, __LINE__,
9494  "An internal error occurred while getting targets list. "
9495  "The current list of targets is not available. "
9496  "Diagnostics: Failure to receive response from manager daemon.",
9497  "/omp?cmd=get_targets", response_data);
9498  }
9499  }
9500 
9501  if (command_enabled (credentials, "GET_PORT_LISTS"))
9502  {
9503  /* Get the port lists. */
9504 
9505  if (openvas_connection_sendf (&connection,
9506  "<get_port_lists"
9507  " filter=\"rows=-1 sort=name\"/>")
9508  == -1)
9509  {
9510  g_string_free (xml, TRUE);
9511  openvas_connection_close (&connection);
9512  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9513  return gsad_message (credentials,
9514  "Internal error", __FUNCTION__, __LINE__,
9515  "An internal error occurred while getting targets list. "
9516  "The current list of targets is not available. "
9517  "Diagnostics: Failure to send command to manager daemon.",
9518  "/omp?cmd=get_tasks", response_data);
9519  }
9520 
9521  if (read_string_c (&connection, &xml))
9522  {
9523  g_string_free (xml, TRUE);
9524  openvas_connection_close (&connection);
9525  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9526  return gsad_message (credentials,
9527  "Internal error", __FUNCTION__, __LINE__,
9528  "An internal error occurred while getting targets list. "
9529  "The current list of targets is not available. "
9530  "Diagnostics: Failure to receive response from manager daemon.",
9531  "/omp?cmd=get_tasks", response_data);
9532  }
9533  }
9534 
9535  end = g_markup_printf_escaped ("<filters><term>%s</term></filters>"
9536  "<targets start=\"%s\" max=\"%s\"/>"
9537  "</new_target>",
9538  filter,
9539  first,
9540  max);
9541  g_string_append (xml, end);
9542  g_free (end);
9543 
9544  openvas_connection_close (&connection);
9545  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
9546  response_data);
9547 }
9548 
9558 char *
9559 new_target_omp (credentials_t *credentials, params_t *params,
9560  cmd_response_data_t* response_data)
9561 {
9562  return new_target (credentials, params, NULL, response_data);
9563 }
9564 
9574 char *
9575 create_target_omp (credentials_t * credentials, params_t *params,
9576  cmd_response_data_t* response_data)
9577 {
9578  int ret;
9579  gchar *html, *response, *command;
9580  const char *no_redirect, *name, *hosts, *exclude_hosts, *comment;
9581  const char *target_ssh_credential, *port, *target_smb_credential;
9582  const char *target_esxi_credential, *target_snmp_credential, *target_source;
9583  const char *port_list_id, *reverse_lookup_only, *reverse_lookup_unify;
9584  const char *alive_tests, *hosts_filter, *file;
9585  gchar *ssh_credentials_element, *smb_credentials_element;
9586  gchar *esxi_credentials_element, *snmp_credentials_element;
9587  gchar *asset_hosts_element;
9588  gchar* comment_element = NULL;
9589  entity_t entity;
9590  GString *xml;
9591 
9592  no_redirect = params_value (params, "no_redirect");
9593  name = params_value (params, "name");
9594  hosts = params_value (params, "hosts");
9595  exclude_hosts = params_value (params, "exclude_hosts");
9596  reverse_lookup_only = params_value (params, "reverse_lookup_only");
9597  reverse_lookup_unify = params_value (params, "reverse_lookup_unify");
9598  target_source = params_value (params, "target_source");
9599  comment = params_value (params, "comment");
9600  port_list_id = params_value (params, "port_list_id");
9601  target_ssh_credential = params_value (params, "ssh_credential_id");
9602  port = params_value (params, "port");
9603  target_smb_credential = params_value (params, "smb_credential_id");
9604  target_esxi_credential = params_value (params, "esxi_credential_id");
9605  target_snmp_credential = params_value (params, "snmp_credential_id");
9606  alive_tests = params_value (params, "alive_tests");
9607  hosts_filter = params_value (params, "hosts_filter");
9608  file = params_value (params, "file");
9609 
9610  CHECK_PARAM_INVALID (name, "Create Target", "new_target");
9611  CHECK_PARAM_INVALID (target_source, "Create Target", "new_target")
9612  if (hosts == NULL && strcmp (target_source, "manual") == 0)
9613  return message_invalid (credentials, params, response_data,
9614  "Missing manual list of hosts",
9615  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
9616  "Create Target", "new_target");
9617  if (strcmp (target_source, "file") == 0 && file == NULL)
9618  return message_invalid (credentials, params, response_data,
9619  "Missing hosts file",
9620  G_STRINGIFY (MHD_HTTP_BAD_REQUEST),
9621  "Create Target", "new_target");
9622 
9623  /* require hosts_filter if target_source is "asset_hosts" */
9624  if (strcmp (target_source, "asset_hosts") == 0)
9625  CHECK_PARAM_INVALID (hosts_filter, "Create Target", "new_target");
9626 
9627  CHECK_PARAM_INVALID (comment, "Create Target", "new_target");
9628  CHECK_PARAM_INVALID (port_list_id, "Create Target", "new_target");
9629  CHECK_PARAM_INVALID (target_ssh_credential, "Create Target", "new_target");
9630  if (strcmp (target_ssh_credential, "--"))
9631  CHECK_PARAM_INVALID (port, "Create Target", "new_target");
9632  CHECK_PARAM_INVALID (target_smb_credential, "Create Target", "new_target");
9633  CHECK_PARAM_INVALID (target_esxi_credential, "Create Target", "new_target");
9634  CHECK_PARAM_INVALID (target_snmp_credential, "Create Target", "new_target");
9635  CHECK_PARAM_INVALID (alive_tests, "Create Target", "new_target");
9636 
9637  if (comment != NULL)
9638  comment_element = g_strdup_printf ("<comment>%s</comment>", comment);
9639  else
9640  comment_element = g_strdup ("");
9641 
9642  if (strcmp (target_ssh_credential, "0") == 0)
9643  ssh_credentials_element = g_strdup ("");
9644  else
9645  ssh_credentials_element =
9646  g_strdup_printf ("<ssh_credential id=\"%s\">"
9647  "<port>%s</port>"
9648  "</ssh_credential>",
9649  target_ssh_credential,
9650  port);
9651 
9652  if (strcmp (target_smb_credential, "0") == 0)
9653  smb_credentials_element = g_strdup ("");
9654  else
9655  smb_credentials_element =
9656  g_strdup_printf ("<smb_credential id=\"%s\"/>",
9657  target_smb_credential);
9658 
9659  if (strcmp (target_esxi_credential, "0") == 0)
9660  esxi_credentials_element = g_strdup ("");
9661  else
9662  esxi_credentials_element =
9663  g_strdup_printf ("<esxi_credential id=\"%s\"/>",
9664  target_esxi_credential);
9665 
9666  if (strcmp (target_snmp_credential, "0") == 0)
9667  snmp_credentials_element = g_strdup ("");
9668  else
9669  snmp_credentials_element =
9670  g_strdup_printf ("<snmp_credential id=\"%s\"/>",
9671  target_snmp_credential);
9672 
9673  if (strcmp (target_source, "asset_hosts") == 0)
9674  asset_hosts_element = g_strdup_printf ("<asset_hosts filter=\"%s\"/>",
9675  hosts_filter);
9676  else
9677  asset_hosts_element = g_strdup ("");
9678 
9679  /* Create the target. */
9680 
9681  xml = g_string_new ("");
9682 
9683  xml_string_append (xml,
9684  "<name>%s</name>"
9685  "<hosts>%s</hosts>"
9686  "<exclude_hosts>%s</exclude_hosts>"
9687  "<reverse_lookup_only>%s</reverse_lookup_only>"
9688  "<reverse_lookup_unify>%s</reverse_lookup_unify>"
9689  "<port_list id=\"%s\"/>"
9690  "<alive_tests>%s</alive_tests>",
9691  name,
9692  strcmp (target_source, "file") == 0 ? file : hosts,
9693  exclude_hosts ? exclude_hosts : "",
9694  reverse_lookup_only ? reverse_lookup_only : "0",
9695  reverse_lookup_unify ? reverse_lookup_unify : "0",
9696  port_list_id,
9697  alive_tests);
9698 
9699  command = g_strdup_printf ("<create_target>"
9700  "%s%s%s%s%s%s%s"
9701  "</create_target>",
9702  xml->str,
9703  comment_element,
9704  ssh_credentials_element,
9705  smb_credentials_element,
9706  esxi_credentials_element,
9707  snmp_credentials_element,
9708  asset_hosts_element);
9709 
9710  g_string_free (xml, TRUE);
9711  g_free (comment_element);
9712  g_free (ssh_credentials_element);
9713  g_free (smb_credentials_element);
9714  g_free (esxi_credentials_element);
9715 
9716  ret = omp (credentials,
9717  &response,
9718  &entity,
9719  response_data,
9720  command);
9721  g_free (command);
9722  switch (ret)
9723  {
9724  case 0:
9725  break;
9726  case -1:
9727  /* 'omp' set response. */
9728  return response;
9729  case 1:
9730  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9731  return gsad_message (credentials,
9732  "Internal error", __FUNCTION__, __LINE__,
9733  "An internal error occurred while creating a new target. "
9734  "No new target was created. "
9735  "Diagnostics: Failure to send command to manager daemon.",
9736  "/omp?cmd=get_targets", response_data);
9737  case 2:
9738  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9739  return gsad_message (credentials,
9740  "Internal error", __FUNCTION__, __LINE__,
9741  "An internal error occurred while creating a new target. "
9742  "It is unclear whether the target has been created or not. "
9743  "Diagnostics: Failure to receive response from manager daemon.",
9744  "/omp?cmd=get_targets", response_data);
9745  default:
9746  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9747  return gsad_message (credentials,
9748  "Internal error", __FUNCTION__, __LINE__,
9749  "An internal error occurred while creating a new target. "
9750  "It is unclear whether the target has been created or not. "
9751  "Diagnostics: Internal Error.",
9752  "/omp?cmd=get_targets", response_data);
9753  }
9754 
9755  if (entity_attribute (entity, "id"))
9756  params_add (params, "target_id", entity_attribute (entity, "id"));
9757  html = response_from_entity (credentials, params, entity,
9758  (no_redirect && strcmp (no_redirect, "0")),
9759  NULL, "get_targets",
9760  NULL, "new_target",
9761  "Create Target", response_data);
9762  free_entity (entity);
9763  g_free (response);
9764  return html;
9765 }
9766 
9772 #define CHECK(name) \
9773  if (name == NULL) \
9774  { \
9775  response_data->http_status_code = MHD_HTTP_BAD_REQUEST; \
9776  return gsad_message (credentials, \
9777  "Internal error", __FUNCTION__, __LINE__, \
9778  "An internal error occurred while cloning a resource. " \
9779  "The resource was not cloned. " \
9780  "Diagnostics: Required parameter '" G_STRINGIFY (name) \
9781  "' was NULL.", \
9782  "/omp?cmd=get_tasks", response_data); \
9783  }
9784 
9794 char *
9795 clone_omp (credentials_t *credentials, params_t *params,
9796  cmd_response_data_t* response_data)
9797 {
9798  openvas_connection_t connection;
9799  gchar *html, *response;
9800  const char *id, *type, *alterable, *no_redirect, *next_id;
9801  gchar *next_id_name, *cap_type, *prev_action;
9802  entity_t entity;
9803 
9804  id = params_value (params, "id");
9805  type = params_value (params, "resource_type");
9806  alterable = params_value (params, "alterable");
9807  no_redirect = params_value (params, "no_redirect");
9808 
9809  CHECK (id);
9810  CHECK (type);
9811 
9812  switch (manager_connect (credentials, &connection, &html,
9813  response_data))
9814  {
9815  case 0:
9816  break;
9817  case -1:
9818  if (html)
9819  return html;
9820  /* Fall through. */
9821  default:
9822  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9823  return gsad_message (credentials,
9824  "Internal error", __FUNCTION__, __LINE__,
9825  "An internal error occurred while cloning a resource. "
9826  "The resource was not cloned. "
9827  "Diagnostics: Failure to connect to manager daemon.",
9828  "/omp?cmd=get_tasks", response_data);
9829  }
9830 
9831  /* Clone the resource. */
9832 
9833  if (alterable && strcmp (alterable, "0"))
9834  {
9835  if (openvas_connection_sendf (&connection,
9836  "<create_%s>"
9837  "<copy>%s</copy>"
9838  "<alterable>1</alterable>"
9839  "</create_%s>",
9840  type,
9841  id,
9842  type)
9843  == -1)
9844  {
9845  openvas_connection_close (&connection);
9846  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9847  return gsad_message (credentials,
9848  "Internal error", __FUNCTION__, __LINE__,
9849  "An internal error occurred while cloning a resource. "
9850  "The resource was not cloned. "
9851  "Diagnostics: Failure to send command to manager daemon.",
9852  "/omp?cmd=get_tasks", response_data);
9853  }
9854  }
9855  else if (openvas_connection_sendf (&connection,
9856  "<create_%s>"
9857  "<copy>%s</copy>"
9858  "</create_%s>",
9859  type,
9860  id,
9861  type)
9862  == -1)
9863  {
9864  openvas_connection_close (&connection);
9865  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9866  return gsad_message (credentials,
9867  "Internal error", __FUNCTION__, __LINE__,
9868  "An internal error occurred while cloning a resource. "
9869  "The resource was not cloned. "
9870  "Diagnostics: Failure to send command to manager daemon.",
9871  "/omp?cmd=get_tasks", response_data);
9872  }
9873 
9874  entity = NULL;
9875  if (read_entity_and_text_c (&connection, &entity, &response))
9876  {
9877  openvas_connection_close (&connection);
9878  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9879  return gsad_message (credentials,
9880  "Internal error", __FUNCTION__, __LINE__,
9881  "An internal error occurred while cloning a resource. "
9882  "It is unclear whether the resource has been cloned or not. "
9883  "Diagnostics: Failure to read response from manager daemon.",
9884  "/omp?cmd=get_tasks", response_data);
9885  }
9886 
9887  openvas_connection_close (&connection);
9888 
9889  /* Cleanup, and return next page. */
9890 
9891  if (omp_success (entity) == 0 || params_given (params, "next") == 0)
9892  {
9893  gchar *next;
9894  next = g_strdup_printf ("get_%ss", type);
9895  params_add (params, "next", next);
9896  g_free (next);
9897  }
9898 
9899  if (omp_success (entity))
9900  {
9901  next_id = entity_attribute (entity, "id");
9902  if (next_id == NULL)
9903  {
9904  free_entity (entity);
9905  g_free (response);
9906  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
9907  return gsad_message (credentials,
9908  "Internal error", __FUNCTION__, __LINE__,
9909  "An internal error occurred while cloning a resource. "
9910  "The resource remains the same. "
9911  "Diagnostics: Error getting new resource.",
9912  "/omp?cmd=get_tasks", response_data);
9913  }
9914  next_id_name = g_strdup_printf ("%s_id", type);
9915  params_add (params, next_id_name, next_id);
9916  g_free (next_id_name);
9917  }
9918  else
9919  {
9920  set_http_status_from_entity (entity, response_data);
9921  next_id_name = NULL;
9922  next_id = NULL;
9923  }
9924 
9925  cap_type = capitalize (type);
9926  prev_action = g_strdup_printf ("Clone %s", cap_type);
9927  html = response_from_entity (credentials, params, entity,
9928  (no_redirect && strcmp (no_redirect, "0")),
9929  NULL, NULL,
9930  NULL, NULL,
9931  prev_action, response_data);
9932 
9933  free_entity (entity);
9934  g_free (cap_type);
9935  g_free (prev_action);
9936  g_free (response);
9937 
9938  return html;
9939 }
9940 
9941 #undef CHECK
9942 
9952 char *
9953 delete_target_omp (credentials_t * credentials, params_t *params,
9954  cmd_response_data_t* response_data)
9955 {
9956  return delete_resource ("target", credentials, params, 0, "get_targets",
9957  response_data);
9958 }
9959 
9969 char *
9971  cmd_response_data_t* response_data)
9972 {
9973  return delete_resource ("agent", credentials, params, 1, "get_trash",
9974  response_data);
9975 }
9976 
9986 char *
9988  cmd_response_data_t* response_data)
9989 {
9990  return delete_resource ("config", credentials, params, 1, "get_trash",
9991  response_data);
9992 }
9993 
10003 char *
10005  cmd_response_data_t* response_data)
10006 {
10007  return delete_resource ("alert", credentials, params, 1, "get_trash",
10008  response_data);
10009 }
10010 
10020 char *
10022  cmd_response_data_t* response_data)
10023 {
10024  return delete_resource ("credential", credentials, params, 1, "get_trash",
10025  response_data);
10026 }
10027 
10037 char *
10039  cmd_response_data_t* response_data)
10040 {
10041  return delete_resource ("report_format", credentials, params, 1, "get_trash",
10042  response_data);
10043 }
10044 
10054 char *
10056  cmd_response_data_t* response_data)
10057 {
10058  return delete_resource ("schedule", credentials, params, 1, "get_trash",
10059  response_data);
10060 }
10061 
10071 char *
10073  cmd_response_data_t* response_data)
10074 {
10075  return delete_resource ("target", credentials, params, 1, "get_trash",
10076  response_data);
10077 }
10078 
10088 char *
10090  cmd_response_data_t* response_data)
10091 {
10092  return delete_resource ("task", credentials, params, 1, "get_trash",
10093  response_data);
10094 }
10095 
10105 char *
10106 restore_omp (credentials_t * credentials, params_t *params,
10107  cmd_response_data_t* response_data)
10108 {
10109  GString *xml;
10110  gchar *ret;
10111  openvas_connection_t connection;
10112  entity_t entity;
10113  gchar *html;
10114  const char *target_id, *no_redirect;
10115 
10116  target_id = params_value (params, "target_id");
10117  no_redirect = params_value (params, "no_redirect");
10118 
10119  if (target_id == NULL)
10120  {
10121  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10122  return gsad_message (credentials,
10123  "Internal error", __FUNCTION__, __LINE__,
10124  "An internal error occurred while restoring a resource. "
10125  "The resource was not restored. "
10126  "Diagnostics: Required parameter was NULL.",
10127  "/omp?cmd=get_tasks", response_data);
10128  }
10129 
10130  switch (manager_connect (credentials, &connection, &html,
10131  response_data))
10132  {
10133  case 0:
10134  break;
10135  case -1:
10136  if (html)
10137  return html;
10138  /* Fall through. */
10139  default:
10140  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10141  return gsad_message (credentials,
10142  "Internal error", __FUNCTION__, __LINE__,
10143  "An internal error occurred while restoring a resource. "
10144  "The resource was not restored. "
10145  "Diagnostics: Failure to connect to manager daemon.",
10146  "/omp?cmd=get_trash", response_data);
10147  }
10148 
10149  xml = g_string_new ("");
10150 
10151  /* Restore the resource. */
10152 
10153  if (openvas_connection_sendf (&connection,
10154  "<restore"
10155  " id=\"%s\"/>",
10156  target_id)
10157  == -1)
10158  {
10159  g_string_free (xml, TRUE);
10160  openvas_connection_close (&connection);
10161  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10162  return gsad_message (credentials,
10163  "Internal error", __FUNCTION__, __LINE__,
10164  "An internal error occurred while restoring a resource. "
10165  "The resource was not deleted. "
10166  "Diagnostics: Failure to send command to manager daemon.",
10167  "/omp?cmd=get_trash", response_data);
10168  }
10169 
10170  if (read_entity_and_string_c (&connection, &entity, &xml))
10171  {
10172  g_string_free (xml, TRUE);
10173  openvas_connection_close (&connection);
10174  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10175  return gsad_message (credentials,
10176  "Internal error", __FUNCTION__, __LINE__,
10177  "An internal error occurred while restoring a resource. "
10178  "It is unclear whether the resource has been restored or not. "
10179  "Diagnostics: Failure to read response from manager daemon.",
10180  "/omp?cmd=get_trash", response_data);
10181  }
10182 
10183  /* Cleanup, and return trash page. */
10184 
10185  openvas_connection_close (&connection);
10186  ret = response_from_entity (credentials, params, entity,
10187  (no_redirect && strcmp (no_redirect, "0")),
10188  NULL, "get_trash",
10189  NULL, "get_trash",
10190  "Restore", response_data);
10191  free_entity (entity);
10192  g_string_free (xml, FALSE);
10193  return ret;
10194 }
10195 
10205 char *
10207  cmd_response_data_t* response_data)
10208 {
10209  GString *xml;
10210  const char* no_redirect;
10211  gchar *ret;
10212  openvas_connection_t connection;
10213  entity_t entity;
10214  gchar *html;
10215 
10216  no_redirect = params_value (params, "no_redirect");
10217 
10218  switch (manager_connect (credentials, &connection, &html,
10219  response_data))
10220  {
10221  case 0:
10222  break;
10223  case -1:
10224  if (html)
10225  return html;
10226  /* Fall through. */
10227  default:
10228  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10229  return gsad_message (credentials,
10230  "Internal error", __FUNCTION__, __LINE__,
10231  "An internal error occurred while emptying the trashcan. "
10232  "Diagnostics: Failure to connect to manager daemon.",
10233  "/omp?cmd=get_trash", response_data);
10234  }
10235 
10236  xml = g_string_new ("");
10237 
10238  /* Empty the trash. */
10239 
10240  if (openvas_connection_sendf (&connection,
10241  "<empty_trashcan/>")
10242  == -1)
10243  {
10244  g_string_free (xml, TRUE);
10245  openvas_connection_close (&connection);
10246  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10247  return gsad_message (credentials,
10248  "Internal error", __FUNCTION__, __LINE__,
10249  "An internal error occurred while emptying the trashcan. "
10250  "Diagnostics: Failure to send command to manager daemon.",
10251  "/omp?cmd=get_trash", response_data);
10252  }
10253 
10254  if (read_entity_and_string_c (&connection, &entity, &xml))
10255  {
10256  g_string_free (xml, TRUE);
10257  openvas_connection_close (&connection);
10258  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10259  return gsad_message (credentials,
10260  "Internal error", __FUNCTION__, __LINE__,
10261  "An internal error occurred while emptying the trashcan. "
10262  "Diagnostics: Failure to read response from manager daemon.",
10263  "/omp?cmd=get_trash", response_data);
10264  }
10265 
10266  /* Cleanup, and return trash page. */
10267 
10268  openvas_connection_close (&connection);
10269 
10270  ret = response_from_entity (credentials, params, entity,
10271  (no_redirect && strcmp (no_redirect, "0")),
10272  NULL, "get_trash",
10273  NULL, "get_trash",
10274  "Empty Trashcan", response_data);
10275  free_entity (entity);
10276  g_string_free (xml, FALSE);
10277  return ret;
10278 }
10279 
10290 static char *
10291 new_tag (credentials_t *credentials, params_t *params, const char *extra_xml,
10292  cmd_response_data_t* response_data)
10293 {
10294  GString *xml;
10295  gchar *end;
10296  const char *resource_type, *resource_id, *tag_id, *tag_name;
10297 
10298  resource_type = params_value (params, "resource_type");
10299  resource_id = params_value (params, "resource_id");
10300 
10301  tag_id = params_value (params, "tag_id");
10302  tag_name = params_value (params, "tag_name");
10303 
10304  xml = g_string_new ("<new_tag>");
10305 
10306  if (extra_xml)
10307  g_string_append (xml, extra_xml);
10308 
10309  end = g_markup_printf_escaped ("<tag id=\"%s\"/>"
10310  "<resource_type>%s</resource_type>"
10311  "<resource_id>%s</resource_id>"
10312  "<tag_name>%s%s</tag_name>"
10313  "<tag_value></tag_value>"
10314  "<comment></comment>"
10315  "<active>1</active>"
10316  "</new_tag>",
10317  tag_id ? tag_id : "0",
10318  resource_type ? resource_type : "",
10319  resource_id ? resource_id : "",
10320  tag_name ? tag_name : (resource_type
10321  ? resource_type
10322  : "default"),
10323  tag_name ? "" : ":unnamed");
10324  g_string_append (xml, end);
10325  g_free (end);
10326 
10327  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
10328  response_data);
10329 }
10330 
10340 char *
10341 new_tag_omp (credentials_t *credentials, params_t *params,
10342  cmd_response_data_t* response_data)
10343 {
10344  return new_tag (credentials, params, NULL, response_data);
10345 }
10346 
10356 char *
10357 create_tag_omp (credentials_t *credentials, params_t *params,
10358  cmd_response_data_t* response_data)
10359 {
10360  char *ret;
10361  const char* no_redirect;
10362  gchar *response;
10363  const char *name, *comment, *value, *resource_type, *resource_id, *active;
10364  entity_t entity;
10365 
10366  no_redirect = params_value (params, "no_redirect");
10367  name = params_value (params, "tag_name");
10368  comment = params_value (params, "comment");
10369  value = params_value (params, "tag_value");
10370  resource_type = params_value (params, "resource_type");
10371  resource_id = params_value (params, "resource_id");
10372  active = params_value (params, "active");
10373 
10374  CHECK_PARAM_INVALID (name, "Create Tag", "new_tag")
10375  CHECK_PARAM_INVALID (comment, "Create Tag", "new_tag")
10376  CHECK_PARAM_INVALID (value, "Create Tag", "new_tag")
10377  CHECK_PARAM_INVALID (resource_type, "Create Tag", "new_tag")
10378  CHECK_PARAM_INVALID (resource_id, "Create Tag", "new_tag")
10379  CHECK_PARAM_INVALID (active, "Create Tag", "new_tag")
10380 
10381  response = NULL;
10382  entity = NULL;
10383  switch (ompf (credentials,
10384  &response,
10385  &entity,
10386  response_data,
10387  "<create_tag>"
10388  "<name>%s</name>"
10389  "<comment>%s</comment>"
10390  "<value>%s</value>"
10391  "<resource id=\"%s\">"
10392  "<type>%s</type>"
10393  "</resource>"
10394  "<active>%s</active>"
10395  "</create_tag>",
10396  name,
10397  comment,
10398  value,
10399  resource_id,
10400  resource_type,
10401  active))
10402  {
10403  case 0:
10404  case -1:
10405  break;
10406  case 1:
10407  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10408  return gsad_message (credentials,
10409  "Internal error", __FUNCTION__, __LINE__,
10410  "An internal error occurred while creating a new tag. "
10411  "No new tag was created. "
10412  "Diagnostics: Failure to send command to manager daemon.",
10413  "/omp?cmd=get_targets", response_data);
10414  case 2:
10415  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10416  return gsad_message (credentials,
10417  "Internal error", __FUNCTION__, __LINE__,
10418  "An internal error occurred while creating a new tag. "
10419  "It is unclear whether the tag has been created or not. "
10420  "Diagnostics: Failure to receive response from manager daemon.",
10421  "/omp?cmd=get_tags", response_data);
10422  default:
10423  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10424  return gsad_message (credentials,
10425  "Internal error", __FUNCTION__, __LINE__,
10426  "An internal error occurred while creating a new tag. "
10427  "It is unclear whether the tag has been created or not. "
10428  "Diagnostics: Internal Error.",
10429  "/omp?cmd=get_tags", response_data);
10430  }
10431 
10432  if (entity_attribute (entity, "id"))
10433  params_add (params, "tag_id", entity_attribute (entity, "id"));
10434  ret = response_from_entity (credentials, params, entity,
10435  (no_redirect && strcmp (no_redirect, "0")),
10436  NULL, "get_tags",
10437  NULL, "new_tag",
10438  "Create Tag", response_data);
10439 
10440  free_entity (entity);
10441  g_free (response);
10442  return ret;
10443 }
10444 
10454 char *
10455 delete_tag_omp (credentials_t * credentials, params_t *params,
10456  cmd_response_data_t* response_data)
10457 {
10458  return delete_resource ("tag", credentials, params, 0, NULL, response_data);
10459 }
10460 
10470 char *
10472  cmd_response_data_t* response_data)
10473 {
10474  return delete_resource ("tag", credentials, params, 1, "get_trash",
10475  response_data);
10476 }
10477 
10488 char *
10489 edit_tag (credentials_t * credentials, params_t *params,
10490  const char *extra_xml, cmd_response_data_t* response_data)
10491 {
10492  GString *xml;
10493  openvas_connection_t connection;
10494  gchar *html, *edit;
10495  const char *tag_id;
10496 
10497  tag_id = params_value (params, "tag_id");
10498  if (tag_id == NULL)
10499  {
10500  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10501  return gsad_message (credentials,
10502  "Internal error", __FUNCTION__, __LINE__,
10503  "An internal error occurred while editing a tag. "
10504  "The tag remains as it was. "
10505  "Diagnostics: Required parameter was NULL.",
10506  "/omp?cmd=get_tags", response_data);
10507  }
10508 
10509  switch (manager_connect (credentials, &connection, &html,
10510  response_data))
10511  {
10512  case 0:
10513  break;
10514  case -1:
10515  if (html)
10516  return html;
10517  /* Fall through. */
10518  default:
10519  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10520  return gsad_message (credentials,
10521  "Internal error", __FUNCTION__, __LINE__,
10522  "An internal error occurred while editing a tag. "
10523  "The tag remains as it was. "
10524  "Diagnostics: Failure to connect to manager daemon.",
10525  "/omp?cmd=get_tags", response_data);
10526  }
10527 
10528  if (openvas_connection_sendf (&connection,
10529  "<get_tags"
10530  " tag_id=\"%s\""
10531  "/>",
10532  tag_id)
10533  == -1)
10534  {
10535  openvas_connection_close (&connection);
10536  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10537  return gsad_message (credentials,
10538  "Internal error", __FUNCTION__, __LINE__,
10539  "An internal error occurred while getting tag info. "
10540  "Diagnostics: Failure to send command to manager daemon.",
10541  "/omp?cmd=get_tags", response_data);
10542  }
10543 
10544  xml = g_string_new ("");
10545 
10546  edit = g_markup_printf_escaped ("<edit_tag>"
10547  "<tag id=\"%s\"/>",
10548  tag_id);
10549 
10550  g_string_append (xml, edit);
10551  g_free (edit);
10552 
10553  if (extra_xml)
10554  g_string_append (xml, extra_xml);
10555 
10556  if (read_string_c (&connection, &xml))
10557  {
10558  g_string_free (xml, TRUE);
10559  openvas_connection_close (&connection);
10560  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10561  return gsad_message (credentials,
10562  "Internal error", __FUNCTION__, __LINE__,
10563  "An internal error occurred while getting target info. "
10564  "Diagnostics: Failure to receive response from manager daemon.",
10565  "/omp?cmd=get_tags", response_data);
10566  }
10567 
10568  /* Cleanup, and return transformed XML. */
10569 
10570  g_string_append (xml, "</edit_tag>");
10571  openvas_connection_close (&connection);
10572  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
10573  response_data);
10574 }
10575 
10585 char *
10586 edit_tag_omp (credentials_t * credentials, params_t *params,
10587  cmd_response_data_t* response_data)
10588 {
10589  return edit_tag (credentials, params, NULL, response_data);
10590 }
10591 
10601 char *
10602 save_tag_omp (credentials_t * credentials, params_t *params,
10603  cmd_response_data_t* response_data)
10604 {
10605  gchar *response;
10606  const char *name, *comment, *value, *resource_type, *resource_id, *active;
10607  const char *tag_id, *no_redirect;
10608  entity_t entity;
10609  char* ret;
10610 
10611  no_redirect = params_value (params, "no_redirect");
10612  tag_id = params_value (params, "tag_id");
10613  name = params_value (params, "tag_name");
10614  comment = params_value (params, "comment");
10615  value = params_value (params, "tag_value");
10616  resource_type = params_value (params, "resource_type");
10617  resource_id = params_value (params, "resource_id");
10618  active = params_value (params, "active");
10619 
10620  CHECK_PARAM_INVALID (tag_id, "Save Tag", "edit_tag")
10621  CHECK_PARAM_INVALID (name, "Save Tag", "edit_tag")
10622  CHECK_PARAM_INVALID (comment, "Save Tag", "edit_tag")
10623  CHECK_PARAM_INVALID (value, "Save Tag", "edit_tag")
10624  CHECK_PARAM_INVALID (resource_type, "Save Tag", "edit_tag")
10625  CHECK_PARAM_INVALID (resource_id, "Save Tag", "edit_tag")
10626  CHECK_PARAM_INVALID (active, "Save Tag", "edit_tag")
10627 
10628  response = NULL;
10629  entity = NULL;
10630  switch (ompf (credentials,
10631  &response,
10632  &entity,
10633  response_data,
10634  "<modify_tag tag_id=\"%s\">"
10635  "<name>%s</name>"
10636  "<comment>%s</comment>"
10637  "<value>%s</value>"
10638  "<resource id=\"%s\">"
10639  "<type>%s</type>"
10640  "</resource>"
10641  "<active>%s</active>"
10642  "</modify_tag>",
10643  tag_id,
10644  name,
10645  comment,
10646  value,
10647  resource_id,
10648  resource_type,
10649  active))
10650  {
10651  case 0:
10652  case -1:
10653  break;
10654  case 1:
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 saving a tag. "
10659  "The tag remains the same. "
10660  "Diagnostics: Failure to send command to "
10661  "manager daemon.",
10662  "/omp?cmd=get_targets", response_data);
10663  case 2:
10664  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10665  return gsad_message (credentials,
10666  "Internal error", __FUNCTION__, __LINE__,
10667  "An internal error occurred while saving a tag. "
10668  "It is unclear whether the tag has been saved "
10669  "or not. "
10670  "Diagnostics: Failure to receive response from "
10671  "manager daemon.",
10672  "/omp?cmd=get_tags", response_data);
10673  default:
10674  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10675  return gsad_message (credentials,
10676  "Internal error", __FUNCTION__, __LINE__,
10677  "An internal error occurred while saving a tag. "
10678  "It is unclear whether the tag has been saved "
10679  "or not. "
10680  "Diagnostics: Internal Error.",
10681  "/omp?cmd=get_tags", response_data);
10682  }
10683 
10684  ret = response_from_entity (credentials, params, entity,
10685  (no_redirect && strcmp (no_redirect, "0")),
10686  NULL, "get_tags",
10687  NULL, "edit_tag",
10688  "Save Tag", response_data);
10689 
10690  free_entity (entity);
10691  g_free (response);
10692  return ret;
10693 
10694 }
10695 
10709 char *
10710 export_tag_omp (credentials_t * credentials, params_t *params,
10711  enum content_type * content_type, char **content_disposition,
10712  gsize *content_length, cmd_response_data_t* response_data)
10713 {
10714  return export_resource ("tag", credentials, params, content_type,
10715  content_disposition, content_length, response_data);
10716 }
10717 
10731 char *
10732 export_tags_omp (credentials_t * credentials, params_t *params,
10733  enum content_type * content_type, char **content_disposition,
10734  gsize *content_length, cmd_response_data_t* response_data)
10735 {
10736  return export_many ("tag", credentials, params, content_type,
10737  content_disposition, content_length, response_data);
10738 }
10739 
10750 static char *
10751 get_tag (credentials_t * credentials, params_t *params,
10752  const char *extra_xml, cmd_response_data_t* response_data)
10753 {
10754  return get_one ("tag", credentials, params, extra_xml, NULL, response_data);
10755 }
10756 
10766 char *
10767 get_tag_omp (credentials_t * credentials, params_t *params,
10768  cmd_response_data_t* response_data)
10769 {
10770  return get_tag (credentials, params, NULL, response_data);
10771 }
10772 
10783 static char *
10784 get_tags (credentials_t * credentials, params_t *params,
10785  const char *extra_xml, cmd_response_data_t* response_data)
10786 {
10787  return get_many ("tag", credentials, params, extra_xml, NULL, response_data);
10788 }
10789 
10799 char *
10800 get_tags_omp (credentials_t * credentials, params_t *params,
10801  cmd_response_data_t* response_data)
10802 {
10803  return get_tags (credentials, params, NULL, response_data);
10804 }
10805 
10815 char *
10816 toggle_tag_omp (credentials_t * credentials, params_t *params,
10817  cmd_response_data_t* response_data)
10818 {
10819  openvas_connection_t connection;
10820  gchar *html, *response;
10821  const char *no_redirect, *tag_id, *enable;
10822  entity_t entity;
10823 
10824  no_redirect = params_value (params, "no_redirect");
10825  tag_id = params_value (params, "tag_id");
10826  enable = params_value (params, "enable");
10827 
10828  if (tag_id == NULL)
10829  {
10830  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10831  return gsad_message (credentials,
10832  "Internal error", __FUNCTION__, __LINE__,
10833  "An internal error occurred while modifying a tag. "
10834  "The tag was not modified. "
10835  "Diagnostics: Required parameter tag_id was NULL.",
10836  "/omp?cmd=get_tasks", response_data);
10837  }
10838  if (enable == NULL)
10839  {
10840  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10841  return gsad_message (credentials,
10842  "Internal error", __FUNCTION__, __LINE__,
10843  "An internal error occurred while modifying a tag. "
10844  "The tag was not modified. "
10845  "Diagnostics: Required parameter enable was NULL.",
10846  "/omp?cmd=get_tasks", response_data);
10847  }
10848 
10849  switch (manager_connect (credentials, &connection, &html,
10850  response_data))
10851  {
10852  case 0:
10853  break;
10854  case -1:
10855  if (html)
10856  return html;
10857  /* Fall through. */
10858  default:
10859  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10860  return gsad_message (credentials,
10861  "Internal error", __FUNCTION__, __LINE__,
10862  "An internal error occurred while modifying a tag."
10863  " The tag is not modified. "
10864  "Diagnostics: Failure to connect to"
10865  " manager daemon.",
10866  "/omp?cmd=get_tasks", response_data);
10867  }
10868 
10869  /* Delete the resource and get all resources. */
10870 
10871  if (openvas_connection_sendf (&connection,
10872  "<modify_tag tag_id=\"%s\">"
10873  "<active>%s</active>"
10874  "</modify_tag>",
10875  tag_id,
10876  enable)
10877  == -1)
10878  {
10879  openvas_connection_close (&connection);
10880  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10881  return gsad_message (credentials,
10882  "Internal error", __FUNCTION__, __LINE__,
10883  "An internal error occurred while modifying a tag. "
10884  "The tag is not modified. "
10885  "Diagnostics: Failure to send command to"
10886  " manager daemon.",
10887  "/omp?cmd=get_tasks", response_data);
10888  }
10889 
10890  entity = NULL;
10891  if (read_entity_and_text_c (&connection, &entity, &response))
10892  {
10893  openvas_connection_close (&connection);
10894  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10895  return gsad_message (credentials,
10896  "Internal error", __FUNCTION__, __LINE__,
10897  "An internal error occurred while modifying a tag. "
10898  "It is unclear whether the tag has been modified"
10899  " or not. "
10900  "Diagnostics: Failure to read response from"
10901  " manager daemon.",
10902  "/omp?cmd=get_tasks", response_data);
10903  }
10904 
10905  if (! omp_success (entity))
10906  set_http_status_from_entity (entity, response_data);
10907  html = response_from_entity (credentials, params, entity,
10908  (no_redirect && strcmp (no_redirect, "0")),
10909  NULL, "get_tags",
10910  NULL, "get_tags",
10911  "Toggle Tag", response_data);
10912 
10913  free_entity (entity);
10914  g_free (response);
10915  openvas_connection_close (&connection);
10916 
10917  return html;
10918 }
10919 
10930 char *
10931 edit_target (credentials_t * credentials, params_t *params,
10932  const char *extra_xml, cmd_response_data_t* response_data)
10933 {
10934  GString *xml;
10935  openvas_connection_t connection;
10936  gchar *html, *edit;
10937  const char *target_id, *next, *filter, *first, *max;
10938 
10939  target_id = params_value (params, "target_id");
10940  filter = params_value (params, "filter");
10941  first = params_value (params, "first");
10942  max = params_value (params, "max");
10943  next = params_value (params, "next");
10944 
10945  if (target_id == NULL)
10946  {
10947  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
10948  return gsad_message (credentials,
10949  "Internal error", __FUNCTION__, __LINE__,
10950  "An internal error occurred while editing a target. "
10951  "The target remains as it was. "
10952  "Diagnostics: Required parameter was NULL.",
10953  "/omp?cmd=get_targets", response_data);
10954  }
10955 
10956  if (next == NULL)
10957  next = "get_target";
10958 
10959  switch (manager_connect (credentials, &connection, &html,
10960  response_data))
10961  {
10962  case 0:
10963  break;
10964  case -1:
10965  if (html)
10966  return html;
10967  /* Fall through. */
10968  default:
10969  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10970  return gsad_message (credentials,
10971  "Internal error", __FUNCTION__, __LINE__,
10972  "An internal error occurred while editing a target. "
10973  "The target remains as it was. "
10974  "Diagnostics: Failure to connect to manager daemon.",
10975  "/omp?cmd=get_targets", response_data);
10976  }
10977 
10978  if (openvas_connection_sendf (&connection,
10979  "<get_targets"
10980  " target_id=\"%s\""
10981  " details=\"1\"/>",
10982  target_id)
10983  == -1)
10984  {
10985  openvas_connection_close (&connection);
10986  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
10987  return gsad_message (credentials,
10988  "Internal error", __FUNCTION__, __LINE__,
10989  "An internal error occurred while getting target info. "
10990  "Diagnostics: Failure to send command to manager daemon.",
10991  "/omp?cmd=get_targets", response_data);
10992  }
10993 
10994  xml = g_string_new ("");
10995 
10996  if (extra_xml)
10997  g_string_append (xml, extra_xml);
10998 
10999 
11000  edit = g_markup_printf_escaped ("<edit_target>"
11001  "<target id=\"%s\"/>"
11002  /* Page that follows. */
11003  "<next>%s</next>"
11004  /* Passthroughs. */
11005  "<filters><term>%s</term></filters>"
11006  "<targets start=\"%s\" max=\"%s\"/>",
11007  target_id,
11008  next,
11009  filter,
11010  first,
11011  max);
11012  g_string_append (xml, edit);
11013  g_free (edit);
11014 
11015  if (read_string_c (&connection, &xml))
11016  {
11017  g_string_free (xml, TRUE);
11018  openvas_connection_close (&connection);
11019  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11020  return gsad_message (credentials,
11021  "Internal error", __FUNCTION__, __LINE__,
11022  "An internal error occurred while getting target info. "
11023  "Diagnostics: Failure to receive response from manager daemon.",
11024  "/omp?cmd=get_targets", response_data);
11025  }
11026 
11027  if (command_enabled (credentials, "GET_CREDENTIALS"))
11028  {
11029  /* Get the credentials. */
11030 
11031  if (openvas_connection_sendf (&connection,
11032  "<get_credentials"
11033  " filter=\"rows=-1 sort=name\"/>")
11034  == -1)
11035  {
11036  g_string_free (xml, TRUE);
11037  openvas_connection_close (&connection);
11038  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11039  return gsad_message (credentials,
11040  "Internal error", __FUNCTION__, __LINE__,
11041  "An internal error occurred while getting targets list. "
11042  "The current list of targets is not available. "
11043  "Diagnostics: Failure to send command to manager daemon.",
11044  "/omp?cmd=get_targets", response_data);
11045  }
11046 
11047  if (read_string_c (&connection, &xml))
11048  {
11049  g_string_free (xml, TRUE);
11050  openvas_connection_close (&connection);
11051  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11052  return gsad_message (credentials,
11053  "Internal error", __FUNCTION__, __LINE__,
11054  "An internal error occurred while getting targets list. "
11055  "The current list of targets is not available. "
11056  "Diagnostics: Failure to receive response from manager daemon.",
11057  "/omp?cmd=get_targets", response_data);
11058  }
11059  }
11060 
11061  if (command_enabled (credentials, "GET_PORT_LISTS"))
11062  {
11063  /* Get the port lists. */
11064 
11065  if (openvas_connection_sendf (&connection,
11066  "<get_port_lists"
11067  " filter=\"rows=-1 sort=name\"/>")
11068  == -1)
11069  {
11070  g_string_free (xml, TRUE);
11071  openvas_connection_close (&connection);
11072  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11073  return gsad_message (credentials,
11074  "Internal error", __FUNCTION__, __LINE__,
11075  "An internal error occurred while getting targets list. "
11076  "The current list of targets is not available. "
11077  "Diagnostics: Failure to send command to manager daemon.",
11078  "/omp?cmd=get_tasks", response_data);
11079  }
11080 
11081  if (read_string_c (&connection, &xml))
11082  {
11083  g_string_free (xml, TRUE);
11084  openvas_connection_close (&connection);
11085  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11086  return gsad_message (credentials,
11087  "Internal error", __FUNCTION__, __LINE__,
11088  "An internal error occurred while getting targets list. "
11089  "The current list of targets is not available. "
11090  "Diagnostics: Failure to receive response from manager daemon.",
11091  "/omp?cmd=get_tasks", response_data);
11092  }
11093  }
11094 
11095  /* Cleanup, and return transformed XML. */
11096 
11097  g_string_append (xml, "</edit_target>");
11098  openvas_connection_close (&connection);
11099  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
11100  response_data);
11101 }
11102 
11112 char *
11113 edit_target_omp (credentials_t * credentials, params_t *params,
11114  cmd_response_data_t* response_data)
11115 {
11116  return edit_target (credentials, params, NULL, response_data);
11117 }
11118 
11129 static char *
11130 get_target (credentials_t * credentials, params_t *params,
11131  const char *extra_xml, cmd_response_data_t* response_data)
11132 {
11133  return get_one ("target", credentials, params, extra_xml, "tasks=\"1\"",
11134  response_data);
11135 }
11136 
11146 char *
11147 get_target_omp (credentials_t * credentials, params_t *params,
11148  cmd_response_data_t* response_data)
11149 {
11150  return get_target (credentials, params, NULL, response_data);
11151 }
11152 
11163 static char *
11164 get_targets (credentials_t * credentials, params_t *params,
11165  const char *extra_xml, cmd_response_data_t* response_data)
11166 {
11167  return get_many ("target", credentials, params, extra_xml, NULL,
11168  response_data);
11169 }
11170 
11180 char *
11181 get_targets_omp (credentials_t * credentials, params_t *params,
11182  cmd_response_data_t* response_data)
11183 {
11184  return get_targets (credentials, params, NULL, response_data);
11185 }
11186 
11196 char *
11197 save_target_omp (credentials_t * credentials, params_t *params,
11198  cmd_response_data_t* response_data)
11199 {
11200  openvas_connection_t connection;
11201  gchar *html, *response;
11202  const char *no_redirect, *name, *hosts, *exclude_hosts, *comment;
11203  const char *target_ssh_credential, *port, *target_smb_credential;
11204  const char *target_esxi_credential, *target_snmp_credential, *target_source;
11205  const char *target_id, *port_list_id, *reverse_lookup_only;
11206  const char *reverse_lookup_unify, *alive_tests, *in_use;
11207  GString *command;
11208 
11209  no_redirect = params_value (params, "no_redirect");
11210  alive_tests = params_value (params, "alive_tests");
11211  name = params_value (params, "name");
11212  comment = params_value (params, "comment");
11213  in_use = params_value (params, "in_use");
11214  target_id = params_value (params, "target_id");
11215 
11216  CHECK_PARAM_INVALID (name, "Save Target", "edit_target");
11217  CHECK_PARAM_INVALID (target_id, "Save Target", "edit_target");
11218  CHECK_PARAM_INVALID (comment, "Save Target", "edit_target");
11219  CHECK_PARAM_INVALID (alive_tests, "Save Target", "edit_target");
11220  CHECK_PARAM_INVALID (in_use, "Save Target", "edit_target");
11221 
11222  if (strcmp (in_use, "0"))
11223  {
11224  entity_t entity;
11225  int ret;
11226 
11227  /* Target is in use. Modify fewer fields. */
11228 
11229  command = g_string_new ("");
11230  xml_string_append (command,
11231  "<modify_target target_id=\"%s\">"
11232  "<name>%s</name>"
11233  "<comment>%s</comment>"
11234  "<alive_tests>%s</alive_tests>"
11235  "</modify_target>",
11236  target_id,
11237  name ? name : "",
11238  comment ? comment : "",
11239  alive_tests);
11240 
11241  response = NULL;
11242  entity = NULL;
11243  ret = omp (credentials, &response, &entity, response_data, command->str);
11244  g_string_free (command, TRUE);
11245  switch (ret)
11246  {
11247  case 0:
11248  case -1:
11249  break;
11250  case 1:
11251  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11252  return gsad_message (credentials,
11253  "Internal error", __FUNCTION__, __LINE__,
11254  "An internal error occurred while saving a target. "
11255  "The target remains the same. "
11256  "Diagnostics: Failure to send command to manager daemon.",
11257  "/omp?cmd=get_targets", response_data);
11258  case 2:
11259  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11260  return gsad_message (credentials,
11261  "Internal error", __FUNCTION__, __LINE__,
11262  "An internal error occurred while saving a target. "
11263  "It is unclear whether the target has been saved or not. "
11264  "Diagnostics: Failure to receive response from manager daemon.",
11265  "/omp?cmd=get_targets", response_data);
11266  default:
11267  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11268  return gsad_message (credentials,
11269  "Internal error", __FUNCTION__, __LINE__,
11270  "An internal error occurred while saving a target. "
11271  "It is unclear whether the target has been saved or not. "
11272  "Diagnostics: Internal Error.",
11273  "/omp?cmd=get_targets", response_data);
11274  }
11275 
11276  html = response_from_entity (credentials, params, entity,
11277  (no_redirect && strcmp (no_redirect, "0")),
11278  NULL, "get_targets",
11279  NULL, "edit_target",
11280  "Save Target", response_data);
11281 
11282  free_entity (entity);
11283  g_free (response);
11284  return html;
11285  }
11286 
11287  hosts = params_value (params, "hosts");
11288  exclude_hosts = params_value (params, "exclude_hosts");
11289  reverse_lookup_only = params_value (params, "reverse_lookup_only");
11290  reverse_lookup_unify = params_value (params, "reverse_lookup_unify");
11291  target_source = params_value (params, "target_source");
11292  port_list_id = params_value (params, "port_list_id");
11293  target_ssh_credential = params_value (params, "ssh_credential_id");
11294  port = params_value (params, "port");
11295  target_smb_credential = params_value (params, "smb_credential_id");
11296  target_esxi_credential = params_value (params, "esxi_credential_id");
11297  target_snmp_credential = params_value (params, "snmp_credential_id");
11298 
11299  CHECK_PARAM_INVALID (target_source, "Save Target", "edit_target");
11300  CHECK_PARAM_INVALID (port_list_id, "Save Target", "edit_target");
11301  CHECK_PARAM_INVALID (target_ssh_credential, "Save Target", "edit_target");
11302  CHECK_PARAM_INVALID (target_smb_credential, "Save Target", "edit_target");
11303  CHECK_PARAM_INVALID (target_esxi_credential, "Save Target", "edit_target");
11304  CHECK_PARAM_INVALID (target_snmp_credential, "Save Target", "edit_target");
11305 
11306  if (strcmp (target_ssh_credential, "--")
11307  && strcmp (target_ssh_credential, "0"))
11308  CHECK_PARAM_INVALID (port, "Save Target", "edit_target");
11309 
11310  switch (manager_connect (credentials, &connection, &html,
11311  response_data))
11312  {
11313  case 0:
11314  break;
11315  case -1:
11316  if (html)
11317  return html;
11318  /* Fall through. */
11319  default:
11320  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11321  return gsad_message (credentials,
11322  "Internal error", __FUNCTION__, __LINE__,
11323  "An internal error occurred while modifying a target. "
11324  "The target was not modified. "
11325  "Diagnostics: Failure to connect to manager daemon.",
11326  "/omp?cmd=get_targets", response_data);
11327  }
11328 
11329  if (hosts == NULL && strcmp (target_source, "manual") == 0)
11330  {
11331  openvas_connection_close (&connection);
11332  return new_target (credentials, params,
11333  GSAD_MESSAGE_INVALID_PARAM ("Modify Target"),
11334  response_data);
11335  }
11336  if (strcmp (target_source, "import") == 0 && name == NULL)
11337  {
11338  gchar *msg;
11339  openvas_connection_close (&connection);
11340  msg = g_strdup_printf (GSAD_MESSAGE_INVALID,
11341  "Given target_source was invalid",
11342  "Modify Target");
11343  html = new_target (credentials, params, msg, response_data);
11344  g_free (msg);
11345  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11346  return html;
11347  }
11348 
11349  {
11350  int ret;
11351  gchar *ssh_credentials_element, *smb_credentials_element;
11352  gchar *esxi_credentials_element, *snmp_credentials_element;
11353  gchar* comment_element;
11354  entity_t entity;
11355 
11356  if (comment)
11357  comment_element = g_strdup_printf ("<comment>%s</comment>", comment);
11358  else
11359  comment_element = g_strdup ("");
11360 
11361  if (strcmp (target_ssh_credential, "--") == 0)
11362  ssh_credentials_element = g_strdup ("");
11363  else
11364  ssh_credentials_element =
11365  g_strdup_printf ("<ssh_credential id=\"%s\">"
11366  "<port>%s</port>"
11367  "</ssh_credential>",
11368  target_ssh_credential,
11369  port);
11370 
11371  if (strcmp (target_smb_credential, "--") == 0)
11372  smb_credentials_element = g_strdup ("");
11373  else
11374  smb_credentials_element =
11375  g_strdup_printf ("<smb_credential id=\"%s\"/>",
11376  target_smb_credential);
11377 
11378  if (strcmp (target_esxi_credential, "--") == 0)
11379  esxi_credentials_element = g_strdup ("");
11380  else
11381  esxi_credentials_element =
11382  g_strdup_printf ("<esxi_credential id=\"%s\"/>",
11383  target_esxi_credential);
11384 
11385  if (strcmp (target_snmp_credential, "--") == 0)
11386  snmp_credentials_element = g_strdup ("");
11387  else
11388  snmp_credentials_element =
11389  g_strdup_printf ("<snmp_credential id=\"%s\"/>",
11390  target_snmp_credential);
11391 
11392  command = g_string_new ("");
11393  xml_string_append (command,
11394  "<modify_target target_id=\"%s\">"
11395  "<name>%s</name>"
11396  "<hosts>%s</hosts>"
11397  "<exclude_hosts>%s</exclude_hosts>"
11398  "<reverse_lookup_only>%s</reverse_lookup_only>"
11399  "<reverse_lookup_unify>%s</reverse_lookup_unify>"
11400  "<port_list id=\"%s\"/>"
11401  "<alive_tests>%s</alive_tests>",
11402  target_id,
11403  name,
11404  strcmp (target_source, "file") == 0
11405  ? params_value (params, "file")
11406  : hosts,
11407  exclude_hosts ? exclude_hosts : "",
11408  reverse_lookup_only ? reverse_lookup_only : "0",
11409  reverse_lookup_unify ? reverse_lookup_unify : "0",
11410  port_list_id,
11411  alive_tests);
11412 
11413  g_string_append_printf (command,
11414  "%s%s%s%s%s"
11415  "</modify_target>",
11416  comment_element,
11417  ssh_credentials_element,
11418  smb_credentials_element,
11419  esxi_credentials_element,
11420  snmp_credentials_element);
11421 
11422  g_free (comment_element);
11423  g_free (ssh_credentials_element);
11424  g_free (smb_credentials_element);
11425  g_free (esxi_credentials_element);
11426  g_free (snmp_credentials_element);
11427 
11428  /* Modify the target. */
11429 
11430  ret = openvas_connection_sendf (&connection, "%s", command->str);
11431  g_string_free (command, TRUE);
11432 
11433  if (ret == -1)
11434  {
11435  openvas_connection_close (&connection);
11436  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11437  return gsad_message (credentials,
11438  "Internal error", __FUNCTION__, __LINE__,
11439  "An internal error occurred while modifying target. "
11440  "No target was modified. "
11441  "Diagnostics: Failure to send command to manager daemon.",
11442  "/omp?cmd=get_targets", response_data);
11443  }
11444 
11445  entity = NULL;
11446  if (read_entity_and_text_c (&connection, &entity, &response))
11447  {
11448  openvas_connection_close (&connection);
11449  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11450  return gsad_message (credentials,
11451  "Internal error", __FUNCTION__, __LINE__,
11452  "An internal error occurred while modifying a target. "
11453  "It is unclear whether the target has been modified or not. "
11454  "Diagnostics: Failure to receive response from manager daemon.",
11455  "/omp?cmd=get_targets", response_data);
11456  }
11457 
11458  openvas_connection_close (&connection);
11459 
11460  html = response_from_entity (credentials, params, entity,
11461  (no_redirect && strcmp (no_redirect, "0")),
11462  NULL, "get_targets",
11463  NULL, "edit_target",
11464  "Save Target", response_data);
11465  }
11466 
11467  /* Pass response to handler of following page. */
11468 
11469  return html;
11470 }
11471 
11485 char *
11486 export_target_omp (credentials_t * credentials, params_t *params,
11487  enum content_type * content_type, char **content_disposition,
11488  gsize *content_length, cmd_response_data_t* response_data)
11489 {
11490  return export_resource ("target", credentials, params, content_type,
11491  content_disposition, content_length, response_data);
11492 }
11493 
11507 char *
11509  enum content_type * content_type, char **content_disposition,
11510  gsize *content_length, cmd_response_data_t* response_data)
11511 {
11512  return export_many ("target", credentials, params, content_type,
11513  content_disposition, content_length, response_data);
11514 }
11515 
11526 static char *
11527 new_config (credentials_t *credentials, params_t *params,
11528  const char *extra_xml, cmd_response_data_t* response_data)
11529 {
11530  GString *xml;
11531  int ret;
11532  entity_t entity = NULL;
11533  gchar *response = NULL;
11534 
11535  xml = g_string_new ("<new_config>");
11536  if (extra_xml)
11537  g_string_append (xml, extra_xml);
11538 
11539  /* Get Scanners. */
11540  ret = omp (credentials, &response, &entity, response_data, "<get_scanners/>");
11541  switch (ret)
11542  {
11543  case 0:
11544  case -1:
11545  break;
11546  case 1:
11547  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11548  return gsad_message (credentials,
11549  "Internal error", __FUNCTION__, __LINE__,
11550  "An internal error occurred while getting scanners"
11551  " for new config. Diagnostics: Failure to send"
11552  " command to manager daemon.",
11553  "/omp?cmd=get_configs", response_data);
11554  case 2:
11555  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11556  return gsad_message (credentials,
11557  "Internal error", __FUNCTION__, __LINE__,
11558  "An internal error occurred while getting scanners"
11559  " for new config. " "Diagnostics: Failure to"
11560  " receive response from manager daemon.",
11561  "/omp?cmd=get_configs", response_data);
11562  default:
11563  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11564  return gsad_message (credentials,
11565  "Internal error", __FUNCTION__, __LINE__,
11566  "An internal error occurred while getting scanners"
11567  "for new config. It is unclear whether the config"
11568  " has been saved or not. "
11569  "Diagnostics: Internal Error.",
11570  "/omp?cmd=get_configs", response_data);
11571  }
11572  g_string_append (xml, response);
11573  g_free (response);
11574  free_entity (entity);
11575 
11576  g_string_append (xml, "</new_config>");
11577  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
11578  response_data);
11579 }
11580 
11590 char *
11591 new_config_omp (credentials_t *credentials, params_t *params,
11592  cmd_response_data_t* response_data)
11593 {
11594  return new_config (credentials, params, NULL, response_data);
11595 }
11596 
11607 static char *
11608 upload_config (credentials_t *credentials, params_t *params,
11609  const char *extra_xml, cmd_response_data_t* response_data)
11610 {
11611  GString *xml;
11612 
11613  xml = g_string_new ("<upload_config>");
11614  if (extra_xml)
11615  g_string_append (xml, extra_xml);
11616  g_string_append (xml, "</upload_config>");
11617 
11618  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
11619  response_data);
11620 }
11621 
11631 char *
11633  cmd_response_data_t* response_data)
11634 {
11635  return upload_config (credentials, params, NULL, response_data);
11636 }
11637 
11647 char *
11648 create_config_omp (credentials_t * credentials, params_t *params,
11649  cmd_response_data_t* response_data)
11650 {
11651  gchar *html, *response;
11652  const char *no_redirect, *name, *comment, *base, *scanner = NULL;
11653  entity_t entity;
11654 
11655  no_redirect = params_value (params, "no_redirect");
11656  name = params_value (params, "name");
11657  comment = params_value (params, "comment");
11658  base = params_value (params, "base");
11659 
11660  CHECK_PARAM_INVALID (name, "New Config", "new_config");
11661  CHECK_PARAM_INVALID (comment, "New Config", "new_config");
11662  CHECK_PARAM_INVALID (base, "New Config", "new_config");
11663  if (!strcmp (base, "0"))
11664  {
11665  scanner = params_value (params, "scanner_id");
11666  CHECK_PARAM_INVALID (scanner, "New Config", "new_config");
11667  }
11668 
11669  /* Create the config. */
11670  switch (ompf (credentials,
11671  &response,
11672  &entity,
11673  response_data,
11674  "<create_config>"
11675  "<name>%s</name>"
11676  "<copy>%s</copy>"
11677  "<comment>%s</comment>"
11678  "<scanner>%s</scanner>"
11679  "</create_config>",
11680  name, base, comment,
11681  scanner ?: ""))
11682  {
11683  case 0:
11684  case -1:
11685  break;
11686  case 1:
11687  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11688  return gsad_message (credentials,
11689  "Internal error", __FUNCTION__, __LINE__,
11690  "An internal error occurred while creating a new config. "
11691  "No new config was created. "
11692  "Diagnostics: Failure to send command to manager daemon.",
11693  "/omp?cmd=get_configs", response_data);
11694  case 2:
11695  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11696  return gsad_message (credentials,
11697  "Internal error", __FUNCTION__, __LINE__,
11698  "An internal error occurred while creating a new config. "
11699  "It is unclear whether the config has been created or not. "
11700  "Diagnostics: Failure to receive response from manager daemon.",
11701  "/omp?cmd=get_configs", response_data);
11702  default:
11703  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11704  return gsad_message (credentials,
11705  "Internal error", __FUNCTION__, __LINE__,
11706  "An internal error occurred while creating a new config. "
11707  "It is unclear whether the config has been created or not. "
11708  "Diagnostics: Internal Error.",
11709  "/omp?cmd=get_configs", response_data);
11710  }
11711 
11712  if (entity_attribute (entity, "id"))
11713  params_add (params, "config_id", entity_attribute (entity, "id"));
11714  html = response_from_entity (credentials, params, entity,
11715  (no_redirect && strcmp (no_redirect, "0")),
11716  NULL, "get_configs",
11717  NULL, "new_config",
11718  "Create Config", response_data);
11719 
11720  free_entity (entity);
11721  g_free (response);
11722  return html;
11723 }
11724 
11734 char *
11735 import_config_omp (credentials_t * credentials, params_t *params,
11736  cmd_response_data_t* response_data)
11737 {
11738  const char *no_redirect;
11739  gchar *command, *html, *response;
11740  entity_t entity;
11741  int ret;
11742 
11743  no_redirect = params_value (params, "no_redirect");
11744 
11745  /* Create the config. */
11746 
11747  response = NULL;
11748  entity = NULL;
11749  command = g_strdup_printf ("<create_config>"
11750  "%s"
11751  "</create_config>",
11752  params_value (params, "xml_file"));
11753  ret = omp (credentials, &response, &entity, response_data, command);
11754  g_free (command);
11755  switch (ret)
11756  {
11757  case 0:
11758  case -1:
11759  break;
11760  case 1:
11761  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11762  return gsad_message (credentials,
11763  "Internal error", __FUNCTION__, __LINE__,
11764  "An internal error occurred while importing a config. "
11765  "The schedule remains the same. "
11766  "Diagnostics: Failure to send command to manager daemon.",
11767  "/omp?cmd=get_configs", response_data);
11768  case 2:
11769  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11770  return gsad_message (credentials,
11771  "Internal error", __FUNCTION__, __LINE__,
11772  "An internal error occurred while importing a config. "
11773  "It is unclear whether the schedule has been saved or not. "
11774  "Diagnostics: Failure to receive response from manager daemon.",
11775  "/omp?cmd=get_configs", response_data);
11776  default:
11777  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11778  return gsad_message (credentials,
11779  "Internal error", __FUNCTION__, __LINE__,
11780  "An internal error occurred while importing a config. "
11781  "It is unclear whether the schedule has been saved or not. "
11782  "Diagnostics: Internal Error.",
11783  "/omp?cmd=get_configs", response_data);
11784  }
11785 
11786  /* Cleanup, and return transformed XML. */
11787 
11788  html = response_from_entity (credentials, params, entity,
11789  (no_redirect && strcmp (no_redirect, "0")),
11790  NULL, "get_configs",
11791  NULL, "new_config",
11792  "Import Config", response_data);
11793  free_entity (entity);
11794  g_free (response);
11795  return html;
11796 }
11797 
11808 static char *
11809 get_configs (credentials_t *credentials, params_t *params,
11810  const char *extra_xml, cmd_response_data_t* response_data)
11811 {
11812  return get_many ("config", credentials, params, extra_xml, NULL,
11813  response_data);
11814 }
11815 
11825 char *
11826 get_configs_omp (credentials_t * credentials, params_t *params,
11827  cmd_response_data_t* response_data)
11828 {
11829  return get_configs (credentials, params, NULL, response_data);
11830 }
11831 
11843 static char *
11844 get_config (credentials_t * credentials, params_t *params,
11845  const char *extra_xml, int edit, cmd_response_data_t* response_data)
11846 {
11847  GString *xml;
11848  openvas_connection_t connection;
11849  gchar *html;
11850  const char *config_id;
11851 
11852  config_id = params_value (params, "config_id");
11853 
11854  if (config_id == NULL)
11855  return get_configs (credentials, params, extra_xml, response_data);
11856 
11857  switch (manager_connect (credentials, &connection, &html,
11858  response_data))
11859  {
11860  case 0:
11861  break;
11862  case -1:
11863  if (html)
11864  return html;
11865  /* Fall through. */
11866  default:
11867  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11868  return gsad_message (credentials,
11869  "Internal error", __FUNCTION__, __LINE__,
11870  "An internal error occurred while getting list of configs. "
11871  "The current list of configs is not available. "
11872  "Diagnostics: Failure to connect to manager daemon.",
11873  "/omp?cmd=get_configs", response_data);
11874  }
11875 
11876  xml = g_string_new ("<get_config_response>");
11877  if (edit) g_string_append (xml, "<edit/>");
11878 
11879  if (extra_xml)
11880  g_string_append (xml, extra_xml);
11881  /* Get the config families. */
11882 
11883  if (openvas_connection_sendf (&connection,
11884  "<get_configs"
11885  " config_id=\"%s\""
11886  " families=\"1\""
11887  " tasks=\"1\""
11888  " preferences=\"1\"/>",
11889  config_id)
11890  == -1)
11891  {
11892  g_string_free (xml, TRUE);
11893  openvas_connection_close (&connection);
11894  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11895  return gsad_message (credentials,
11896  "Internal error", __FUNCTION__, __LINE__,
11897  "An internal error occurred while getting the config. "
11898  "The config is not available. "
11899  "Diagnostics: Failure to send command to manager daemon.",
11900  "/omp?cmd=get_configs", response_data);
11901  }
11902 
11903  if (read_string_c (&connection, &xml))
11904  {
11905  g_string_free (xml, TRUE);
11906  openvas_connection_close (&connection);
11907  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11908  return gsad_message (credentials,
11909  "Internal error", __FUNCTION__, __LINE__,
11910  "An internal error occurred while getting the config. "
11911  "The config is not available. "
11912  "Diagnostics: Failure to receive response from manager daemon.",
11913  "/omp?cmd=get_configs", response_data);
11914  }
11915 
11916  /* Get all the families. */
11917 
11918  if (openvas_connection_sendf (&connection, "<get_nvt_families/>") == -1)
11919  {
11920  g_string_free (xml, TRUE);
11921  openvas_connection_close (&connection);
11922  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11923  return gsad_message (credentials,
11924  "Internal error", __FUNCTION__, __LINE__,
11925  "An internal error occurred while getting the config. "
11926  "The config is not available. "
11927  "Diagnostics: Failure to send command to manager daemon.",
11928  "/omp?cmd=get_configs", response_data);
11929  }
11930 
11931  if (read_string_c (&connection, &xml))
11932  {
11933  g_string_free (xml, TRUE);
11934  openvas_connection_close (&connection);
11935  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11936  return gsad_message (credentials,
11937  "Internal error", __FUNCTION__, __LINE__,
11938  "An internal error occurred while getting the config. "
11939  "The config is not available. "
11940  "Diagnostics: Failure to receive response from manager daemon.",
11941  "/omp?cmd=get_configs", response_data);
11942  }
11943 
11944  if (edit)
11945  {
11946  /* Get OSP scanners */
11947  if (openvas_connection_sendf (&connection, "<get_scanners filter=\"type=1\"/>")
11948  == -1)
11949  {
11950  g_string_free (xml, TRUE);
11951  openvas_connection_close (&connection);
11952  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11953  return gsad_message
11954  (credentials, "Internal error", __FUNCTION__, __LINE__,
11955  "An internal error occurred while getting the config. "
11956  "The config is not available. "
11957  "Diagnostics: Failure to send command to manager daemon.",
11958  "/omp?cmd=get_configs", response_data);
11959  }
11960 
11961  if (read_string_c (&connection, &xml))
11962  {
11963  g_string_free (xml, TRUE);
11964  openvas_connection_close (&connection);
11965  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11966  return gsad_message
11967  (credentials, "Internal error", __FUNCTION__, __LINE__,
11968  "An internal error occurred while getting the config. "
11969  "The config is not available. "
11970  "Diagnostics: Failure to receive response from manager daemon.",
11971  "/omp?cmd=get_configs", response_data);
11972  }
11973 
11974  }
11975 
11976  /* Get Credentials */
11977  if (openvas_connection_sendf (&connection, "<get_credentials/>") == -1)
11978  {
11979  g_string_free (xml, TRUE);
11980  openvas_connection_close (&connection);
11981  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11982  return gsad_message
11983  (credentials, "Internal error", __FUNCTION__, __LINE__,
11984  "An internal error occurred while getting the config. "
11985  "The config is not available. "
11986  "Diagnostics: Failure to send command to manager daemon.",
11987  "/omp?cmd=get_configs", response_data);
11988  }
11989  if (read_string_c (&connection, &xml))
11990  {
11991  g_string_free (xml, TRUE);
11992  openvas_connection_close (&connection);
11993  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
11994  return gsad_message
11995  (credentials, "Internal error", __FUNCTION__, __LINE__,
11996  "An internal error occurred while getting the config. "
11997  "The config is not available. "
11998  "Diagnostics: Failure to receive response from manager daemon.",
11999  "/omp?cmd=get_configs", response_data);
12000  }
12001 
12002  /* Get the permissions */
12003 
12004  g_string_append (xml, "<permissions>");
12005 
12006  if (openvas_connection_sendf (&connection,
12007  "<get_permissions"
12008  " filter=\"name:^.*(config)s?$"
12009  " and resource_uuid=%s"
12010  " first=1 rows=-1\"/>",
12011  config_id)
12012  == -1)
12013  {
12014  g_string_free (xml, TRUE);
12015  openvas_connection_close (&connection);
12016  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12017  return gsad_message (credentials,
12018  "Internal error", __FUNCTION__, __LINE__,
12019  "An internal error occurred while getting permissions list. "
12020  "The current list of resources is not available. "
12021  "Diagnostics: Failure to send command to manager daemon.",
12022  "/omp?cmd=get_resources", response_data);
12023  }
12024 
12025  if (read_string_c (&connection, &xml))
12026  {
12027  g_string_free (xml, TRUE);
12028  openvas_connection_close (&connection);
12029  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12030  return gsad_message (credentials,
12031  "Internal error", __FUNCTION__, __LINE__,
12032  "An internal error occurred while getting permissions list. "
12033  "The current list of resources is not available. "
12034  "Diagnostics: Failure to receive response from manager daemon.",
12035  "/omp?cmd=get_resources", response_data);
12036  }
12037 
12038  g_string_append (xml, "</permissions>");
12039 
12040  /* Cleanup, and return transformed XML. */
12041 
12042  g_string_append (xml, "</get_config_response>");
12043  openvas_connection_close (&connection);
12044  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
12045  response_data);
12046 }
12047 
12057 char *
12058 get_config_omp (credentials_t * credentials, params_t *params,
12059  cmd_response_data_t* response_data)
12060 {
12061  return get_config (credentials, params, NULL, 0, response_data);
12062 }
12063 
12073 static char *
12074 edit_config (credentials_t * credentials, params_t *params,
12075  const char *extra_xml, cmd_response_data_t* response_data)
12076 {
12077  return get_config (credentials, params, extra_xml, 1, response_data);
12078 }
12079 
12089 char *
12090 edit_config_omp (credentials_t * credentials, params_t *params,
12091  cmd_response_data_t* response_data)
12092 {
12093  return edit_config (credentials, params, NULL, response_data);
12094 }
12095 
12105 char *
12106 sync_config_omp (credentials_t * credentials, params_t *params,
12107  cmd_response_data_t* response_data)
12108 {
12109  GString *xml;
12110  openvas_connection_t connection;
12111  gchar *html;
12112  const char *config_id, *next;
12113  char *ret;
12114 
12115  config_id = params_value (params, "config_id");
12116  CHECK_PARAM (config_id, "Synchronize Config", get_configs);
12117  switch (manager_connect (credentials, &connection, &html,
12118  response_data))
12119  {
12120  case 0:
12121  break;
12122  case -1:
12123  if (html)
12124  return html;
12125  /* Fall through. */
12126  default:
12127  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12128  return gsad_message
12129  (credentials, "Internal error", __FUNCTION__, __LINE__,
12130  "An internal error occurred while synchronizing a config. "
12131  "The config is not synchronized. "
12132  "Diagnostics: Failure to connect to manager daemon.",
12133  "/omp?cmd=get_configs", response_data);
12134  }
12135 
12136  if (openvas_connection_sendf (&connection, "<sync_config config_id=\"%s\"/>",
12137  config_id)
12138  == -1)
12139  {
12140  openvas_connection_close (&connection);
12141  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12142  return gsad_message
12143  (credentials, "Internal error", __FUNCTION__, __LINE__,
12144  "An internal error occurred while synchronizing a config. "
12145  "The config is not synchronized. "
12146  "Diagnostics: Failure to send command to manager daemon.",
12147  "/omp?cmd=get_configs", response_data);
12148  }
12149 
12150  xml = g_string_new ("");
12151 
12152  if (read_string_c (&connection, &xml))
12153  {
12154  g_string_free (xml, TRUE);
12155  openvas_connection_close (&connection);
12156  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12157  return gsad_message
12158  (credentials, "Internal error", __FUNCTION__, __LINE__,
12159  "An internal error occurred while synchronizing a config. "
12160  "It is unclear whether the config has been synchronized or not. "
12161  "Diagnostics: Failure to receive response from manager daemon.",
12162  "/omp?cmd=get_configs", response_data);
12163  }
12164 
12165  next = params_value (params, "next");
12166  if (next && !strcmp (next, "get_config"))
12167  ret = get_config (credentials, params, xml->str, 0, response_data);
12168  else
12169  ret = get_configs (credentials, params, xml->str, response_data);
12170  openvas_connection_close (&connection);
12171  g_string_free (xml, TRUE);
12172  return ret;
12173 }
12174 
12175 
12189 static char *
12190 save_osp_prefs (credentials_t *credentials, openvas_connection_t *connection,
12191  params_t *params, const char *next, const char *fail_next,
12192  int *success, cmd_response_data_t* response_data)
12193 {
12194  GHashTableIter iter;
12195  gpointer param_name, val;
12196  char *ret;
12197  const char *config_id;
12198 
12199  config_id = params_value (params, "config_id");
12200  g_hash_table_iter_init (&iter, params);
12201  ret = NULL;
12202  while (g_hash_table_iter_next (&iter, &param_name, &val))
12203  {
12204  gchar *value;
12205  param_t *param = val;
12206 
12207  g_free (ret);
12208  ret = NULL;
12209 
12210  if (!g_str_has_prefix (param_name, "osp_pref_"))
12211  continue;
12212  value = param->value_size ? g_base64_encode ((guchar *) param->value,
12213  param->value_size)
12214  : g_strdup ("");
12215 
12216  /* Send the name without the osp_pref_ prefix. */
12217  param_name = ((char *) param_name) + 9;
12218  if (openvas_connection_sendf (connection,
12219  "<modify_config config_id=\"%s\">"
12220  "<preference><name>%s</name>"
12221  "<value>%s</value></preference>"
12222  "</modify_config>",
12223  config_id,
12224  (char *) param_name,
12225  value)
12226  == -1)
12227  {
12228  g_free (value);
12229  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12230  return gsad_message
12231  (credentials, "Internal error", __FUNCTION__, __LINE__,
12232  "An internal error occurred while saving a config. It is"
12233  " unclear whether the entire config has been saved. "
12234  "Diagnostics: Failure to send command to manager daemon.",
12235  "/omp?cmd=get_configs", response_data);
12236  }
12237  g_free (value);
12238 
12239  ret = check_modify_config (credentials, connection, params, next,
12240  fail_next, success, response_data);
12241  if (*success == 0)
12242  return ret;
12243  }
12244  return ret;
12245 }
12246 
12256 char *
12257 save_config_omp (credentials_t * credentials, params_t *params,
12258  cmd_response_data_t* response_data)
12259 {
12260  openvas_connection_t connection;
12261  int omp_ret;
12262  char *ret;
12263  gchar *html;
12264  params_t *preferences, *selects, *trends;
12265  const char *config_id, *name, *comment, *scanner_id;
12266  int success;
12267 
12268  config_id = params_value (params, "config_id");
12269  name = params_value (params, "name");
12270  comment = params_value (params, "comment");
12271  scanner_id = params_value (params, "scanner_id");
12272 
12273  CHECK_PARAM_INVALID (config_id, "Save Config", "edit_config");
12274  CHECK_PARAM_INVALID (name, "Save Config", "edit_config");
12275  CHECK_PARAM_INVALID (comment, "Save Config", "edit_config");
12276 
12277  switch (manager_connect (credentials, &connection, &html,
12278  response_data))
12279  {
12280  case 0:
12281  break;
12282  case -1:
12283  if (html)
12284  return html;
12285  /* Fall through. */
12286  default:
12287  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12288  return gsad_message (credentials,
12289  "Internal error", __FUNCTION__, __LINE__,
12290  "An internal error occurred while saving a config. "
12291  "The current list of configs is not available. "
12292  "Diagnostics: Failure to connect to manager daemon.",
12293  "/omp?cmd=get_configs", response_data);
12294  }
12295 
12296  /* Save name and comment. */
12297 
12298  if (scanner_id)
12299  omp_ret = openvas_connection_sendf_xml (&connection,
12300  "<modify_config config_id=\"%s\">"
12301  "<name>%s</name>"
12302  "<comment>%s</comment>"
12303  "<scanner>%s</scanner>"
12304  "</modify_config>",
12305  params_value (params, "config_id"),
12306  name,
12307  comment,
12308  scanner_id);
12309  else
12310  omp_ret = openvas_connection_sendf_xml (&connection,
12311  "<modify_config config_id=\"%s\">"
12312  "<name>%s</name>"
12313  "<comment>%s</comment>"
12314  "</modify_config>",
12315  params_value (params, "config_id"),
12316  name,
12317  comment);
12318 
12319  if (omp_ret == -1)
12320  {
12321  openvas_connection_close (&connection);
12322  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12323  return gsad_message (credentials,
12324  "Internal error", __FUNCTION__, __LINE__,
12325  "An internal error occurred while saving a config. "
12326  "It is unclear whether the entire config has been saved. "
12327  "Diagnostics: Failure to send command to manager daemon.",
12328  "/omp?cmd=get_configs", response_data);
12329  }
12330 
12331  ret = check_modify_config (credentials, &connection, params,
12332  "get_config", "edit_config",
12333  &success, response_data);
12334  if (success == 0)
12335  {
12336  openvas_connection_close (&connection);
12337  return ret;
12338  }
12339  g_free (ret);
12340 
12341  /* Save preferences. */
12342 
12343  preferences = params_values (params, "preference:");
12344  if (preferences)
12345  {
12346  params_iterator_t iter;
12347  char *param_name;
12348  param_t *param;
12349 
12350  params_iterator_init (&iter, preferences);
12351  while (params_iterator_next (&iter, &param_name, &param))
12352  {
12353  gchar *value;
12354 
12355  value = param->value_size
12356  ? g_base64_encode ((guchar *) param->value,
12357  param->value_size)
12358  : g_strdup ("");
12359 
12360  if (openvas_connection_sendf (&connection,
12361  "<modify_config config_id=\"%s\">"
12362  "<preference>"
12363  "<name>%s</name>"
12364  "<value>%s</value>"
12365  "</preference>"
12366  "</modify_config>",
12367  params_value (params, "config_id"),
12368  param_name,
12369  value)
12370  == -1)
12371  {
12372  g_free (value);
12373  openvas_connection_close (&connection);
12374  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12375  return gsad_message (credentials,
12376  "Internal error", __FUNCTION__, __LINE__,
12377  "An internal error occurred while saving a config. "
12378  "It is unclear whether the entire config has been saved. "
12379  "Diagnostics: Failure to send command to manager daemon.",
12380  "/omp?cmd=get_configs", response_data);
12381  }
12382  g_free (value);
12383 
12384  ret = check_modify_config (credentials, &connection, params,
12385  "get_config", "edit_config",
12386  &success, response_data);
12387  if (success == 0)
12388  {
12389  openvas_connection_close (&connection);
12390  return ret;
12391  }
12392  }
12393  }
12394 
12395  /* OSP config file preference. */
12396  ret = save_osp_prefs (credentials, &connection, params,
12397  "get_config", "edit_config",
12398  &success, response_data);
12399  if (success == 0)
12400  {
12401  openvas_connection_close (&connection);
12402  return ret;
12403  }
12404  g_free (ret);
12405 
12406  /* Update the config. */
12407 
12408  trends = params_values (params, "trend:");
12409 
12410  if (openvas_connection_sendf (&connection,
12411  "<modify_config config_id=\"%s\">"
12412  "<family_selection>"
12413  "<growing>%i</growing>",
12414  params_value (params, "config_id"),
12415  trends
12416  && params_value (params, "trend")
12417  && strcmp (params_value (params, "trend"), "0"))
12418  == -1)
12419  {
12420  openvas_connection_close (&connection);
12421  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12422  return gsad_message (credentials,
12423  "Internal error", __FUNCTION__, __LINE__,
12424  "An internal error occurred while saving a config. "
12425  "It is unclear whether the entire config has been saved. "
12426  "Diagnostics: Failure to send command to manager daemon.",
12427  "/omp?cmd=get_configs", response_data);
12428  }
12429 
12430  selects = params_values (params, "select:");
12431 
12432  if (selects)
12433  {
12434  gchar *family;
12435  params_iterator_t iter;
12436  param_t *param;
12437 
12438  params_iterator_init (&iter, selects);
12439  while (params_iterator_next (&iter, &family, &param))
12440  if (openvas_connection_sendf (&connection,
12441  "<family>"
12442  "<name>%s</name>"
12443  "<all>1</all>"
12444  "<growing>%i</growing>"
12445  "</family>",
12446  family,
12447  trends && member1 (trends, family))
12448  == -1)
12449  {
12450  openvas_connection_close (&connection);
12451  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12452  return gsad_message (credentials,
12453  "Internal error", __FUNCTION__, __LINE__,
12454  "An internal error occurred while saving a config. "
12455  "It is unclear whether the entire config has been saved. "
12456  "Diagnostics: Failure to send command to manager daemon.",
12457  "/omp?cmd=get_configs", response_data);
12458  }
12459  }
12460 
12461  if (trends)
12462  {
12463  gchar *family;
12464  params_iterator_t iter;
12465  param_t *param;
12466 
12467  params_iterator_init (&iter, trends);
12468  while (params_iterator_next (&iter, &family, &param))
12469  {
12470  if (param->value_size == 0) continue;
12471  if (param->value[0] == '0') continue;
12472  if (selects && member (selects, family)) continue;
12473  if (openvas_connection_sendf (&connection,
12474  "<family>"
12475  "<name>%s</name>"
12476  "<all>0</all>"
12477  "<growing>1</growing>"
12478  "</family>",
12479  family)
12480  == -1)
12481  {
12482  openvas_connection_close (&connection);
12483  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12484  return gsad_message (credentials,
12485  "Internal error", __FUNCTION__, __LINE__,
12486  "An internal error occurred while saving a config. "
12487  "It is unclear whether the entire config has been saved. "
12488  "Diagnostics: Failure to send command to manager daemon.",
12489  "/omp?cmd=get_configs", response_data);
12490  }
12491  }
12492  }
12493 
12494  if (openvas_connection_sendf (&connection,
12495  "</family_selection>"
12496  "</modify_config>")
12497  == -1)
12498  {
12499  openvas_connection_close (&connection);
12500  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12501  return gsad_message (credentials,
12502  "Internal error", __FUNCTION__, __LINE__,
12503  "An internal error occurred while saving a config. "
12504  "It is unclear whether the entire config has been saved. "
12505  "Diagnostics: Failure to send command to manager daemon.",
12506  "/omp?cmd=get_configs", response_data);
12507  }
12508 
12509  ret = check_modify_config (credentials, &connection, params,
12510  "get_config", "edit_config",
12511  NULL, response_data);
12512 
12513  openvas_connection_close (&connection);
12514  return ret;
12515 }
12516 
12527 static char *
12528 get_config_family (credentials_t * credentials, params_t *params, int edit,
12529  cmd_response_data_t* response_data)
12530 {
12531  GString *xml;
12532  openvas_connection_t connection;
12533  gchar *html;
12534  const char *config_id, *name, *family, *sort_field, *sort_order;
12535 
12536  config_id = params_value (params, "config_id");
12537  name = params_value (params, "name");
12538  family = params_value (params, "family");
12539 
12540  if ((config_id == NULL) || (name == NULL) || (family == NULL))
12541  {
12542  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12543  return gsad_message (credentials,
12544  "Internal error", __FUNCTION__, __LINE__,
12545  "An internal error occurred while getting config family. "
12546  "Diagnostics: Required parameter was NULL.",
12547  "/omp?cmd=get_configs", response_data);
12548  }
12549 
12550  switch (manager_connect (credentials, &connection, &html,
12551  response_data))
12552  {
12553  case 0:
12554  break;
12555  case -1:
12556  if (html)
12557  return html;
12558  /* Fall through. */
12559  default:
12560  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12561  return gsad_message (credentials,
12562  "Internal error", __FUNCTION__, __LINE__,
12563  "An internal error occurred while getting config family. "
12564  "The current list of configs is not available. "
12565  "Diagnostics: Failure to connect to manager daemon.",
12566  "/omp?cmd=get_configs", response_data);
12567  }
12568 
12569  xml = g_string_new ("<get_config_family_response>");
12570  if (edit) g_string_append (xml, "<edit/>");
12571  /* @todo Would it be better include this in the get_nvts response? */
12572  g_string_append_printf (xml,
12573  "<config id=\"%s\">"
12574  "<name>%s</name><family>%s</family>"
12575  "</config>",
12576  config_id,
12577  name,
12578  family);
12579 
12580  /* Get the details for all NVT's in the config in the family. */
12581 
12582  sort_field = params_value (params, "sort_field");
12583  sort_order = params_value (params, "sort_order");
12584 
12585  if (openvas_connection_sendf (&connection,
12586  "<get_nvts"
12587  " config_id=\"%s\" details=\"1\""
12588  " family=\"%s\" timeout=\"1\" preference_count=\"1\""
12589  " sort_field=\"%s\" sort_order=\"%s\"/>",
12590  config_id,
12591  family,
12592  sort_field ? sort_field : "nvts.name",
12593  sort_order ? sort_order : "ascending")
12594  == -1)
12595  {
12596  g_string_free (xml, TRUE);
12597  openvas_connection_close (&connection);
12598  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12599  return gsad_message (credentials,
12600  "Internal error", __FUNCTION__, __LINE__,
12601  "An internal error occurred while getting list of configs. "
12602  "The current list of configs is not available. "
12603  "Diagnostics: Failure to send command to manager daemon.",
12604  "/omp?cmd=get_configs", response_data);
12605  }
12606 
12607  if (read_string_c (&connection, &xml))
12608  {
12609  g_string_free (xml, TRUE);
12610  openvas_connection_close (&connection);
12611  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12612  return gsad_message (credentials,
12613  "Internal error", __FUNCTION__, __LINE__,
12614  "An internal error occurred while getting list of configs. "
12615  "The current list of configs is not available. "
12616  "Diagnostics: Failure to receive response from manager daemon.",
12617  "/omp?cmd=get_configs", response_data);
12618  }
12619 
12620  if (edit)
12621  {
12622  /* Get the details for all NVT's in the family. */
12623 
12624  g_string_append (xml, "<all>");
12625 
12626  if (openvas_connection_sendf (&connection,
12627  "<get_nvts"
12628  " details=\"1\""
12629  " timeout=\"1\""
12630  " family=\"%s\""
12631  " preferences_config_id=\"%s\""
12632  " preference_count=\"1\""
12633  " sort_field=\"%s\""
12634  " sort_order=\"%s\"/>",
12635  family,
12636  config_id,
12637  sort_field ? sort_field : "nvts.name",
12638  sort_order ? sort_order : "ascending")
12639  == -1)
12640  {
12641  g_string_free (xml, TRUE);
12642  openvas_connection_close (&connection);
12643  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12644  return gsad_message (credentials,
12645  "Internal error", __FUNCTION__, __LINE__,
12646  "An internal error occurred while getting list of configs. "
12647  "The current list of configs is not available. "
12648  "Diagnostics: Failure to send command to manager daemon.",
12649  "/omp?cmd=get_configs", response_data);
12650  }
12651 
12652  if (read_string_c (&connection, &xml))
12653  {
12654  g_string_free (xml, TRUE);
12655  openvas_connection_close (&connection);
12656  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12657  return gsad_message (credentials,
12658  "Internal error", __FUNCTION__, __LINE__,
12659  "An internal error occurred while getting list of configs. "
12660  "The current list of configs is not available. "
12661  "Diagnostics: Failure to receive response from manager daemon.",
12662  "/omp?cmd=get_configs", response_data);
12663  }
12664 
12665  g_string_append (xml, "</all>");
12666  }
12667 
12668  g_string_append (xml, "</get_config_family_response>");
12669  openvas_connection_close (&connection);
12670  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
12671  response_data);
12672 }
12673 
12683 char *
12685  cmd_response_data_t* response_data)
12686 {
12687  return get_config_family (credentials, params, 0, response_data);
12688 }
12689 
12699 char *
12701  cmd_response_data_t* response_data)
12702 {
12703  return get_config_family (credentials, params, 1, response_data);
12704 }
12705 
12715 char *
12717  cmd_response_data_t* response_data)
12718 {
12719  openvas_connection_t connection;
12720  char *ret;
12721  gchar *html;
12722  const char *config_id, *family;
12723  params_t *nvts;
12724 
12725  config_id = params_value (params, "config_id");
12726  family = params_value (params, "family");
12727 
12728  if ((config_id == NULL) || (family == NULL))
12729  {
12730  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12731  return gsad_message (credentials,
12732  "Internal error", __FUNCTION__, __LINE__,
12733  "An internal error occurred while saving getting config family. "
12734  "The config has not been saved. "
12735  "Diagnostics: Required parameter was NULL.",
12736  "/omp?cmd=get_configs", response_data);
12737  }
12738 
12739  switch (manager_connect (credentials, &connection, &html,
12740  response_data))
12741  {
12742  case 0:
12743  break;
12744  case -1:
12745  if (html)
12746  return html;
12747  /* Fall through. */
12748  default:
12749  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12750  return gsad_message (credentials,
12751  "Internal error", __FUNCTION__, __LINE__,
12752  "An internal error occurred while saving a config. "
12753  "The current list of configs is not available. "
12754  "The config has not been saved. "
12755  "Diagnostics: Failure to connect to manager daemon.",
12756  "/omp?cmd=get_configs", response_data);
12757  }
12758 
12759  /* Set the NVT selection. */
12760 
12761  if (openvas_connection_sendf (&connection,
12762  "<modify_config config_id=\"%s\">"
12763  "<nvt_selection>"
12764  "<family>%s</family>",
12765  config_id,
12766  family)
12767  == -1)
12768  {
12769  openvas_connection_close (&connection);
12770  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12771  return gsad_message (credentials,
12772  "Internal error", __FUNCTION__, __LINE__,
12773  "An internal error occurred while saving a config. "
12774  "It is unclear whether the entire config has been saved. "
12775  "Diagnostics: Failure to send command to manager daemon.",
12776  "/omp?cmd=get_configs", response_data);
12777  }
12778 
12779  nvts = params_values (params, "nvt:");
12780  if (nvts)
12781  {
12782  params_iterator_t iter;
12783  char *name;
12784  param_t *param;
12785 
12786  params_iterator_init (&iter, nvts);
12787  while (params_iterator_next (&iter, &name, &param))
12788  if (openvas_connection_sendf (&connection,
12789  "<nvt oid=\"%s\"/>",
12790  name)
12791  == -1)
12792  {
12793  openvas_connection_close (&connection);
12794  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12795  return gsad_message (credentials,
12796  "Internal error", __FUNCTION__, __LINE__,
12797  "An internal error occurred while saving a config. "
12798  "It is unclear whether the entire config has been saved. "
12799  "Diagnostics: Failure to send command to manager daemon.",
12800  "/omp?cmd=get_configs", response_data);
12801  }
12802  }
12803 
12804  if (openvas_connection_sendf (&connection,
12805  "</nvt_selection>"
12806  "</modify_config>")
12807  == -1)
12808  {
12809  openvas_connection_close (&connection);
12810  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12811  return gsad_message (credentials,
12812  "Internal error", __FUNCTION__, __LINE__,
12813  "An internal error occurred while saving a config. "
12814  "It is unclear whether the entire config has been saved. "
12815  "Diagnostics: Failure to send command to manager daemon.",
12816  "/omp?cmd=get_configs", response_data);
12817  }
12818 
12819  ret = check_modify_config (credentials, &connection, params,
12820  "get_config_family", "edit_config_family",
12821  NULL, response_data);
12822 
12823  openvas_connection_close (&connection);
12824  return ret;
12825 }
12826 
12837 static char *
12838 get_config_nvt (credentials_t * credentials, params_t *params, int edit,
12839  cmd_response_data_t* response_data)
12840 {
12841  GString *xml;
12842  openvas_connection_t connection;
12843  gchar *html;
12844  const char *config_id, *name, *family, *sort_field, *sort_order, *nvt;
12845 
12846  config_id = params_value (params, "config_id");
12847  name = params_value (params, "name");
12848  family = params_value (params, "family");
12849  nvt = params_value (params, "oid");
12850 
12851  if ((config_id == NULL) || (name == NULL))
12852  {
12853  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
12854  return gsad_message (credentials,
12855  "Internal error", __FUNCTION__, __LINE__,
12856  "An internal error occurred while getting config family. "
12857  "Diagnostics: Required parameter was NULL.",
12858  "/omp?cmd=get_configs", response_data);
12859  }
12860 
12861  switch (manager_connect (credentials, &connection, &html,
12862  response_data))
12863  {
12864  case 0:
12865  break;
12866  case -1:
12867  if (html)
12868  return html;
12869  /* Fall through. */
12870  default:
12871  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12872  return gsad_message (credentials,
12873  "Internal error", __FUNCTION__, __LINE__,
12874  "An internal error occurred while getting list of configs. "
12875  "The current list of configs is not available. "
12876  "Diagnostics: Failure to connect to manager daemon.",
12877  "/omp?cmd=get_configs", response_data);
12878  }
12879 
12880  xml = g_string_new ("<get_config_nvt_response>");
12881  if (edit) g_string_append (xml, "<edit/>");
12882  /* @todo Would it be better include this in the get_nvts response? */
12883  g_string_append_printf (xml,
12884  "<config id=\"%s\">"
12885  "<name>%s</name><family>%s</family>"
12886  "</config>",
12887  config_id,
12888  name,
12889  family ? family : "");
12890 
12891  sort_field = params_value (params, "sort_field");
12892  sort_order = params_value (params, "sort_order");
12893 
12894  if (openvas_connection_sendf (&connection,
12895  "<get_nvts"
12896  " config_id=\"%s\" nvt_oid=\"%s\""
12897  " details=\"1\" preferences=\"1\""
12898  " sort_field=\"%s\" sort_order=\"%s\"/>",
12899  config_id,
12900  nvt,
12901  sort_field ? sort_field : "nvts.name",
12902  sort_order ? sort_order : "ascending")
12903  == -1)
12904  {
12905  g_string_free (xml, TRUE);
12906  openvas_connection_close (&connection);
12907  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12908  return gsad_message (credentials,
12909  "Internal error", __FUNCTION__, __LINE__,
12910  "An internal error occurred while getting list of configs. "
12911  "The current list of configs is not available. "
12912  "Diagnostics: Failure to send command to manager daemon.",
12913  "/omp?cmd=get_configs", response_data);
12914  }
12915 
12916  if (read_string_c (&connection, &xml))
12917  {
12918  g_string_free (xml, TRUE);
12919  openvas_connection_close (&connection);
12920  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12921  return gsad_message (credentials,
12922  "Internal error", __FUNCTION__, __LINE__,
12923  "An internal error occurred while getting list of configs. "
12924  "The current list of configs is not available. "
12925  "Diagnostics: Failure to receive response from manager daemon.",
12926  "/omp?cmd=get_configs", response_data);
12927  }
12928 
12929  g_string_append (xml, "</get_config_nvt_response>");
12930 
12931  if (openvas_connection_sendf (&connection,
12932  "<get_notes"
12933  " nvt_oid=\"%s\""
12934  " sort_field=\"notes.text\"/>",
12935  nvt)
12936  == -1)
12937  {
12938  g_string_free (xml, TRUE);
12939  openvas_connection_close (&connection);
12940  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12941  return gsad_message (credentials,
12942  "Internal error", __FUNCTION__, __LINE__,
12943  "An internal error occurred while getting list of notes. "
12944  "The current list of notes is not available. "
12945  "Diagnostics: Failure to send command to manager daemon.",
12946  "/omp?cmd=get_configs", response_data);
12947  }
12948 
12949  if (read_string_c (&connection, &xml))
12950  {
12951  g_string_free (xml, TRUE);
12952  openvas_connection_close (&connection);
12953  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12954  return gsad_message (credentials,
12955  "Internal error", __FUNCTION__, __LINE__,
12956  "An internal error occurred while getting list of notes. "
12957  "The current list of notes is not available. "
12958  "Diagnostics: Failure to receive response from manager daemon.",
12959  "/omp?cmd=get_configs", response_data);
12960  }
12961 
12962  if (openvas_connection_sendf (&connection,
12963  "<get_overrides"
12964  " nvt_oid=\"%s\""
12965  " sort_field=\"overrides.text\"/>",
12966  nvt)
12967  == -1)
12968  {
12969  g_string_free (xml, TRUE);
12970  openvas_connection_close (&connection);
12971  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12972  return gsad_message (credentials,
12973  "Internal error", __FUNCTION__, __LINE__,
12974  "An internal error occurred while getting list of overrides. "
12975  "The current list of overrides is not available. "
12976  "Diagnostics: Failure to send command to manager daemon.",
12977  "/omp?cmd=get_configs", response_data);
12978  }
12979 
12980  if (read_string_c (&connection, &xml))
12981  {
12982  g_string_free (xml, TRUE);
12983  openvas_connection_close (&connection);
12984  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
12985  return gsad_message (credentials,
12986  "Internal error", __FUNCTION__, __LINE__,
12987  "An internal error occurred while getting list of overrides. "
12988  "The current list of overrides is not available. "
12989  "Diagnostics: Failure to receive response from manager daemon.",
12990  "/omp?cmd=get_configs", response_data);
12991  }
12992 
12993  openvas_connection_close (&connection);
12994  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
12995  response_data);
12996 }
12997 
13007 char *
13009  cmd_response_data_t* response_data)
13010 {
13011  return get_config_nvt (credentials, params, 0, response_data);
13012 }
13013 
13023 char *
13025  cmd_response_data_t* response_data)
13026 {
13027  return get_config_nvt (credentials, params, 1, response_data);
13028 }
13029 
13039 char *
13041  cmd_response_data_t* response_data)
13042 {
13043  params_t *preferences;
13044  const char *config_id;
13045  int success;
13046  char *modify_config_ret;
13047 
13048  modify_config_ret = NULL;
13049  config_id = params_value (params, "config_id");
13050 
13051  preferences = params_values (params, "preference:");
13052  if (preferences)
13053  {
13054  openvas_connection_t connection;
13055  gchar *html;
13056  param_t *preference;
13057  gchar *preference_name;
13058  params_iterator_t iter;
13059 
13060  /* Save preferences. */
13061 
13062  switch (manager_connect (credentials, &connection, &html,
13063  response_data))
13064  {
13065  case 0:
13066  break;
13067  case -1:
13068  if (html)
13069  return html;
13070  /* Fall through. */
13071  default:
13072  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13073  return gsad_message (credentials,
13074  "Internal error", __FUNCTION__, __LINE__,
13075  "An internal error occurred while getting list of configs. "
13076  "The current list of configs is not available. "
13077  "Diagnostics: Failure to connect to manager daemon.",
13078  "/omp?cmd=get_configs", response_data);
13079  }
13080 
13081  params_iterator_init (&iter, preferences);
13082  while (params_iterator_next (&iter, &preference_name, &preference))
13083  {
13084  int type_start, type_end, count, ret, is_timeout = 0;
13085  gchar *value;
13086 
13087  g_free (modify_config_ret);
13088  modify_config_ret = NULL;
13089 
13090  /* Passwords and files have checkboxes to control whether they
13091  * must be reset. This works around the need for the Manager to
13092  * send the actual password or show the actual file. */
13093 
13094  /* LDAPsearch[entry]:Timeout value */
13095  count = sscanf (preference_name,
13096  "%*[^[][%n%*[^]]%n]:",
13097  &type_start,
13098  &type_end);
13099  if (count == 0 && type_start > 0 && type_end > 0)
13100  {
13101  if (strncmp (preference_name + type_start,
13102  "password",
13103  type_end - type_start)
13104  == 0)
13105  {
13106  int found = 0;
13107  params_t *passwords;
13108 
13109  passwords = params_values (params, "password:");
13110  if (passwords)
13111  {
13112  param_t *password;
13113  gchar *password_name;
13114  params_iterator_t password_params;
13115 
13116  params_iterator_init (&password_params, passwords);
13117  while (params_iterator_next (&password_params,
13118  &password_name,
13119  &password))
13120  if (strcmp (password_name, preference_name) == 0)
13121  {
13122  found = 1;
13123  break;
13124  }
13125  }
13126  if (found == 0)
13127  /* Skip modifying the password preference. */
13128  continue;
13129  }
13130  else if (strncmp (preference_name + type_start,
13131  "file",
13132  type_end - type_start)
13133  == 0)
13134  {
13135  int found = 0;
13136  params_t *files;
13137 
13138  files = params_values (params, "file:");
13139  if (files)
13140  {
13141  param_t *file;
13142  gchar *file_name;
13143  params_iterator_t file_params;
13144 
13145  params_iterator_init (&file_params, files);
13146  while (params_iterator_next (&file_params,
13147  &file_name,
13148  &file))
13149  if (strcmp (file_name, preference_name) == 0)
13150  {
13151  found = 1;
13152  break;
13153  }
13154  }
13155  if (found == 0)
13156  /* Skip modifying the file preference. */
13157  continue;
13158  }
13159  else if (strncmp (preference_name + type_start,
13160  "scanner",
13161  type_end - type_start)
13162  == 0)
13163  {
13164  /* Presume it's the timeout. */
13165  is_timeout = 1;
13166  }
13167  }
13168 
13169  value = preference->value_size
13170  ? g_base64_encode ((guchar *) preference->value,
13171  preference->value_size)
13172  : g_strdup ("");
13173 
13174  if (is_timeout)
13175  {
13176  const char *timeout;
13177  gchar *preference_name_escaped;
13178 
13179  timeout = params_value (params, "timeout");
13180 
13181  if (timeout == NULL)
13182  {
13183  g_free (value);
13184  openvas_connection_close (&connection);
13185  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
13186  return gsad_message (credentials,
13187  "Internal error", __FUNCTION__, __LINE__,
13188  "An internal error occurred while saving a config. "
13189  "It is unclear whether the entire config has been saved. "
13190  "Diagnostics: Required parameter was NULL.",
13191  "/omp?cmd=get_configs", response_data);
13192  }
13193 
13194  preference_name_escaped = g_markup_escape_text (preference_name,
13195  -1);
13196 
13197  if (strcmp (timeout, "0") == 0)
13198  /* Leave out the value to clear the preference. */
13199  ret = openvas_connection_sendf (&connection,
13200  "<modify_config"
13201  " config_id=\"%s\">"
13202  "<preference>"
13203  "<name>%s</name>"
13204  "</preference>"
13205  "</modify_config>",
13206  config_id,
13207  preference_name_escaped);
13208  else
13209  ret = openvas_connection_sendf (&connection,
13210  "<modify_config"
13211  " config_id=\"%s\">"
13212  "<preference>"
13213  "<name>%s</name>"
13214  "<value>%s</value>"
13215  "</preference>"
13216  "</modify_config>",
13217  config_id,
13218  preference_name_escaped,
13219  value);
13220 
13221  g_free (preference_name_escaped);
13222  }
13223  else
13224  {
13225  gchar *preference_name_escaped;
13226  preference_name_escaped = g_markup_escape_text (preference_name,
13227  -1);
13228  ret = openvas_connection_sendf (&connection,
13229  "<modify_config"
13230  " config_id=\"%s\">"
13231  "<preference>"
13232  "<nvt oid=\"%s\"/>"
13233  "<name>%s</name>"
13234  "<value>%s</value>"
13235  "</preference>"
13236  "</modify_config>",
13237  config_id,
13238  params_value (params, "oid"),
13239  preference_name_escaped,
13240  value);
13241  g_free (preference_name_escaped);
13242  }
13243 
13244  if (ret == -1)
13245  {
13246  g_free (value);
13247  openvas_connection_close (&connection);
13248  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13249  return gsad_message (credentials,
13250  "Internal error", __FUNCTION__, __LINE__,
13251  "An internal error occurred while saving a config. "
13252  "It is unclear whether the entire config has been saved. "
13253  "Diagnostics: Failure to send command to manager daemon.",
13254  "/omp?cmd=get_configs", response_data);
13255  }
13256  g_free (value);
13257 
13258  modify_config_ret = check_modify_config (credentials, &connection,
13259  params,
13260  "get_config_nvt",
13261  "edit_config_nvt",
13262  &success, response_data);
13263  if (success == 0)
13264  {
13265  openvas_connection_close (&connection);
13266  return modify_config_ret;
13267  }
13268  }
13269 
13270  openvas_connection_close (&connection);
13271  }
13272 
13273  return modify_config_ret;
13274 }
13275 
13285 char *
13286 delete_config_omp (credentials_t * credentials, params_t *params,
13287  cmd_response_data_t* response_data)
13288 {
13289  return delete_resource ("config", credentials, params, 0, "get_configs",
13290  response_data);
13291 }
13292 
13305 char *
13306 export_config_omp (credentials_t * credentials, params_t *params,
13307  enum content_type * content_type, char **content_disposition,
13308  gsize *content_length, cmd_response_data_t* response_data)
13309 {
13310  return export_resource ("config", credentials, params, content_type,
13311  content_disposition, content_length, response_data);
13312 }
13313 
13327 char *
13329  enum content_type * content_type, char **content_disposition,
13330  gsize *content_length, cmd_response_data_t* response_data)
13331 {
13332  return export_many ("config", credentials, params, content_type,
13333  content_disposition, content_length, response_data);
13334 }
13335 
13348 char *
13349 export_note_omp (credentials_t * credentials, params_t *params,
13350  enum content_type * content_type, char **content_disposition,
13351  gsize *content_length, cmd_response_data_t* response_data)
13352 {
13353  return export_resource ("note", credentials, params, content_type,
13354  content_disposition, content_length, response_data);
13355 }
13356 
13370 char *
13371 export_notes_omp (credentials_t * credentials, params_t *params,
13372  enum content_type * content_type, char **content_disposition,
13373  gsize *content_length, cmd_response_data_t* response_data)
13374 {
13375  return export_many ("note", credentials, params, content_type,
13376  content_disposition, content_length, response_data);
13377 }
13378 
13391 char *
13393  enum content_type * content_type,
13394  char **content_disposition, gsize *content_length,
13395  cmd_response_data_t* response_data)
13396 {
13397  return export_resource ("override", credentials, params, content_type,
13398  content_disposition, content_length, response_data);
13399 }
13400 
13414 char *
13416  enum content_type * content_type,
13417  char **content_disposition, gsize *content_length,
13418  cmd_response_data_t* response_data)
13419 {
13420  return export_many ("override", credentials, params, content_type,
13421  content_disposition, content_length, response_data);
13422 }
13423 
13437 char *
13439  enum content_type * content_type,
13440  char **content_disposition, gsize *content_length,
13441  cmd_response_data_t* response_data)
13442 {
13443  return export_resource ("port_list", credentials, params, content_type,
13444  content_disposition, content_length,
13445  response_data);
13446 }
13447 
13461 char *
13463  enum content_type * content_type,
13464  char **content_disposition, gsize *content_length,
13465  cmd_response_data_t* response_data)
13466 {
13467  return export_many ("port_list", credentials, params, content_type,
13468  content_disposition, content_length, response_data);
13469 }
13470 
13483 char *
13485  enum content_type * content_type,
13486  char **content_disposition, gsize *content_length,
13487  cmd_response_data_t* response_data)
13488 {
13489  GString *xml;
13490  entity_t entity, preference_entity, value_entity;
13491  openvas_connection_t connection;
13492  gchar *html;
13493  const char *config_id, *oid, *preference_name;
13494 
13495  *content_length = 0;
13496 
13497  switch (manager_connect (credentials, &connection, &html,
13498  response_data))
13499  {
13500  case 0:
13501  break;
13502  case -1:
13503  if (html)
13504  return html;
13505  /* Fall through. */
13506  default:
13507  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13508  return gsad_message (credentials,
13509  "Internal error", __FUNCTION__, __LINE__,
13510  "An internal error occurred while getting a preference file. "
13511  "The file could not be delivered. "
13512  "Diagnostics: Failure to connect to manager daemon.",
13513  "/omp?cmd=get_tasks", response_data);
13514  }
13515 
13516  config_id = params_value (params, "config_id");
13517  oid = params_value (params, "oid");
13518  preference_name = params_value (params, "preference_name");
13519 
13520  xml = g_string_new ("<get_preferences_response>");
13521 
13522  if (config_id == NULL || oid == NULL || preference_name == NULL)
13523  g_string_append (xml, GSAD_MESSAGE_INVALID_PARAM ("Export Preference File"));
13524  else
13525  {
13526  if (openvas_connection_sendf (&connection,
13527  "<get_preferences"
13528  " config_id=\"%s\""
13529  " nvt_oid=\"%s\""
13530  " preference=\"%s\"/>",
13531  config_id,
13532  oid,
13533  preference_name)
13534  == -1)
13535  {
13536  g_string_free (xml, TRUE);
13537  openvas_connection_close (&connection);
13538  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13539  return gsad_message (credentials,
13540  "Internal error", __FUNCTION__, __LINE__,
13541  "An internal error occurred while getting a preference file. "
13542  "The file could not be delivered. "
13543  "Diagnostics: Failure to send command to manager daemon.",
13544  "/omp?cmd=get_tasks", response_data);
13545  }
13546 
13547  entity = NULL;
13548  if (read_entity_c (&connection, &entity))
13549  {
13550  g_string_free (xml, TRUE);
13551  openvas_connection_close (&connection);
13552  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13553  return gsad_message (credentials,
13554  "Internal error", __FUNCTION__, __LINE__,
13555  "An internal error occurred while getting a preference file. "
13556  "The file could not be delivered. "
13557  "Diagnostics: Failure to receive response from manager daemon.",
13558  "/omp?cmd=get_tasks", response_data);
13559  }
13560 
13561  preference_entity = entity_child (entity, "preference");
13562  if (preference_entity != NULL
13563  && (value_entity = entity_child (preference_entity, "value")))
13564  {
13565  char *content = strdup (entity_text (value_entity));
13566  *content_type = GSAD_CONTENT_TYPE_OCTET_STREAM;
13567  *content_disposition = g_strdup_printf ("attachment; filename=\"pref_file.bin\"");
13568  *content_length = strlen (content);
13569  free_entity (entity);
13570  g_string_free (xml, TRUE);
13571  openvas_connection_close (&connection);
13572  return content;
13573  }
13574  else
13575  {
13576  free_entity (entity);
13577  g_string_free (xml, TRUE);
13578  openvas_connection_close (&connection);
13579  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13580  return gsad_message (credentials,
13581  "Internal error", __FUNCTION__, __LINE__,
13582  "An internal error occurred while getting a preference file. "
13583  "The file could not be delivered. "
13584  "Diagnostics: Failure to receive file from manager daemon.",
13585  "/omp?cmd=get_tasks", response_data);
13586  }
13587  }
13588 
13589  g_string_append (xml, "</get_preferences_response>");
13590  openvas_connection_close (&connection);
13591  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
13592  response_data);
13593 }
13594 
13608 char *
13610  enum content_type * content_type,
13611  char **content_disposition, gsize *content_length,
13612  cmd_response_data_t* response_data)
13613 {
13614  return export_resource ("report_format", credentials, params, content_type,
13615  content_disposition, content_length, response_data);
13616 }
13617 
13631 char *
13633  enum content_type * content_type,
13634  char **content_disposition, gsize *content_length,
13635  cmd_response_data_t* response_data)
13636 {
13637  return export_many ("report_format", credentials, params, content_type,
13638  content_disposition, content_length, response_data);
13639 }
13640 
13650 char *
13651 delete_report_omp (credentials_t * credentials, params_t *params,
13652  cmd_response_data_t* response_data)
13653 {
13654  return delete_resource ("report", credentials, params, 0, NULL,
13655  response_data);
13656 }
13657 
13673 char *
13674 get_report (credentials_t * credentials, params_t *params, const char *commands,
13675  gsize *report_len, gchar **content_type, char **content_disposition,
13676  const char *extra_xml, int *error,
13677  cmd_response_data_t* response_data)
13678 {
13679  GString *xml, *commands_xml;
13680  entity_t entity;
13681  entity_t report_entity;
13682  openvas_connection_t connection;
13683  gchar *html;
13684  unsigned int first, max;
13685  GString *levels, *delta_states;
13686  const char *alert_id, *search_phrase, *min_qod, *type, *zone;
13687  const char *autofp, *autofp_value, *notes, *overrides, *result_hosts_only;
13688  const char *apply_overrides;
13689  const char *report_id, *sort_field, *sort_order, *result_id, *delta_report_id;
13690  const char *format_id, *first_result, *max_results, *host, *pos;
13691  const char *given_filt_id, *filt_id, *filter, *apply_filter, *report_section;
13692  const char *build_filter, *filter_extra;
13693  const char *host_search_phrase, *host_levels;
13694  const char *host_first_result, *host_max_results;
13695  int ret;
13696  int ignore_filter, ignore_pagination;
13697  gchar *built_filter;
13698  gchar *fname_format, *esc_response;
13699 
13700  build_filter = params_value (params, "build_filter");
13701 
13702  if (params_given (params, "apply_filter")
13703  && params_valid (params, "apply_filter"))
13704  apply_filter = params_value (params, "apply_filter");
13705  else
13706  apply_filter = "no_pagination";
13707 
13708  if (params_given (params, "report_section")
13709  && params_valid (params, "report_section"))
13710  report_section = params_value (params, "report_section");
13711  else
13712  report_section = "";
13713 
13714  ignore_filter = (strcmp (apply_filter, "full")
13715  && strcmp (apply_filter, "no_pagination")
13716  && strcmp (report_section, "")
13717  && strcmp (report_section, "results")
13718  && strcmp (report_section, "summary"));
13719 
13720  if (params_given (params, "ignore_pagination"))
13721  {
13722  const char *ignore_pagination_str = params_value (params,
13723  "ignore_pagination");
13724  ignore_pagination = (ignore_pagination_str
13725  && strcmp (ignore_pagination_str, "")
13726  && strcmp (ignore_pagination_str, "0"));
13727  }
13728  else
13729  {
13730  ignore_pagination = (strcmp (apply_filter, "full")
13731  && strcmp (report_section, "")
13732  && strcmp (report_section, "results")
13733  && strcmp (report_section, "summary"));
13734  }
13735 
13736  alert_id = params_value (params, "alert_id");
13737  if (alert_id == NULL)
13738  params_given (params, "alert_id") || (alert_id = "0");
13739 
13740  search_phrase = params_value (params, "search_phrase");
13741  if (search_phrase == NULL)
13742  params_given (params, "search_phrase") || (search_phrase = "");
13743 
13744  zone = params_value (params, "timezone");
13745  if (zone == NULL)
13746  params_given (params, "zone") || (zone = "");
13747 
13748  min_qod = params_value (params, "min_qod");
13749 
13750  type = params_value (params, "type");
13751  host = params_value (params, "host");
13752  pos = params_value (params, "pos");
13753 
13754  autofp = params_value (params, "autofp");
13755  if (autofp == NULL)
13756  params_given (params, "autofp") || (autofp = "0");
13757 
13758  autofp_value = params_value (params, "autofp_value");
13759  if (autofp_value == NULL)
13760  params_given (params, "autofp_value") || (autofp_value = "1");
13761 
13762  notes = params_value (params, "notes");
13763  if (notes == NULL)
13764  {
13765  if (params_given (params, "max_results"))
13766  /* Use the max_results param to determine if the request is from
13767  * the Result Filtering form, because the notes param is only sent
13768  * when the checkbox is ticked. */
13769  notes = "0";
13770  else
13771  params_given (params, "notes") || (notes = "1");
13772  }
13773 
13774  overrides = params_value (params, "overrides");
13775  if (overrides == NULL)
13776  {
13777  if (params_given (params, "max_results"))
13778  /* Use the max_results param to check for filtering form as above */
13779  overrides = "0";
13780  else
13781  params_given (params, "overrides") || (overrides = "1");
13782  }
13783 
13784  apply_overrides = params_value (params, "apply_overrides");
13785  if (apply_overrides == NULL)
13786  {
13787  if (params_given (params, "max_results"))
13788  /* Use the max_results param to check for filtering form as above */
13789  apply_overrides = "0";
13790  else
13791  params_given (params, "apply_overrides") || (apply_overrides = "1");
13792  }
13793 
13794  result_hosts_only = params_value (params, "result_hosts_only");
13795  if (result_hosts_only == NULL)
13796  {
13797  if (params_given (params, "max_results"))
13798  /* Use the max_results params to determine if the request is from
13799  * the Result Filtering form, because the result_hosts_only param is
13800  * only sent when the checkbox is ticked. */
13801  result_hosts_only = "0";
13802  else
13803  params_given (params, "result_hosts_only") || (result_hosts_only = "1");
13804  }
13805 
13806  if (content_type) *content_type = NULL;
13807  if (report_len) *report_len = 0;
13808 
13809  if (autofp == NULL || strlen (autofp) == 0) autofp = "0";
13810 
13811  if (autofp_value == NULL || strlen (autofp_value) == 0) autofp_value = "1";
13812 
13813  if (strcmp (autofp, "2") == 0)
13814  autofp_value = "2";
13815 
13816  if (notes == NULL || strlen (notes) == 0) notes = "1";
13817 
13818  if (overrides == NULL || strlen (overrides) == 0) overrides = "1";
13819 
13820  if (result_hosts_only == NULL || strlen (result_hosts_only) == 0)
13821  result_hosts_only = "1";
13822 
13823  switch (manager_connect (credentials, &connection, &html,
13824  response_data))
13825  {
13826  case 0:
13827  break;
13828  case -1:
13829  if (html)
13830  {
13831  if (error) *error = 1;
13832  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13833  return html;
13834  }
13835  /* Fall through. */
13836  default:
13837  {
13838  if (error) *error = 1;
13839  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13840  return gsad_message (credentials,
13841  "Internal error", __FUNCTION__, __LINE__,
13842  "An internal error occurred while getting a report. "
13843  "The report could not be delivered. "
13844  "Diagnostics: Failure to connect to manager daemon.",
13845  "/omp?cmd=get_tasks", response_data);
13846  }
13847  }
13848 
13849  /* Run any extra commands. */
13850 
13851  commands_xml = g_string_new ("");
13852  if (commands)
13853  {
13854  if (openvas_connection_sendf (&connection, "%s", commands)
13855  == -1)
13856  {
13857  g_string_free (commands_xml, TRUE);
13858  openvas_connection_close (&connection);
13859  if (error) *error = 1;
13860  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13861  return gsad_message (credentials,
13862  "Internal error", __FUNCTION__, __LINE__,
13863  "An internal error occurred while getting a report. "
13864  "The report could not be delivered. "
13865  "Diagnostics: Failure to send extra commands to manager daemon.",
13866  "/omp?cmd=get_tasks", response_data);
13867  }
13868 
13869  if (read_string_c (&connection, &commands_xml))
13870  {
13871  g_string_free (commands_xml, TRUE);
13872  openvas_connection_close (&connection);
13873  if (error) *error = 1;
13874  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13875  return gsad_message (credentials,
13876  "Internal error", __FUNCTION__, __LINE__,
13877  "An internal error occurred while getting a report. "
13878  "The report could not be delivered. "
13879  "Diagnostics: Failure to receive response from manager daemon.",
13880  "/omp?cmd=get_tasks", response_data);
13881  }
13882  }
13883 
13884  /* Get the report. */
13885 
13886  if (params_value (params, "delta_states"))
13887  delta_states = g_string_new (params_value (params, "delta_states"));
13888  else
13889  {
13890  delta_states = g_string_new ("");
13891  if (params_value (params, "delta_state_changed")
13892  && atoi (params_value (params, "delta_state_changed")))
13893  g_string_append (delta_states, "c");
13894  if (params_value (params, "delta_state_gone")
13895  && atoi (params_value (params, "delta_state_gone")))
13896  g_string_append (delta_states, "g");
13897  if (params_value (params, "delta_state_new")
13898  && atoi (params_value (params, "delta_state_new")))
13899  g_string_append (delta_states, "n");
13900  if (params_value (params, "delta_state_same")
13901  && atoi (params_value (params, "delta_state_same")))
13902  g_string_append (delta_states, "s");
13903  }
13904 
13905  if (strlen (delta_states->str) == 0) g_string_append (delta_states, "gn");
13906 
13907  if (params_value (params, "levels"))
13908  levels = g_string_new (params_value (params, "levels"));
13909  else
13910  {
13911  levels = g_string_new ("");
13912  if (params_value (params, "level_high")
13913  && atoi (params_value (params, "level_high")))
13914  g_string_append (levels, "h");
13915  if (params_value (params, "level_medium")
13916  && atoi (params_value (params, "level_medium")))
13917  g_string_append (levels, "m");
13918  if (params_value (params, "level_low")
13919  && atoi (params_value (params, "level_low")))
13920  g_string_append (levels, "l");
13921  if (params_value (params, "level_log")
13922  && atoi (params_value (params, "level_log")))
13923  g_string_append (levels, "g");
13924  if (params_value (params, "level_false_positive")
13925  && atoi (params_value (params, "level_false_positive")))
13926  g_string_append (levels, "f");
13927  }
13928 
13929  if (type && (strcmp (type, "assets") == 0))
13930  {
13931  if (strlen (levels->str) == 0)
13932  g_string_append (levels, "");
13933  }
13934  else if (strlen (levels->str) == 0)
13935  g_string_append (levels, "hml");
13936 
13937  sort_field = params_value (params, "sort_field");
13938  sort_order = params_value (params, "sort_order");
13939  report_id = params_value (params, "report_id");
13940 
13941  if (report_id == NULL
13942  && (type == NULL
13943  || (strcmp (type, "prognostic")
13944  && strcmp (type, "assets"))))
13945  return get_reports (credentials, params, extra_xml, response_data);
13946 
13947  if (strcmp (alert_id, "0"))
13948  {
13949  const char *status, *esc_filter;
13950 
13951  esc_filter = params_value (params, "esc_filter");
13952  if (esc_filter == NULL)
13953  params_given (params, "esc_filter")
13954  || (esc_filter = "first=1 rows=-1"
13955  " result_hosts_only=0"
13956  " apply_overrides=1"
13957  " notes=1 overrides=1"
13958  " sort-reverse=severity");
13959 
13960  if (ignore_filter)
13961  ret = openvas_connection_sendf_xml (&connection,
13962  "<get_reports"
13963  " report_id=\"%s\""
13964  " filter=\"first=1 rows=-1"
13965  " result_hosts_only=0"
13966  " apply_overrides=1"
13967  " notes=1 overrides=1"
13968  " sort-reverse=severity\""
13969  " alert_id=\"%s\"/>",
13970  report_id,
13971  alert_id);
13972  else
13973  ret = openvas_connection_sendf_xml (&connection,
13974  "<get_reports"
13975  " report_id=\"%s\""
13976  " ignore_pagination=\"%d\""
13977  " filter=\"%s\""
13978  " alert_id=\"%s\"/>",
13979  report_id,
13980  ignore_pagination,
13981  esc_filter ? esc_filter : "",
13982  alert_id);
13983  if (ret == -1)
13984  {
13985  openvas_connection_close (&connection);
13986  g_string_free (commands_xml, TRUE);
13987  g_string_free (delta_states, TRUE);
13988  g_string_free (levels, TRUE);
13989  if (error) *error = 1;
13990  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
13991  return gsad_message (credentials,
13992  "Internal error", __FUNCTION__, __LINE__,
13993  "An internal error occurred while getting a report. "
13994  "The report could not be delivered. "
13995  "Diagnostics: Failure to send command to manager daemon.",
13996  "/omp?cmd=get_tasks", response_data);
13997  }
13998 
13999  if (read_entity_and_text_c (&connection, &entity, &esc_response))
14000  {
14001  openvas_connection_close (&connection);
14002  g_string_free (commands_xml, TRUE);
14003  g_string_free (delta_states, TRUE);
14004  g_string_free (levels, TRUE);
14005  if (error) *error = 1;
14006  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14007  return gsad_message (credentials,
14008  "Internal error", __FUNCTION__, __LINE__,
14009  "An internal error occurred while getting a report. "
14010  "The report could not be delivered. "
14011  "Diagnostics: Failure to receive response from manager daemon.",
14012  "/omp?cmd=get_tasks", response_data);
14013  }
14014  status = entity_attribute (entity, "status");
14015  if ((status == NULL)
14016  || (strlen (status) == 0))
14017  {
14018  free_entity (entity);
14019  openvas_connection_close (&connection);
14020  g_string_free (commands_xml, TRUE);
14021  g_string_free (delta_states, TRUE);
14022  if (error) *error = 1;
14023  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14024  return gsad_message (credentials,
14025  "Internal error", __FUNCTION__, __LINE__,
14026  "An internal error occurred while getting a report. "
14027  "The report could not be delivered. "
14028  "Diagnostics: Failure to parse response from manager daemon.",
14029  "/omp?cmd=get_tasks", response_data);
14030  }
14031  free_entity (entity);
14032  }
14033  else
14034  esc_response = NULL;
14035 
14036  result_id = params_value (params, "result_id");
14037  delta_report_id = params_value (params, "delta_report_id");
14038  format_id = params_value (params, "report_format_id");
14039 
14040  first_result = params_value (params, "first_result");
14041  if (first_result == NULL
14042  || sscanf (first_result, "%u", &first) != 1)
14043  first_result = "1";
14044 
14045  max_results = params_value (params, "max_results");
14046  if (max_results == NULL
14047  || sscanf (max_results, "%u", &max) != 1)
14048  max_results = G_STRINGIFY (RESULTS_PER_PAGE);
14049 
14050  if (openvas_connection_sendf (&connection,
14051  "<get_reports"
14052  "%s%s"
14053  " details=\"%i\""
14054  "%s%s%s",
14055  (type && (strcmp (type, "prognostic") == 0))
14056  ? " type=\"prognostic\""
14057  : "",
14058  (type && (strcmp (type, "assets") == 0))
14059  ? " type=\"assets\""
14060  : "",
14061  (type
14062  && (strcmp (type, "assets") == 0)
14063  && host)
14064  || delta_report_id
14065  || strcmp (report_section, "summary"),
14066  host ? " host=\"" : "",
14067  host ? host : "",
14068  host ? "\"" : "")
14069  == -1)
14070  {
14071  openvas_connection_close (&connection);
14072  g_string_free (delta_states, TRUE);
14073  g_string_free (commands_xml, TRUE);
14074  g_string_free (levels, TRUE);
14075  if (error) *error = 1;
14076  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14077  return gsad_message (credentials,
14078  "Internal error", __FUNCTION__, __LINE__,
14079  "An internal error occurred while getting a report. "
14080  "The report could not be delivered. "
14081  "Diagnostics: Failure to send command to manager daemon.",
14082  "/omp?cmd=get_tasks", response_data);
14083  }
14084 
14085  given_filt_id = params_value (params, "filt_id");
14086  filter = params_value (params, "filter");
14087  filter_extra = params_value (params, "filter_extra");
14088 
14089  if (params_given (params, "filt_id"))
14090  {
14091  g_tree_replace (credentials->last_filt_ids, g_strdup ("report_result"),
14092  g_strdup (given_filt_id));
14093  filt_id = given_filt_id;
14094  }
14095  else if (filter == NULL || strcmp (filter, "") == 0)
14096  filt_id = g_tree_lookup (credentials->last_filt_ids, "report_result");
14097  else
14098  filt_id = NULL;
14099 
14100  if (filter == NULL)
14101  filter = "";
14102 
14103  if ((build_filter && (strcmp (build_filter, "1") == 0))
14104  || ((filter == NULL || strcmp (filter, "") == 0)
14105  && (filter_extra == NULL || strcmp (filter_extra, "") == 0)))
14106  {
14107  GString *filter_buffer;
14108  filter_buffer = g_string_new ("");
14109 
14110  g_string_append_printf (filter_buffer,
14111  "autofp=%s"
14112  " apply_overrides=%i"
14113  " notes=%i"
14114  " overrides=%i"
14115  " result_hosts_only=%i"
14116  " first=%s"
14117  " rows=%s"
14118  " sort%s=%s"
14119  " levels=%s",
14120  strcmp (autofp, "0") ? autofp_value : "0",
14121  apply_overrides
14122  ? (strcmp (apply_overrides, "0") ? 1 : 0)
14123  : 1,
14124  strcmp (notes, "0") ? 1 : 0,
14125  strcmp (overrides, "0") ? 1 : 0,
14126  strcmp (result_hosts_only, "0") ? 1 : 0,
14127  first_result,
14128  max_results,
14129  sort_order
14130  ? strcmp (sort_order, "ascending")
14131  ? "-reverse"
14132  : ""
14133  : ((sort_field == NULL
14134  || strcmp (sort_field, "type") == 0
14135  || strcmp (sort_field, "severity") == 0)
14136  ? "-reverse"
14137  : ""),
14138  sort_field ? sort_field : "severity",
14139  levels->str);
14140 
14141  if (search_phrase && strcmp (search_phrase, ""))
14142  {
14143  gchar *search_phrase_escaped;
14144  search_phrase_escaped = g_markup_escape_text (search_phrase, -1);
14145  g_string_append_printf (filter_buffer,
14146  " \"%s\"",
14147  search_phrase_escaped);
14148  g_free (search_phrase_escaped);
14149  }
14150 
14151  if (delta_states->str && strcmp (delta_states->str, "")
14152  && delta_report_id && strcmp (delta_report_id, ""))
14153  g_string_append_printf (filter_buffer,
14154  " delta_states=%s",
14155  delta_states->str);
14156 
14157  if (min_qod && strcmp (min_qod, ""))
14158  g_string_append_printf (filter_buffer,
14159  " min_qod=%s",
14160  min_qod);
14161 
14162  if (zone && strcmp (zone, ""))
14163  g_string_append_printf (filter_buffer,
14164  " timezone=%s",
14165  zone);
14166 
14167  if (filter && strcmp (filter, ""))
14168  g_string_append_printf (filter_buffer,
14169  " %s",
14170  filter);
14171 
14172  built_filter = g_string_free (filter_buffer, FALSE);
14173  }
14174  else if (filter || filter_extra)
14175  built_filter = g_strdup_printf ("%s%s%s",
14176  filter ? filter : "",
14177  filter && filter_extra ? " " : "",
14178  filter_extra ? filter_extra : "");
14179  else
14180  built_filter = NULL;
14181 
14182  if (type && (strcmp (type, "prognostic") == 0))
14183  {
14184  host_search_phrase = params_value (params, "host_search_phrase");
14185  if (host_search_phrase == NULL)
14186  params_given (params, "host_search_phrase")
14187  || (host_search_phrase = "");
14188 
14189  host_levels = params_value (params, "host_levels");
14190  if (host_levels == NULL)
14191  params_given (params, "host_levels")
14192  || (host_levels = "");
14193 
14194  host_first_result = params_value (params, "host_first_result");
14195  if (host_first_result == NULL
14196  || sscanf (host_first_result, "%u", &first) != 1)
14197  host_first_result = "1";
14198 
14199  host_max_results = params_value (params, "host_max_results");
14200  if (host_max_results == NULL
14201  || sscanf (host_max_results, "%u", &max) != 1)
14202  host_max_results = G_STRINGIFY (RESULTS_PER_PAGE);
14203 
14204  if (host_search_phrase == NULL)
14205  {
14206  openvas_connection_close (&connection);
14207  g_string_free (delta_states, TRUE);
14208  g_string_free (commands_xml, TRUE);
14209  g_string_free (levels, TRUE);
14210  xml = g_string_new ("");
14211  g_string_append_printf (xml, GSAD_MESSAGE_INVALID,
14212  "Given host search_phrase was invalid",
14213  "Get Report");
14214  if (error) *error = 1;
14215  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
14216  return g_string_free (xml, FALSE);
14217  }
14218 
14219  if (openvas_connection_sendf_xml (&connection,
14220  " host_search_phrase=\"%s\""
14221  " host_levels=\"%s\""
14222  " host_first_result=\"%s\""
14223  " host_max_results=\"%s\"",
14224  host_search_phrase,
14225  host_levels,
14226  host_first_result,
14227  host_max_results))
14228  {
14229  openvas_connection_close (&connection);
14230  g_string_free (delta_states, TRUE);
14231  g_string_free (commands_xml, TRUE);
14232  g_string_free (levels, TRUE);
14233  if (error) *error = 1;
14234  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14235  return gsad_message (credentials,
14236  "Internal error", __FUNCTION__, __LINE__,
14237  "An internal error occurred while getting a report. "
14238  "The report could not be delivered. "
14239  "Diagnostics: Failure to send command to manager daemon.",
14240  "/omp?cmd=get_tasks", response_data);
14241  }
14242  }
14243  else
14244  {
14245  host_search_phrase = NULL;
14246  host_levels = NULL;
14247  host_first_result = NULL;
14248  host_max_results = NULL;
14249  }
14250 
14251  /* Don't apply default filter when applying result filter checkboxes/textboxes
14252  */
14253  if (sort_field == NULL && sort_order == NULL)
14254  if ((filt_id == NULL || strcmp (filt_id, "") == 0)
14255  && (filter == NULL || strcmp (filter, "") == 0))
14256  filt_id = "-2";
14257 
14258  if (ignore_filter)
14259  ret = openvas_connection_sendf_xml (&connection,
14260  " filt_id=\"0\""
14261  " filter=\"first=1 rows=-1"
14262  " result_hosts_only=0 apply_overrides=1"
14263  " notes=1 overrides=1"
14264  " sort-reverse=severity\""
14265  " report_id=\"%s\""
14266  " delta_report_id=\"%s\""
14267  " format_id=\"%s\"/>",
14268  (type && ((strcmp (type, "assets") == 0)
14269  || (strcmp (type, "prognostic")
14270  == 0)))
14271  ? ""
14272  : report_id,
14273  delta_report_id ? delta_report_id : "0",
14274  format_id
14275  ? format_id
14276  : "a994b278-1f62-11e1-96ac-406186ea4fc5");
14277  else
14278  ret = openvas_connection_sendf_xml (&connection,
14279  " ignore_pagination=\"%d\""
14280  " filt_id=\"%s\""
14281  " filter=\"%s\""
14282  " pos=\"%s\""
14283  " notes_details=\"1\""
14284  " overrides_details=\"1\""
14285  " report_id=\"%s\""
14286  " delta_report_id=\"%s\""
14287  " format_id=\"%s\"/>",
14288  ignore_pagination,
14289  filt_id ? filt_id : "0",
14290  built_filter ? built_filter : "",
14291  pos ? pos : "1",
14292  (type && ((strcmp (type, "assets") == 0)
14293  || (strcmp (type, "prognostic")
14294  == 0)))
14295  ? ""
14296  : report_id,
14297  delta_report_id ? delta_report_id : "0",
14298  format_id
14299  ? format_id
14300  : "a994b278-1f62-11e1-96ac-406186ea4fc5",
14301  first_result,
14302  max_results,
14303  sort_field ? sort_field : "severity",
14304  sort_order
14305  ? sort_order
14306  : ((sort_field == NULL
14307  || strcmp (sort_field, "type") == 0
14308  || strcmp (sort_field, "severity") == 0)
14309  ? "descending"
14310  : "ascending"),
14311  levels->str,
14312  delta_states->str,
14313  search_phrase,
14314  min_qod,
14315  zone);
14316  if (ret == -1)
14317  {
14318  openvas_connection_close (&connection);
14319  g_string_free (delta_states, TRUE);
14320  g_string_free (commands_xml, TRUE);
14321  g_string_free (levels, TRUE);
14322  if (error) *error = 1;
14323  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14324  return gsad_message (credentials,
14325  "Internal error", __FUNCTION__, __LINE__,
14326  "An internal error occurred while getting a report. "
14327  "The report could not be delivered. "
14328  "Diagnostics: Failure to send command to manager daemon.",
14329  "/omp?cmd=get_tasks", response_data);
14330  }
14331 
14332  g_string_free (delta_states, TRUE);
14333 
14334  if (format_id)
14335  {
14336  g_string_free (commands_xml, TRUE);
14337  g_string_free (levels, TRUE);
14338  if ((strcmp (format_id, "a994b278-1f62-11e1-96ac-406186ea4fc5") == 0)
14339  || strcmp (format_id, "5057e5cc-b825-11e4-9d0e-28d24461215b") == 0)
14340  {
14341  const char *extension, *requested_content_type;
14342  /* Manager sends XML report as plain XML. */
14343 
14344  if (read_entity_c (&connection, &entity))
14345  {
14346  openvas_connection_close (&connection);
14347  if (error) *error = 1;
14348  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14349  return gsad_message (credentials,
14350  "Internal error", __FUNCTION__, __LINE__,
14351  "An internal error occurred while getting a report. "
14352  "The report could not be delivered. "
14353  "Diagnostics: Failure to receive response from manager daemon.",
14354  "/omp?cmd=get_tasks", response_data);
14355  }
14356  entity_t report = entity_child (entity, "report");
14357  if (report == NULL)
14358  {
14359  free_entity (entity);
14360  openvas_connection_close (&connection);
14361  if (error) *error = 1;
14362  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14363  return gsad_message (credentials,
14364  "Internal error", __FUNCTION__, __LINE__,
14365  "An internal error occurred while getting a report. "
14366  "The report could not be delivered. "
14367  "Diagnostics: Response from manager daemon did not contain a report.",
14368  "/omp?cmd=get_tasks", response_data);
14369  }
14370  extension = entity_attribute (report, "extension");
14371  requested_content_type = entity_attribute (report, "content_type");
14372  if (extension && requested_content_type && content_type
14373  && content_disposition)
14374  {
14375  gchar *file_name;
14376  ret = setting_get_value (&connection,
14377  "e1a2ae0b-736e-4484-b029-330c9e15b900",
14378  &fname_format,
14379  response_data);
14380  if (ret)
14381  {
14382  openvas_connection_close (&connection);
14383  switch (ret)
14384  {
14385  case 1:
14386  response_data->http_status_code
14387  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14388  return gsad_message (credentials,
14389  "Internal error", __FUNCTION__, __LINE__,
14390  "An internal error occurred while getting a setting. "
14391  "The setting could not be delivered. "
14392  "Diagnostics: Failure to send command to manager daemon.",
14393  "/omp?cmd=get_tasks",
14394  response_data);
14395  case 2:
14396  response_data->http_status_code
14397  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14398  return gsad_message (credentials,
14399  "Internal error", __FUNCTION__, __LINE__,
14400  "An internal error occurred while getting a setting. "
14401  "The setting could not be delivered. "
14402  "Diagnostics: Failure to receive response from manager daemon.",
14403  "/omp?cmd=get_tasks",
14404  response_data);
14405  default:
14406  response_data->http_status_code
14407  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14408  return gsad_message (credentials,
14409  "Internal error", __FUNCTION__, __LINE__,
14410  "An internal error occurred while getting a setting. "
14411  "The setting could not be delivered. "
14412  "Diagnostics: Internal error.",
14413  "/omp?cmd=get_tasks",
14414  response_data);
14415  }
14416  }
14417 
14418  if (fname_format == NULL)
14419  {
14420  g_warning ("%s : File name format setting not found.",
14421  __FUNCTION__);
14422  fname_format = "%T-%U";
14423  }
14424 
14425  file_name = format_file_name (fname_format,
14426  credentials,
14427  "report",
14428  (type
14429  && ((strcmp (type, "assets") == 0)
14430  || (strcmp (type, "prognostic")
14431  == 0)))
14432  ? type
14433  : report_id,
14434  report);
14435  if (file_name == NULL)
14436  file_name = g_strdup_printf ("%s-%s",
14437  "report",
14438  (type
14439  && ((strcmp (type, "assets") == 0)
14440  || (strcmp (type, "prognostic")
14441  == 0)))
14442  ? type
14443  : report_id);
14444 
14445  *content_type = g_strdup (requested_content_type);
14446  *content_disposition
14447  = g_strdup_printf ("attachment; filename=\"%s.%s\"",
14448  file_name,
14449  extension);
14450 
14451  g_free (file_name);
14452  }
14453  openvas_connection_close (&connection);
14454  xml = g_string_new ("");
14455  print_entity_to_string (report, xml);
14456  free_entity (entity);
14457  if (error) *error = 1;
14458  return g_string_free (xml, FALSE);
14459  }
14460  else
14461  {
14462  /* "nbe", "pdf", "dvi", "html", "html-pdf"... */
14463 
14464  if (report_len == NULL)
14465  {
14466  if (error) *error = 1;
14467  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14468  return gsad_message (credentials,
14469  "Internal error", __FUNCTION__, __LINE__,
14470  "An internal error occurred while getting a report. "
14471  "The report could not be delivered. "
14472  "Diagnostics: Parameter error.",
14473  "/omp?cmd=get_tasks", response_data);
14474  }
14475 
14476  entity = NULL;
14477  if (read_entity_c (&connection, &entity))
14478  {
14479  openvas_connection_close (&connection);
14480  if (error) *error = 1;
14481  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14482  return gsad_message (credentials,
14483  "Internal error", __FUNCTION__, __LINE__,
14484  "An internal error occurred while getting a report. "
14485  "The report could not be delivered. "
14486  "Diagnostics: Failure to receive response from manager daemon.",
14487  "/omp?cmd=get_tasks", response_data);
14488  }
14489 
14490  report_entity = entity_child (entity, "report");
14491  if (report_entity != NULL)
14492  {
14493  const char *extension, *requested_content_type;
14494  char *report_encoded;
14495  gchar *report_decoded;
14496  extension = entity_attribute (report_entity, "extension");
14497  requested_content_type = entity_attribute (report_entity,
14498  "content_type");
14499  report_encoded = entity_text (report_entity);
14500  report_decoded =
14501  (gchar *) g_base64_decode (report_encoded, report_len);
14502  /* g_base64_decode can return NULL (Glib 2.12.4-2), at least
14503  * when *report_len is zero. */
14504  if (report_decoded == NULL)
14505  {
14506  report_decoded = g_strdup ("");
14507  *report_len = 0;
14508  }
14509  if (extension && requested_content_type && content_type
14510  && content_disposition)
14511  {
14512  gchar *file_name;
14513  const char *id;
14514  if (report_id)
14515  id = report_id;
14516  else if (type && (strcmp (type, "prognostic") == 0))
14517  id = "prognostic";
14518  else
14519  id = "ERROR";
14520 
14521  ret = setting_get_value
14522  (&connection,
14523  "e1a2ae0b-736e-4484-b029-330c9e15b900",
14524  &fname_format,
14525  response_data);
14526  if (ret)
14527  {
14528  openvas_connection_close (&connection);
14529  switch (ret)
14530  {
14531  case 1:
14532  response_data->http_status_code
14533  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14534  return gsad_message (credentials,
14535  "Internal error", __FUNCTION__, __LINE__,
14536  "An internal error occurred while getting a setting. "
14537  "The setting could not be delivered. "
14538  "Diagnostics: Failure to send command to manager daemon.",
14539  "/omp?cmd=get_tasks",
14540  response_data);
14541  case 2:
14542  response_data->http_status_code
14543  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14544  return gsad_message (credentials,
14545  "Internal error", __FUNCTION__, __LINE__,
14546  "An internal error occurred while getting a setting. "
14547  "The setting could not be delivered. "
14548  "Diagnostics: Failure to receive response from manager daemon.",
14549  "/omp?cmd=get_tasks",
14550  response_data);
14551  default:
14552  response_data->http_status_code
14553  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14554  return gsad_message (credentials,
14555  "Internal error", __FUNCTION__, __LINE__,
14556  "An internal error occurred while getting a setting. "
14557  "The setting could not be delivered. "
14558  "Diagnostics: Internal error.",
14559  "/omp?cmd=get_tasks",
14560  response_data);
14561  }
14562  }
14563 
14564  if (fname_format == NULL)
14565  {
14566  g_warning ("%s : File name format setting not found.",
14567  __FUNCTION__);
14568  fname_format = "%T-%U";
14569  }
14570 
14571  file_name = format_file_name (fname_format, credentials,
14572  "report", id, report_entity);
14573  if (file_name == NULL)
14574  file_name = g_strdup_printf ("%s-%s",
14575  "report", id);
14576 
14577  *content_type = g_strdup (requested_content_type);
14578  *content_disposition
14579  = g_strdup_printf ("attachment; filename=\"%s.%s\"",
14580  file_name,
14581  extension);
14582 
14583  g_free (file_name);
14584  }
14585  free_entity (entity);
14586  openvas_connection_close (&connection);
14587  if (error) *error = 1;
14588  return report_decoded;
14589  }
14590  else
14591  {
14592  free_entity (entity);
14593  openvas_connection_close (&connection);
14594  if (error) *error = 1;
14595  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14596  return gsad_message (credentials,
14597  "Internal error", __FUNCTION__, __LINE__,
14598  "An internal error occurred while getting a report. "
14599  "The report could not be delivered. "
14600  "Diagnostics: Failure to receive report from manager daemon.",
14601  "/omp?cmd=get_tasks", response_data);
14602  }
14603  }
14604  }
14605  else
14606  {
14607  gchar *task_id;
14608 
14609  task_id = NULL;
14610 
14611  /* Format is NULL, send XSL transformed XML. */
14612 
14613  if (delta_report_id && result_id && strcmp (result_id, "0"))
14614  xml = g_string_new ("<get_delta_result>");
14615  else if (host || (type && (strcmp (type, "prognostic") == 0)))
14616  {
14617  if (type && (strcmp (type, "prognostic") == 0))
14618  {
14619  xml = g_string_new ("<get_prognostic_report>");
14620 
14621  xml_string_append (xml,
14622  "<host_search_phrase>"
14623  "%s"
14624  "</host_search_phrase>"
14625  "<host_levels>%s</host_levels>"
14626  "<results start=\"%s\" max=\"%s\"/>",
14627  host_search_phrase,
14628  host_levels,
14629  host_first_result,
14630  host_max_results);
14631  }
14632  else
14633  xml = g_string_new ("<get_asset>");
14634  xml_string_append (xml,
14635  "<search_phrase>%s</search_phrase>"
14636  "<levels>%s</levels>"
14637  "<hosts start=\"%s\" max=\"%s\"/>",
14638  search_phrase,
14639  levels->str,
14640  first_result,
14641  max_results);
14642  }
14643  else
14644  xml = g_string_new ("<get_report>");
14645 
14646  if (extra_xml)
14647  g_string_append (xml, extra_xml);
14648 
14649  if (commands)
14650  g_string_append (xml, commands_xml->str);
14651  g_string_free (commands_xml, TRUE);
14652  g_string_free (levels, TRUE);
14653 
14654  if (strcmp (alert_id, "0"))
14655  {
14656  g_string_append_printf (xml, "<get_reports_alert_response>");
14657  if (esc_response)
14658  {
14659  g_string_append (xml, esc_response);
14660  g_free (esc_response);
14661  }
14662  g_string_append_printf (xml, "</get_reports_alert_response>");
14663  }
14664  else if (delta_report_id)
14665  g_string_append_printf (xml,
14666  "<delta>%s</delta>"
14667  "<result id=\"%s\"/>",
14668  delta_report_id,
14669  result_id ? result_id : "0");
14670 
14671  entity = NULL;
14672  if (read_entity_and_string_c (&connection, &entity, &xml))
14673  {
14674  openvas_connection_close (&connection);
14675  if (error) *error = 1;
14676  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14677  return gsad_message (credentials,
14678  "Internal error", __FUNCTION__, __LINE__,
14679  "An internal error occurred while getting a report. "
14680  "The report could not be delivered. "
14681  "Diagnostics: Failure to receive response from manager daemon.",
14682  "/omp?cmd=get_tasks", response_data);
14683  }
14684 
14685  if ((filt_id == NULL) && (params_value (params, "filter") == NULL))
14686  {
14687  entity_t term;
14688 
14689  /* Add the filter from the report as a param, because it's easier to
14690  * get from the envelope for things like the New Note icon. */
14691 
14692  term = entity_child (entity, "report");
14693  if (term
14694  && ((term = entity_child (term, "report")))
14695  && ((term = entity_child (term, "filters")))
14696  && ((term = entity_child (term, "term"))))
14697  {
14698  param_t *param;
14699  param = params_add (params, "filter", entity_text (term));
14700  param->valid = 1;
14701  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
14702  }
14703  }
14704 
14705  if ((type && (strcmp (type, "prognostic") == 0))
14706  && (command_enabled (credentials, "GET_REPORT_FORMATS")))
14707  {
14708  if (openvas_connection_sendf
14709  (&connection,
14710  "<get_report_formats"
14711  " filter=\"rows=-1 sort=name\"/>")
14712  == -1)
14713  {
14714  g_string_free (xml, TRUE);
14715  openvas_connection_close (&connection);
14716  if (error) *error = 1;
14717  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14718  return gsad_message (credentials,
14719  "Internal error", __FUNCTION__, __LINE__,
14720  "An internal error occurred while getting a report. "
14721  "The report could not be delivered. "
14722  "Diagnostics: Failure to send command to manager daemon.",
14723  "/omp?cmd=get_tasks", response_data);
14724  }
14725 
14726  if (read_string_c (&connection, &xml))
14727  {
14728  g_string_free (xml, TRUE);
14729  openvas_connection_close (&connection);
14730  if (error) *error = 1;
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 a report. "
14735  "The report could not be delivered. "
14736  "Diagnostics: Failure to receive response from manager daemon.",
14737  "/omp?cmd=get_tasks", response_data);
14738  }
14739  }
14740 
14741  if (type && (strcmp (type, "prognostic") == 0))
14742  {
14743  if (command_enabled (credentials, "GET_FILTERS"))
14744  {
14745  /* Get the filters. */
14746 
14747  g_string_append (xml, "<filters>");
14748 
14749  if (openvas_connection_sendf_xml (&connection,
14750  "<get_filters"
14751  " filter=\"type=result\"/>")
14752  == -1)
14753  {
14754  g_string_free (xml, TRUE);
14755  openvas_connection_close (&connection);
14756  if (error) *error = 1;
14757  response_data->http_status_code
14758  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14759  return gsad_message (credentials,
14760  "Internal error", __FUNCTION__, __LINE__,
14761  "An internal error occurred while getting the filter list. "
14762  "The current list of filters is not available. "
14763  "Diagnostics: Failure to send command to manager daemon.",
14764  "/omp?cmd=get_tasks", response_data);
14765  }
14766 
14767  if (read_string_c (&connection, &xml))
14768  {
14769  g_string_free (xml, TRUE);
14770  openvas_connection_close (&connection);
14771  if (error) *error = 1;
14772  response_data->http_status_code
14773  = MHD_HTTP_INTERNAL_SERVER_ERROR;
14774  return gsad_message (credentials,
14775  "Internal error", __FUNCTION__, __LINE__,
14776  "An internal error occurred while getting the filter list. "
14777  "The current list of filters is not available. "
14778  "Diagnostics: Failure to receive response from manager daemon.",
14779  "/omp?cmd=get_tasks", response_data);
14780  }
14781 
14782  g_string_append (xml, "</filters>");
14783  }
14784 
14785  g_string_append (xml, "</get_prognostic_report>");
14786  openvas_connection_close (&connection);
14787  return g_string_free (xml, FALSE);
14788  }
14789 
14790  if (type && (strcmp (type, "assets") == 0))
14791  {
14792  if (host)
14793  g_string_append (xml, "</get_asset>");
14794  else
14795  g_string_append (xml, "</get_report>");
14796  openvas_connection_close (&connection);
14797  return g_string_free (xml, FALSE);
14798  }
14799 
14800  report_entity = entity_child (entity, "report");
14801  if (report_entity)
14802  report_entity = entity_child (report_entity, "report");
14803  if (report_entity)
14804  {
14805  const char *id;
14806  entity_t task_entity, name;
14807 
14808  id = NULL;
14809  task_entity = entity_child (report_entity, "task");
14810  if (task_entity)
14811  {
14812  id = entity_attribute (task_entity, "id");
14813  name = entity_child (task_entity, "name");
14814  }
14815  else
14816  name = NULL;
14817  if (id)
14818  task_id = g_strdup (id);
14819  if (delta_report_id && result_id && id && name)
14820  g_string_append_printf (xml,
14821  "<task id=\"%s\"><name>%s</name></task>",
14822  id,
14823  entity_text (name));
14824 
14825  free_entity (entity);
14826  }
14827 
14828  if (task_id)
14829  {
14830  if (openvas_connection_sendf (&connection,
14831  "<get_tasks task_id=\"%s\" details=\"0\" />",
14832  task_id)
14833  == -1)
14834  {
14835  g_free (task_id);
14836  g_string_free (xml, TRUE);
14837  openvas_connection_close (&connection);
14838  if (error) *error = 1;
14839  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14840  return gsad_message (credentials,
14841  "Internal error", __FUNCTION__, __LINE__,
14842  "An internal error occurred while getting a report. "
14843  "The report could not be delivered. "
14844  "Diagnostics: Failure to send command to manager daemon.",
14845  "/omp?cmd=get_tasks", response_data);
14846  }
14847 
14848  if (read_string_c (&connection, &xml))
14849  {
14850  g_free (task_id);
14851  g_string_free (xml, TRUE);
14852  openvas_connection_close (&connection);
14853  if (error) *error = 1;
14854  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14855  return gsad_message (credentials,
14856  "Internal error", __FUNCTION__, __LINE__,
14857  "An internal error occurred while getting a report. "
14858  "The report could not be delivered. "
14859  "Diagnostics: Failure to send command to manager daemon.",
14860  "/omp?cmd=get_tasks", response_data);
14861  }
14862 
14863  g_free (task_id);
14864  }
14865 
14866  if (delta_report_id && result_id && strcmp (result_id, "0"))
14867  {
14868  g_string_append (xml, "</get_delta_result>");
14869  openvas_connection_close (&connection);
14870  return g_string_free (xml, FALSE);
14871  }
14872 
14873  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
14874  {
14875  gchar *default_report_format, *err;
14876 
14877  /* Get Default Report Format. */
14878 
14879  err = setting_get_value_error (credentials,
14880  &connection,
14881  "353304fc-645e-11e6-ba7a-28d24461215b",
14882  &default_report_format,
14883  response_data);
14884  if (err)
14885  {
14886  g_string_free (xml, TRUE);
14887  openvas_connection_close (&connection);
14888  if (error) *error = 1;
14889  return err;
14890  }
14891 
14892  g_string_append_printf (xml,
14893  "<report_format_id>%s</report_format_id>",
14894  default_report_format);
14895 
14896  /* Get all the report formats. */
14897 
14898  if (openvas_connection_sendf
14899  (&connection,
14900  "<get_report_formats"
14901  " filter=\"rows=-1 sort=name\"/>")
14902  == -1)
14903  {
14904  g_string_free (xml, TRUE);
14905  openvas_connection_close (&connection);
14906  if (error) *error = 1;
14907  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14908  return gsad_message (credentials,
14909  "Internal error", __FUNCTION__, __LINE__,
14910  "An internal error occurred while getting a report. "
14911  "The report could not be delivered. "
14912  "Diagnostics: Failure to send command to manager daemon.",
14913  "/omp?cmd=get_tasks", response_data);
14914  }
14915 
14916  if (read_string_c (&connection, &xml))
14917  {
14918  g_string_free (xml, TRUE);
14919  openvas_connection_close (&connection);
14920  if (error) *error = 1;
14921  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14922  return gsad_message (credentials,
14923  "Internal error", __FUNCTION__, __LINE__,
14924  "An internal error occurred while getting a report. "
14925  "The report could not be delivered. "
14926  "Diagnostics: Failure to receive response from manager daemon.",
14927  "/omp?cmd=get_tasks", response_data);
14928  }
14929  }
14930 
14931  if (command_enabled (credentials, "GET_ALERTS"))
14932  {
14933  if (openvas_connection_sendf
14934  (&connection,
14935  "<get_alerts"
14936  " filter=\"rows=-1 sort=name\"/>")
14937  == -1)
14938  {
14939  g_string_free (xml, TRUE);
14940  openvas_connection_close (&connection);
14941  if (error) *error = 1;
14942  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14943  return gsad_message (credentials,
14944  "Internal error", __FUNCTION__, __LINE__,
14945  "An internal error occurred while getting a report. "
14946  "The report could not be delivered. "
14947  "Diagnostics: Failure to send command to manager daemon.",
14948  "/omp?cmd=get_tasks", response_data);
14949  }
14950 
14951  if (read_string_c (&connection, &xml))
14952  {
14953  g_string_free (xml, TRUE);
14954  openvas_connection_close (&connection);
14955  if (error) *error = 1;
14956  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14957  return gsad_message (credentials,
14958  "Internal error", __FUNCTION__, __LINE__,
14959  "An internal error occurred while getting a report. "
14960  "The report could not be delivered. "
14961  "Diagnostics: Failure to receive response from manager daemon.",
14962  "/omp?cmd=get_tasks", response_data);
14963  }
14964  }
14965 
14966  if (command_enabled (credentials, "GET_FILTERS"))
14967  {
14968  /* Get the filters. */
14969 
14970  g_string_append (xml, "<filters>");
14971 
14972  if (openvas_connection_sendf_xml (&connection,
14973  "<get_filters"
14974  " filter=\"type=result\"/>")
14975  == -1)
14976  {
14977  g_string_free (xml, TRUE);
14978  openvas_connection_close (&connection);
14979  if (error) *error = 1;
14980  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14981  return gsad_message (credentials,
14982  "Internal error", __FUNCTION__, __LINE__,
14983  "An internal error occurred while getting the filter list. "
14984  "The current list of filters is not available. "
14985  "Diagnostics: Failure to send command to manager daemon.",
14986  "/omp?cmd=get_tasks", response_data);
14987  }
14988 
14989  if (read_string_c (&connection, &xml))
14990  {
14991  g_string_free (xml, TRUE);
14992  openvas_connection_close (&connection);
14993  if (error) *error = 1;
14994  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
14995  return gsad_message (credentials,
14996  "Internal error", __FUNCTION__, __LINE__,
14997  "An internal error occurred while getting the filter list. "
14998  "The current list of filters is not available. "
14999  "Diagnostics: Failure to receive response from manager daemon.",
15000  "/omp?cmd=get_tasks", response_data);
15001  }
15002 
15003  g_string_append (xml, "</filters>");
15004  }
15005 
15006  /* Get tag names */
15007 
15008  if (openvas_connection_sendf (&connection,
15009  "<get_tags"
15010  " filter=\"resource_type=report"
15011  " first=1"
15012  " rows=-1\""
15013  " names_only=\"1\""
15014  "/>")
15015  == -1)
15016  {
15017  g_string_free (xml, TRUE);
15018  openvas_connection_close (&connection);
15019  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15020  return gsad_message (credentials,
15021  "Internal error", __FUNCTION__, __LINE__,
15022  "An internal error occurred while getting tag names list. "
15023  "The current list of resources is not available. "
15024  "Diagnostics: Failure to send command to manager daemon.",
15025  "/omp?cmd=get_resources", response_data);
15026  }
15027 
15028  if (read_string_c (&connection, &xml))
15029  {
15030  g_string_free (xml, TRUE);
15031  openvas_connection_close (&connection);
15032  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15033  return gsad_message (credentials,
15034  "Internal error", __FUNCTION__, __LINE__,
15035  "An internal error occurred while getting tag names list. "
15036  "The current list of resources is not available. "
15037  "Diagnostics: Failure to receive response from manager daemon.",
15038  "/omp?cmd=get_resources", response_data);
15039  }
15040 
15041  g_string_append (xml, "</get_report>");
15042  openvas_connection_close (&connection);
15043  return g_string_free (xml, FALSE);
15044  }
15045 }
15046 
15059 char *
15060 get_report_omp (credentials_t * credentials, params_t *params,
15061  gsize *report_len, gchar ** content_type,
15062  char **content_disposition, cmd_response_data_t* response_data)
15063 {
15064  char *result;
15065  int error = 0;
15066 
15067  result = get_report (credentials, params, NULL, report_len, content_type,
15068  content_disposition, NULL, &error, response_data);
15069 
15070  return error ? result : xsl_transform_omp (credentials, result,
15071  response_data);
15072 }
15073 
15084 static char *
15085 get_reports (credentials_t * credentials, params_t *params,
15086  const char *extra_xml, cmd_response_data_t* response_data)
15087 {
15088  const char *overrides;
15089 
15090  overrides = params_value (params, "overrides");
15091  if (overrides)
15092  /* User toggled overrides. Set the overrides value in the filter. */
15093  params_toggle_overrides (params, overrides);
15094 
15095  return get_many ("report", credentials, params, extra_xml, NULL,
15096  response_data);
15097 }
15098 
15108 char *
15109 get_reports_omp (credentials_t * credentials, params_t *params,
15110  cmd_response_data_t* response_data)
15111 {
15112  return get_reports (credentials, params, NULL, response_data);
15113 }
15114 
15124 static char *
15125 get_report_section (credentials_t * credentials, params_t *params,
15126  const char *extra_xml, cmd_response_data_t* response_data)
15127 {
15128  GString *xml;
15129  const char *report_id, *report_section, *type;
15130  char *result;
15131  int error = 0;
15132 
15133  report_section = params_value (params, "report_section");
15134  report_id = params_value (params, "report_id");
15135  type = params_value (params, "type");
15136 
15137  if (report_section == NULL)
15138  report_section = "results";
15139 
15140  if (report_id == NULL && (type == NULL || strcmp (type, "prognostic")))
15141  {
15142  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
15143  return gsad_message (credentials,
15144  "Internal error", __FUNCTION__, __LINE__,
15145  "An internal error occurred."
15146  " Diagnostics: report_id was NULL.",
15147  "/omp?cmd=get_tasks", response_data);
15148  }
15149 
15150  if (!strcmp (report_section, "results"))
15151  {
15152  char *result;
15153 
15154  result = get_report (credentials, params, NULL, NULL, NULL, NULL,
15155  extra_xml, &error, response_data);
15156 
15157  return error ? result : xsl_transform_omp (credentials, result,
15158  response_data);
15159  }
15160 
15161  result = get_report (credentials, params, NULL, NULL, NULL,
15162  NULL, NULL, &error, response_data);
15163  if (error)
15164  return result;
15165 
15166  xml = g_string_new ("");
15167  g_string_append_printf (xml, "<get_report_%s_response>", report_section);
15168  if (extra_xml)
15169  g_string_append (xml, extra_xml);
15170  g_string_append (xml, result);
15171  if (strcmp (params_value (params, "report_section"), "topology") == 0)
15172  {
15173  int ret;
15174  char *response;
15175 
15176  ret = omp (credentials,
15177  &response,
15178  NULL,
15179  response_data,
15180  "<get_report_formats"
15181  " filter=\"rows=-1\"/>");
15182 
15183  switch (ret)
15184  {
15185  case 0:
15186  case -1:
15187  break;
15188  case 1:
15189  g_string_free (xml, TRUE);
15190  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15191  return gsad_message (credentials,
15192  "Internal error", __FUNCTION__, __LINE__,
15193  "An internal error occurred while getting the "
15194  "result formats list. "
15195  "Diagnostics: Failure to send command to manager daemon.",
15196  "/omp?cmd=get_tasks", response_data);
15197  case 2:
15198  g_string_free (xml, TRUE);
15199  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15200  return gsad_message (credentials,
15201  "Internal error", __FUNCTION__, __LINE__,
15202  "An internal error occurred while getting the "
15203  "result formats list. "
15204  "Diagnostics: Failure to receive response from manager daemon.",
15205  "/omp?cmd=get_tasks", response_data);
15206  default:
15207  g_string_free (xml, TRUE);
15208  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15209  return gsad_message (credentials,
15210  "Internal error", __FUNCTION__, __LINE__,
15211  "An internal error occurred while getting the "
15212  "result formats list. "
15213  "Diagnostics: Internal Error.",
15214  "/omp?cmd=get_tasks", response_data);
15215  }
15216 
15217  g_string_append (xml, response);
15218  g_free (response);
15219  }
15220 
15221  g_string_append_printf (xml, "</get_report_%s_response>", report_section);
15222 
15223  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
15224  response_data);
15225 }
15226 
15236 char *
15238  cmd_response_data_t* response_data)
15239 {
15240  return get_report_section (credentials, params, NULL, response_data);
15241 }
15242 
15253 char *
15254 download_ssl_cert (credentials_t * credentials, params_t *params,
15255  gsize *response_size, cmd_response_data_t* response_data)
15256 {
15257  const char *ssl_cert;
15258  gchar *cert;
15259  char *unescaped;
15260 
15261  ssl_cert = params_value (params, "ssl_cert");
15262  if (ssl_cert == NULL)
15263  {
15264  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
15265  return gsad_message (credentials,
15266  "Internal error", __FUNCTION__, __LINE__,
15267  "An internal error occurred."
15268  " Diagnostics: ssl_cert was NULL.",
15269  "/omp?cmd=get_reports", response_data);
15270  }
15271  /* The Base64 comes URI escaped as it may contain special characters. */
15272  unescaped = g_uri_unescape_string (ssl_cert, NULL);
15273 
15274  cert = g_strdup_printf ("-----BEGIN CERTIFICATE-----\n"
15275  "%s\n-----END CERTIFICATE-----\n",
15276  unescaped);
15277 
15278  *response_size = strlen (cert);
15279 
15280  g_free (unescaped);
15281  return cert;
15282 }
15283 
15294 char *
15295 download_ca_pub (credentials_t * credentials, params_t *params,
15296  gsize *response_size, cmd_response_data_t* response_data)
15297 {
15298  const char *ca_pub;
15299  char *unescaped;
15300 
15301  ca_pub = params_value (params, "ca_pub");
15302  if (ca_pub == NULL)
15303  {
15304  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
15305  return gsad_message (credentials,
15306  "Internal error", __FUNCTION__, __LINE__,
15307  "An internal error occurred."
15308  " Diagnostics: ca_pub was NULL.",
15309  "/omp?cmd=get_reports", response_data);
15310  }
15311  /* The Base64 comes URI escaped as it may contain special characters. */
15312  unescaped = g_uri_unescape_string (ca_pub, NULL);
15313  *response_size = strlen (unescaped);
15314  return unescaped;
15315 }
15316 
15327 char *
15328 download_key_pub (credentials_t * credentials, params_t *params,
15329  gsize *response_size, cmd_response_data_t* response_data)
15330 {
15331  const char *key_pub;
15332  char *unescaped;
15333 
15334  key_pub = params_value (params, "key_pub");
15335  if (key_pub == NULL)
15336  {
15337  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
15338  return gsad_message (credentials,
15339  "Internal error", __FUNCTION__, __LINE__,
15340  "An internal error occurred."
15341  " Diagnostics: key_pub was NULL.",
15342  "/omp?cmd=get_reports", response_data);
15343  }
15344 
15345  /* The Base64 comes URI escaped as it may contain special characters. */
15346  unescaped = g_uri_unescape_string (key_pub, NULL);
15347  *response_size = strlen (unescaped);
15348  return unescaped;
15349 }
15350 
15364 char *
15365 export_result_omp (credentials_t * credentials, params_t *params,
15366  enum content_type * content_type, char **content_disposition,
15367  gsize *content_length, cmd_response_data_t* response_data)
15368 {
15369  return export_resource ("result", credentials, params, content_type,
15370  content_disposition, content_length, response_data);
15371 }
15372 
15386 char *
15388  enum content_type * content_type,
15389  char **content_disposition, gsize *content_length,
15390  cmd_response_data_t* response_data)
15391 {
15392  return export_many ("result", credentials, params, content_type,
15393  content_disposition, content_length, response_data);
15394 }
15395 
15406 char *
15407 get_results (credentials_t * credentials, params_t *params,
15408  const char *extra_xml, cmd_response_data_t* response_data)
15409 {
15410  const char *overrides;
15411  overrides = params_value (params, "overrides");
15412 
15413  if (overrides)
15414  /* User toggled overrides. Set the overrides value in the filter. */
15415  params_toggle_overrides (params, overrides);
15416 
15417  return get_many ("result", credentials, params, extra_xml, NULL,
15418  response_data);
15419 }
15420 
15430 char *
15431 get_results_omp (credentials_t *credentials, params_t *params,
15432  cmd_response_data_t* response_data)
15433 {
15434  return get_results (credentials,
15435  params,
15436  NULL,
15437  response_data);
15438 }
15439 
15455 static char *
15456 get_result (credentials_t *credentials, const char *result_id,
15457  const char *task_id, const char *task_name,
15458  const char *apply_overrides, const char *commands,
15459  const char *report_id, const char *autofp,
15460  const char *extra_xml, cmd_response_data_t* response_data)
15461 {
15462  GString *xml;
15463  openvas_connection_t connection;
15464  gchar *html;
15465 
15466  if (apply_overrides == NULL)
15467  apply_overrides = "1";
15468 
15469  if (autofp == NULL)
15470  autofp = "0";
15471 
15472  switch (manager_connect (credentials, &connection, &html,
15473  response_data))
15474  {
15475  case 0:
15476  break;
15477  case -1:
15478  if (html)
15479  return html;
15480  /* Fall through. */
15481  default:
15482  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15483  return gsad_message (credentials,
15484  "Internal error", __FUNCTION__, __LINE__,
15485  "An internal error occurred while getting a result. "
15486  "Diagnostics: Failure to connect to manager daemon.",
15487  "/omp?cmd=get_tasks", response_data);
15488  }
15489 
15490  xml = g_string_new ("<get_result>");
15491 
15492  if (extra_xml)
15493  g_string_append (xml, extra_xml);
15494 
15495  xml_string_append (xml,
15496  "<task id=\"%s\"><name>%s</name></task>"
15497  "<report id=\"%s\"/>",
15498  task_id,
15499  task_name,
15500  report_id);
15501 
15502  /* Get the result. */
15503 
15504  if (openvas_connection_sendf (&connection,
15505  "<commands>"
15506  "%s"
15507  "<get_results"
15508  " result_id=\"%s\""
15509  "%s%s%s"
15510  " filter=\"autofp=%s"
15511  " apply_overrides=%s"
15512  " overrides=%s"
15513  " notes=1\""
15514  " overrides_details=\"1\""
15515  " notes_details=\"1\""
15516  " details=\"1\"/>"
15517  "</commands>",
15518  commands ? commands : "",
15519  result_id,
15520  task_id ? " task_id=\"" : "",
15521  task_id ? task_id : "",
15522  task_id ? "\"" : "",
15523  autofp,
15524  apply_overrides,
15525  apply_overrides)
15526  == -1)
15527  {
15528  g_string_free (xml, TRUE);
15529  openvas_connection_close (&connection);
15530  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15531  return gsad_message (credentials,
15532  "Internal error", __FUNCTION__, __LINE__,
15533  "An internal error occurred while getting a result. "
15534  "Diagnostics: Failure to send command to manager daemon.",
15535  "/omp?cmd=get_tasks", response_data);
15536  }
15537 
15538  if (read_string_c (&connection, &xml))
15539  {
15540  g_string_free (xml, TRUE);
15541  openvas_connection_close (&connection);
15542  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15543  return gsad_message (credentials,
15544  "Internal error", __FUNCTION__, __LINE__,
15545  "An internal error occurred while getting a result. "
15546  "Diagnostics: Failure to receive response from manager daemon.",
15547  "/omp?cmd=get_tasks", response_data);
15548  }
15549 
15550  /* Get tag names */
15551 
15552  if (openvas_connection_sendf (&connection,
15553  "<get_tags"
15554  " filter=\"resource_type=result"
15555  " first=1"
15556  " rows=-1\""
15557  " names_only=\"1\""
15558  "/>")
15559  == -1)
15560  {
15561  g_string_free (xml, TRUE);
15562  openvas_connection_close (&connection);
15563  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15564  return gsad_message (credentials,
15565  "Internal error", __FUNCTION__, __LINE__,
15566  "An internal error occurred while getting tag names list. "
15567  "The current list of resources is not available. "
15568  "Diagnostics: Failure to send command to manager daemon.",
15569  "/omp?cmd=get_resources", response_data);
15570  }
15571 
15572  if (read_string_c (&connection, &xml))
15573  {
15574  g_string_free (xml, TRUE);
15575  openvas_connection_close (&connection);
15576  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15577  return gsad_message (credentials,
15578  "Internal error", __FUNCTION__, __LINE__,
15579  "An internal error occurred while getting tag names list. "
15580  "The current list of resources is not available. "
15581  "Diagnostics: Failure to receive response from manager daemon.",
15582  "/omp?cmd=get_resources", response_data);
15583  }
15584 
15585  /* Cleanup, and return transformed XML. */
15586 
15587  g_string_append (xml, "</get_result>");
15588  openvas_connection_close (&connection);
15589  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
15590  response_data);
15591 }
15592 
15602 char *
15603 get_result_omp (credentials_t *credentials, params_t *params,
15604  cmd_response_data_t* response_data)
15605 {
15606  return get_result (credentials,
15607  params_value (params, "result_id"),
15608  params_value (params, "task_id"),
15609  params_value (params, "name"),
15610  params_value (params, "apply_overrides"),
15611  NULL,
15612  params_value (params, "report_id"),
15613  params_value (params, "autofp"),
15614  NULL,
15615  response_data);
15616 }
15617 
15628 static char *
15629 get_result_page (credentials_t *credentials, params_t *params,
15630  const char *extra_xml, cmd_response_data_t* response_data)
15631 {
15632  return get_result (credentials,
15633  params_value (params, "result_id"),
15634  params_value (params, "task_id"),
15635  params_value (params, "name"),
15636  params_value (params, "apply_overrides"),
15637  NULL,
15638  params_value (params, "report_id"),
15639  params_value (params, "autofp"),
15640  extra_xml,
15641  response_data);
15642 
15643 }
15644 
15655 static char *
15656 get_notes (credentials_t *credentials, params_t *params, const char *extra_xml,
15657  cmd_response_data_t* response_data)
15658 {
15659  return get_many ("note", credentials, params, extra_xml, NULL, response_data);
15660 }
15661 
15671 char *
15672 get_notes_omp (credentials_t *credentials, params_t *params,
15673  cmd_response_data_t* response_data)
15674 {
15675  return get_notes (credentials, params, NULL, response_data);
15676 }
15677 
15688 static char *
15689 get_note (credentials_t *credentials, params_t *params, const char *extra_xml,
15690  cmd_response_data_t* response_data)
15691 {
15692  return get_one ("note", credentials, params, extra_xml, NULL, response_data);
15693 }
15694 
15704 char *
15705 get_note_omp (credentials_t *credentials, params_t *params,
15706  cmd_response_data_t* response_data)
15707 {
15708  return get_note (credentials, params, NULL, response_data);
15709 }
15710 
15721 char *
15722 new_note (credentials_t *credentials, params_t *params, const char *extra_xml,
15723  cmd_response_data_t* response_data)
15724 {
15725  GString *xml;
15726  openvas_connection_t connection;
15727  gchar *html;
15728  const char *oid, *hosts, *port, *severity, *task_id, *task_name, *result_id;
15729  const char *next;
15730  /* Passthroughs. */
15731  const char *report_id, *first_result, *max_results, *sort_field;
15732  const char *sort_order, *levels, *autofp, *notes;
15733  const char *overrides, *result_hosts_only, *search_phrase;
15734 
15735  result_id = params_value (params, "result_id");
15736  task_id = params_value (params, "task_id");
15737 
15738  next = params_value (params, "next");
15739  first_result = params_value (params, "first_result");
15740  max_results = params_value (params, "max_results");
15741  levels = params_value (params, "levels");
15742  autofp = params_value (params, "autofp");
15743  notes = params_value (params, "notes");
15744  report_id = params_value (params, "report_id");
15745  search_phrase = params_value (params, "search_phrase");
15746  sort_field = params_value (params, "sort_field");
15747  sort_order = params_value (params, "sort_order");
15748  task_name = params_value (params, "name");
15749  severity = params_value (params, "severity");
15750  result_hosts_only = params_value (params, "result_hosts_only");
15751 
15752  hosts = params_value (params, "hosts");
15753  oid = params_value (params, "oid");
15754  port = params_value (params, "port");
15755  overrides = params_value (params, "overrides");
15756 
15757  switch (manager_connect (credentials, &connection, &html,
15758  response_data))
15759  {
15760  case 0:
15761  break;
15762  case -1:
15763  if (html)
15764  return html;
15765  /* Fall through. */
15766  default:
15767  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15768  return gsad_message (credentials,
15769  "Internal error", __FUNCTION__, __LINE__,
15770  "An internal error occurred while creating a new note. "
15771  "No new note was created. "
15772  "Diagnostics: Failure to connect to manager daemon.",
15773  "/omp?cmd=get_notes", response_data);
15774  }
15775 
15776  if (result_id == NULL || task_id == NULL)
15777  {
15778  xml = g_string_new ("");
15779 
15780  xml_string_append (xml,
15781  "<new_note>"
15782  "<nvt id=\"%s\"/>"
15783  "<next>%s</next>",
15784  oid ? oid : "0",
15785  next ? next : "");
15786 
15787  if (extra_xml)
15788  g_string_append (xml, extra_xml);
15789 
15790  if (openvas_connection_sendf (&connection,
15791  "<get_tasks"
15792  " schedules_only=\"1\""
15793  " details=\"0\"/>")
15794  == -1)
15795  {
15796  openvas_connection_close (&connection);
15797  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15798  return gsad_message (credentials,
15799  "Internal error", __FUNCTION__, __LINE__,
15800  "An internal error occurred while creating a new note. "
15801  "No new note was created. "
15802  "Diagnostics: Failure to send command to manager daemon.",
15803  "/omp?cmd=get_notes", response_data);
15804  }
15805 
15806  if (read_string_c (&connection, &xml))
15807  {
15808  g_string_free (xml, TRUE);
15809  openvas_connection_close (&connection);
15810  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15811  return gsad_message (credentials,
15812  "Internal error", __FUNCTION__, __LINE__,
15813  "An internal error occurred while creating a new note. "
15814  "No new note was created. "
15815  "Diagnostics: Failure to receive response from manager daemon.",
15816  "/omp?cmd=get_notes", response_data);
15817  }
15818 
15819  g_string_append (xml, "</new_note>");
15820  openvas_connection_close (&connection);
15821  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
15822  response_data);
15823  }
15824 
15825  if (openvas_connection_sendf (&connection,
15826  "<get_results"
15827  " result_id=\"%s\""
15828  " task_id=\"%s\""
15829  " notes_details=\"1\""
15830  " notes=\"1\""
15831  " result_hosts_only=\"1\"/>",
15832  result_id,
15833  task_id)
15834  == -1)
15835  {
15836  openvas_connection_close (&connection);
15837  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15838  return gsad_message (credentials,
15839  "Internal error", __FUNCTION__, __LINE__,
15840  "An internal error occurred while creating a new note. "
15841  "No new note was created. "
15842  "Diagnostics: Failure to send command to manager daemon.",
15843  "/omp?cmd=get_notes", response_data);
15844  }
15845 
15846  xml = g_string_new ("");
15847 
15848  xml_string_append (xml,
15849  "<new_note>"
15850  "<nvt id=\"%s\"/>"
15851  "<hosts>%s</hosts>"
15852  "<port>%s</port>"
15853  "<severity>%s</severity>"
15854  "<task id=\"%s\">"
15855  "<name>%s</name>"
15856  "</task>"
15857  "<result id=\"%s\"/>"
15858  "<next>%s</next>"
15859  /* Passthroughs. */
15860  "<report id=\"%s\"/>"
15861  "<first_result>%s</first_result>"
15862  "<max_results>%s</max_results>"
15863  "<sort_field>%s</sort_field>"
15864  "<sort_order>%s</sort_order>"
15865  "<levels>%s</levels>"
15866  "<autofp>%s</autofp>"
15867  "<notes>%s</notes>"
15868  "<overrides>%s</overrides>"
15869  "<result_hosts_only>%s</result_hosts_only>"
15870  "<search_phrase>%s</search_phrase>",
15871  oid,
15872  hosts,
15873  /* port is NULL for CVE scan results. */
15874  port ? port : "",
15875  severity,
15876  task_id,
15877  task_name,
15878  result_id,
15879  next ? next : "",
15880  report_id,
15881  first_result,
15882  max_results,
15883  sort_field,
15884  sort_order,
15885  levels,
15886  autofp,
15887  notes,
15888  overrides,
15889  result_hosts_only,
15890  search_phrase);
15891 
15892  if (extra_xml)
15893  g_string_append (xml, extra_xml);
15894 
15895  if (read_string_c (&connection, &xml))
15896  {
15897  g_string_free (xml, TRUE);
15898  openvas_connection_close (&connection);
15899  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
15900  return gsad_message (credentials,
15901  "Internal error", __FUNCTION__, __LINE__,
15902  "An internal error occurred while creating a new note. "
15903  "It is unclear whether the note has been created or not. "
15904  "Diagnostics: Failure to receive response from manager daemon.",
15905  "/omp?cmd=get_notes", response_data);
15906  }
15907 
15908  /* Cleanup, and return transformed XML. */
15909 
15910  g_string_append (xml, "</new_note>");
15911  openvas_connection_close (&connection);
15912  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
15913  response_data);
15914 }
15915 
15925 char *
15926 new_note_omp (credentials_t *credentials, params_t *params,
15927  cmd_response_data_t* response_data)
15928 {
15929  return new_note (credentials, params, NULL, response_data);
15930 }
15931 
15941 char *
15942 create_note_omp (credentials_t *credentials, params_t *params,
15943  cmd_response_data_t* response_data)
15944 {
15945  char *ret;
15946  gchar *response;
15947  const char *no_redirect, *oid, *severity, *port, *hosts;
15948  const char *text, *task_id, *note_result_id;
15949  /* For get_report. */
15950  const char *active, *days;
15951  entity_t entity;
15952 
15953  no_redirect = params_value (params, "no_redirect");
15954  oid = params_value (params, "oid");
15955  CHECK_PARAM_INVALID (oid, "Create Note", "new_note");
15956 
15957  if (params_valid (params, "severity"))
15958  severity = params_value (params, "severity");
15959  else if (params_given (params, "severity")
15960  && strcmp (params_original_value (params, "severity"), ""))
15961  severity = NULL;
15962  else
15963  severity = "";
15964  CHECK_PARAM_INVALID (severity, "Create Note", "new_note");
15965 
15966  port = params_value (params, "port");
15967  if (port == NULL)
15968  port = "";
15969  if (strcmp (port, "--") == 0)
15970  {
15971  int num = -1;
15972 
15973  port = params_value (params, "port_manual");
15974  if (port)
15975  num = atoi (port);
15976  if (num < 0 || num > 65535)
15977  port = NULL;
15978  }
15979  CHECK_PARAM_INVALID (port, "Create Note", "new_note");
15980 
15981  if (params_valid (params, "hosts"))
15982  {
15983  hosts = params_value (params, "hosts");
15984  if (strcmp (hosts, "--") == 0)
15985  {
15986  if (params_valid (params, "hosts_manual"))
15987  hosts = params_value (params, "hosts_manual");
15988  else if (params_given (params, "hosts_manual")
15989  && strcmp (params_original_value (params, "hosts_manual"),
15990  ""))
15991  hosts = NULL;
15992  else
15993  hosts = "";
15994  }
15995  }
15996  else if (strcmp (params_original_value (params, "hosts"), ""))
15997  hosts = NULL;
15998  else
15999  hosts = "";
16000  CHECK_PARAM_INVALID (hosts, "Create Note", "new_note");
16001 
16002  if (params_valid (params, "note_task_id"))
16003  {
16004  task_id = params_value (params, "note_task_id");
16005  if (task_id && (strcmp (task_id, "0") == 0))
16006  task_id = params_value (params, "note_task_uuid");
16007  }
16008  else if (params_given (params, "note_task_id")
16009  && strcmp (params_original_value (params, "note_task_id"), ""))
16010  task_id = NULL;
16011  else
16012  task_id = "";
16013 
16014  active = params_value (params, "active");
16015  CHECK_PARAM_INVALID (active, "Create Note", "new_note");
16016 
16017  text = params_value (params, "text");
16018  days = params_value (params, "days");
16019 
16020  note_result_id = params_value (params, "note_result_id");
16021  if (note_result_id && (strcmp (note_result_id, "0") == 0))
16022  note_result_id = params_value (params, "note_result_uuid");
16023 
16024  response = NULL;
16025  entity = NULL;
16026  switch (ompf (credentials,
16027  &response,
16028  &entity,
16029  response_data,
16030  "<create_note>"
16031  "<active>%s</active>"
16032  "<nvt oid=\"%s\"/>"
16033  "<hosts>%s</hosts>"
16034  "<port>%s</port>"
16035  "<severity>%s</severity>"
16036  "<text>%s</text>"
16037  "<task id=\"%s\"/>"
16038  "<result id=\"%s\"/>"
16039  "</create_note>",
16040  strcmp (active, "1")
16041  ? active
16042  : (days ? days : "-1"),
16043  oid,
16044  hosts,
16045  port,
16046  severity,
16047  text ? text : "",
16048  task_id,
16049  note_result_id))
16050  {
16051  case 0:
16052  case -1:
16053  break;
16054  case 1:
16055  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16056  return gsad_message (credentials,
16057  "Internal error", __FUNCTION__, __LINE__,
16058  "An internal error occurred while creating a new note. "
16059  "No new note was created. "
16060  "Diagnostics: Failure to send command to manager daemon.",
16061  "/omp?cmd=get_notes", response_data);
16062  case 2:
16063  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16064  return gsad_message (credentials,
16065  "Internal error", __FUNCTION__, __LINE__,
16066  "An internal error occurred while creating a new note. "
16067  "It is unclear whether the note has been created or not. "
16068  "Diagnostics: Failure to receive response from manager daemon.",
16069  "/omp?cmd=get_notes", response_data);
16070  default:
16071  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16072  return gsad_message (credentials,
16073  "Internal error", __FUNCTION__, __LINE__,
16074  "An internal error occurred while creating a new note. "
16075  "It is unclear whether the note has been created or not. "
16076  "Diagnostics: Internal Error.",
16077  "/omp?cmd=get_notes", response_data);
16078  }
16079 
16080  if (entity_attribute (entity, "id"))
16081  params_add (params, "note_id", entity_attribute (entity, "id"));
16082  ret = response_from_entity (credentials, params, entity,
16083  (no_redirect && strcmp (no_redirect, "0")),
16084  NULL, "get_notes",
16085  NULL, "new_note",
16086  "Create Note", response_data);
16087  free_entity (entity);
16088  g_free (response);
16089  return ret;
16090 }
16091 
16101 char *
16102 delete_note_omp (credentials_t * credentials, params_t *params,
16103  cmd_response_data_t* response_data)
16104 {
16105  return delete_resource ("note", credentials, params, 0, NULL, response_data);
16106 }
16107 
16117 char *
16119  cmd_response_data_t* response_data)
16120 {
16121  return delete_resource ("note", credentials, params, 1, "get_trash",
16122  response_data);
16123 }
16124 
16135 char *
16136 edit_note (credentials_t *credentials, params_t *params, const char *extra_xml,
16137  cmd_response_data_t* response_data)
16138 {
16139  GString *xml;
16140  openvas_connection_t connection;
16141  gchar *html;
16142  const char *note_id;
16143 
16144  note_id = params_value (params, "note_id");
16145 
16146  switch (manager_connect (credentials, &connection, &html,
16147  response_data))
16148  {
16149  case 0:
16150  break;
16151  case -1:
16152  if (html)
16153  return html;
16154  /* Fall through. */
16155  default:
16156  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16157  return gsad_message (credentials,
16158  "Internal error", __FUNCTION__, __LINE__,
16159  "An internal error occurred while editing a note. "
16160  "The note remains as it was. "
16161  "Diagnostics: Failure to connect to manager daemon.",
16162  "/omp?cmd=get_notes", response_data);
16163  }
16164 
16165  if (openvas_connection_sendf (&connection,
16166  "<get_notes"
16167  " note_id=\"%s\""
16168  " details=\"1\""
16169  " result=\"1\"/>",
16170  note_id)
16171  == -1)
16172  {
16173  openvas_connection_close (&connection);
16174  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16175  return gsad_message (credentials,
16176  "Internal error", __FUNCTION__, __LINE__,
16177  "An internal error occurred while editing a note. "
16178  "The note remains as it was. "
16179  "Diagnostics: Failure to send command to manager daemon.",
16180  "/omp?cmd=get_notes", response_data);
16181  }
16182 
16183  xml = g_string_new ("");
16184 
16185  xml_string_append (xml, "<edit_note>");
16186 
16187  if (extra_xml)
16188  g_string_append (xml, extra_xml);
16189 
16190  if (read_string_c (&connection, &xml))
16191  {
16192  g_string_free (xml, TRUE);
16193  openvas_connection_close (&connection);
16194  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16195  return gsad_message (credentials,
16196  "Internal error", __FUNCTION__, __LINE__,
16197  "An internal error occurred while editing a note. "
16198  "The note remains as it was. "
16199  "Diagnostics: Failure to receive response from manager daemon.",
16200  "/omp?cmd=get_notes", response_data);
16201  }
16202 
16203  /* Cleanup, and return transformed XML. */
16204 
16205  g_string_append (xml, "</edit_note>");
16206  openvas_connection_close (&connection);
16207  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
16208  response_data);
16209 }
16210 
16220 char *
16221 edit_note_omp (credentials_t *credentials, params_t *params,
16222  cmd_response_data_t* response_data)
16223 {
16224  return edit_note (credentials, params, NULL, response_data);
16225 }
16226 
16236 char *
16237 save_note_omp (credentials_t * credentials, params_t *params,
16238  cmd_response_data_t* response_data)
16239 {
16240  gchar *response;
16241  entity_t entity;
16242  const char *no_redirect;
16243  const char *note_id, *text, *hosts, *port, *severity, *note_task_id;
16244  const char *note_result_id, *active, *days;
16245  char *ret;
16246 
16247  no_redirect = params_value (params, "no_redirect");
16248  note_id = params_value (params, "note_id");
16249 
16250  text = params_value (params, "text");
16251  if (text == NULL)
16252  params_given (params, "text") || (text = "");
16253 
16254  if (params_valid (params, "hosts"))
16255  hosts = params_value (params, "hosts");
16256  else if (strcmp (params_original_value (params, "hosts"), ""))
16257  hosts = NULL;
16258  else
16259  hosts = "";
16260 
16261  if (params_valid (params, "port"))
16262  port = params_value (params, "port");
16263  else if (strcmp (params_original_value (params, "port"), ""))
16264  port = NULL;
16265  else
16266  port = "";
16267 
16268  if (params_valid (params, "severity"))
16269  severity = params_value (params, "severity");
16270  else if (strcmp (params_original_value (params, "severity"), ""))
16271  severity = NULL;
16272  else
16273  severity = "";
16274 
16275  note_task_id = params_value (params, "note_task_id");
16276  note_result_id = params_value (params, "note_result_id");
16277 
16278  active = params_value (params, "active");
16279  days = params_value (params, "days");
16280 
16281  CHECK_PARAM_INVALID (note_task_id, "Save Note", "edit_note");
16282  CHECK_PARAM_INVALID (note_result_id, "Save Note", "edit_note");
16283  CHECK_PARAM_INVALID (active, "Save Note", "edit_note");
16284  CHECK_PARAM_INVALID (note_id, "Save Note", "edit_note");
16285  CHECK_PARAM_INVALID (text, "Save Note", "edit_note");
16286  CHECK_PARAM_INVALID (hosts, "Save Note", "edit_note");
16287  CHECK_PARAM_INVALID (port, "Save Note", "edit_note");
16288  CHECK_PARAM_INVALID (severity, "Save Note", "edit_note");
16289  CHECK_PARAM_INVALID (days, "Save Note", "edit_note");
16290 
16291  response = NULL;
16292  entity = NULL;
16293  switch (ompf (credentials,
16294  &response,
16295  &entity,
16296  response_data,
16297  "<modify_note note_id=\"%s\">"
16298  "<active>%s</active>"
16299  "<hosts>%s</hosts>"
16300  "<port>%s</port>"
16301  "<severity>%s</severity>"
16302  "<text>%s</text>"
16303  "<task id=\"%s\"/>"
16304  "<result id=\"%s\"/>"
16305  "</modify_note>",
16306  note_id,
16307  strcmp (active, "1")
16308  ? active
16309  : (days ? days : "-1"),
16310  hosts ? hosts : "",
16311  port ? port : "",
16312  severity ? severity : "",
16313  text ? text : "",
16314  note_task_id,
16315  note_result_id))
16316  {
16317  case 0:
16318  case -1:
16319  break;
16320  case 1:
16321  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16322  return gsad_message (credentials,
16323  "Internal error", __FUNCTION__, __LINE__,
16324  "An internal error occurred while saving a note. "
16325  "The note remains the same. "
16326  "Diagnostics: Failure to send command to manager daemon.",
16327  "/omp?cmd=get_notes", response_data);
16328  case 2:
16329  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16330  return gsad_message (credentials,
16331  "Internal error", __FUNCTION__, __LINE__,
16332  "An internal error occurred while saving a note. "
16333  "It is unclear whether the note has been saved or not. "
16334  "Diagnostics: Failure to receive response from manager daemon.",
16335  "/omp?cmd=get_notes", response_data);
16336  default:
16337  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16338  return gsad_message (credentials,
16339  "Internal error", __FUNCTION__, __LINE__,
16340  "An internal error occurred while saving a note. "
16341  "It is unclear whether the note has been saved or not. "
16342  "Diagnostics: Internal Error.",
16343  "/omp?cmd=get_notes", response_data);
16344  }
16345 
16346  ret = response_from_entity (credentials, params, entity,
16347  (no_redirect && strcmp (no_redirect, "0")),
16348  NULL, "get_notes",
16349  NULL, "edit_note",
16350  "Save Note", response_data);
16351 
16352  free_entity (entity);
16353  g_free (response);
16354  return ret;
16355 }
16356 
16367 static char *
16368 get_overrides (credentials_t *credentials, params_t *params,
16369  const char *extra_xml, cmd_response_data_t* response_data)
16370 {
16371  return get_many ("override", credentials, params, extra_xml, NULL,
16372  response_data);
16373 }
16374 
16384 char *
16386  cmd_response_data_t* response_data)
16387 {
16388  return get_overrides (credentials, params, NULL, response_data);
16389 }
16390 
16401 static char *
16402 get_override (credentials_t *credentials, params_t *params,
16403  const char *extra_xml, cmd_response_data_t* response_data)
16404 {
16405  return get_one ("override", credentials, params, extra_xml, NULL,
16406  response_data);
16407 }
16408 
16418 char *
16419 get_override_omp (credentials_t *credentials, params_t *params,
16420  cmd_response_data_t* response_data)
16421 {
16422  return get_override (credentials, params, NULL, response_data);
16423 }
16424 
16435 char *
16436 new_override (credentials_t *credentials, params_t *params,
16437  const char *extra_xml, cmd_response_data_t* response_data)
16438 {
16439  GString *xml;
16440  openvas_connection_t connection;
16441  gchar *html;
16442  const char *oid, *hosts, *port, *severity, *task_id, *task_name, *result_id;
16443  const char *next;
16444  /* Passthroughs. */
16445  const char *report_id, *first_result, *max_results, *sort_field;
16446  const char *sort_order, *levels, *autofp, *notes;
16447  const char *overrides, *result_hosts_only, *search_phrase;
16448 
16449  result_id = params_value (params, "result_id");
16450  task_id = params_value (params, "task_id");
16451 
16452  next = params_value (params, "next");
16453  first_result = params_value (params, "first_result");
16454  max_results = params_value (params, "max_results");
16455  levels = params_value (params, "levels");
16456  autofp = params_value (params, "autofp");
16457  notes = params_value (params, "notes");
16458  report_id = params_value (params, "report_id");
16459  search_phrase = params_value (params, "search_phrase");
16460  sort_field = params_value (params, "sort_field");
16461  sort_order = params_value (params, "sort_order");
16462  task_name = params_value (params, "name");
16463  severity = params_value (params, "severity");
16464  result_hosts_only = params_value (params, "result_hosts_only");
16465 
16466  hosts = params_value (params, "hosts");
16467  oid = params_value (params, "oid");
16468  port = params_value (params, "port");
16469  overrides = params_value (params, "overrides");
16470 
16471  switch (manager_connect (credentials, &connection, &html,
16472  response_data))
16473  {
16474  case 0:
16475  break;
16476  case -1:
16477  if (html)
16478  return html;
16479  /* Fall through. */
16480  default:
16481  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16482  return gsad_message (credentials,
16483  "Internal error", __FUNCTION__, __LINE__,
16484  "An internal error occurred while creating a new override. "
16485  "No new override was created. "
16486  "Diagnostics: Failure to connect to manager daemon.",
16487  "/omp?cmd=get_overrides", response_data);
16488  }
16489 
16490  if (result_id == NULL || task_id == NULL)
16491  {
16492  xml = g_string_new ("");
16493 
16494  xml_string_append (xml,
16495  "<new_override>"
16496  "<nvt id=\"%s\"/>"
16497  "<next>%s</next>",
16498  oid ? oid : "0",
16499  next ? next : "");
16500 
16501  if (extra_xml)
16502  g_string_append (xml, extra_xml);
16503 
16504  if (openvas_connection_sendf (&connection,
16505  "<get_tasks"
16506  " schedules_only=\"1\""
16507  " details=\"0\"/>")
16508  == -1)
16509  {
16510  openvas_connection_close (&connection);
16511  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16512  return gsad_message (credentials,
16513  "Internal error", __FUNCTION__, __LINE__,
16514  "An internal error occurred while creating a new override. "
16515  "No new override was created. "
16516  "Diagnostics: Failure to send command to manager daemon.",
16517  "/omp?cmd=get_overrides", response_data);
16518  }
16519 
16520  if (read_string_c (&connection, &xml))
16521  {
16522  g_string_free (xml, TRUE);
16523  openvas_connection_close (&connection);
16524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16525  return gsad_message (credentials,
16526  "Internal error", __FUNCTION__, __LINE__,
16527  "An internal error occurred while creating a new override. "
16528  "No new override was created. "
16529  "Diagnostics: Failure to receive response from manager daemon.",
16530  "/omp?cmd=get_overrides", response_data);
16531  }
16532 
16533  g_string_append (xml, "</new_override>");
16534  openvas_connection_close (&connection);
16535  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
16536  response_data);
16537  }
16538 
16539  if (openvas_connection_sendf (&connection,
16540  "<get_results"
16541  " result_id=\"%s\""
16542  " task_id=\"%s\""
16543  " notes_details=\"1\""
16544  " notes=\"1\""
16545  " overrides_details=\"1\""
16546  " overrides=\"1\""
16547  " result_hosts_only=\"1\"/>",
16548  result_id,
16549  task_id)
16550  == -1)
16551  {
16552  openvas_connection_close (&connection);
16553  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16554  return gsad_message (credentials,
16555  "Internal error", __FUNCTION__, __LINE__,
16556  "An internal error occurred while creating a new override. "
16557  "No new override was created. "
16558  "Diagnostics: Failure to send command to manager daemon.",
16559  "/omp?cmd=get_overrides", response_data);
16560  }
16561 
16562  xml = g_string_new ("");
16563 
16564  xml_string_append (xml,
16565  "<new_override>"
16566  "<nvt id=\"%s\"/>"
16567  "<hosts>%s</hosts>"
16568  "<port>%s</port>"
16569  "<severity>%s</severity>"
16570  "<task id=\"%s\">"
16571  "<name>%s</name>"
16572  "</task>"
16573  "<result id=\"%s\"/>"
16574  "<next>%s</next>"
16575  /* Passthroughs. */
16576  "<report id=\"%s\"/>"
16577  "<first_result>%s</first_result>"
16578  "<max_results>%s</max_results>"
16579  "<sort_field>%s</sort_field>"
16580  "<sort_order>%s</sort_order>"
16581  "<levels>%s</levels>"
16582  "<autofp>%s</autofp>"
16583  "<notes>%s</notes>"
16584  "<overrides>%s</overrides>"
16585  "<result_hosts_only>%s</result_hosts_only>"
16586  "<search_phrase>%s</search_phrase>",
16587  oid,
16588  hosts,
16589  /* port is NULL for CVE scan results. */
16590  port ? port : "",
16591  severity,
16592  task_id,
16593  task_name,
16594  result_id,
16595  next ? next : "",
16596  report_id,
16597  first_result,
16598  max_results,
16599  sort_field,
16600  sort_order,
16601  levels,
16602  autofp,
16603  notes,
16604  overrides,
16605  result_hosts_only,
16606  search_phrase);
16607 
16608  if (extra_xml)
16609  g_string_append (xml, extra_xml);
16610 
16611  if (read_string_c (&connection, &xml))
16612  {
16613  g_string_free (xml, TRUE);
16614  openvas_connection_close (&connection);
16615  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16616  return gsad_message (credentials,
16617  "Internal error", __FUNCTION__, __LINE__,
16618  "An internal error occurred while creating a new override. "
16619  "It is unclear whether the override has been created or not. "
16620  "Diagnostics: Failure to receive response from manager daemon.",
16621  "/omp?cmd=get_overrides", response_data);
16622  }
16623 
16624  /* Cleanup, and return transformed XML. */
16625 
16626  g_string_append (xml, "</new_override>");
16627  openvas_connection_close (&connection);
16628  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
16629  response_data);
16630 }
16631 
16641 char *
16642 new_override_omp (credentials_t *credentials, params_t *params,
16643  cmd_response_data_t* response_data)
16644 {
16645  return new_override (credentials, params, NULL, response_data);
16646 }
16647 
16657 char *
16659  cmd_response_data_t* response_data)
16660 {
16661  char *ret;
16662  gchar *response;
16663  const char *no_redirect;
16664  const char *oid, *severity, *custom_severity, *new_severity, *port, *hosts;
16665  const char *text, *task_id, *override_result_id;
16666  /* For get_report. */
16667  const char *active, *days;
16668  entity_t entity;
16669 
16670  no_redirect = params_value (params, "no_redirect");
16671  oid = params_value (params, "oid");
16672  CHECK_PARAM_INVALID (oid, "Create Override", "new_override");
16673 
16674  if (params_valid (params, "severity"))
16675  severity = params_value (params, "severity");
16676  else if (params_given (params, "severity")
16677  && strcmp (params_original_value (params, "severity"), ""))
16678  severity = NULL;
16679  else
16680  severity = "";
16681  CHECK_PARAM_INVALID (severity, "Create Override", "new_override");
16682 
16683  custom_severity = params_value (params, "custom_severity");
16684  CHECK_PARAM_INVALID (custom_severity, "Create Override", "new_override");
16685 
16686  if (custom_severity != NULL && strcmp (custom_severity, "0"))
16687  {
16688  if (params_valid (params, "new_severity"))
16689  new_severity = params_value (params, "new_severity");
16690  else if (params_original_value (params, "new_severity") == NULL
16691  || strcmp (params_original_value (params, "new_severity"), ""))
16692  new_severity = NULL;
16693  else
16694  new_severity = "";
16695  CHECK_PARAM_INVALID (new_severity, "Create Override", "new_override");
16696  }
16697  else
16698  {
16699  if (params_valid (params, "new_severity_from_list"))
16700  new_severity = params_value (params, "new_severity_from_list");
16701  else if (params_original_value (params, "new_severity_from_list") == NULL
16702  || strcmp (params_original_value (params,
16703  "new_severity_from_list"),
16704  ""))
16705  new_severity = NULL;
16706  else
16707  new_severity = "";
16708  CHECK_PARAM_INVALID (new_severity, "Create Override", "new_override");
16709  }
16710 
16711  port = params_value (params, "port");
16712  if (port == NULL)
16713  port = "";
16714  if (strcmp (port, "--") == 0)
16715  {
16716  int num = -1;
16717 
16718  port = params_value (params, "port_manual");
16719  if (port)
16720  num = atoi (port);
16721  if (num < 0 || num > 65535)
16722  port = NULL;
16723  }
16724  CHECK_PARAM_INVALID (port, "Create Override", "new_override");
16725 
16726  if (params_valid (params, "hosts"))
16727  {
16728  hosts = params_value (params, "hosts");
16729  if (strcmp (hosts, "--") == 0)
16730  {
16731  if (params_valid (params, "hosts_manual"))
16732  hosts = params_value (params, "hosts_manual");
16733  else if (params_given (params, "hosts_manual")
16734  && strcmp (params_original_value (params, "hosts_manual"),
16735  ""))
16736  hosts = NULL;
16737  else
16738  hosts = "";
16739  }
16740  }
16741  else if (strcmp (params_original_value (params, "hosts"), ""))
16742  hosts = NULL;
16743  else
16744  hosts = "";
16745  CHECK_PARAM_INVALID (hosts, "Create Override", "new_override");
16746 
16747  if (params_valid (params, "override_task_id"))
16748  {
16749  task_id = params_value (params, "override_task_id");
16750  if (task_id && (strcmp (task_id, "0") == 0))
16751  task_id = params_value (params, "override_task_uuid");
16752  }
16753  else
16754  task_id = "";
16755 
16756  active = params_value (params, "active");
16757  CHECK_PARAM_INVALID (active, "Create Override", "new_override");
16758 
16759  text = params_value (params, "text");
16760  days = params_value (params, "days");
16761 
16762  override_result_id = params_value (params, "override_result_id");
16763  if (override_result_id && (strcmp (override_result_id, "0") == 0))
16764  override_result_id = params_value (params, "override_result_uuid");
16765 
16766  response = NULL;
16767  entity = NULL;
16768  switch (ompf (credentials,
16769  &response,
16770  &entity,
16771  response_data,
16772  "<create_override>"
16773  "<active>%s</active>"
16774  "<nvt oid=\"%s\"/>"
16775  "<hosts>%s</hosts>"
16776  "<port>%s</port>"
16777  "<severity>%s</severity>"
16778  "<new_severity>%s</new_severity>"
16779  "<text>%s</text>"
16780  "<task id=\"%s\"/>"
16781  "<result id=\"%s\"/>"
16782  "</create_override>",
16783  strcmp (active, "1")
16784  ? active
16785  : (days ? days : "-1"),
16786  oid,
16787  hosts,
16788  port,
16789  severity,
16790  new_severity,
16791  text ? text : "",
16792  task_id,
16793  override_result_id))
16794  {
16795  case 0:
16796  case -1:
16797  break;
16798  case 1:
16799  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16800  return gsad_message (credentials,
16801  "Internal error", __FUNCTION__, __LINE__,
16802  "An internal error occurred while creating a new override. "
16803  "No new override was created. "
16804  "Diagnostics: Failure to send command to manager daemon.",
16805  "/omp?cmd=get_overrides", response_data);
16806  case 2:
16807  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16808  return gsad_message (credentials,
16809  "Internal error", __FUNCTION__, __LINE__,
16810  "An internal error occurred while creating a new override. "
16811  "It is unclear whether the override has been created or not. "
16812  "Diagnostics: Failure to receive response from manager daemon.",
16813  "/omp?cmd=get_overrides", response_data);
16814  default:
16815  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16816  return gsad_message (credentials,
16817  "Internal error", __FUNCTION__, __LINE__,
16818  "An internal error occurred while creating a new override. "
16819  "It is unclear whether the override has been created or not. "
16820  "Diagnostics: Internal Error.",
16821  "/omp?cmd=get_overrides", response_data);
16822  }
16823 
16824  if (entity_attribute (entity, "id"))
16825  params_add (params, "override_id", entity_attribute (entity, "id"));
16826  ret = response_from_entity (credentials, params, entity,
16827  (no_redirect && strcmp (no_redirect, "0")),
16828  NULL, "get_overrides",
16829  NULL, "new_override",
16830  "Create Override", response_data);
16831  free_entity (entity);
16832  g_free (response);
16833  return ret;
16834 }
16835 
16845 char *
16847  cmd_response_data_t* response_data)
16848 {
16849  return delete_resource ("override", credentials, params, 0, NULL,
16850  response_data);
16851 }
16852 
16862 char *
16864  cmd_response_data_t* response_data)
16865 {
16866  return delete_resource ("override", credentials, params, 1, "get_trash",
16867  response_data);
16868 }
16869 
16880 char *
16881 edit_override (credentials_t *credentials, params_t *params,
16882  const char *extra_xml, cmd_response_data_t* response_data)
16883 {
16884  GString *xml;
16885  openvas_connection_t connection;
16886  gchar *html;
16887  const char *override_id;
16888 
16889  override_id = params_value (params, "override_id");
16890 
16891  switch (manager_connect (credentials, &connection, &html,
16892  response_data))
16893  {
16894  case 0:
16895  break;
16896  case -1:
16897  if (html)
16898  return html;
16899  /* Fall through. */
16900  default:
16901  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16902  return gsad_message (credentials,
16903  "Internal error", __FUNCTION__, __LINE__,
16904  "An internal error occurred while editing an override. "
16905  "The override remains as it was. "
16906  "Diagnostics: Failure to connect to manager daemon.",
16907  "/omp?cmd=get_overrides", response_data);
16908  }
16909 
16910  if (openvas_connection_sendf (&connection,
16911  "<get_overrides"
16912  " override_id=\"%s\""
16913  " details=\"1\""
16914  " result=\"1\"/>",
16915  override_id)
16916  == -1)
16917  {
16918  openvas_connection_close (&connection);
16919  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16920  return gsad_message (credentials,
16921  "Internal error", __FUNCTION__, __LINE__,
16922  "An internal error occurred while editing an override. "
16923  "The override remains as it was. "
16924  "Diagnostics: Failure to send command to manager daemon.",
16925  "/omp?cmd=get_overrides", response_data);
16926  }
16927 
16928  xml = g_string_new ("");
16929 
16930  xml_string_append (xml, "<edit_override>");
16931 
16932  if (extra_xml)
16933  g_string_append (xml, extra_xml);
16934 
16935  if (read_string_c (&connection, &xml))
16936  {
16937  g_string_free (xml, TRUE);
16938  openvas_connection_close (&connection);
16939  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
16940  return gsad_message (credentials,
16941  "Internal error", __FUNCTION__, __LINE__,
16942  "An internal error occurred while editing an override. "
16943  "The override remains as it was. "
16944  "Diagnostics: Failure to receive response from manager daemon.",
16945  "/omp?cmd=get_overrides", response_data);
16946  }
16947 
16948  /* Cleanup, and return transformed XML. */
16949 
16950  g_string_append (xml, "</edit_override>");
16951  openvas_connection_close (&connection);
16952  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
16953  response_data);
16954 }
16955 
16965 char *
16967  cmd_response_data_t* response_data)
16968 {
16969  return edit_override (credentials, params, NULL, response_data);
16970 }
16971 
16981 char *
16982 save_override_omp (credentials_t * credentials, params_t *params,
16983  cmd_response_data_t* response_data)
16984 {
16985  gchar *response;
16986  entity_t entity;
16987  const char *no_redirect, *override_id, *text, *hosts, *port;
16988  const char *severity, *custom_severity, *new_severity;
16989  const char *override_task_id, *override_result_id, *active, *days;
16990  char *ret;
16991 
16992  no_redirect = params_value (params, "no_redirect");
16993  override_id = params_value (params, "override_id");
16994 
16995  text = params_value (params, "text");
16996  if (text == NULL)
16997  params_given (params, "text") || (text = "");
16998 
16999  if (params_valid (params, "hosts"))
17000  hosts = params_value (params, "hosts");
17001  else if (strcmp (params_original_value (params, "hosts"), ""))
17002  hosts = NULL;
17003  else
17004  hosts = "";
17005 
17006  if (params_valid (params, "port"))
17007  port = params_value (params, "port");
17008  else if (strcmp (params_original_value (params, "port"), ""))
17009  port = NULL;
17010  else
17011  port = "";
17012 
17013  if (params_valid (params, "severity"))
17014  severity = params_value (params, "severity");
17015  else if (strcmp (params_original_value (params, "severity"), ""))
17016  severity = NULL;
17017  else
17018  severity = "";
17019 
17020  custom_severity = params_value (params, "custom_severity");
17021  if (custom_severity && strcmp (custom_severity, "0") != 0)
17022  new_severity = params_value (params, "new_severity");
17023  else
17024  new_severity = params_value (params, "new_severity_from_list");
17025 
17026  override_task_id = params_value (params, "override_task_id");
17027  override_result_id = params_value (params, "override_result_id");
17028 
17029  active = params_value (params, "active");
17030  days = params_value (params, "days");
17031 
17032  CHECK_PARAM_INVALID (override_task_id, "Save Override", "edit_override");
17033  CHECK_PARAM_INVALID (override_result_id, "Save Override", "edit_override");
17034  CHECK_PARAM_INVALID (active, "Save Override", "edit_override");
17035  CHECK_PARAM_INVALID (override_id, "Save Override", "edit_override");
17036  CHECK_PARAM_INVALID (text, "Save Override", "edit_override");
17037  CHECK_PARAM_INVALID (hosts, "Save Override", "edit_override");
17038  CHECK_PARAM_INVALID (port, "Save Override", "edit_override");
17039  CHECK_PARAM_INVALID (severity, "Save Override", "edit_override");
17040  CHECK_PARAM_INVALID (new_severity, "Save Override", "edit_override");
17041  CHECK_PARAM_INVALID (days, "Save Override", "edit_override");
17042 
17043  response = NULL;
17044  entity = NULL;
17045  switch (ompf (credentials,
17046  &response,
17047  &entity,
17048  response_data,
17049  "<modify_override override_id=\"%s\">"
17050  "<active>%s</active>"
17051  "<hosts>%s</hosts>"
17052  "<port>%s</port>"
17053  "<severity>%s</severity>"
17054  "<new_severity>%s</new_severity>"
17055  "<text>%s</text>"
17056  "<task id=\"%s\"/>"
17057  "<result id=\"%s\"/>"
17058  "</modify_override>",
17059  override_id,
17060  strcmp (active, "1")
17061  ? active
17062  : (days ? days : "-1"),
17063  hosts ? hosts : "",
17064  port ? port : "",
17065  severity ? severity : "",
17066  new_severity,
17067  text ? text : "",
17068  override_task_id,
17069  override_result_id))
17070  {
17071  case 0:
17072  case -1:
17073  break;
17074  case 1:
17075  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17076  return gsad_message (credentials,
17077  "Internal error", __FUNCTION__, __LINE__,
17078  "An internal error occurred while saving a override. "
17079  "The override remains the same. "
17080  "Diagnostics: Failure to send command to manager daemon.",
17081  "/omp?cmd=get_overrides", response_data);
17082  case 2:
17083  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17084  return gsad_message (credentials,
17085  "Internal error", __FUNCTION__, __LINE__,
17086  "An internal error occurred while saving a override. "
17087  "It is unclear whether the override has been saved or not. "
17088  "Diagnostics: Failure to receive response from manager daemon.",
17089  "/omp?cmd=get_overrides", response_data);
17090  default:
17091  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17092  return gsad_message (credentials,
17093  "Internal error", __FUNCTION__, __LINE__,
17094  "An internal error occurred while saving a override. "
17095  "It is unclear whether the override has been saved or not. "
17096  "Diagnostics: Internal Error.",
17097  "/omp?cmd=get_overrides", response_data);
17098  }
17099 
17100  ret = response_from_entity (credentials, params, entity,
17101  (no_redirect && strcmp (no_redirect, "0")),
17102  NULL, "get_overrides",
17103  NULL, "edit_override",
17104  "Save Override", response_data);
17105 
17106  free_entity (entity);
17107  g_free (response);
17108  return ret;
17109 }
17110 
17111 
17112 /* Scanners. */
17113 
17124 static char *
17125 get_scanners (credentials_t *credentials, params_t *params,
17126  const char *extra_xml, cmd_response_data_t* response_data)
17127 {
17128  return get_many ("scanner", credentials, params, extra_xml, NULL,
17129  response_data);
17130 }
17131 
17141 char *
17142 get_scanners_omp (credentials_t * credentials, params_t *params,
17143  cmd_response_data_t* response_data)
17144 {
17145  return get_scanners (credentials, params, NULL, response_data);
17146 }
17147 
17158 static char *
17159 get_scanner (credentials_t * credentials, params_t *params,
17160  const char *extra_xml, cmd_response_data_t* response_data)
17161 {
17162  return get_one ("scanner", credentials, params, extra_xml, NULL,
17163  response_data);
17164 }
17165 
17175 char *
17176 get_scanner_omp (credentials_t * credentials, params_t *params,
17177  cmd_response_data_t* response_data)
17178 {
17179  return get_scanner (credentials, params, NULL, response_data);
17180 }
17181 
17194 char *
17196  enum content_type * content_type,
17197  char **content_disposition, gsize *content_length,
17198  cmd_response_data_t* response_data)
17199 {
17200  return export_resource ("scanner", credentials, params, content_type,
17201  content_disposition, content_length, response_data);
17202 }
17203 
17216 char *
17218  enum content_type * content_type,
17219  char **content_disposition, gsize *content_length,
17220  cmd_response_data_t* response_data)
17221 {
17222  return export_many ("scanner", credentials, params, content_type,
17223  content_disposition, content_length, response_data);
17224 }
17225 
17236 static char *
17237 new_scanner (credentials_t *credentials, params_t *params,
17238  const char *extra_xml, cmd_response_data_t* response_data)
17239 {
17240  openvas_connection_t connection;
17241  gchar *html;
17242  GString *xml;
17243 
17244  switch (manager_connect (credentials, &connection, &html,
17245  response_data))
17246  {
17247  case 0:
17248  break;
17249  case -1:
17250  if (html)
17251  return html;
17252  /* Fall through. */
17253  default:
17254  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17255  return gsad_message (credentials,
17256  "Internal error", __FUNCTION__, __LINE__,
17257  "An internal error occurred while getting the credentials list. "
17258  "Diagnostics: Failure to connect to manager daemon.",
17259  "/omp?cmd=get_tasks", response_data);
17260  }
17261 
17262  if (openvas_connection_sendf (&connection,
17263  "<get_credentials"
17264  " filter=\"first=1 rows=-1\" />")
17265  == -1)
17266  {
17267  openvas_connection_close (&connection);
17268  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17269  return gsad_message (credentials,
17270  "Internal error", __FUNCTION__, __LINE__,
17271  "An internal error occurred while getting the credentials list. "
17272  "Diagnostics: Failure to send command to manager daemon.",
17273  "/omp?cmd=get_tasks", response_data);
17274  }
17275 
17276  xml = g_string_new ("<new_scanner>");
17277  if (extra_xml)
17278  g_string_append (xml, extra_xml);
17279 
17280  if (read_string_c (&connection, &xml))
17281  {
17282  g_string_free (xml, TRUE);
17283  openvas_connection_close (&connection);
17284  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17285  return gsad_message (credentials,
17286  "Internal error", __FUNCTION__, __LINE__,
17287  "An internal error occurred while getting the credentials list. "
17288  "Diagnostics: Failure to receive response from manager daemon.",
17289  "/omp?cmd=get_tasks", response_data);
17290  }
17291 
17292  g_string_append (xml, "</new_scanner>");
17293  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
17294  response_data);
17295 }
17296 
17306 char *
17307 new_scanner_omp (credentials_t *credentials, params_t *params,
17308  cmd_response_data_t* response_data)
17309 {
17310  return new_scanner (credentials, params, NULL, response_data);
17311 }
17312 
17322 char *
17324  cmd_response_data_t* response_data)
17325 {
17326  gchar *html, *response;
17327  const char *scanner_id, *next;
17328  int ret;
17329  entity_t entity;
17330 
17331  scanner_id = params_value (params, "scanner_id");
17332  CHECK_PARAM (scanner_id, "Verify Scanner", get_scanners);
17333 
17334 
17335  ret = ompf (credentials,
17336  &response,
17337  &entity,
17338  response_data,
17339  "<verify_scanner scanner_id=\"%s\"/>",
17340  scanner_id);
17341 
17342  switch (ret)
17343  {
17344  case 0:
17345  case -1:
17346  break;
17347  case 1:
17348  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17349  return gsad_message (credentials,
17350  "Internal error", __FUNCTION__, __LINE__,
17351  "An internal error occurred while verifying a scanner. "
17352  "The scanner was not verified. "
17353  "Diagnostics: Failure to send command to manager daemon.",
17354  "/omp?cmd=get_scanners", response_data);
17355  case 2:
17356  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17357  return gsad_message (credentials,
17358  "Internal error", __FUNCTION__, __LINE__,
17359  "An internal error occurred while verifying a scanner. "
17360  "It is unclear whether the scanner was verified or not. "
17361  "Diagnostics: Failure to send command to manager daemon.",
17362  "/omp?cmd=get_scanners", response_data);
17363  default:
17364  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17365  return gsad_message (credentials,
17366  "Internal error", __FUNCTION__, __LINE__,
17367  "An internal error occurred while verifying a scanner. "
17368  "It is unclear whether the scanner was verified or not. "
17369  "Diagnostics: Failure to send command to manager daemon.",
17370  "/omp?cmd=get_scanners", response_data);
17371  }
17372 
17373  if (omp_success (entity))
17374  {
17375  html = next_page (credentials, params, response, response_data);
17376  if (html == NULL)
17377  {
17378  free_entity (entity);
17379  g_free (response);
17380  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17381  return gsad_message (credentials,
17382  "Internal error", __FUNCTION__, __LINE__,
17383  "An internal error occurred while verifying a scanner. "
17384  "It is unclear whether the scanner was verified or not. "
17385  "Diagnostics: Failure to receive response from manager daemon.",
17386  "/omp?cmd=get_scanners", response_data);
17387  }
17388  }
17389  else
17390  {
17391  set_http_status_from_entity (entity, response_data);
17392  next = params_value (params, "next");
17393  if (next && !strcmp (next, "get_scanner"))
17394  html = get_scanner (credentials, params, response, response_data);
17395  else
17396  html = get_scanners (credentials, params, response, response_data);
17397  }
17398 
17399  free_entity (entity);
17400  g_free (response);
17401  return html;
17402 }
17403 
17413 char *
17415  cmd_response_data_t* response_data)
17416 {
17417  int ret;
17418  char *html;
17419  gchar *response = NULL;
17420  const char *no_redirect;
17421  const char *name, *comment, *host, *port, *type, *ca_pub, *credential_id;
17422  entity_t entity = NULL;
17423 
17424  no_redirect = params_value (params, "no_redirect");
17425  name = params_value (params, "name");
17426  comment = params_value (params, "comment");
17427  host = params_value (params, "scanner_host");
17428  port = params_value (params, "port");
17429  type = params_value (params, "scanner_type");
17430  ca_pub = params_value (params, "ca_pub");
17431  credential_id = params_value (params, "credential_id");
17432  CHECK_PARAM_INVALID (name, "Create Scanner", "new_scanner");
17433  CHECK_PARAM_INVALID (comment, "Create Scanner", "new_scanner");
17434  CHECK_PARAM_INVALID (host, "Create Scanner", "new_scanner");
17435  CHECK_PARAM_INVALID (port, "Create Scanner", "new_scanner");
17436  CHECK_PARAM_INVALID (type, "Create Scanner", "new_scanner");
17437  if (params_given (params, "ca_pub"))
17438  CHECK_PARAM_INVALID (ca_pub, "Create Scanner", "new_scanner");
17439  CHECK_PARAM_INVALID (credential_id, "Create Scanner", "new_scanner");
17440 
17441  if (ca_pub)
17442  ret = ompf (credentials, &response, &entity, response_data,
17443  "<create_scanner>"
17444  "<name>%s</name><comment>%s</comment>"
17445  "<host>%s</host><port>%s</port><type>%s</type>"
17446  "<ca_pub>%s</ca_pub>"
17447  "<credential id=\"%s\"/>"
17448  "</create_scanner>",
17449  name, comment, host, port, type, ca_pub, credential_id);
17450  else
17451  ret = ompf (credentials, &response, &entity, response_data,
17452  "<create_scanner>"
17453  "<name>%s</name><comment>%s</comment>"
17454  "<host>%s</host><port>%s</port><type>%s</type>"
17455  "<credential id=\"%s\"/>"
17456  "</create_scanner>",
17457  name, comment, host, port, type, credential_id);
17458  switch (ret)
17459  {
17460  case 0:
17461  case -1:
17462  break;
17463  case 1:
17464  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17465  return gsad_message
17466  (credentials, "Internal error", __FUNCTION__, __LINE__,
17467  "An internal error occurred while creating a new scanner. "
17468  "No new scanner was created. "
17469  "Diagnostics: Failure to send command to manager daemon.",
17470  "/omp?cmd=get_scanners", response_data);
17471  case 2:
17472  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17473  return gsad_message
17474  (credentials, "Internal error", __FUNCTION__, __LINE__,
17475  "An internal error occurred while creating a new scanner. "
17476  "It is unclear whether the scanner has been created or not. "
17477  "Diagnostics: Failure to receive response from manager daemon.",
17478  "/omp?cmd=get_scanners", response_data);
17479  default:
17480  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17481  return gsad_message
17482  (credentials, "Internal error", __FUNCTION__, __LINE__,
17483  "An internal error occurred while creating a new scanner. "
17484  "It is unclear whether the scanner has been created or not. "
17485  "Diagnostics: Internal Error.",
17486  "/omp?cmd=get_scanners", response_data);
17487  }
17488 
17489  if (entity_attribute (entity, "id"))
17490  params_add (params, "scanner_id", entity_attribute (entity, "id"));
17491  html = response_from_entity (credentials, params, entity,
17492  (no_redirect && strcmp (no_redirect, "0")),
17493  NULL, "get_scanners",
17494  NULL, "new_scanner",
17495  "Create Scanner", response_data);
17496  free_entity (entity);
17497  g_free (response);
17498  return html;
17499 }
17500 
17510 char *
17512  cmd_response_data_t* response_data)
17513 {
17514  return delete_resource ("scanner", credentials, params, 0, "get_scanners",
17515  response_data);
17516 }
17517 
17527 char *
17529  cmd_response_data_t* response_data)
17530 {
17531  return delete_resource ("scanner", credentials, params, 1, "get_trash",
17532  response_data);
17533 }
17534 
17545 char *
17546 edit_scanner (credentials_t * credentials, params_t *params,
17547  const char *extra_xml, cmd_response_data_t* response_data)
17548 {
17549  GString *xml;
17550  openvas_connection_t connection;
17551  gchar *html;
17552  const char *scanner_id, *next;
17553 
17554  scanner_id = params_value (params, "scanner_id");
17555  next = params_value (params, "next");
17556 
17557  if (scanner_id == NULL)
17558  {
17559  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
17560  return gsad_message (credentials,
17561  "Internal error", __FUNCTION__, __LINE__,
17562  "An internal error occurred while editing a scanner. "
17563  "The scanner remains as it was. "
17564  "Diagnostics: Required parameter was NULL.",
17565  "/omp?cmd=get_tasks", response_data);
17566  }
17567 
17568  if (next == NULL)
17569  next = "get_scanner";
17570 
17571  switch (manager_connect (credentials, &connection, &html,
17572  response_data))
17573  {
17574  case 0:
17575  break;
17576  case -1:
17577  if (html)
17578  return html;
17579  /* Fall through. */
17580  default:
17581  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17582  return gsad_message (credentials,
17583  "Internal error", __FUNCTION__, __LINE__,
17584  "An internal error occurred while editing a scanner. "
17585  "The scanner remains as it was. "
17586  "Diagnostics: Failure to connect to manager daemon.",
17587  "/omp?cmd=get_tasks", response_data);
17588  }
17589 
17590  if (openvas_connection_sendf (&connection,
17591  "<commands>"
17592  "<get_scanners scanner_id=\"%s\" details=\"1\" />"
17593  "<get_credentials filter=\"first=1 rows=-1\" />"
17594  "</commands>",
17595  scanner_id)
17596  == -1)
17597  {
17598  openvas_connection_close (&connection);
17599  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17600  return gsad_message (credentials,
17601  "Internal error", __FUNCTION__, __LINE__,
17602  "An internal error occurred while getting scanner info. "
17603  "Diagnostics: Failure to send command to manager daemon.",
17604  "/omp?cmd=get_tasks", response_data);
17605  }
17606 
17607  xml = g_string_new ("");
17608 
17609  if (extra_xml)
17610  g_string_append (xml, extra_xml);
17611 
17612  g_string_append_printf (xml,
17613  "<edit_scanner>"
17614  "<scanner id=\"%s\"/>"
17615  /* Page that follows. */
17616  "<next>%s</next>",
17617  scanner_id,
17618  next);
17619 
17620  if (read_string_c (&connection, &xml))
17621  {
17622  g_string_free (xml, TRUE);
17623  openvas_connection_close (&connection);
17624  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17625  return gsad_message (credentials,
17626  "Internal error", __FUNCTION__, __LINE__,
17627  "An internal error occurred while getting scanner info. "
17628  "Diagnostics: Failure to receive response from manager daemon.",
17629  "/omp?cmd=get_tasks", response_data);
17630  }
17631 
17632  /* Cleanup, and return transformed XML. */
17633 
17634  g_string_append (xml, "</edit_scanner>");
17635  openvas_connection_close (&connection);
17636  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
17637  response_data);
17638 }
17639 
17649 char *
17650 edit_scanner_omp (credentials_t * credentials, params_t *params,
17651  cmd_response_data_t* response_data)
17652 {
17653  return edit_scanner (credentials, params, NULL, response_data);
17654 }
17655 
17665 char *
17666 save_scanner_omp (credentials_t * credentials, params_t *params,
17667  cmd_response_data_t* response_data)
17668 {
17669  gchar *response = NULL;
17670  entity_t entity = NULL;
17671  const char *no_redirect;
17672  const char *scanner_id, *name, *comment, *port, *host, *type, *ca_pub;
17673  const char *credential_id, *which_cert;
17674  char *html;
17675  int ret, in_use;
17676 
17677  no_redirect = params_value (params, "no_redirect");
17678  scanner_id = params_value (params, "scanner_id");
17679  name = params_value (params, "name");
17680  comment = params_value (params, "comment");
17681  host = params_value (params, "scanner_host");
17682  port = params_value (params, "port");
17683  type = params_value (params, "scanner_type");
17684  which_cert = params_value (params, "which_cert");
17685  ca_pub = params_value (params, "ca_pub");
17686  credential_id = params_value (params, "credential_id");
17687  CHECK_PARAM_INVALID (scanner_id, "Edit Scanner", "edit_scanner");
17688  CHECK_PARAM_INVALID (name, "Edit Scanner", "edit_scanner");
17689  if (params_given (params, "scanner_host") == 0)
17690  in_use = 1;
17691  else
17692  {
17693  in_use = 0;
17694  CHECK_PARAM_INVALID (host, "Edit Scanner", "edit_scanner");
17695  CHECK_PARAM_INVALID (port, "Edit Scanner", "edit_scanner");
17696  CHECK_PARAM_INVALID (type, "Edit Scanner", "edit_scanner");
17697  }
17698  CHECK_PARAM_INVALID (ca_pub, "Edit Scanner", "edit_scanner");
17699  CHECK_PARAM_INVALID (credential_id, "Edit Scanner", "edit_scanner");
17700  CHECK_PARAM_INVALID (which_cert, "Edit Scanner", "edit_scanner");
17701 
17702  if (strcmp (which_cert, "new") == 0 || strcmp (which_cert, "default") == 0)
17703  {
17704  if (ca_pub == NULL)
17705  ca_pub = "";
17706  if (in_use)
17707  ret = ompf (credentials, &response, &entity, response_data,
17708  "<modify_scanner scanner_id=\"%s\">"
17709  "<name>%s</name>"
17710  "<comment>%s</comment>"
17711  "<ca_pub>%s</ca_pub>"
17712  "<credential id=\"%s\"/>"
17713  "</modify_scanner>",
17714  scanner_id, name, comment ?: "",
17715  strcmp (which_cert, "new") == 0 ? ca_pub : "",
17716  credential_id);
17717  else
17718  ret = ompf (credentials, &response, &entity, response_data,
17719  "<modify_scanner scanner_id=\"%s\">"
17720  "<name>%s</name>"
17721  "<comment>%s</comment>"
17722  "<host>%s</host>"
17723  "<port>%s</port>"
17724  "<type>%s</type>"
17725  "<ca_pub>%s</ca_pub>"
17726  "<credential id=\"%s\"/>"
17727  "</modify_scanner>",
17728  scanner_id, name, comment ?: "", host, port, type,
17729  strcmp (which_cert, "new") == 0 ? ca_pub : "",
17730  credential_id);
17731  }
17732  else
17733  {
17734  /* Using existing CA cert. */
17735  if (in_use)
17736  ret = ompf (credentials, &response, &entity, response_data,
17737  "<modify_scanner scanner_id=\"%s\">"
17738  "<name>%s</name>"
17739  "<comment>%s</comment>"
17740  "<credential id=\"%s\"/>"
17741  "</modify_scanner>",
17742  scanner_id, name, comment ?: "", credential_id);
17743  else
17744  ret = ompf (credentials, &response, &entity, response_data,
17745  "<modify_scanner scanner_id=\"%s\">"
17746  "<name>%s</name>"
17747  "<comment>%s</comment>"
17748  "<host>%s</host>"
17749  "<port>%s</port>"
17750  "<type>%s</type>"
17751  "<credential id=\"%s\"/>"
17752  "</modify_scanner>",
17753  scanner_id, name, comment ?: "", host, port, type,
17754  credential_id);
17755  }
17756 
17757  switch (ret)
17758  {
17759  case 0:
17760  case -1:
17761  break;
17762  case 1:
17763  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17764  return gsad_message
17765  (credentials, "Internal error", __FUNCTION__, __LINE__,
17766  "An internal error occurred while saving a scanner. "
17767  "The scanner remains the same. "
17768  "Diagnostics: Failure to send command to manager daemon.",
17769  "/omp?cmd=get_scanners", response_data);
17770  case 2:
17771  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17772  return gsad_message
17773  (credentials, "Internal error", __FUNCTION__, __LINE__,
17774  "An internal error occurred while saving a scanner. "
17775  "It is unclear whether the scanner has been saved or not. "
17776  "Diagnostics: Failure to receive response from manager daemon.",
17777  "/omp?cmd=get_scanners", response_data);
17778  default:
17779  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
17780  return gsad_message
17781  (credentials, "Internal error", __FUNCTION__, __LINE__,
17782  "An internal error occurred while saving a scanner. "
17783  "It is unclear whether the scanner has been saved or not. "
17784  "Diagnostics: Internal Error.",
17785  "/omp?cmd=get_scanners", response_data);
17786  }
17787 
17788  html = response_from_entity (credentials, params, entity,
17789  (no_redirect && strcmp (no_redirect, "0")),
17790  NULL, "get_scanners",
17791  NULL, "edit_scanner",
17792  "Save Scanner", response_data);
17793 
17794  free_entity (entity);
17795  g_free (response);
17796  return html;
17797 }
17798 
17799 
17800 /* Schedules. */
17801 
17812 static char *
17813 get_schedule (credentials_t * credentials, params_t *params,
17814  const char *extra_xml, cmd_response_data_t* response_data)
17815 {
17816  return get_one ("schedule", credentials, params, extra_xml, "tasks=\"1\"",
17817  response_data);
17818 }
17819 
17829 char *
17830 get_schedule_omp (credentials_t * credentials, params_t *params,
17831  cmd_response_data_t* response_data)
17832 {
17833  return get_schedule (credentials, params, NULL, response_data);
17834 }
17835 
17846 static char *
17847 get_schedules (credentials_t *credentials, params_t *params,
17848  const char *extra_xml, cmd_response_data_t* response_data)
17849 {
17850  return get_many ("schedule", credentials, params, extra_xml, NULL,
17851  response_data);
17852 }
17853 
17863 char *
17864 get_schedules_omp (credentials_t * credentials, params_t *params,
17865  cmd_response_data_t* response_data)
17866 {
17867  return get_schedules (credentials, params, NULL, response_data);
17868 }
17869 
17880 static char *
17881 new_schedule (credentials_t *credentials, params_t *params,
17882  const char *extra_xml, cmd_response_data_t* response_data)
17883 {
17884  GString *xml;
17885  time_t now;
17886  struct tm *now_broken;
17887  xml = g_string_new ("<new_schedule>");
17888  now = time (NULL);
17889  now_broken = localtime (&now);
17890  g_string_append_printf (xml,
17891  "<time>"
17892  "<minute>%s%i</minute>"
17893  "<hour>%s%i</hour>"
17894  "<day_of_month>%s%i</day_of_month>"
17895  "<month>%s%i</month>"
17896  "<year>%i</year>"
17897  "</time>",
17898  (now_broken->tm_min > 9 ? "" : "0"),
17899  now_broken->tm_min,
17900  (now_broken->tm_hour > 9 ? "" : "0"),
17901  now_broken->tm_hour,
17902  (now_broken->tm_mday > 9 ? "" : "0"),
17903  now_broken->tm_mday,
17904  ((now_broken->tm_mon + 1) > 9 ? "" : "0"),
17905  (now_broken->tm_mon + 1),
17906  (now_broken->tm_year + 1900));
17907 
17908  if (extra_xml)
17909  g_string_append (xml, extra_xml);
17910  g_string_append (xml, "</new_schedule>");
17911  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
17912  response_data);
17913 }
17914 
17924 char *
17925 new_schedule_omp (credentials_t *credentials, params_t *params,
17926  cmd_response_data_t* response_data)
17927 {
17928  return new_schedule (credentials, params, NULL, response_data);
17929 }
17930 
17940 char *
17942  cmd_response_data_t* response_data)
17943 {
17944  char *ret;
17945  gchar *response;
17946  const char *no_redirect;
17947  const char *name, *comment, *hour, *minute, *day_of_month, *month, *year;
17948  const char *period, *period_unit, *duration, *duration_unit, *timezone;
17949  entity_t entity;
17950 
17951  no_redirect = params_value (params, "no_redirect");
17952  name = params_value (params, "name");
17953  comment = params_value (params, "comment");
17954  hour = params_value (params, "hour");
17955  minute = params_value (params, "minute");
17956  day_of_month = params_value (params, "day_of_month");
17957  duration = params_value (params, "duration");
17958  duration_unit = params_value (params, "duration_unit");
17959  month = params_value (params, "month");
17960  period = params_value (params, "period");
17961  period_unit = params_value (params, "period_unit");
17962  year = params_value (params, "year");
17963  timezone = params_value (params, "timezone");
17964 
17965  CHECK_PARAM_INVALID (name, "Create Schedule", "new_schedule");
17966  CHECK_PARAM_INVALID (comment, "Create Schedule", "new_schedule");
17967  CHECK_PARAM_INVALID (hour, "Create Schedule", "new_schedule");
17968  CHECK_PARAM_INVALID (minute, "Create Schedule", "new_schedule");
17969  CHECK_PARAM_INVALID (day_of_month, "Create Schedule", "new_schedule");
17970  CHECK_PARAM_INVALID (duration, "Create Schedule", "new_schedule");
17971  CHECK_PARAM_INVALID (duration_unit, "Create Schedule", "new_schedule");
17972  CHECK_PARAM_INVALID (month, "Create Schedule", "new_schedule");
17973  CHECK_PARAM_INVALID (period, "Create Schedule", "new_schedule");
17974  CHECK_PARAM_INVALID (period_unit, "period_unit", "new_schedule");
17975  CHECK_PARAM_INVALID (year, "Create Schedule", "new_schedule");
17976  CHECK_PARAM_INVALID (timezone, "Create Schedule", "new_schedule");
17977 
17978  response = NULL;
17979  entity = NULL;
17980  switch (ompf (credentials,
17981  &response,
17982  &entity,
17983  response_data,
17984  "<create_schedule>"
17985  "<name>%s</name>"
17986  "<comment>%s</comment>"
17987  "<first_time>"
17988  "<hour>%s</hour>"
17989  "<minute>%s</minute>"
17990  "<day_of_month>%s</day_of_month>"
17991  "<month>%s</month>"
17992  "<year>%s</year>"
17993  "</first_time>"
17994  "<period>"
17995  "<unit>%s</unit>"
17996  "%s"
17997  "</period>"
17998  "<duration>"
17999  "<unit>%s</unit>"
18000  "%s"
18001  "</duration>"
18002  "<timezone>%s</timezone>"
18003  "</create_schedule>",
18004  name,
18005  comment,
18006  hour,
18007  minute,
18008  day_of_month,
18009  month,
18010  year,
18011  (strcmp (period_unit, "")
18012  ? period_unit
18013  : "second"),
18014  period,
18015  (strcmp (duration_unit, "")
18016  ? duration_unit
18017  : "second"),
18018  duration,
18019  timezone))
18020  {
18021  case 0:
18022  case -1:
18023  break;
18024  case 1:
18025  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18026  return gsad_message (credentials,
18027  "Internal error", __FUNCTION__, __LINE__,
18028  "An internal error occurred while creating a new schedule. "
18029  "No new schedule was created. "
18030  "Diagnostics: Failure to send command to manager daemon.",
18031  "/omp?cmd=get_schedules", response_data);
18032  case 2:
18033  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18034  return gsad_message (credentials,
18035  "Internal error", __FUNCTION__, __LINE__,
18036  "An internal error occurred while creating a new schedule. "
18037  "It is unclear whether the schedule has been created or not. "
18038  "Diagnostics: Failure to receive response from manager daemon.",
18039  "/omp?cmd=get_schedules", response_data);
18040  default:
18041  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18042  return gsad_message (credentials,
18043  "Internal error", __FUNCTION__, __LINE__,
18044  "An internal error occurred while creating a new schedule. "
18045  "It is unclear whether the schedule has been created or not. "
18046  "Diagnostics: Internal Error.",
18047  "/omp?cmd=get_schedules", response_data);
18048  }
18049 
18050  if (entity_attribute (entity, "id"))
18051  params_add (params, "schedule_id", entity_attribute (entity, "id"));
18052  ret = response_from_entity (credentials, params, entity,
18053  (no_redirect && strcmp (no_redirect, "0")),
18054  NULL, "get_schedules",
18055  NULL, "new_schedule",
18056  "Create Schedule", response_data);
18057  free_entity (entity);
18058  g_free (response);
18059  return ret;
18060 }
18061 
18071 char *
18073  cmd_response_data_t* response_data)
18074 {
18075  return delete_resource ("schedule", credentials, params, 0, "get_schedules",
18076  response_data);
18077 }
18078 
18088 char *
18090  cmd_response_data_t* response_data)
18091 {
18092  GString *xml;
18093  openvas_connection_t connection;
18094  gchar *html;
18095  time_t now;
18096  struct tm *now_broken;
18097  struct tm start_time, end_time;
18098 
18099  const char *slave_id;
18100  const char *start_year, *start_month, *start_day, *start_hour, *start_minute;
18101  const char *end_year, *end_month, *end_day, *end_hour, *end_minute;
18102 
18103  slave_id = params_value (params, "slave_id");
18104 
18105  now = time (NULL);
18106  now_broken = localtime (&now);
18107 
18108  start_year = params_value (params, "start_year");
18109  start_month = params_value (params, "start_month");
18110  start_day = params_value (params, "start_day");
18111  start_hour = params_value (params, "start_hour");
18112  start_minute = params_value (params, "start_minute");
18113 
18114  end_year = params_value (params, "end_year");
18115  end_month = params_value (params, "end_month");
18116  end_day = params_value (params, "end_day");
18117  end_hour = params_value (params, "end_hour");
18118  end_minute = params_value (params, "end_minute");
18119 
18120  start_time.tm_year = start_year ? atoi (start_year) - 1900
18121  : now_broken->tm_year;
18122  start_time.tm_mon = start_month ? atoi (start_month) - 1
18123  : now_broken->tm_mon;
18124  start_time.tm_mday = start_day ? atoi (start_day) : now_broken->tm_mday;
18125  start_time.tm_hour = start_hour ? atoi (start_hour) : now_broken->tm_hour;
18126  start_time.tm_min = start_minute ? atoi (start_minute) : now_broken->tm_min;
18127 
18128  end_time.tm_year = end_year ? atoi (end_year) - 1900
18129  : now_broken->tm_year;
18130  end_time.tm_mon = end_month ? atoi (end_month) - 1
18131  : now_broken->tm_mon;
18132  end_time.tm_mday = end_day ? atoi (end_day) : now_broken->tm_mday;
18133  end_time.tm_hour = end_hour ? atoi (end_hour) : now_broken->tm_hour;
18134  end_time.tm_min = end_minute ? atoi (end_minute) : now_broken->tm_min;
18135 
18136  switch (manager_connect (credentials, &connection, &html,
18137  response_data))
18138  {
18139  case 0:
18140  break;
18141  case -1:
18142  if (html)
18143  return html;
18144  /* Fall through. */
18145  default:
18146  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18147  return gsad_message (credentials,
18148  "Internal error", __FUNCTION__, __LINE__,
18149  "An internal error occurred while getting the system reports. "
18150  "The current list of system reports is not available. "
18151  "Diagnostics: Failure to connect to manager daemon.",
18152  "/omp?cmd=get_tasks", response_data);
18153  }
18154 
18155  xml = g_string_new ("<get_system_reports>");
18156 
18157  g_string_append_printf (xml,
18158  "<slave id=\"%s\"/>",
18159  slave_id ? slave_id : "0");
18160 
18161 
18162  g_string_append_printf (xml,
18163  "<start_time>"
18164  "<minute>%i</minute>"
18165  "<hour>%i</hour>"
18166  "<day_of_month>%i</day_of_month>"
18167  "<month>%i</month>"
18168  "<year>%i</year>"
18169  "</start_time>",
18170  start_time.tm_min,
18171  start_time.tm_hour,
18172  start_time.tm_mday,
18173  start_time.tm_mon + 1,
18174  start_time.tm_year + 1900);
18175 
18176  g_string_append_printf (xml,
18177  "<end_time>"
18178  "<minute>%i</minute>"
18179  "<hour>%i</hour>"
18180  "<day_of_month>%i</day_of_month>"
18181  "<month>%i</month>"
18182  "<year>%i</year>"
18183  "</end_time>",
18184  end_time.tm_min,
18185  end_time.tm_hour,
18186  end_time.tm_mday,
18187  end_time.tm_mon + 1,
18188  end_time.tm_year + 1900);
18189 
18190  /* Get the system reports. */
18191 
18192  if (openvas_connection_sendf (&connection,
18193  "<get_system_reports brief=\"1\" slave_id=\"%s\"/>",
18194  slave_id ? slave_id : "0")
18195  == -1)
18196  {
18197  g_string_free (xml, TRUE);
18198  openvas_connection_close (&connection);
18199  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18200  return gsad_message (credentials,
18201  "Internal error", __FUNCTION__, __LINE__,
18202  "An internal error occurred while getting the system reports. "
18203  "The current list of system reports is not available. "
18204  "Diagnostics: Failure to send command to manager daemon.",
18205  "/omp?cmd=get_tasks", response_data);
18206  }
18207 
18208  if (read_string_c (&connection, &xml))
18209  {
18210  g_string_free (xml, TRUE);
18211  openvas_connection_close (&connection);
18212  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18213  return gsad_message (credentials,
18214  "Internal error", __FUNCTION__, __LINE__,
18215  "An internal error occurred while getting the system reports. "
18216  "The current list of system reports is not available. "
18217  "Diagnostics: Failure to receive response from manager daemon.",
18218  "/omp?cmd=get_tasks", response_data);
18219  }
18220 
18221  if (command_enabled (credentials, "GET_SCANNERS"))
18222  {
18223  /* Get the OMP scanners. */
18224 
18225  if (openvas_connection_sendf (&connection,
18226  "<get_scanners"
18227  " filter=\"sort=name rows=-1 type=4\"/>")
18228  == -1)
18229  {
18230  g_string_free (xml, TRUE);
18231  openvas_connection_close (&connection);
18232  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18233  return gsad_message (credentials,
18234  "Internal error", __FUNCTION__, __LINE__,
18235  "An internal error occurred while getting the system reports. "
18236  "The current list of system reports is not available. "
18237  "Diagnostics: Failure to send command to manager daemon.",
18238  "/omp?cmd=get_tasks", response_data);
18239  }
18240 
18241  if (read_string_c (&connection, &xml))
18242  {
18243  g_string_free (xml, TRUE);
18244  openvas_connection_close (&connection);
18245  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18246  return gsad_message (credentials,
18247  "Internal error", __FUNCTION__, __LINE__,
18248  "An internal error occurred while getting the system reports. "
18249  "The current list of system reports is not available. "
18250  "Diagnostics: Failure to receive response from manager daemon.",
18251  "/omp?cmd=get_tasks", response_data);
18252  }
18253  }
18254 
18255  /* Cleanup, and return transformed XML. */
18256 
18257  g_string_append (xml, "</get_system_reports>");
18258  openvas_connection_close (&connection);
18259  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
18260  response_data);
18261 }
18262 
18275 char *
18276 get_system_report_omp (credentials_t *credentials, const char *url,
18277  params_t *params,
18278  enum content_type *content_type,
18279  gsize *content_length,
18280  cmd_response_data_t* response_data)
18281 {
18282  entity_t entity;
18283  entity_t report_entity;
18284  openvas_connection_t connection;
18285  char name[501];
18286  time_t now;
18287  struct tm *now_broken;
18288  const char *slave_id;
18289  const char *start_year, *start_month, *start_day, *start_hour, *start_minute;
18290  const char *end_year, *end_month, *end_day, *end_hour, *end_minute;
18291  struct tm start_time, end_time;
18292 
18293  *content_length = 0;
18294 
18295  if (url == NULL)
18296  return NULL;
18297 
18298  slave_id = params_value (params, "slave_id");
18299 
18300  now = time (NULL);
18301  now_broken = localtime (&now);
18302 
18303  start_year = params_value (params, "start_year");
18304  start_month = params_value (params, "start_month");
18305  start_day = params_value (params, "start_day");
18306  start_hour = params_value (params, "start_hour");
18307  start_minute = params_value (params, "start_minute");
18308 
18309  end_year = params_value (params, "end_year");
18310  end_month = params_value (params, "end_month");
18311  end_day = params_value (params, "end_day");
18312  end_hour = params_value (params, "end_hour");
18313  end_minute = params_value (params, "end_minute");
18314 
18315  start_time.tm_year = start_year ? atoi (start_year) - 1900
18316  : now_broken->tm_year;
18317  start_time.tm_mon = start_month ? atoi (start_month) - 1
18318  : now_broken->tm_mon;
18319  start_time.tm_mday = start_day ? atoi (start_day) : now_broken->tm_mday;
18320  start_time.tm_hour = start_hour ? atoi (start_hour) : now_broken->tm_hour;
18321  start_time.tm_min = start_minute ? atoi (start_minute) : now_broken->tm_min;
18322  start_time.tm_zone = now_broken->tm_zone;
18323 
18324  end_time.tm_year = end_year ? atoi (end_year) - 1900
18325  : now_broken->tm_year;
18326  end_time.tm_mon = end_month ? atoi (end_month) - 1
18327  : now_broken->tm_mon;
18328  end_time.tm_mday = end_day ? atoi (end_day) : now_broken->tm_mday;
18329  end_time.tm_hour = end_hour ? atoi (end_hour) : now_broken->tm_hour;
18330  end_time.tm_min = end_minute ? atoi (end_minute) : now_broken->tm_min;
18331  end_time.tm_zone = now_broken->tm_zone;
18332 
18333  /* fan/report.png */
18334  if (sscanf (url, "%500[^ /]./report.png", name) == 1)
18335  {
18336  gchar *omp_command;
18337 
18338  if (manager_connect (credentials, &connection, NULL,
18339  response_data))
18340  return NULL;
18341 
18342  gchar *start_time_str, *end_time_str;
18343 
18344 
18345  start_time_str
18346  = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:00",
18347  start_time.tm_year + 1900,
18348  start_time.tm_mon + 1,
18349  start_time.tm_mday,
18350  start_time.tm_hour,
18351  start_time.tm_min);
18352 
18353  end_time_str
18354  = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:00",
18355  end_time.tm_year + 1900,
18356  end_time.tm_mon + 1,
18357  end_time.tm_mday,
18358  end_time.tm_hour,
18359  end_time.tm_min);
18360 
18361  omp_command
18362  = g_markup_printf_escaped ("<get_system_reports"
18363  " name=\"%s\""
18364  " start_time=\"%s\""
18365  " end_time=\"%s\""
18366  " slave_id=\"%s\"/>",
18367  name,
18368  start_time_str,
18369  end_time_str,
18370  slave_id ? slave_id : "0");
18371  g_free (start_time_str);
18372  g_free (end_time_str);
18373 
18374  if (openvas_connection_sendf (&connection,
18375  "%s",
18376  omp_command)
18377  == -1)
18378  {
18379  g_free (omp_command);
18380  openvas_connection_close (&connection);
18381  return NULL;
18382  }
18383  g_free (omp_command);
18384 
18385  entity = NULL;
18386  if (read_entity_c (&connection, &entity))
18387  {
18388  openvas_connection_close (&connection);
18389  return NULL;
18390  }
18391 
18392  report_entity = entity_child (entity, "system_report");
18393  if (report_entity == NULL)
18394  {
18395  free_entity (entity);
18396  openvas_connection_close (&connection);
18397  return NULL;
18398  }
18399 
18400  report_entity = entity_child (report_entity, "report");
18401  if (report_entity == NULL)
18402  {
18403  free_entity (entity);
18404  openvas_connection_close (&connection);
18405  return NULL;
18406  }
18407  else
18408  {
18409  char *content_64 = entity_text (report_entity);
18410  char *content = NULL;
18411 
18412  if (content_64 && strlen (content_64))
18413  {
18414  content = (char *) g_base64_decode (content_64,
18415  content_length);
18416 
18417 #if 1
18418  *content_type = GSAD_CONTENT_TYPE_IMAGE_PNG;
18419  //*content_disposition = g_strdup_printf ("attachment; filename=\"xxx.png\"");
18420 #else
18421  g_free (content);
18422  content = g_strdup ("helo");
18423 #endif
18424  }
18425 
18426  free_entity (entity);
18427  openvas_connection_close (&connection);
18428  return content;
18429  }
18430  }
18431 
18432  return NULL;
18433 }
18434 
18445 static char *
18446 get_report_format (credentials_t * credentials, params_t *params,
18447  const char *extra_xml, cmd_response_data_t* response_data)
18448 {
18449  return get_one ("report_format", credentials, params, extra_xml,
18450  "alerts =\"1\" params=\"1\"", response_data);
18451 }
18452 
18462 char *
18464  cmd_response_data_t* response_data)
18465 {
18466  return get_report_format (credentials, params, NULL, response_data);
18467 }
18468 
18479 static char *
18480 get_report_formats (credentials_t * credentials, params_t *params,
18481  const char *extra_xml, cmd_response_data_t* response_data)
18482 {
18483  return get_many ("report_format", credentials, params, extra_xml, NULL,
18484  response_data);
18485 }
18486 
18496 char *
18498  cmd_response_data_t* response_data)
18499 {
18500  return get_report_formats (credentials, params, NULL, response_data);
18501 }
18502 
18513 static char *
18514 new_report_format (credentials_t *credentials, params_t *params,
18515  const char *extra_xml, cmd_response_data_t* response_data)
18516 {
18517  GString *xml;
18518  xml = g_string_new ("<new_report_format>");
18519  if (extra_xml)
18520  g_string_append (xml, extra_xml);
18521  g_string_append (xml, "</new_report_format>");
18522  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
18523  response_data);
18524 }
18525 
18535 char *
18537  cmd_response_data_t* response_data)
18538 {
18539  return new_report_format (credentials, params, NULL, response_data);
18540 }
18541 
18551 char *
18553  cmd_response_data_t* response_data)
18554 {
18555  return delete_resource ("report_format", credentials, params, 0,
18556  "get_report_formats", response_data);
18557 }
18558 
18569 static char *
18570 edit_report_format (credentials_t * credentials, params_t *params,
18571  const char *extra_xml, cmd_response_data_t* response_data)
18572 {
18573  gchar *all_rfs_response, *response, *ext_extra_xml;
18574 
18575  if (simple_ompf ("getting Report Formats",
18576  credentials, &all_rfs_response, response_data,
18577  "<get_report_formats"
18578  " filter=\"rows=-1\"/>"))
18579  {
18580  return all_rfs_response;
18581  }
18582 
18583  ext_extra_xml = g_strdup_printf ("%s"
18584  "<all_formats>%s</all_formats>",
18585  extra_xml ? extra_xml : "",
18586  all_rfs_response);
18587  g_free (all_rfs_response);
18588 
18589  response = edit_resource ("report_format", credentials, params, NULL,
18590  ext_extra_xml, response_data);
18591  g_free (ext_extra_xml);
18592  return response;
18593 }
18594 
18604 char *
18606  cmd_response_data_t* response_data)
18607 {
18608  return edit_report_format (credentials, params, NULL, response_data);
18609 }
18610 
18620 char *
18622  cmd_response_data_t* response_data)
18623 {
18624  const char* no_redirect;
18625  gchar *command, *html, *response;
18626  entity_t entity;
18627  int ret;
18628 
18629  no_redirect = params_value (params, "no_redirect");
18630 
18631  /* Create the report format. */
18632 
18633  response = NULL;
18634  entity = NULL;
18635  command = g_strdup_printf ("<create_report_format>"
18636  "%s"
18637  "</create_report_format>",
18638  params_value (params, "xml_file"));
18639  ret = omp (credentials, &response, &entity, response_data, command);
18640  g_free (command);
18641  switch (ret)
18642  {
18643  case 0:
18644  case -1:
18645  break;
18646  case 1:
18647  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18648  return gsad_message (credentials,
18649  "Internal error", __FUNCTION__, __LINE__,
18650  "An internal error occurred while importing a report format. "
18651  "The schedule remains the same. "
18652  "Diagnostics: Failure to send command to manager daemon.",
18653  "/omp?cmd=get_report_formats", response_data);
18654  case 2:
18655  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18656  return gsad_message (credentials,
18657  "Internal error", __FUNCTION__, __LINE__,
18658  "An internal error occurred while importing a report format. "
18659  "It is unclear whether the schedule has been saved or not. "
18660  "Diagnostics: Failure to receive response from manager daemon.",
18661  "/omp?cmd=get_report_formats", response_data);
18662  default:
18663  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18664  return gsad_message (credentials,
18665  "Internal error", __FUNCTION__, __LINE__,
18666  "An internal error occurred while importing a report format. "
18667  "It is unclear whether the schedule has been saved or not. "
18668  "Diagnostics: Internal Error.",
18669  "/omp?cmd=get_report_formats", response_data);
18670  }
18671 
18672  /* Cleanup, and return transformed XML. */
18673 
18674  if (entity_attribute (entity, "id"))
18675  params_add (params, "report_format_id", entity_attribute (entity, "id"));
18676  html = response_from_entity (credentials, params, entity,
18677  (no_redirect && strcmp (no_redirect, "0")),
18678  NULL, "get_report_formats",
18679  NULL, "new_report_format",
18680  "Create Report Format", response_data);
18681 
18682  free_entity (entity);
18683  g_free (response);
18684  return html;
18685 }
18686 
18696 char *
18698  cmd_response_data_t* response_data)
18699 {
18700  int ret;
18701  gchar *html, *response;
18702  params_t *preferences, *id_list_params, *include_id_lists;
18703  const char *no_redirect, *report_format_id, *name, *summary, *enable;
18704  entity_t entity;
18705 
18706  no_redirect = params_value (params, "no_redirect");
18707  report_format_id = params_value (params, "report_format_id");
18708  name = params_value (params, "name");
18709  summary = params_value (params, "summary");
18710  enable = params_value (params, "enable");
18711 
18712  CHECK_PARAM_INVALID (report_format_id, "Save Report Format",
18713  "edit_report_format");
18714  CHECK_PARAM_INVALID (name, "Save Report Format", "edit_report_format");
18715  CHECK_PARAM_INVALID (summary, "Save Report Format", "edit_report_format");
18716  CHECK_PARAM_INVALID (enable, "Save Report Format", "edit_report_format");
18717 
18718  id_list_params = params_values (params, "id_list:");
18719  include_id_lists = params_values (params, "include_id_list:");
18720  if (include_id_lists)
18721  {
18722  GHashTable *id_lists;
18723  param_t *param;
18724  gchar *param_name, *pref_name, *value, *old_values, *new_values;
18725  params_iterator_t iter;
18726  GHashTableIter hash_table_iter;
18727 
18728  id_lists = g_hash_table_new_full (g_str_hash, g_str_equal,
18729  g_free, g_free);
18730 
18731  params_iterator_init (&iter, include_id_lists);
18732  while (params_iterator_next (&iter, &param_name, &param))
18733  {
18734  if (param->value == NULL)
18735  continue;
18736 
18737  g_hash_table_insert (id_lists, g_strdup (param_name), g_strdup (""));
18738  }
18739 
18740  params_iterator_init (&iter, id_list_params);
18741  while (params_iterator_next (&iter, &param_name, &param))
18742  {
18743  if (param->value == NULL)
18744  continue;
18745 
18746  gchar *colon_pos = strchr (param->value, ':');
18747 
18748  pref_name = g_strndup (param->value, colon_pos - param->value);
18749  value = g_strdup (colon_pos + 1);
18750 
18751  old_values = g_hash_table_lookup (id_lists, pref_name);
18752 
18753  if (old_values && strcmp (old_values, ""))
18754  {
18755  new_values = g_strdup_printf ("%s,%s", old_values, value);
18756  g_hash_table_insert (id_lists, pref_name, new_values);
18757  g_free (value);
18758  }
18759  else if (old_values)
18760  {
18761  g_hash_table_insert (id_lists, pref_name, value);
18762  }
18763  }
18764 
18765  g_hash_table_iter_init (&hash_table_iter, id_lists);
18766  while (g_hash_table_iter_next (&hash_table_iter,
18767  (void**)&pref_name, (void**)&value))
18768  {
18769  gchar *value_64;
18770 
18771  value_64 = strlen (value)
18772  ? g_base64_encode ((guchar *) value, strlen (value))
18773  : g_strdup ("");
18774 
18775  response = NULL;
18776  entity = NULL;
18777  ret = ompf (credentials,
18778  &response,
18779  &entity,
18780  response_data,
18781  "<modify_report_format"
18782  " report_format_id=\"%s\">"
18783  "<param>"
18784  "<name>%s</name>"
18785  "<value>%s</value>"
18786  "</param>"
18787  "</modify_report_format>",
18788  report_format_id,
18789  pref_name,
18790  value_64);
18791  g_free (value_64);
18792  switch (ret)
18793  {
18794  case 0:
18795  break;
18796  case 1:
18797  response_data->http_status_code
18798  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18799  return gsad_message (credentials,
18800  "Internal error", __FUNCTION__, __LINE__,
18801  "An internal error occurred while saving a Report Format. "
18802  "The Report Format was not saved. "
18803  "Diagnostics: Failure to send command to manager daemon.",
18804  "/omp?cmd=get_report_formats",
18805  response_data);
18806  case 2:
18807  response_data->http_status_code
18808  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18809  return gsad_message (credentials,
18810  "Internal error", __FUNCTION__, __LINE__,
18811  "An internal error occurred while saving a Report Format. "
18812  "It is unclear whether the Report Format has been saved or not. "
18813  "Diagnostics: Failure to receive response from manager daemon.",
18814  "/omp?cmd=get_report_formats",
18815  response_data);
18816  case -1:
18817  default:
18818  response_data->http_status_code
18819  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18820  return gsad_message (credentials,
18821  "Internal error", __FUNCTION__, __LINE__,
18822  "An internal error occurred while saving a Report Format. "
18823  "It is unclear whether the Report Format has been saved or not. "
18824  "Diagnostics: Internal Error.",
18825  "/omp?cmd=get_report_formats",
18826  response_data);
18827  }
18828 
18829  /* TODO Check if succeeded. response_from_entity_if_failed? */
18830  }
18831  }
18832 
18833  /* Modify the Report Format. */
18834 
18835  preferences = params_values (params, "preference:");
18836  if (preferences)
18837  {
18838  param_t *param;
18839  gchar *param_name;
18840  params_iterator_t iter;
18841 
18842  /* The naming is a bit subtle here, because the HTTP request
18843  * parameters are called "param"s and so are the OMP report format
18844  * parameters. */
18845 
18846  params_iterator_init (&iter, preferences);
18847  while (params_iterator_next (&iter, &param_name, &param))
18848  {
18849  int type_start, type_end, count;
18850  /* LDAPsearch[entry]:Timeout value */
18851  count = sscanf (param_name,
18852  "%*[^[][%n%*[^]]%n]:",
18853  &type_start,
18854  &type_end);
18855  if (count == 0 && type_start > 0 && type_end > 0)
18856  {
18857  gchar *value;
18858 
18859  value = param->value_size
18860  ? g_base64_encode ((guchar *) param->value,
18861  param->value_size)
18862  : g_strdup ("");
18863 
18864  response = NULL;
18865  entity = NULL;
18866  ret = ompf (credentials,
18867  &response,
18868  &entity,
18869  response_data,
18870  "<modify_report_format"
18871  " report_format_id=\"%s\">"
18872  "<param>"
18873  "<name>%s</name>"
18874  "<value>%s</value>"
18875  "</param>"
18876  "</modify_report_format>",
18877  report_format_id,
18878  param_name + type_end + 2,
18879  value);
18880  g_free (value);
18881  switch (ret)
18882  {
18883  case 0:
18884  break;
18885  case 1:
18886  response_data->http_status_code
18887  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18888  return gsad_message (credentials,
18889  "Internal error", __FUNCTION__, __LINE__,
18890  "An internal error occurred while saving a Report Format. "
18891  "The Report Format was not saved. "
18892  "Diagnostics: Failure to send command to manager daemon.",
18893  "/omp?cmd=get_report_formats",
18894  response_data);
18895  case 2:
18896  response_data->http_status_code
18897  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18898  return gsad_message (credentials,
18899  "Internal error", __FUNCTION__, __LINE__,
18900  "An internal error occurred while saving a Report Format. "
18901  "It is unclear whether the Report Format has been saved or not. "
18902  "Diagnostics: Failure to receive response from manager daemon.",
18903  "/omp?cmd=get_report_formats",
18904  response_data);
18905  case -1:
18906  default:
18907  response_data->http_status_code
18908  = MHD_HTTP_INTERNAL_SERVER_ERROR;
18909  return gsad_message (credentials,
18910  "Internal error", __FUNCTION__, __LINE__,
18911  "An internal error occurred while saving a Report Format. "
18912  "It is unclear whether the Report Format has been saved or not. "
18913  "Diagnostics: Internal Error.",
18914  "/omp?cmd=get_report_formats",
18915  response_data);
18916  }
18917 
18918  /* TODO Check if succeeded. response_from_entity_if_failed? */
18919  }
18920  }
18921  }
18922 
18923  response = NULL;
18924  entity = NULL;
18925  ret = ompf (credentials,
18926  &response,
18927  &entity,
18928  response_data,
18929  "<modify_report_format"
18930  " report_format_id=\"%s\">"
18931  "<name>%s</name>"
18932  "<summary>%s</summary>"
18933  "<active>%s</active>"
18934  "</modify_report_format>",
18935  report_format_id,
18936  name,
18937  summary,
18938  enable);
18939 
18940  switch (ret)
18941  {
18942  case 0:
18943  break;
18944  case -1:
18945  return response;
18946  case 1:
18947  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18948  return gsad_message (credentials,
18949  "Internal error", __FUNCTION__, __LINE__,
18950  "An internal error occurred while saving a Report Format. "
18951  "The Report Format was not saved. "
18952  "Diagnostics: Failure to send command to manager daemon.",
18953  "/omp?cmd=get_report_formats", response_data);
18954  case 2:
18955  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18956  return gsad_message (credentials,
18957  "Internal error", __FUNCTION__, __LINE__,
18958  "An internal error occurred while saving a Report Format. "
18959  "It is unclear whether the Report Format has been saved or not. "
18960  "Diagnostics: Failure to receive response from manager daemon.",
18961  "/omp?cmd=get_report_formats", response_data);
18962  default:
18963  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
18964  return gsad_message (credentials,
18965  "Internal error", __FUNCTION__, __LINE__,
18966  "An internal error occurred while saving a Report Format. "
18967  "It is unclear whether the Report Format has been saved or not. "
18968  "Diagnostics: Internal Error.",
18969  "/omp?cmd=get_report_formats", response_data);
18970  }
18971 
18972  html = response_from_entity (credentials, params, entity,
18973  (no_redirect && strcmp (no_redirect, "0")),
18974  NULL, "get_report_formats",
18975  NULL, "edit_report_format",
18976  "Save Report Format", response_data);
18977  free_entity (entity);
18978  g_free (response);
18979  return html;
18980 }
18981 
18991 char *
18993  cmd_response_data_t* response_data)
18994 {
18995  int ret;
18996  gchar *html, *response;
18997  const char *report_format_id;
18998  entity_t entity;
18999 
19000  report_format_id = params_value (params, "report_format_id");
19001  if (report_format_id == NULL)
19002  {
19003  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
19004  return gsad_message (credentials,
19005  "Internal error", __FUNCTION__, __LINE__,
19006  "An internal error occurred while verifying a report format. "
19007  "Diagnostics: Required parameter was NULL.",
19008  "/omp?cmd=get_report_formats", response_data);
19009  }
19010 
19011  /* Verify the report format. */
19012 
19013  response = NULL;
19014  entity = NULL;
19015  ret = ompf (credentials,
19016  &response,
19017  &entity,
19018  response_data,
19019  "<verify_report_format report_format_id=\"%s\"/>",
19020  report_format_id);
19021 
19022  switch (ret)
19023  {
19024  case 0:
19025  case -1:
19026  break;
19027  case 1:
19028  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19029  return gsad_message (credentials,
19030  "Internal error", __FUNCTION__, __LINE__,
19031  "An internal error occurred while verifying a report format. "
19032  "The report format was not verified. "
19033  "Diagnostics: Failure to send command to manager daemon.",
19034  "/omp?cmd=get_report_formats", response_data);
19035  case 2:
19036  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19037  return gsad_message (credentials,
19038  "Internal error", __FUNCTION__, __LINE__,
19039  "An internal error occurred while verifying a report format. "
19040  "It is unclear whether the report format was verified or not. "
19041  "Diagnostics: Failure to send command to manager daemon.",
19042  "/omp?cmd=get_report_formats", response_data);
19043  default:
19044  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19045  return gsad_message (credentials,
19046  "Internal error", __FUNCTION__, __LINE__,
19047  "An internal error occurred while verifying a report format. "
19048  "It is unclear whether the report format was verified or not. "
19049  "Diagnostics: Failure to send command to manager daemon.",
19050  "/omp?cmd=get_report_formats", response_data);
19051  }
19052 
19053  if (omp_success (entity))
19054  {
19055  html = next_page (credentials, params, response, response_data);
19056  if (html == NULL)
19057  {
19058  free_entity (entity);
19059  g_free (response);
19060  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19061  return gsad_message (credentials,
19062  "Internal error", __FUNCTION__, __LINE__,
19063  "An internal error occurred while verifying a report format. "
19064  "It is unclear whether the report format was verified or not. "
19065  "Diagnostics: Failure to receive response from manager daemon.",
19066  "/omp?cmd=get_report_formats", response_data);
19067  }
19068  }
19069  else
19070  {
19071  set_http_status_from_entity (entity, response_data);
19072  html = get_report_formats (credentials, params, response, response_data);
19073  }
19074  free_entity (entity);
19075  g_free (response);
19076  return html;
19077 }
19078 
19088 char *
19089 run_wizard_omp (credentials_t *credentials, params_t *params,
19090  cmd_response_data_t* response_data)
19091 {
19092  const char *no_redirect, *name;
19093  int ret;
19094  GString *run;
19095  param_t *param;
19096  gchar *param_name, *html, *response;
19097  params_iterator_t iter;
19098  params_t *wizard_params;
19099  entity_t entity;
19100 
19101  /* The naming is a bit subtle here, because the HTTP request
19102  * parameters are called "param"s and so are the OMP wizard
19103  * parameters. */
19104 
19105  no_redirect = params_value (params, "no_redirect");
19106  name = params_value (params, "name");
19107  if (name == NULL)
19108  {
19109  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
19110  return gsad_message (credentials,
19111  "Internal error", __FUNCTION__, __LINE__,
19112  "An internal error occurred while trying to start a wizard. "
19113  "Diagnostics: Required parameter 'name' was NULL.",
19114  "/omp?cmd=get_tasks", response_data);
19115  }
19116  run = g_string_new ("<run_wizard>");
19117 
19118  g_string_append_printf (run,
19119  "<name>%s</name>"
19120  "<params>",
19121  name);
19122 
19123  wizard_params = params_values (params, "event_data:");
19124  if (wizard_params)
19125  {
19126  params_iterator_init (&iter, wizard_params);
19127  while (params_iterator_next (&iter, &param_name, &param))
19128  xml_string_append (run,
19129  "<param>"
19130  "<name>%s</name>"
19131  "<value>%s</value>"
19132  "</param>",
19133  param_name,
19134  param->value);
19135  }
19136 
19137  g_string_append (run, "</params></run_wizard>");
19138 
19139  response = NULL;
19140  entity = NULL;
19141  ret = omp (credentials, &response, &entity, response_data, run->str);
19142  g_string_free (run, TRUE);
19143  switch (ret)
19144  {
19145  case 0:
19146  case -1:
19147  break;
19148  case 1:
19149  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19150  return gsad_message (credentials,
19151  "Internal error", __FUNCTION__, __LINE__,
19152  "An internal error occurred while running a wizard. "
19153  "The wizard did not start. "
19154  "Diagnostics: Failure to send command to manager daemon.",
19155  "/omp?cmd=get_tasks", response_data);
19156  case 2:
19157  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19158  return gsad_message (credentials,
19159  "Internal error", __FUNCTION__, __LINE__,
19160  "An internal error occurred while running a wizard. "
19161  "It is unclear whether the wizard started or not. "
19162  "Diagnostics: Failure to receive response from manager daemon.",
19163  "/omp?cmd=get_tasks", response_data);
19164  default:
19165  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19166  return gsad_message (credentials,
19167  "Internal error", __FUNCTION__, __LINE__,
19168  "An internal error occurred while running a wizard. "
19169  "It is unclear whether the wizard started or not. "
19170  "Diagnostics: Internal Error.",
19171  "/omp?cmd=get_tasks", response_data);
19172  }
19173 
19174  html = response_from_entity (credentials, params, entity,
19175  (no_redirect && strcmp (no_redirect, "0")),
19176  NULL, "wizard",
19177  NULL, "wizard",
19178  "Run Wizard", response_data);
19179  free_entity (entity);
19180  g_free (response);
19181  return html;
19182 }
19183 
19184 #define GET_TRASH_RESOURCE(capability, command, name) \
19185  if (command_enabled (credentials, capability)) \
19186  { \
19187  if (openvas_connection_sendf \
19188  (&connection, \
19189  "<" command \
19190  " filter=\"rows=-1 sort=name\"" \
19191  " trash=\"1\"/>") \
19192  == -1) \
19193  { \
19194  g_string_free (xml, TRUE); \
19195  openvas_connection_close (&connection); \
19196  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
19197  return gsad_message \
19198  (credentials, \
19199  "Internal error", __FUNCTION__, __LINE__, \
19200  "An internal error occurred while getting " \
19201  name " list for trash." \
19202  "Diagnostics: Failure to send command to" \
19203  " manager daemon.", \
19204  "/omp?cmd=get_trash", response_data); \
19205  } \
19206  \
19207  if (read_string_c (&connection, &xml)) \
19208  { \
19209  g_string_free (xml, TRUE); \
19210  openvas_connection_close (&connection); \
19211  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
19212  return gsad_message \
19213  (credentials, \
19214  "Internal error", __FUNCTION__, __LINE__, \
19215  "An internal error occurred while getting " name " list."\
19216  "Diagnostics: Failure to receive response from" \
19217  " manager daemon.", \
19218  "/omp?cmd=get_tasks", response_data); \
19219  } \
19220  }
19221 
19232 char *
19233 get_trash (credentials_t * credentials, params_t *params, const char *extra_xml,
19234  cmd_response_data_t* response_data)
19235 {
19236  GString *xml;
19237  openvas_connection_t connection;
19238  gchar *html;
19239 
19240  switch (manager_connect (credentials, &connection, &html,
19241  response_data))
19242  {
19243  case 0:
19244  break;
19245  case -1:
19246  if (html)
19247  return html;
19248  /* Fall through. */
19249  default:
19250  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19251  return gsad_message (credentials,
19252  "Internal error", __FUNCTION__, __LINE__,
19253  "An internal error occurred while getting the trash. "
19254  "Diagnostics: Failure to connect to manager daemon.",
19255  "/omp?cmd=get_tasks", response_data);
19256  }
19257 
19258  xml = g_string_new ("<get_trash>");
19259 
19260  if (extra_xml)
19261  g_string_append (xml, extra_xml);
19262 
19263  GET_TRASH_RESOURCE ("GET_AGENTS", "get_agents", "agents");
19264 
19265  GET_TRASH_RESOURCE ("GET_CONFIGS", "get_configs", "configs");
19266 
19267  GET_TRASH_RESOURCE ("GET_CREDENTIALS", "get_credentials", "credentials");
19268 
19269  GET_TRASH_RESOURCE ("GET_ALERTS", "get_alerts", "alerts");
19270 
19271  GET_TRASH_RESOURCE ("GET_GROUPS", "get_groups", "groups");
19272 
19273  GET_TRASH_RESOURCE ("GET_FILTERS", "get_filters", "filters");
19274 
19275  GET_TRASH_RESOURCE ("GET_NOTES", "get_notes", "notes");
19276 
19277  GET_TRASH_RESOURCE ("GET_OVERRIDES", "get_overrides", "overrides");
19278 
19279  GET_TRASH_RESOURCE ("GET_PERMISSIONS", "get_permissions", "permissions");
19280 
19281  GET_TRASH_RESOURCE ("GET_PORT_LISTS", "get_port_lists", "port lists");
19282 
19283  GET_TRASH_RESOURCE ("GET_REPORT_FORMATS", "get_report_formats",
19284  "report formats");
19285 
19286  GET_TRASH_RESOURCE ("GET_ROLES", "get_roles", "roles");
19287 
19288  GET_TRASH_RESOURCE ("GET_SCANNERS", "get_scanners", "scanners");
19289 
19290  GET_TRASH_RESOURCE ("GET_SCHEDULES", "get_schedules", "schedules");
19291 
19292  GET_TRASH_RESOURCE ("GET_TAGS", "get_tags", "tags");
19293 
19294  GET_TRASH_RESOURCE ("GET_TARGETS", "get_targets", "targets");
19295 
19296  GET_TRASH_RESOURCE ("GET_TASKS", "get_tasks", "tasks");
19297 
19298  /* Cleanup, and return transformed XML. */
19299 
19300  g_string_append (xml, "</get_trash>");
19301  openvas_connection_close (&connection);
19302  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
19303  response_data);
19304 }
19305 #undef GET_TRASH_RESOURCE
19306 
19316 char *
19317 get_trash_omp (credentials_t * credentials, params_t *params,
19318  cmd_response_data_t* response_data)
19319 {
19320  return get_trash (credentials, params, NULL, response_data);
19321 }
19322 
19333 static char *
19334 get_my_settings (credentials_t * credentials, params_t *params,
19335  const char *extra_xml, cmd_response_data_t* response_data)
19336 {
19337  GString *xml;
19338  openvas_connection_t connection;
19339  gchar *html;
19340 
19341  switch (manager_connect (credentials, &connection, &html,
19342  response_data))
19343  {
19344  case 0:
19345  break;
19346  case -1:
19347  if (html)
19348  return html;
19349  /* Fall through. */
19350  default:
19351  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19352  return gsad_message (credentials,
19353  "Internal error", __FUNCTION__, __LINE__,
19354  "An internal error occurred while getting the settings. "
19355  "Diagnostics: Failure to connect to manager daemon.",
19356  "/omp?cmd=get_tasks", response_data);
19357  }
19358 
19359  xml = g_string_new ("<get_my_settings>");
19360 
19361  if (extra_xml)
19362  g_string_append (xml, extra_xml);
19363 
19364  /* Get the settings. */
19365 
19366  if (openvas_connection_sendf (&connection,
19367  "<get_settings"
19368  " sort_field=\"name\""
19369  " sort_order=\"ascending\"/>")
19370  == -1)
19371  {
19372  g_string_free (xml, TRUE);
19373  openvas_connection_close (&connection);
19374  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19375  return gsad_message (credentials,
19376  "Internal error", __FUNCTION__, __LINE__,
19377  "An internal error occurred while getting the settings. "
19378  "The current list of settings is not available. "
19379  "Diagnostics: Failure to send command to manager daemon.",
19380  "/omp?cmd=get_tasks", response_data);
19381  }
19382 
19383  if (read_string_c (&connection, &xml))
19384  {
19385  g_string_free (xml, TRUE);
19386  openvas_connection_close (&connection);
19387  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19388  return gsad_message (credentials,
19389  "Internal error", __FUNCTION__, __LINE__,
19390  "An internal error occurred while getting the settings. "
19391  "The current list of settings is not available. "
19392  "Diagnostics: Failure to receive response from manager daemon.",
19393  "/omp?cmd=get_tasks", response_data);
19394  }
19395 
19396  buffer_languages_xml (xml);
19397 
19398  g_string_append (xml, "</get_my_settings>");
19399  openvas_connection_close (&connection);
19400  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
19401  response_data);
19402 }
19403 
19413 char *
19415  cmd_response_data_t* response_data)
19416 {
19417  GString *commands;
19418  int ret;
19419  entity_t entity;
19420  gchar *response;
19421 
19422  commands = g_string_new ("<commands>");
19423  if (command_enabled (credentials, "GET_ALERTS"))
19424  g_string_append (commands, "<get_alerts/>");
19425  if (command_enabled (credentials, "GET_CONFIGS"))
19426  g_string_append (commands, "<get_configs/>");
19427  if (command_enabled (credentials, "GET_FILTERS"))
19428  g_string_append (commands, "<get_filters/>");
19429  if (command_enabled (credentials, "GET_CREDENTIALS"))
19430  g_string_append (commands, "<get_credentials/>");
19431  if (command_enabled (credentials, "GET_PORT_LISTS"))
19432  g_string_append (commands, "<get_port_lists/>");
19433  if (command_enabled (credentials, "GET_REPORT_FORMATS"))
19434  g_string_append (commands, "<get_report_formats/>");
19435  if (command_enabled (credentials, "GET_SCANNERS"))
19436  g_string_append (commands, "<get_scanners/>");
19437  if (command_enabled (credentials, "GET_SCHEDULES"))
19438  g_string_append (commands, "<get_schedules/>");
19439  if (command_enabled (credentials, "GET_TARGETS"))
19440  g_string_append (commands, "<get_targets/>");
19441  g_string_append (commands, "</commands>");
19442 
19443  /* Get Filters and other resource lists. */
19444  response = NULL;
19445  entity = NULL;
19446  ret = omp (credentials, &response, &entity, response_data, commands->str);
19447  g_string_free (commands, TRUE);
19448  switch (ret)
19449  {
19450  case 0:
19451  case -1:
19452  break;
19453  case 1:
19454  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19455  return gsad_message (credentials,
19456  "Internal error", __FUNCTION__, __LINE__,
19457  "An internal error occurred while getting resources "
19458  "for the settings. "
19459  "Diagnostics: Failure to send command to manager daemon.",
19460  "/omp?cmd=get_my_settings", response_data);
19461  case 2:
19462  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19463  return gsad_message (credentials,
19464  "Internal error", __FUNCTION__, __LINE__,
19465  "An internal error occurred while getting resources "
19466  "for the settings. "
19467  "Diagnostics: Failure to receive response from manager daemon.",
19468  "/omp?cmd=get_alerts", response_data);
19469  default:
19470  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19471  return gsad_message (credentials,
19472  "Internal error", __FUNCTION__, __LINE__,
19473  "An internal error occurred while getting resources "
19474  "for the settings. "
19475  "It is unclear whether the task has been saved or not. "
19476  "Diagnostics: Internal Error.",
19477  "/omp?cmd=get_alerts", response_data);
19478  }
19479  free_entity (entity);
19480 
19481  return get_my_settings (credentials, params, response, response_data);
19482 }
19483 
19494 static char *
19495 edit_my_settings (credentials_t * credentials, params_t *params,
19496  const char *extra_xml, cmd_response_data_t* response_data)
19497 {
19498  GString *commands, *xml;
19499  openvas_connection_t connection;
19500  int ret;
19501  gchar *html, *filters_xml;
19502  entity_t entity;
19503 
19504  /* Get the Filters and other resources. */
19505  commands = g_string_new ("<commands>");
19506  if (command_enabled (credentials, "GET_ALERTS"))
19507  g_string_append (commands, "<get_alerts/>");
19508  if (command_enabled (credentials, "GET_CONFIGS"))
19509  g_string_append (commands, "<get_configs/>");
19510  if (command_enabled (credentials, "GET_CREDENTIALS"))
19511  g_string_append (commands, "<get_credentials/>");
19512  if (command_enabled (credentials, "GET_FILTERS"))
19513  g_string_append (commands, "<get_filters/>");
19514  if (command_enabled (credentials, "GET_PORT_LISTS"))
19515  g_string_append (commands, "<get_port_lists/>");
19516  if (command_enabled (credentials, "GET_REPORT_FORMAT"))
19517  g_string_append (commands, "<get_report_formats/>");
19518  if (command_enabled (credentials, "GET_SCANNERS"))
19519  g_string_append (commands, "<get_scanners/>");
19520  if (command_enabled (credentials, "GET_SCHEDULES"))
19521  g_string_append (commands, "<get_schedules/>");
19522  if (command_enabled (credentials, "GET_TARGETS"))
19523  g_string_append (commands, "<get_targets/>");
19524  g_string_append (commands, "</commands>");
19525 
19526  filters_xml = NULL;
19527  entity = NULL;
19528  ret = omp (credentials, &filters_xml, &entity, response_data, commands->str);
19529  g_string_free (commands, TRUE);
19530  switch (ret)
19531  {
19532  case 0:
19533  case -1:
19534  break;
19535  case 1:
19536  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19537  return gsad_message (credentials,
19538  "Internal error", __FUNCTION__, __LINE__,
19539  "An internal error occurred while getting resources "
19540  "for the settings. "
19541  "Diagnostics: Failure to send command to manager daemon.",
19542  "/omp?cmd=get_my_settings", response_data);
19543  case 2:
19544  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19545  return gsad_message (credentials,
19546  "Internal error", __FUNCTION__, __LINE__,
19547  "An internal error occurred while getting resources "
19548  "for the alert. "
19549  "Diagnostics: Failure to receive response from manager daemon.",
19550  "/omp?cmd=get_my_settings", response_data);
19551  default:
19552  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19553  return gsad_message (credentials,
19554  "Internal error", __FUNCTION__, __LINE__,
19555  "An internal error occurred while getting resources "
19556  "for the settings. "
19557  "Diagnostics: Internal Error.",
19558  "/omp?cmd=get_my_settings", response_data);
19559  }
19560  free_entity (entity);
19561 
19562  switch (manager_connect (credentials, &connection, &html,
19563  response_data))
19564  {
19565  case 0:
19566  break;
19567  case -1:
19568  if (html)
19569  return html;
19570  /* Fall through. */
19571  default:
19572  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19573  return gsad_message (credentials,
19574  "Internal error", __FUNCTION__, __LINE__,
19575  "An internal error occurred while getting the settings. "
19576  "Diagnostics: Failure to connect to manager daemon.",
19577  "/omp?cmd=get_my_settings", response_data);
19578  }
19579 
19580  xml = g_string_new ("<edit_my_settings>");
19581 
19582  if (extra_xml)
19583  g_string_append (xml, extra_xml);
19584 
19585  g_string_append (xml, filters_xml);
19586  g_free (filters_xml);
19587 
19588  /* Get the settings. */
19589 
19590  if (openvas_connection_sendf (&connection,
19591  "<get_settings"
19592  " sort_field=\"name\""
19593  " sort_order=\"ascending\"/>")
19594  == -1)
19595  {
19596  g_string_free (xml, TRUE);
19597  openvas_connection_close (&connection);
19598  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19599  return gsad_message (credentials,
19600  "Internal error", __FUNCTION__, __LINE__,
19601  "An internal error occurred while getting the settings. "
19602  "The current list of settings is not available. "
19603  "Diagnostics: Failure to send command to manager daemon.",
19604  "/omp?cmd=get_my_settings", response_data);
19605  }
19606 
19607  if (read_string_c (&connection, &xml))
19608  {
19609  g_string_free (xml, TRUE);
19610  openvas_connection_close (&connection);
19611  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19612  return gsad_message (credentials,
19613  "Internal error", __FUNCTION__, __LINE__,
19614  "An internal error occurred while getting the settings. "
19615  "The current list of settings is not available. "
19616  "Diagnostics: Failure to receive response from manager daemon.",
19617  "/omp?cmd=get_my_settings", response_data);
19618  }
19619 
19620  buffer_languages_xml (xml);
19621 
19622  g_string_append (xml, "</edit_my_settings>");
19623  openvas_connection_close (&connection);
19624  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
19625  response_data);
19626 }
19627 
19637 char *
19639  cmd_response_data_t* response_data)
19640 {
19641  return edit_my_settings (credentials, params, NULL, response_data);
19642 }
19643 
19655 static int
19656 send_settings_filters (openvas_connection_t *connection, params_t *data,
19657  params_t *changed, GString *xml, int *modify_failed_flag,
19658  cmd_response_data_t* response_data)
19659 {
19660  if (data)
19661  {
19662  params_iterator_t iter;
19663  char *uuid;
19664  param_t *param;
19665  entity_t entity;
19666 
19667  params_iterator_init (&iter, data);
19668  while (params_iterator_next (&iter, &uuid, &param))
19669  {
19670  const char* changed_value = params_value (changed, uuid);
19671  if (changed_value == NULL
19672  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19673  {
19674  gchar *base64;
19675  if (param->value)
19676  base64 = g_base64_encode ((guchar*) param->value,
19677  strlen (param->value));
19678  else
19679  base64 = g_strdup("");
19680  if (openvas_connection_sendf_xml (connection,
19681  "<modify_setting setting_id=\"%s\">"
19682  "<value>%s</value>"
19683  "</modify_setting>",
19684  uuid,
19685  base64))
19686  {
19687  g_free (base64);
19688  return -1;
19689  }
19690 
19691  g_free (base64);
19692 
19693  entity = NULL;
19694  xml_string_append (xml,
19695  "<save_setting id=\"%s\">",
19696  uuid);
19697  if (read_entity_and_string_c (connection, &entity, &xml))
19698  {
19699  free_entity (entity);
19700  return -1;
19701  }
19702  xml_string_append (xml, "</save_setting>");
19703  if (! omp_success (entity))
19704  {
19705  set_http_status_from_entity (entity, response_data);
19706  if (modify_failed_flag)
19707  *modify_failed_flag = 1;
19708  }
19709  free_entity(entity);
19710  }
19711  }
19712  }
19713  return 0;
19714 }
19715 
19730 char *
19732  const char *accept_language, char **timezone,
19733  char **password, char **severity, char **language,
19734  cmd_response_data_t* response_data)
19735 {
19736  openvas_connection_t connection;
19737  gchar *html;
19738  const char *lang, *text, *old_passwd, *passwd, *status, *max;
19739  const char *details_fname, *list_fname, *report_fname;
19740  gchar *lang_64, *text_64, *max_64, *fname_64;
19741  GString *xml;
19742  entity_t entity;
19743  params_t *changed, *defaults, *filters;
19744  int modify_failed = 0;
19745  const char *changed_value;
19746 
19747  changed = params_values (params, "settings_changed:");
19748  *timezone = NULL;
19749  *password = NULL;
19750  *severity = NULL;
19751  *language = NULL;
19752 
19753  text = params_value (params, "text");
19754  old_passwd = params_value (params, "old_password");
19755  passwd = params_value (params, "password");
19756  max = params_value (params, "max");
19757  lang = params_value (params, "lang");
19758  details_fname = params_value (params, "details_fname");
19759  list_fname = params_value (params, "list_fname");
19760  report_fname = params_value (params, "report_fname");
19761  if ((text == NULL)
19762  || (passwd == NULL)
19763  || (old_passwd == NULL)
19764  || (max == NULL)
19765  || (lang == NULL)
19766  || (details_fname == NULL)
19767  || (list_fname == NULL)
19768  || (report_fname == NULL))
19769  return edit_my_settings (credentials, params,
19771  ("Save My Settings"),
19772  response_data);
19773 
19774  switch (manager_connect (credentials, &connection, &html,
19775  response_data))
19776  {
19777  case 0:
19778  break;
19779  case -1:
19780  if (html)
19781  return html;
19782  /* Fall through. */
19783  default:
19784  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19785  return gsad_message (credentials,
19786  "Internal error", __FUNCTION__, __LINE__,
19787  "An internal error occurred while saving settings. "
19788  "The settings remains the same. "
19789  "Diagnostics: Failure to connect to manager daemon.",
19790  "/omp?cmd=get_my_settings", response_data);
19791  }
19792 
19793  xml = g_string_new ("");
19794 
19795  changed_value = params_value (changed, "password");
19796  if ((strlen (passwd) || strlen (old_passwd))
19797  && (changed_value == NULL
19798  || (strcmp (changed_value, "") && strcmp (changed_value, "0"))))
19799  {
19800  gchar *passwd_64;
19801  omp_authenticate_info_opts_t auth_opts;
19802 
19803  /* Send Password setting */
19804 
19805  auth_opts = omp_authenticate_info_opts_defaults;
19806  auth_opts.username = credentials->username;
19807  auth_opts.password = old_passwd;
19808  switch (omp_authenticate_info_ext_c (&connection, auth_opts))
19809  {
19810  case 0:
19811  break;
19812  case 1:
19813  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19814  return gsad_message (credentials,
19815  "Internal error", __FUNCTION__, __LINE__,
19816  "An internal error occurred while saving settings. "
19817  "The settings remains the same. "
19818  "Diagnostics: Manager closed connection during authenticate.",
19819  "/omp?cmd=get_my_settings", response_data);
19820  case 2:
19821  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19822  g_string_append (xml,
19823  "<gsad_msg"
19824  " status_text=\"Password error\""
19825  " operation=\"Save My Settings\">"
19826  "You tried to change your password, but the old"
19827  " password was not provided or was incorrect. "
19828  " Please enter the correct old password or remove"
19829  " old and new passwords to apply any other changes"
19830  " of your settings."
19831  "</gsad_msg>");
19832  return edit_my_settings (credentials, params,
19833  g_string_free (xml, FALSE),
19834  response_data);
19835  default:
19836  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19837  return gsad_message (credentials,
19838  "Internal error", __FUNCTION__, __LINE__,
19839  "An internal error occurred while saving settings. "
19840  "The settings remains the same. "
19841  "Diagnostics: Internal Error.",
19842  "/omp?cmd=get_my_settings", response_data);
19843  }
19844 
19845  passwd_64 = g_base64_encode ((guchar*) passwd, strlen (passwd));
19846 
19847  if (openvas_connection_sendf (&connection,
19848  "<modify_setting>"
19849  "<name>Password</name>"
19850  "<value>%s</value>"
19851  "</modify_setting>",
19852  passwd_64 ? passwd_64 : "")
19853  == -1)
19854  {
19855  g_free (passwd_64);
19856  openvas_connection_close (&connection);
19857  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19858  return gsad_message (credentials,
19859  "Internal error", __FUNCTION__, __LINE__,
19860  "An internal error occurred while saving settings. "
19861  "It is unclear whether all the settings were saved. "
19862  "Diagnostics: Failure to send command to manager daemon.",
19863  "/omp?cmd=get_my_settings", response_data);
19864  }
19865  g_free (passwd_64);
19866 
19867  entity = NULL;
19868  xml_string_append (xml, "<save_setting name=\"Password\">");
19869  if (read_entity_and_string_c (&connection, &entity, &xml))
19870  {
19871  g_string_free (xml, TRUE);
19872  openvas_connection_close (&connection);
19873  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19874  return gsad_message (credentials,
19875  "Internal error", __FUNCTION__, __LINE__,
19876  "An internal error occurred while saving settings. "
19877  "Diagnostics: Failure to receive response from manager daemon.",
19878  "/omp?cmd=get_my_settings", response_data);
19879  }
19880  xml_string_append (xml, "</save_setting>");
19881 
19882  status = entity_attribute (entity, "status");
19883  if (status && (strlen (status) > 0) && (status[0] == '2'))
19884  {
19885  g_free (credentials->password);
19886  credentials->password = g_strdup (passwd);
19887  *password = g_strdup (passwd);
19888  }
19889  else
19890  {
19891  set_http_status_from_entity (entity, response_data);
19892  modify_failed = 1;
19893  }
19894  }
19895 
19896  /* Send Timezone */
19897  changed_value = params_value (changed, "timezone");
19898  if (changed_value == NULL
19899  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19900  {
19901  text_64 = g_base64_encode ((guchar*) text, strlen (text));
19902 
19903  if (openvas_connection_sendf (&connection,
19904  "<modify_setting>"
19905  "<name>Timezone</name>"
19906  "<value>%s</value>"
19907  "</modify_setting>",
19908  text_64 ? text_64 : "")
19909  == -1)
19910  {
19911  g_free (text_64);
19912  openvas_connection_close (&connection);
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, "<save_setting name=\"Timezone\">");
19925  if (read_entity_and_string_c (&connection, &entity, &xml))
19926  {
19927  g_string_free (xml, TRUE);
19928  openvas_connection_close (&connection);
19929  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
19930  return gsad_message (credentials,
19931  "Internal error", __FUNCTION__, __LINE__,
19932  "An internal error occurred while saving settings. "
19933  "Diagnostics: Failure to receive response from manager daemon.",
19934  "/omp?cmd=get_my_settings", response_data);
19935  }
19936  xml_string_append (xml, "</save_setting>");
19937 
19938  status = entity_attribute (entity, "status");
19939  if (status && (strlen (status) > 0) && (status[0] == '2'))
19940  {
19941  g_free (credentials->timezone);
19942  credentials->timezone = g_strdup (strlen (text) ? text : "UTC");
19943  *timezone = g_strdup (strlen (text) ? text : "UTC");
19944 
19945  /* Set the timezone, so that the ENVELOPE/TIME
19946  * uses the right timezone. */
19947 
19948  if (setenv ("TZ", credentials->timezone, 1) == -1)
19949  {
19950  openvas_connection_close (&connection);
19951  g_critical ("%s: failed to set TZ\n", __FUNCTION__);
19952  exit (EXIT_FAILURE);
19953  }
19954  tzset ();
19955  }
19956  else
19957  {
19958  set_http_status_from_entity (entity, response_data);
19959  modify_failed = 1;
19960  }
19961  }
19962 
19963  /* Send Rows Per Page */
19964  changed_value = params_value (changed, "max");
19965  if (changed_value == NULL
19966  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
19967  {
19968  max_64 = g_base64_encode ((guchar*) max, strlen (max));
19969 
19970  if (openvas_connection_sendf (&connection,
19971  "<modify_setting"
19972  " setting_id"
19973  "=\"5f5a8712-8017-11e1-8556-406186ea4fc5\">"
19974  "<value>%s</value>"
19975  "</modify_setting>",
19976  max_64 ? max_64 : "")
19977  == -1)
19978  {
19979  g_free (max_64);
19980  openvas_connection_close (&connection);
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 saving settings. "
19985  "It is unclear whether all the settings were saved. "
19986  "Diagnostics: Failure to send command to manager daemon.",
19987  "/omp?cmd=get_my_settings", response_data);
19988  }
19989  g_free (max_64);
19990 
19991  entity = NULL;
19992  xml_string_append (xml,
19993  "<save_setting id=\"%s\">",
19994  "5f5a8712-8017-11e1-8556-406186ea4fc5");
19995  if (read_entity_and_string_c (&connection, &entity, &xml))
19996  {
19997  g_string_free (xml, TRUE);
19998  openvas_connection_close (&connection);
19999  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20000  return gsad_message (credentials,
20001  "Internal error", __FUNCTION__, __LINE__,
20002  "An internal error occurred while saving settings. "
20003  "It is unclear whether all the settings were saved. "
20004  "Diagnostics: Failure to receive response from manager daemon.",
20005  "/omp?cmd=get_my_settings", response_data);
20006  }
20007  xml_string_append (xml, "</save_setting>");
20008  if (! omp_success (entity))
20009  {
20010  set_http_status_from_entity (entity, response_data);
20011  modify_failed = 1;
20012  }
20013  }
20014 
20015  /* Send resource details export file name format. */
20016  changed_value = params_value (changed, "details_fname");
20017  if (changed_value == NULL
20018  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20019  {
20020  fname_64 = g_base64_encode ((guchar*) details_fname, strlen (details_fname));
20021 
20022  if (openvas_connection_sendf (&connection,
20023  "<modify_setting"
20024  " setting_id"
20025  "=\"a6ac88c5-729c-41ba-ac0a-deea4a3441f2\">"
20026  "<value>%s</value>"
20027  "</modify_setting>",
20028  fname_64 ? fname_64 : "")
20029  == -1)
20030  {
20031  g_free (fname_64);
20032  openvas_connection_close (&connection);
20033  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20034  return gsad_message (credentials,
20035  "Internal error", __FUNCTION__, __LINE__,
20036  "An internal error occurred while saving settings. "
20037  "It is unclear whether all the settings were saved. "
20038  "Diagnostics: Failure to send command to manager daemon.",
20039  "/omp?cmd=get_my_settings", response_data);
20040  }
20041  g_free (fname_64);
20042 
20043  entity = NULL;
20044  xml_string_append (xml,
20045  "<save_setting id=\"%s\">",
20046  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2");
20047  if (read_entity_and_string_c (&connection, &entity, &xml))
20048  {
20049  g_string_free (xml, TRUE);
20050  openvas_connection_close (&connection);
20051  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20052  return gsad_message (credentials,
20053  "Internal error", __FUNCTION__, __LINE__,
20054  "An internal error occurred while saving settings. "
20055  "It is unclear whether all the settings were saved. "
20056  "Diagnostics: Failure to receive response from manager daemon.",
20057  "/omp?cmd=get_my_settings", response_data);
20058  }
20059  xml_string_append (xml, "</save_setting>");
20060  if (omp_success (entity) != 1)
20061  {
20062  set_http_status_from_entity (entity, response_data);
20063  modify_failed = 1;
20064  }
20065  }
20066 
20067  /* Send resource list export file name format. */
20068  changed_value = params_value (changed, "list_fname");
20069  if (changed_value == NULL
20070  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20071  {
20072  fname_64 = g_base64_encode ((guchar*) list_fname, strlen (list_fname));
20073 
20074  if (openvas_connection_sendf (&connection,
20075  "<modify_setting"
20076  " setting_id"
20077  "=\"0872a6ed-4f85-48c5-ac3f-a5ef5e006745\">"
20078  "<value>%s</value>"
20079  "</modify_setting>",
20080  fname_64 ? fname_64 : "")
20081  == -1)
20082  {
20083  g_free (fname_64);
20084  openvas_connection_close (&connection);
20085  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20086  return gsad_message (credentials,
20087  "Internal error", __FUNCTION__, __LINE__,
20088  "An internal error occurred while saving settings. "
20089  "It is unclear whether all the settings were saved. "
20090  "Diagnostics: Failure to send command to manager daemon.",
20091  "/omp?cmd=get_my_settings", response_data);
20092  }
20093  g_free (fname_64);
20094 
20095  entity = NULL;
20096  xml_string_append (xml,
20097  "<save_setting id=\"%s\">",
20098  "a6ac88c5-729c-41ba-ac0a-deea4a3441f2");
20099  if (read_entity_and_string_c (&connection, &entity, &xml))
20100  {
20101  g_string_free (xml, TRUE);
20102  openvas_connection_close (&connection);
20103  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20104  return gsad_message (credentials,
20105  "Internal error", __FUNCTION__, __LINE__,
20106  "An internal error occurred while saving settings. "
20107  "It is unclear whether all the settings were saved. "
20108  "Diagnostics: Failure to receive response from manager daemon.",
20109  "/omp?cmd=get_my_settings", response_data);
20110  }
20111  xml_string_append (xml, "</save_setting>");
20112  if (omp_success (entity) != 1)
20113  {
20114  set_http_status_from_entity (entity, response_data);
20115  modify_failed = 1;
20116  }
20117  }
20118 
20119  /* Send report export file name format. */
20120  changed_value = params_value (changed, "report_fname");
20121  if (changed_value == NULL
20122  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20123  {
20124  fname_64 = g_base64_encode ((guchar*) report_fname, strlen (report_fname));
20125 
20126  if (openvas_connection_sendf (&connection,
20127  "<modify_setting"
20128  " setting_id"
20129  "=\"e1a2ae0b-736e-4484-b029-330c9e15b900\">"
20130  "<value>%s</value>"
20131  "</modify_setting>",
20132  fname_64 ? fname_64 : "")
20133  == -1)
20134  {
20135  g_free (fname_64);
20136  openvas_connection_close (&connection);
20137  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20138  return gsad_message (credentials,
20139  "Internal error", __FUNCTION__, __LINE__,
20140  "An internal error occurred while saving settings. "
20141  "It is unclear whether all the settings were saved. "
20142  "Diagnostics: Failure to send command to manager daemon.",
20143  "/omp?cmd=get_my_settings", response_data);
20144  }
20145  g_free (fname_64);
20146 
20147  entity = NULL;
20148  xml_string_append (xml,
20149  "<save_setting id=\"%s\">",
20150  "e1a2ae0b-736e-4484-b029-330c9e15b900");
20151  if (read_entity_and_string_c (&connection, &entity, &xml))
20152  {
20153  g_string_free (xml, TRUE);
20154  openvas_connection_close (&connection);
20155  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20156  return gsad_message (credentials,
20157  "Internal error", __FUNCTION__, __LINE__,
20158  "An internal error occurred while saving settings. "
20159  "It is unclear whether all the settings were saved. "
20160  "Diagnostics: Failure to receive response from manager daemon.",
20161  "/omp?cmd=get_my_settings", response_data);
20162  }
20163  xml_string_append (xml, "</save_setting>");
20164  if (omp_success (entity) != 1)
20165  {
20166  set_http_status_from_entity (entity, response_data);
20167  modify_failed = 1;
20168  }
20169  }
20170 
20171  /* Send User Interface Language. */
20172  changed_value = params_value (changed, "lang");
20173  if (changed_value == NULL
20174  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20175  {
20176  lang_64 = g_base64_encode ((guchar*) lang, strlen (lang));
20177 
20178  if (openvas_connection_sendf (&connection,
20179  "<modify_setting"
20180  " setting_id"
20181  "=\"6765549a-934e-11e3-b358-406186ea4fc5\">"
20182  "<value>%s</value>"
20183  "</modify_setting>",
20184  lang_64 ? lang_64 : "")
20185  == -1)
20186  {
20187  g_free (lang_64);
20188  openvas_connection_close (&connection);
20189  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20190  return gsad_message (credentials,
20191  "Internal error", __FUNCTION__, __LINE__,
20192  "An internal error occurred while saving settings. "
20193  "It is unclear whether all the settings were saved. "
20194  "Diagnostics: Failure to send command to manager daemon.",
20195  "/omp?cmd=get_my_settings", response_data);
20196  }
20197  g_free (lang_64);
20198 
20199  entity = NULL;
20200  xml_string_append (xml,
20201  "<save_setting id=\"%s\">",
20202  "6765549a-934e-11e3-b358-406186ea4fc5");
20203  if (read_entity_and_string_c (&connection, &entity, &xml))
20204  {
20205  g_string_free (xml, TRUE);
20206  openvas_connection_close (&connection);
20207  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20208  return gsad_message (credentials,
20209  "Internal error", __FUNCTION__, __LINE__,
20210  "An internal error occurred while saving settings. "
20211  "It is unclear whether all the settings were saved. "
20212  "Diagnostics: Failure to receive response from manager daemon.",
20213  "/omp?cmd=get_my_settings", response_data);
20214  }
20215  xml_string_append (xml, "</save_setting>");
20216  if (omp_success (entity))
20217  {
20218  gchar *language_code;
20219  set_language_code (&language_code, lang);
20220  if (language_code)
20221  {
20222  g_free (credentials->language);
20223  credentials->language = language_code;
20224  *language = g_strdup (lang);
20225  }
20226  else
20227  {
20228  g_free (credentials->language);
20229  credentials->language = accept_language_to_env_fmt (accept_language);
20230  *language = NULL;
20231  }
20232  }
20233  else
20234  {
20235  set_http_status_from_entity (entity, response_data);
20236  modify_failed = 1;
20237  }
20238  }
20239 
20240  /* Send default resources */
20241 
20242  defaults = params_values (params, "settings_default:");
20243  if (send_settings_filters (&connection, defaults, changed, xml,
20244  &modify_failed, response_data))
20245  {
20246  openvas_connection_close (&connection);
20247  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20248  return gsad_message (credentials,
20249  "Internal error", __FUNCTION__, __LINE__,
20250  "An internal error occurred while saving settings. "
20251  "It is unclear whether all the settings were saved. "
20252  "Diagnostics: Failure to send command to manager daemon.",
20253  "/omp?cmd=get_my_settings", response_data);
20254  }
20255 
20256  /* Send resources filters */
20257 
20258  filters = params_values (params, "settings_filter:");
20259  if (send_settings_filters (&connection, filters, changed, xml, &modify_failed,
20260  response_data))
20261  {
20262  openvas_connection_close (&connection);
20263  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20264  return gsad_message (credentials,
20265  "Internal error", __FUNCTION__, __LINE__,
20266  "An internal error occurred while saving settings. "
20267  "It is unclear whether all the settings were saved. "
20268  "Diagnostics: Failure to send command to manager daemon.",
20269  "/omp?cmd=get_my_settings", response_data);
20270  }
20271 
20272  /* Send Severity Class. */
20273 
20274  changed_value = params_value (changed, "severity_class");
20275  if (changed_value == NULL
20276  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20277  {
20278  text = params_value (params, "severity_class");
20279  text_64 = (text
20280  ? g_base64_encode ((guchar*) text, strlen (text))
20281  : g_strdup (""));
20282 
20283  if (openvas_connection_sendf (&connection,
20284  "<modify_setting"
20285  " setting_id"
20286  "=\"f16bb236-a32d-4cd5-a880-e0fcf2599f59\">"
20287  "<value>%s</value>"
20288  "</modify_setting>",
20289  text_64 ? text_64 : "")
20290  == -1)
20291  {
20292  g_free (text_64);
20293  openvas_connection_close (&connection);
20294  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20295  return gsad_message (credentials,
20296  "Internal error", __FUNCTION__, __LINE__,
20297  "An internal error occurred while saving settings. "
20298  "It is unclear whether all the settings were saved. "
20299  "Diagnostics: Failure to send command to manager daemon.",
20300  "/omp?cmd=get_my_settings", response_data);
20301  }
20302  g_free (text_64);
20303 
20304  entity = NULL;
20305  xml_string_append (xml,
20306  "<save_setting id=\"%s\">",
20307  "f16bb236-a32d-4cd5-a880-e0fcf2599f59");
20308  if (read_entity_and_string_c (&connection, &entity, &xml))
20309  {
20310  g_string_free (xml, TRUE);
20311  openvas_connection_close (&connection);
20312  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20313  return gsad_message (credentials,
20314  "Internal error", __FUNCTION__, __LINE__,
20315  "An internal error occurred while saving settings. "
20316  "It is unclear whether all the settings were saved. "
20317  "Diagnostics: Failure to receive response from manager daemon.",
20318  "/omp?cmd=get_my_settings", response_data);
20319  }
20320  xml_string_append (xml, "</save_setting>");
20321 
20322  status = entity_attribute (entity, "status");
20323  if (status && (strlen (status) > 0) && (status[0] == '2'))
20324  {
20325  g_free (credentials->severity);
20326  if ((text != NULL) && (strlen (text) > 0))
20327  {
20328  credentials->severity = g_strdup (text);
20329  *severity = g_strdup (text);
20330  }
20331  }
20332  else
20333  {
20334  set_http_status_from_entity (entity, response_data);
20335  modify_failed = 1;
20336  }
20337  }
20338 
20339  /* Send Dynamic Severity setting. */
20340 
20341  changed_value = params_value (changed, "dynamic_severity");
20342  if (changed_value == NULL
20343  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20344  {
20345  text = params_value (params, "dynamic_severity");
20346  text_64 = (text
20347  ? g_base64_encode ((guchar*) text, strlen (text))
20348  : g_strdup (""));
20349 
20350  if (openvas_connection_sendf (&connection,
20351  "<modify_setting"
20352  " setting_id"
20353  "=\"77ec2444-e7f2-4a80-a59b-f4237782d93f\">"
20354  "<value>%s</value>"
20355  "</modify_setting>",
20356  text_64 ? text_64 : "")
20357  == -1)
20358  {
20359  g_free (text_64);
20360  openvas_connection_close (&connection);
20361  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20362  return gsad_message (credentials,
20363  "Internal error", __FUNCTION__, __LINE__,
20364  "An internal error occurred while saving settings. "
20365  "It is unclear whether all the settings were saved. "
20366  "Diagnostics: Failure to send command to manager daemon.",
20367  "/omp?cmd=get_my_settings", response_data);
20368  }
20369  g_free (text_64);
20370 
20371  entity = NULL;
20372  xml_string_append (xml,
20373  "<save_setting id=\"%s\">",
20374  "77ec2444-e7f2-4a80-a59b-f4237782d93f");
20375  if (read_entity_and_string_c (&connection, &entity, &xml))
20376  {
20377  g_string_free (xml, TRUE);
20378  openvas_connection_close (&connection);
20379  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20380  return gsad_message (credentials,
20381  "Internal error", __FUNCTION__, __LINE__,
20382  "An internal error occurred while saving settings. "
20383  "It is unclear whether all the settings were saved. "
20384  "Diagnostics: Failure to receive response from manager daemon.",
20385  "/omp?cmd=get_my_settings", response_data);
20386  }
20387  xml_string_append (xml, "</save_setting>");
20388  if (! omp_success (entity))
20389  {
20390  set_http_status_from_entity (entity, response_data);
20391  modify_failed = 1;
20392  }
20393  }
20394 
20395  /* Send Default Severity setting. */
20396  changed_value = params_value (changed, "default_severity");
20397  if (changed_value == NULL
20398  || (strcmp (changed_value, "") && strcmp (changed_value, "0")))
20399  {
20400  text = params_value (params, "default_severity");
20401  text_64 = (text
20402  ? g_base64_encode ((guchar*) text, strlen (text))
20403  : g_strdup (""));
20404 
20405  if (openvas_connection_sendf (&connection,
20406  "<modify_setting"
20407  " setting_id"
20408  "=\"7eda49c5-096c-4bef-b1ab-d080d87300df\">"
20409  "<value>%s</value>"
20410  "</modify_setting>",
20411  text_64 ? text_64 : "")
20412  == -1)
20413  {
20414  g_free (text_64);
20415  openvas_connection_close (&connection);
20416  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20417  return gsad_message (credentials,
20418  "Internal error", __FUNCTION__, __LINE__,
20419  "An internal error occurred while saving settings. "
20420  "It is unclear whether all the settings were saved. "
20421  "Diagnostics: Failure to send command to manager daemon.",
20422  "/omp?cmd=get_my_settings", response_data);
20423  }
20424  g_free (text_64);
20425 
20426  entity = NULL;
20427  xml_string_append (xml,
20428  "<save_setting id=\"%s\">",
20429  "7eda49c5-096c-4bef-b1ab-d080d87300df");
20430  if (read_entity_and_string_c (&connection, &entity, &xml))
20431  {
20432  g_string_free (xml, TRUE);
20433  openvas_connection_close (&connection);
20434  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20435  return gsad_message (credentials,
20436  "Internal error", __FUNCTION__, __LINE__,
20437  "An internal error occurred while saving settings. "
20438  "It is unclear whether all the settings were saved. "
20439  "Diagnostics: Failure to receive response from manager daemon.",
20440  "/omp?cmd=get_my_settings", response_data);
20441  }
20442  xml_string_append (xml, "</save_setting>");
20443  if (! omp_success (entity))
20444  {
20445  set_http_status_from_entity (entity, response_data);
20446  modify_failed = 1;
20447  }
20448  }
20449 
20450  openvas_connection_close (&connection);
20451  if (modify_failed)
20452  return edit_my_settings (credentials, params, g_string_free (xml, FALSE),
20453  response_data);
20454  else
20455  return get_my_settings (credentials, params, g_string_free (xml, FALSE),
20456  response_data);
20457 }
20458 
20468 char *
20470  cmd_response_data_t* response_data)
20471 {
20472  GString *xml;
20473  openvas_connection_t connection;
20474  gchar *html;
20475  entity_t help_response;
20476 
20477  switch (manager_connect (credentials, &connection, &html,
20478  response_data))
20479  {
20480  case 0:
20481  break;
20482  case -1:
20483  if (html)
20484  return html;
20485  /* Fall through. */
20486  default:
20487  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20488  return gsad_message (credentials,
20489  "Internal error", __FUNCTION__, __LINE__,
20490  "An internal error occurred while getting the OMP doc. "
20491  "The resource is currently not available. "
20492  "Diagnostics: Failure to connect to manager daemon.",
20493  "/omp?cmd=get_tasks", response_data);
20494  }
20495 
20496  xml = g_string_new ("");
20497  g_string_append_printf (xml, "<get_protocol_doc>");
20498 
20499  /* Get the resource. */
20500 
20501  if (openvas_connection_sendf (&connection, "<help format=\"XML\"/>")
20502  == -1)
20503  {
20504  g_string_free (xml, TRUE);
20505  openvas_connection_close (&connection);
20506  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20507  return gsad_message (credentials,
20508  "Internal error", __FUNCTION__, __LINE__,
20509  "An internal error occurred while getting the OMP doc. "
20510  "Diagnostics: Failure to send command to manager daemon.",
20511  "/omp?cmd=get_tasks", response_data);
20512  }
20513 
20514  help_response = NULL;
20515  if (read_entity_and_string_c (&connection, &help_response, &xml))
20516  {
20517  g_string_free (xml, TRUE);
20518  openvas_connection_close (&connection);
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 getting the OMP doc. "
20523  "Diagnostics: Failure to receive response from manager daemon.",
20524  "/omp?cmd=get_tasks", response_data);
20525  }
20526  free_entity (help_response);
20527 
20528  openvas_connection_close (&connection);
20529 
20530  /* Cleanup, and return transformed XML. */
20531 
20532  g_string_append_printf (xml, "</get_protocol_doc>");
20533  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
20534  response_data);
20535 }
20536 
20549 char *
20551  enum content_type * content_type,
20552  char **content_disposition, gsize *content_length,
20553  cmd_response_data_t* response_data)
20554 {
20555  entity_t entity, response;
20556  openvas_connection_t connection;
20557  char *content = NULL;
20558  gchar *html;
20559  const char *format;
20560  time_t now;
20561  struct tm *tm;
20562 
20563  *content_length = 0;
20564 
20565  switch (manager_connect (credentials, &connection, &html,
20566  response_data))
20567  {
20568  case 0:
20569  break;
20570  case -1:
20571  if (html)
20572  return html;
20573  /* Fall through. */
20574  default:
20575  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20576  return gsad_message (credentials,
20577  "Internal error", __FUNCTION__, __LINE__,
20578  "An internal error occurred while getting the OMP doc. "
20579  "Diagnostics: Failure to connect to manager daemon.",
20580  "/omp?cmd=get_protocol_doc", response_data);
20581  }
20582 
20583  format = params_value (params, "protocol_format")
20584  ? params_value (params, "protocol_format")
20585  : "xml";
20586 
20587  if (openvas_connection_sendf (&connection,
20588  "<help format=\"%s\"/>",
20589  format)
20590  == -1)
20591  {
20592  openvas_connection_close (&connection);
20593  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20594  return gsad_message (credentials,
20595  "Internal error", __FUNCTION__, __LINE__,
20596  "An internal error occurred while getting a list. "
20597  "The list could not be delivered. "
20598  "Diagnostics: Failure to send command to manager daemon.",
20599  "/omp?cmd=get_protocol_doc", response_data);
20600  }
20601 
20602  response = NULL;
20603  if (read_entity_and_text_c (&connection, &response, &content))
20604  {
20605  openvas_connection_close (&connection);
20606  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20607  return gsad_message (credentials,
20608  "Internal error", __FUNCTION__, __LINE__,
20609  "An internal error occurred while getting OMP doc. "
20610  "Diagnostics: Failure to receive response from manager daemon.",
20611  "/omp?cmd=get_protocol_doc", response_data);
20612  }
20613  openvas_connection_close (&connection);
20614 
20615  if (strcmp (format, "xml") == 0)
20616  *content_length = strlen (content);
20617  else
20618  {
20619  char *content_64;
20620  entity = entity_child (response, "schema");
20621  if (entity == NULL)
20622  {
20623  free_entity (response);
20624  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20625  return gsad_message (credentials,
20626  "Internal error", __FUNCTION__, __LINE__,
20627  "An internal error occurred while getting OMP doc. "
20628  "Diagnostics: Schema element missing.",
20629  "/omp?cmd=get_protocol_doc", response_data);
20630  }
20631 
20632  content_64 = entity_text (entity);
20633  if (strlen (content_64) == 0)
20634  {
20635  free_entity (response);
20636  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20637  return gsad_message (credentials,
20638  "Internal error", __FUNCTION__, __LINE__,
20639  "An internal error occurred while getting OMP doc. "
20640  "Diagnostics: Schema empty.",
20641  "/omp?cmd=get_protocol_doc", response_data);
20642  }
20643 
20644  content = (char *) g_base64_decode (content_64, content_length);
20645  }
20646 
20647  now = time (NULL);
20648  tm = localtime (&now);
20649  *content_type = GSAD_CONTENT_TYPE_APP_XML;
20650  *content_disposition = g_strdup_printf ("attachment;"
20651  " filename=\"omp-%d-%d-%d.%s\"",
20652  tm->tm_mday,
20653  tm->tm_mon + 1,
20654  tm->tm_year +1900,
20655  format);
20656  free_entity (response);
20657  openvas_connection_close (&connection);
20658  return content;
20659 }
20660 
20661 
20662 /* Groups. */
20663 
20674 static char *
20675 get_group (credentials_t * credentials, params_t *params,
20676  const char *extra_xml, cmd_response_data_t* response_data)
20677 {
20678  return get_one ("group", credentials, params, extra_xml, NULL, response_data);
20679 }
20680 
20690 char *
20691 get_group_omp (credentials_t * credentials, params_t *params,
20692  cmd_response_data_t* response_data)
20693 {
20694  return get_group (credentials, params, NULL, response_data);
20695 }
20696 
20707 static char *
20708 get_groups (credentials_t * credentials, params_t *params,
20709  const char *extra_xml, cmd_response_data_t* response_data)
20710 {
20711  return get_many ("group", credentials, params, extra_xml, NULL,
20712  response_data);
20713 }
20714 
20724 char *
20725 get_groups_omp (credentials_t * credentials, params_t *params,
20726  cmd_response_data_t* response_data)
20727 {
20728  return get_groups (credentials, params, NULL, response_data);
20729 }
20730 
20741 static char *
20742 new_group (credentials_t *credentials, params_t *params, const char *extra_xml,
20743  cmd_response_data_t* response_data)
20744 {
20745  GString *xml;
20746  xml = g_string_new ("<new_group>");
20747  if (extra_xml)
20748  g_string_append (xml, extra_xml);
20749  g_string_append (xml, "</new_group>");
20750  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
20751  response_data);
20752 }
20753 
20763 char *
20764 new_group_omp (credentials_t *credentials, params_t *params,
20765  cmd_response_data_t* response_data)
20766 {
20767  return new_group (credentials, params, NULL, response_data);
20768 }
20769 
20779 char *
20781  cmd_response_data_t* response_data)
20782 {
20783  return delete_resource ("group", credentials, params, 1, "get_trash",
20784  response_data);
20785 }
20786 
20796 char *
20797 delete_group_omp (credentials_t * credentials, params_t *params,
20798  cmd_response_data_t* response_data)
20799 {
20800  return delete_resource ("group", credentials, params, 0,
20801  "get_groups", response_data);
20802 }
20803 
20813 char *
20814 create_group_omp (credentials_t *credentials, params_t *params,
20815  cmd_response_data_t* response_data)
20816 {
20817  gchar *html, *response, *command, *specials_element;
20818  const char *no_redirect, *name, *comment, *users, *grant_full;
20819  entity_t entity;
20820  GString *xml;
20821  int ret;
20822 
20823  no_redirect = params_value (params, "no_redirect");
20824  name = params_value (params, "name");
20825  comment = params_value (params, "comment");
20826  grant_full = params_value (params, "grant_full");
20827  users = params_value (params, "users");
20828 
20829  CHECK_PARAM_INVALID (name, "Create Group", "new_group");
20830  CHECK_PARAM_INVALID (comment, "Create Group", "new_group");
20831  CHECK_PARAM_INVALID (users, "Create Group", "new_group");
20832 
20833  /* Create the group. */
20834 
20835  xml = g_string_new ("");
20836 
20837  xml_string_append (xml,
20838  "<name>%s</name>"
20839  "<comment>%s</comment>"
20840  "<users>%s</users>",
20841  name,
20842  comment,
20843  users);
20844 
20845  if (grant_full)
20846  specials_element = g_strdup_printf ("<full/>");
20847  else
20848  specials_element = NULL;
20849 
20850  command = g_strdup_printf ("<create_group>"
20851  "%s"
20852  "<specials>"
20853  "%s"
20854  "</specials>"
20855  "</create_group>",
20856  xml->str,
20857  specials_element);
20858 
20859  g_string_free (xml, TRUE);
20860  g_free (specials_element);
20861 
20862  ret = omp (credentials, &response, &entity, response_data, command);
20863  g_free (command);
20864  switch (ret)
20865  {
20866  case 0:
20867  case -1:
20868  break;
20869  case 1:
20870  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20871  return gsad_message (credentials,
20872  "Internal error", __FUNCTION__, __LINE__,
20873  "An internal error occurred while creating a new group. "
20874  "No new group was created. "
20875  "Diagnostics: Failure to send command to manager daemon.",
20876  "/omp?cmd=get_groups", response_data);
20877  case 2:
20878  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20879  return gsad_message (credentials,
20880  "Internal error", __FUNCTION__, __LINE__,
20881  "An internal error occurred while creating a new group. "
20882  "It is unclear whether the group has been created or not. "
20883  "Diagnostics: Failure to receive response from manager daemon.",
20884  "/omp?cmd=get_groups", response_data);
20885  default:
20886  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
20887  return gsad_message (credentials,
20888  "Internal error", __FUNCTION__, __LINE__,
20889  "An internal error occurred while creating a new group. "
20890  "It is unclear whether the group has been created or not. "
20891  "Diagnostics: Internal Error.",
20892  "/omp?cmd=get_groups", response_data);
20893  }
20894 
20895  if (entity_attribute (entity, "id"))
20896  params_add (params, "group_id", entity_attribute (entity, "id"));
20897  html = response_from_entity (credentials, params, entity,
20898  (no_redirect && strcmp (no_redirect, "0")),
20899  NULL, "get_groups",
20900  NULL, "new_group",
20901  "Create Group", response_data);
20902  free_entity (entity);
20903  g_free (response);
20904  return html;
20905 }
20906 
20917 char *
20918 edit_group (credentials_t * credentials, params_t *params,
20919  const char *extra_xml, cmd_response_data_t* response_data)
20920 {
20921  return edit_resource ("group", credentials, params, NULL, extra_xml,
20922  response_data);
20923 }
20924 
20934 char *
20935 edit_group_omp (credentials_t * credentials, params_t *params,
20936  cmd_response_data_t* response_data)
20937 {
20938  return edit_group (credentials, params, NULL, response_data);
20939 }
20940 
20953 char *
20954 export_group_omp (credentials_t * credentials, params_t *params,
20955  enum content_type * content_type, char **content_disposition,
20956  gsize *content_length, cmd_response_data_t* response_data)
20957 {
20958  return export_resource ("group", credentials, params, content_type,
20959  content_disposition, content_length, response_data);
20960 }
20961 
20975 char *
20976 export_groups_omp (credentials_t * credentials, params_t *params,
20977  enum content_type * content_type, char **content_disposition,
20978  gsize *content_length, cmd_response_data_t* response_data)
20979 {
20980  return export_many ("group", credentials, params, content_type,
20981  content_disposition, content_length, response_data);
20982 }
20983 
20993 char *
20994 save_group_omp (credentials_t * credentials, params_t *params,
20995  cmd_response_data_t* response_data)
20996 {
20997  int ret;
20998  gchar *html, *response;
20999  const char *no_redirect, *group_id, *name, *comment, *users;
21000  entity_t entity;
21001 
21002  no_redirect = params_value (params, "no_redirect");
21003  group_id = params_value (params, "group_id");
21004  name = params_value (params, "name");
21005  comment = params_value (params, "comment");
21006  users = params_value (params, "users");
21007 
21008  CHECK_PARAM_INVALID (group_id, "Save Group", "edit_group");
21009  CHECK_PARAM_INVALID (name, "Save Group", "edit_group");
21010  CHECK_PARAM_INVALID (comment, "Save Group", "edit_group");
21011  CHECK_PARAM_INVALID (users, "Save Group", "edit_group");
21012 
21013  /* Modify the Group. */
21014 
21015  response = NULL;
21016  entity = NULL;
21017  ret = ompf (credentials,
21018  &response,
21019  &entity,
21020  response_data,
21021  "<modify_group group_id=\"%s\">"
21022  "<name>%s</name>"
21023  "<comment>%s</comment>"
21024  "<users>%s</users>"
21025  "</modify_group>",
21026  group_id,
21027  name,
21028  comment,
21029  users);
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 saving a group. "
21041  "The group was not saved. "
21042  "Diagnostics: Failure to send command to manager daemon.",
21043  "/omp?cmd=get_groups", 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 saving a group. "
21049  "It is unclear whether the group has been saved or not. "
21050  "Diagnostics: Failure to receive response from manager daemon.",
21051  "/omp?cmd=get_groups", 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 saving a group. "
21057  "It is unclear whether the group has been saved or not. "
21058  "Diagnostics: Internal Error.",
21059  "/omp?cmd=get_groups", response_data);
21060  }
21061 
21062  html = response_from_entity (credentials, params, entity,
21063  (no_redirect && strcmp (no_redirect, "0")),
21064  NULL, "get_groups",
21065  NULL, "edit_group",
21066  "Save Group", response_data);
21067  free_entity (entity);
21068  g_free (response);
21069  return html;
21070 }
21071 
21072 
21073 /* Permissions. */
21074 
21085 char *
21086 get_permission (credentials_t * credentials, params_t *params,
21087  const char *extra_xml, cmd_response_data_t* response_data)
21088 {
21089  return get_one ("permission", credentials, params, extra_xml, "alerts=\"1\"",
21090  response_data);
21091 }
21092 
21102 char *
21104  cmd_response_data_t* response_data)
21105 {
21106  return get_permission (credentials, params, NULL, response_data);
21107 }
21108 
21119 static char *
21120 get_permissions (credentials_t * credentials, params_t *params,
21121  const char *extra_xml, cmd_response_data_t* response_data)
21122 {
21123  return get_many ("permission", credentials, params, extra_xml, NULL,
21124  response_data);
21125 }
21126 
21136 char *
21138  cmd_response_data_t* response_data)
21139 {
21140  return get_permissions (credentials, params, NULL, response_data);
21141 }
21142 
21152 char *
21154  cmd_response_data_t* response_data)
21155 {
21156  return delete_resource ("permission", credentials, params, 1, "get_trash",
21157  response_data);
21158 }
21159 
21169 char *
21171  cmd_response_data_t* response_data)
21172 {
21173  return delete_resource ("permission", credentials, params, 0, NULL,
21174  response_data);
21175 }
21176 
21187 char *
21188 new_permission (credentials_t * credentials, params_t *params,
21189  const char *extra_xml, cmd_response_data_t* response_data)
21190 {
21191  GString *xml;
21192 
21193  xml = g_string_new ("<new_permission>");
21194 
21195  if (command_enabled (credentials, "GET_USERS"))
21196  {
21197  gchar *response;
21198  entity_t entity;
21199 
21200  response = NULL;
21201  entity = NULL;
21202  switch (omp (credentials, &response, &entity, response_data,
21203  "<get_users filter=\"rows=-1 permission=get_users\"/>"))
21204  {
21205  case 0:
21206  case -1:
21207  break;
21208  case 1:
21209  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21210  return gsad_message (credentials,
21211  "Internal error", __FUNCTION__, __LINE__,
21212  "An internal error occurred getting the user list. "
21213  "No new user was created. "
21214  "Diagnostics: Failure to send command to manager daemon.",
21215  "/omp?cmd=get_users", response_data);
21216  case 2:
21217  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21218  return gsad_message (credentials,
21219  "Internal error", __FUNCTION__, __LINE__,
21220  "An internal error occurred getting the user list. "
21221  "No new user was created. "
21222  "Diagnostics: Failure to receive response from manager daemon.",
21223  "/omp?cmd=get_users", response_data);
21224  default:
21225  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21226  return gsad_message (credentials,
21227  "Internal error", __FUNCTION__, __LINE__,
21228  "An internal error occurred getting the user list. "
21229  "No new user was created. "
21230  "Diagnostics: Internal Error.",
21231  "/omp?cmd=get_users", response_data);
21232  }
21233 
21234  g_string_append (xml, response);
21235 
21236  free_entity (entity);
21237  g_free (response);
21238  }
21239 
21240  if (command_enabled (credentials, "GET_GROUPS"))
21241  {
21242  gchar *response;
21243  entity_t entity;
21244 
21245  response = NULL;
21246  entity = NULL;
21247  switch (omp (credentials, &response, &entity, response_data,
21248  "<get_groups filter=\"rows=-1 permission=get_groups\"/>"))
21249  {
21250  case 0:
21251  case -1:
21252  break;
21253  case 1:
21254  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21255  return gsad_message (credentials,
21256  "Internal error", __FUNCTION__, __LINE__,
21257  "An internal error occurred getting the group list. "
21258  "No new user was created. "
21259  "Diagnostics: Failure to send command to manager daemon.",
21260  "/omp?cmd=get_users", response_data);
21261  case 2:
21262  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21263  return gsad_message (credentials,
21264  "Internal error", __FUNCTION__, __LINE__,
21265  "An internal error occurred getting the group list. "
21266  "No new user was created. "
21267  "Diagnostics: Failure to receive response from manager daemon.",
21268  "/omp?cmd=get_users", response_data);
21269  default:
21270  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21271  return gsad_message (credentials,
21272  "Internal error", __FUNCTION__, __LINE__,
21273  "An internal error occurred getting the group list. "
21274  "No new user was created. "
21275  "Diagnostics: Internal Error.",
21276  "/omp?cmd=get_users", response_data);
21277  }
21278 
21279  g_string_append (xml, response);
21280 
21281  free_entity (entity);
21282  g_free (response);
21283  }
21284 
21285  if (command_enabled (credentials, "GET_ROLES"))
21286  {
21287  gchar *response;
21288  entity_t entity;
21289 
21290  response = NULL;
21291  entity = NULL;
21292  switch (omp (credentials, &response, &entity, response_data,
21293  "<get_roles filter=\"rows=-1 permission=get_roles\"/>"))
21294  {
21295  case 0:
21296  case -1:
21297  break;
21298  case 1:
21299  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21300  return gsad_message (credentials,
21301  "Internal error", __FUNCTION__, __LINE__,
21302  "An internal error occurred getting the role list. "
21303  "No new user was created. "
21304  "Diagnostics: Failure to send command to manager daemon.",
21305  "/omp?cmd=get_users", response_data);
21306  case 2:
21307  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21308  return gsad_message (credentials,
21309  "Internal error", __FUNCTION__, __LINE__,
21310  "An internal error occurred getting the role list. "
21311  "No new user was created. "
21312  "Diagnostics: Failure to receive response from manager daemon.",
21313  "/omp?cmd=get_users", response_data);
21314  default:
21315  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21316  return gsad_message (credentials,
21317  "Internal error", __FUNCTION__, __LINE__,
21318  "An internal error occurred getting the role list. "
21319  "No new user was created. "
21320  "Diagnostics: Internal Error.",
21321  "/omp?cmd=get_users", response_data);
21322  }
21323 
21324  g_string_append (xml, response);
21325 
21326  free_entity (entity);
21327  g_free (response);
21328  }
21329 
21330  if (extra_xml)
21331  g_string_append (xml, extra_xml);
21332 
21333  g_string_append (xml, "</new_permission>");
21334 
21335  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
21336  response_data);
21337 }
21338 
21348 char *
21350  cmd_response_data_t* response_data)
21351 {
21352  return new_permission (credentials, params, NULL, response_data);
21353 }
21354 
21364 char *
21366  cmd_response_data_t* response_data)
21367 {
21368  int ret;
21369  gchar *html, *response;
21370  const char *no_redirect, *name, *comment, *resource_id, *resource_type;
21371  const char *subject_id, *subject_type, *subject_name;
21372  entity_t entity;
21373 
21374  gchar *subject_response;
21375  entity_t get_subject_entity = NULL;
21376  entity_t subject_entity;
21377 
21378  no_redirect = params_value (params, "no_redirect");
21379  name = params_value (params, "permission");
21380  comment = params_value (params, "comment");
21381  resource_id = params_value (params, "id_or_empty");
21382  resource_type = params_value (params, "optional_resource_type");
21383  subject_type = params_value (params, "subject_type");
21384  subject_name = params_value (params, "subject_name");
21385 
21386  CHECK_PARAM_INVALID (name, "Create Permission", "new_permission");
21387  CHECK_PARAM_INVALID (comment, "Create Permission", "new_permission");
21388  if (params_given (params, "id_or_empty"))
21389  CHECK_PARAM_INVALID (resource_id, "Create Permission", "new_permission");
21390  CHECK_PARAM_INVALID (subject_type, "Create Permission", "new_permission");
21391  if (params_given (params, "optional_resource_type"))
21392  CHECK_PARAM_INVALID (resource_type, "Create Permission", "new_permission");
21393 
21394  if (params_given (params, "subject_name"))
21395  {
21396  CHECK_PARAM_INVALID (subject_name,
21397  "Create Permission", "new_permission");
21398  subject_id = NULL;
21399  ret = ompf (credentials,
21400  &subject_response,
21401  &get_subject_entity,
21402  response_data,
21403  "<get_%ss filter=\"rows=1 name=%s\">"
21404  "</get_%ss>",
21405  subject_type,
21406  subject_name,
21407  subject_type);
21408 
21409  switch (ret)
21410  {
21411  case 0:
21412  case -1:
21413  break;
21414  case 1:
21415  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21416  return gsad_message (credentials,
21417  "Internal error", __FUNCTION__, __LINE__,
21418  "An internal error occurred while getting"
21419  " the subject for a permission. "
21420  "The permission was not created. "
21421  "Diagnostics: Failure to send command"
21422  " to manager daemon.",
21423  "/omp?cmd=get_permissions", response_data);
21424  case 2:
21425  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21426  return gsad_message (credentials,
21427  "Internal error", __FUNCTION__, __LINE__,
21428  "An internal error occurred while getting"
21429  " the subject for a permission. "
21430  "The permission was not created. "
21431  "Diagnostics: Failure to receive response"
21432  " from manager daemon.",
21433  "/omp?cmd=get_permissions", response_data);
21434  default:
21435  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21436  return gsad_message (credentials,
21437  "Internal error", __FUNCTION__, __LINE__,
21438  "An internal error occurred while getting"
21439  " the subject for a permission. "
21440  "The permission was not created. "
21441  "Diagnostics: Internal Error.",
21442  "/omp?cmd=get_permissions", response_data);
21443  }
21444 
21445  subject_entity = entity_child (get_subject_entity, subject_type);
21446 
21447  if (subject_entity)
21448  subject_id = entity_attribute (subject_entity, "id");
21449 
21450  if (subject_id == NULL)
21451  {
21452  gchar *msg;
21453  msg = g_strdup_printf ("<gsad_msg status_text=\"Subject not found\""
21454  " operation=\"create_permission\">"
21455  "Could not find a %s with name '%s'."
21456  "</gsad_msg>",
21457  subject_type,
21458  subject_name ? subject_name : "");
21459  return next_page (credentials, params, msg, response_data);
21460  }
21461  }
21462  else if (strcmp (subject_type, "user") == 0)
21463  subject_id = params_value (params, "permission_user_id");
21464  else if (strcmp (subject_type, "group") == 0)
21465  subject_id = params_value (params, "permission_group_id");
21466  else if (strcmp (subject_type, "role") == 0)
21467  subject_id = params_value (params, "permission_role_id");
21468  else
21469  subject_id = NULL;
21470  CHECK_PARAM_INVALID (subject_id, "Create Permission", "new_permission");
21471 
21472  /* Create the permission(s). */
21473 
21474  if (strcmp (name, "task_proxy") == 0)
21475  {
21476  response = NULL;
21477  entity = NULL;
21478  ret = ompf (credentials,
21479  &response,
21480  &entity,
21481  response_data,
21482  "<commands>"
21483  "<create_permission>"
21484  "<name>get_tasks</name>"
21485  "<comment>%s</comment>"
21486  "<resource id=\"%s\"/>"
21487  "<subject id=\"%s\"><type>%s</type></subject>"
21488  "</create_permission>"
21489  "<create_permission>"
21490  "<name>modify_task</name>"
21491  "<comment>%s</comment>"
21492  "<resource id=\"%s\"/>"
21493  "<subject id=\"%s\"><type>%s</type></subject>"
21494  "</create_permission>"
21495  "<create_permission>"
21496  "<name>start_task</name>"
21497  "<comment>%s</comment>"
21498  "<resource id=\"%s\"/>"
21499  "<subject id=\"%s\"><type>%s</type></subject>"
21500  "</create_permission>"
21501  "<create_permission>"
21502  "<name>stop_task</name>"
21503  "<comment>%s</comment>"
21504  "<resource id=\"%s\"/>"
21505  "<subject id=\"%s\"><type>%s</type></subject>"
21506  "</create_permission>"
21507  "<create_permission>"
21508  "<name>resume_task</name>"
21509  "<comment>%s</comment>"
21510  "<resource id=\"%s\"/>"
21511  "<subject id=\"%s\"><type>%s</type></subject>"
21512  "</create_permission>"
21513  "</commands>",
21514  comment ? comment : "",
21515  resource_id ? resource_id : "",
21516  subject_id,
21517  subject_type,
21518  comment ? comment : "",
21519  resource_id ? resource_id : "",
21520  subject_id,
21521  subject_type,
21522  comment ? comment : "",
21523  resource_id ? resource_id : "",
21524  subject_id,
21525  subject_type,
21526  comment ? comment : "",
21527  resource_id ? resource_id : "",
21528  subject_id,
21529  subject_type,
21530  comment ? comment : "",
21531  resource_id ? resource_id : "",
21532  subject_id,
21533  subject_type);
21534 
21535  if (get_subject_entity)
21536  free_entity (get_subject_entity);
21537 
21538  switch (ret)
21539  {
21540  case 0:
21541  case -1:
21542  break;
21543  case 1:
21544  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21545  return gsad_message (credentials,
21546  "Internal error", __FUNCTION__, __LINE__,
21547  "An internal error occurred while creating a permission. "
21548  "The permission was not created. "
21549  "Diagnostics: Failure to send command to manager daemon.",
21550  "/omp?cmd=get_permissions", response_data);
21551  case 2:
21552  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21553  return gsad_message (credentials,
21554  "Internal error", __FUNCTION__, __LINE__,
21555  "An internal error occurred while creating a permission. "
21556  "It is unclear whether the permission has been created or not. "
21557  "Diagnostics: Failure to receive response from manager daemon.",
21558  "/omp?cmd=get_permissions", response_data);
21559  default:
21560  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21561  return gsad_message (credentials,
21562  "Internal error", __FUNCTION__, __LINE__,
21563  "An internal error occurred while creating a permission. "
21564  "It is unclear whether the permission has been created or not. "
21565  "Diagnostics: Internal Error.",
21566  "/omp?cmd=get_permissions", response_data);
21567  }
21568 
21569  if (entity_attribute (entity, "id"))
21570  params_add (params, "permission_id", entity_attribute (entity, "id"));
21571  html = response_from_entity (credentials, params, entity,
21572  (no_redirect && strcmp (no_redirect, "0")),
21573  NULL, "get_permissions",
21574  NULL, "new_permission",
21575  "Create Permission", response_data);
21576  }
21577  else
21578  {
21579  response = NULL;
21580  entity = NULL;
21581  ret = ompf (credentials,
21582  &response,
21583  &entity,
21584  response_data,
21585  "<create_permission>"
21586  "<name>%s</name>"
21587  "<comment>%s</comment>"
21588  "<resource id=\"%s\">"
21589  "<type>%s</type>"
21590  "</resource>"
21591  "<subject id=\"%s\"><type>%s</type></subject>"
21592  "</create_permission>",
21593  name,
21594  comment ? comment : "",
21595  resource_id ? resource_id : "",
21596  resource_type ? resource_type : "",
21597  subject_id,
21598  subject_type);
21599 
21600  if (get_subject_entity)
21601  free_entity (get_subject_entity);
21602 
21603  switch (ret)
21604  {
21605  case 0:
21606  case -1:
21607  break;
21608  case 1:
21609  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21610  return gsad_message (credentials,
21611  "Internal error", __FUNCTION__, __LINE__,
21612  "An internal error occurred while creating a permission. "
21613  "The permission was not created. "
21614  "Diagnostics: Failure to send command to manager daemon.",
21615  "/omp?cmd=get_permissions", response_data);
21616  case 2:
21617  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21618  return gsad_message (credentials,
21619  "Internal error", __FUNCTION__, __LINE__,
21620  "An internal error occurred while creating a permission. "
21621  "It is unclear whether the permission has been created or not. "
21622  "Diagnostics: Failure to receive response from manager daemon.",
21623  "/omp?cmd=get_permissions", response_data);
21624  default:
21625  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21626  return gsad_message (credentials,
21627  "Internal error", __FUNCTION__, __LINE__,
21628  "An internal error occurred while creating a permission. "
21629  "It is unclear whether the permission has been created or not. "
21630  "Diagnostics: Internal Error.",
21631  "/omp?cmd=get_permissions", response_data);
21632  }
21633 
21634  if (entity_attribute (entity, "id"))
21635  params_add (params, "permission_id", entity_attribute (entity, "id"));
21636  html = response_from_entity (credentials, params, entity,
21637  (no_redirect && strcmp (no_redirect, "0")),
21638  NULL, "get_permissions",
21639  NULL, "new_permission",
21640  "Create Permission", response_data);
21641  }
21642  free_entity (entity);
21643  g_free (response);
21644  return html;
21645 }
21646 
21647 #define CHECK_OMPF_RET \
21648  switch (ret) \
21649  { \
21650  case 0: \
21651  case -1: \
21652  break; \
21653  case 1: \
21654  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21655  return gsad_message (credentials, \
21656  "Internal error", __FUNCTION__, __LINE__, \
21657  "An internal error occurred while creating a permission. " \
21658  "The permission was not created. " \
21659  "Diagnostics: Failure to send command to manager daemon.", \
21660  "/omp?cmd=get_permissions", response_data); \
21661  case 2: \
21662  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21663  return gsad_message (credentials, \
21664  "Internal error", __FUNCTION__, __LINE__, \
21665  "An internal error occurred while creating a permission. " \
21666  "It is unclear whether the permission has been created or not. " \
21667  "Diagnostics: Failure to receive response from manager daemon.", \
21668  "/omp?cmd=get_permissions", response_data); \
21669  default: \
21670  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR; \
21671  return gsad_message (credentials, \
21672  "Internal error", __FUNCTION__, __LINE__, \
21673  "An internal error occurred while creating a permission. " \
21674  "It is unclear whether the permission has been created or not. " \
21675  "Diagnostics: Internal Error.", \
21676  "/omp?cmd=get_permissions", response_data); \
21677  } \
21678  if (omp_success (entity)) \
21679  { \
21680  successes ++; \
21681  free_entity (entity); \
21682  g_free (response); \
21683  } \
21684  else \
21685  { \
21686  html \
21687  = response_from_entity (credentials, params, entity, \
21688  (no_redirect && strcmp (no_redirect, "0")), \
21689  NULL, "get_permissions", \
21690  NULL, "new_permissions", \
21691  "Create Permissions", response_data); \
21692  free_entity (entity); \
21693  g_free (response); \
21694  return html; \
21695  } \
21696 
21697 
21707 char *
21708 new_permissions (credentials_t * credentials, params_t *params,
21709  const char *extra_xml, cmd_response_data_t* response_data)
21710 {
21711  GString *xml;
21712  const char *resource_id, *restrict_type;
21713  params_t *related;
21714  params_iterator_t related_iterator;
21715  param_t *related_param;
21716  char *related_id;
21717 
21718  xml = g_string_new ("<new_permissions>");
21719 
21720  if (command_enabled (credentials, "GET_USERS"))
21721  {
21722  gchar *response;
21723  entity_t entity;
21724 
21725  response = NULL;
21726  entity = NULL;
21727  switch (omp (credentials, &response, &entity, response_data,
21728  "<get_users filter=\"rows=-1 permission=get_users\"/>"))
21729  {
21730  case 0:
21731  case -1:
21732  break;
21733  case 1:
21734  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21735  return gsad_message (credentials,
21736  "Internal error", __FUNCTION__, __LINE__,
21737  "An internal error occurred getting the user list. "
21738  "No new user was created. "
21739  "Diagnostics: Failure to send command to manager daemon.",
21740  "/omp?cmd=get_users", response_data);
21741  case 2:
21742  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21743  return gsad_message (credentials,
21744  "Internal error", __FUNCTION__, __LINE__,
21745  "An internal error occurred getting the user list. "
21746  "No new user was created. "
21747  "Diagnostics: Failure to receive response from manager daemon.",
21748  "/omp?cmd=get_users", response_data);
21749  default:
21750  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21751  return gsad_message (credentials,
21752  "Internal error", __FUNCTION__, __LINE__,
21753  "An internal error occurred getting the user list. "
21754  "No new user was created. "
21755  "Diagnostics: Internal Error.",
21756  "/omp?cmd=get_users", response_data);
21757  }
21758 
21759  g_string_append (xml, response);
21760 
21761  free_entity (entity);
21762  g_free (response);
21763  }
21764 
21765  if (command_enabled (credentials, "GET_GROUPS"))
21766  {
21767  gchar *response;
21768  entity_t entity;
21769 
21770  response = NULL;
21771  entity = NULL;
21772  switch (omp (credentials, &response, &entity, response_data,
21773  "<get_groups filter=\"rows=-1 permission=get_groups\"/>"))
21774  {
21775  case 0:
21776  case -1:
21777  break;
21778  case 1:
21779  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21780  return gsad_message (credentials,
21781  "Internal error", __FUNCTION__, __LINE__,
21782  "An internal error occurred getting the group list. "
21783  "No new user was created. "
21784  "Diagnostics: Failure to send command to manager daemon.",
21785  "/omp?cmd=get_users", response_data);
21786  case 2:
21787  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21788  return gsad_message (credentials,
21789  "Internal error", __FUNCTION__, __LINE__,
21790  "An internal error occurred getting the group list. "
21791  "No new user was created. "
21792  "Diagnostics: Failure to receive response from manager daemon.",
21793  "/omp?cmd=get_users", response_data);
21794  default:
21795  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21796  return gsad_message (credentials,
21797  "Internal error", __FUNCTION__, __LINE__,
21798  "An internal error occurred getting the group list. "
21799  "No new user was created. "
21800  "Diagnostics: Internal Error.",
21801  "/omp?cmd=get_users", response_data);
21802  }
21803 
21804  g_string_append (xml, response);
21805 
21806  free_entity (entity);
21807  g_free (response);
21808  }
21809 
21810  if (command_enabled (credentials, "GET_ROLES"))
21811  {
21812  gchar *response;
21813  entity_t entity;
21814 
21815  response = NULL;
21816  entity = NULL;
21817  switch (omp (credentials, &response, &entity, response_data,
21818  "<get_roles filter=\"rows=-1 permission=get_roles\"/>"))
21819  {
21820  case 0:
21821  case -1:
21822  break;
21823  case 1:
21824  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21825  return gsad_message (credentials,
21826  "Internal error", __FUNCTION__, __LINE__,
21827  "An internal error occurred getting the role list. "
21828  "No new user was created. "
21829  "Diagnostics: Failure to send command to manager daemon.",
21830  "/omp?cmd=get_users", response_data);
21831  case 2:
21832  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21833  return gsad_message (credentials,
21834  "Internal error", __FUNCTION__, __LINE__,
21835  "An internal error occurred getting the role list. "
21836  "No new user was created. "
21837  "Diagnostics: Failure to receive response from manager daemon.",
21838  "/omp?cmd=get_users", response_data);
21839  default:
21840  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
21841  return gsad_message (credentials,
21842  "Internal error", __FUNCTION__, __LINE__,
21843  "An internal error occurred getting the role list. "
21844  "No new user was created. "
21845  "Diagnostics: Internal Error.",
21846  "/omp?cmd=get_users", response_data);
21847  }
21848 
21849  g_string_append (xml, response);
21850 
21851  free_entity (entity);
21852  g_free (response);
21853  }
21854 
21855  resource_id = params_value (params, "resource_id");
21856  restrict_type = params_value (params, "restrict_type");
21857  if (resource_id && restrict_type)
21858  {
21859  gchar *get_command;
21860  gchar *response;
21861  entity_t entity;
21862 
21863  get_command = g_strdup_printf ("get_%ss", restrict_type);
21864  response = NULL;
21865  entity = NULL;
21866  switch (ompf (credentials, &response, &entity, response_data,
21867  "<%s %s_id=\"%s\" details=\"0\"/>",
21868  get_command, restrict_type, resource_id))
21869  {
21870  case 0:
21871  case -1:
21872  break;
21873  case 1:
21874  g_free (get_command);
21875  response_data->http_status_code
21876  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21877  return gsad_message (credentials,
21878  "Internal error", __FUNCTION__, __LINE__,
21879  "An internal error occurred getting a resource. "
21880  "No new permission was created. "
21881  "Diagnostics: Failure to send command to manager daemon.",
21882  "/omp?cmd=get_users", response_data);
21883  case 2:
21884  g_free (get_command);
21885  response_data->http_status_code
21886  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21887  return gsad_message (credentials,
21888  "Internal error", __FUNCTION__, __LINE__,
21889  "An internal error occurred getting a resource. "
21890  "No new permission was created. "
21891  "Diagnostics: Failure to receive response from manager daemon.",
21892  "/omp?cmd=get_users", response_data);
21893  default:
21894  g_free (get_command);
21895  response_data->http_status_code
21896  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21897  return gsad_message (credentials,
21898  "Internal error", __FUNCTION__, __LINE__,
21899  "An internal error occurred getting a resource. "
21900  "No new permission was created. "
21901  "Diagnostics: Internal Error.",
21902  "/omp?cmd=get_users", response_data);
21903  }
21904  g_string_append (xml, response);
21905 
21906  free_entity (entity);
21907  g_free (response);
21908  }
21909 
21910  related = params_values (params, "related:");
21911  params_iterator_init (&related_iterator, related);
21912  while (params_iterator_next (&related_iterator, &related_id, &related_param))
21913  {
21914  const char* related_type;
21915  gchar *get_command;
21916  gchar *response;
21917  entity_t entity;
21918 
21919  related_type = related_param->value;
21920 
21921  get_command = g_strdup_printf ("get_%ss", related_type);
21922  response = NULL;
21923  entity = NULL;
21924  switch (ompf (credentials, &response, &entity, response_data,
21925  "<%s %s_id=\"%s\" details=\"0\"/>",
21926  get_command, related_type, related_id))
21927  {
21928  case 0:
21929  case -1:
21930  break;
21931  case 1:
21932  g_free (get_command);
21933  response_data->http_status_code
21934  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21935  return gsad_message (credentials,
21936  "Internal error", __FUNCTION__, __LINE__,
21937  "An internal error occurred getting a resource. "
21938  "No new permission was created. "
21939  "Diagnostics: Failure to send command to manager daemon.",
21940  "/omp?cmd=get_users", response_data);
21941  case 2:
21942  g_free (get_command);
21943  response_data->http_status_code
21944  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21945  return gsad_message (credentials,
21946  "Internal error", __FUNCTION__, __LINE__,
21947  "An internal error occurred getting a resource. "
21948  "No new permission was created. "
21949  "Diagnostics: Failure to receive response from manager daemon.",
21950  "/omp?cmd=get_users", response_data);
21951  default:
21952  g_free (get_command);
21953  response_data->http_status_code
21954  = MHD_HTTP_INTERNAL_SERVER_ERROR;
21955  return gsad_message (credentials,
21956  "Internal error", __FUNCTION__, __LINE__,
21957  "An internal error occurred getting a resource. "
21958  "No new permission was created. "
21959  "Diagnostics: Internal Error.",
21960  "/omp?cmd=get_users", response_data);
21961  }
21962  g_string_append (xml, response);
21963 
21964  free_entity (entity);
21965  g_free (response);
21966  }
21967 
21968  if (extra_xml)
21969  g_string_append (xml, extra_xml);
21970 
21971  g_string_append (xml, "</new_permissions>");
21972 
21973  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
21974  response_data);
21975 }
21976 
21986 char *
21988  cmd_response_data_t* response_data)
21989 {
21990  return new_permissions (credentials, params, NULL, response_data);
21991 }
21992 
22002 char *
22004  cmd_response_data_t* response_data)
22005 {
22006  int ret;
22007  gchar *html, *response, *summary_response, *next_url;
22008  int successes;
22009  const char *no_redirect, *permission, *comment, *resource_id, *resource_type;
22010  const char *subject_id, *subject_type, *subject_name;
22011  int include_related;
22012 
22013  entity_t entity;
22014 
22015  gchar *subject_response;
22016  entity_t get_subject_entity = NULL;
22017  entity_t subject_entity;
22018 
22019  no_redirect = params_value (params, "no_redirect");
22020  permission = params_value (params, "permission");
22021  comment = params_value (params, "comment");
22022  resource_id = params_value (params, "resource_id");
22023  resource_type = params_value (params, "resource_type");
22024  subject_type = params_value (params, "subject_type");
22025  subject_name = params_value (params, "subject_name");
22026 
22027  include_related = atoi (params_value (params, "include_related"));
22028  CHECK_PARAM (params_value (params, "include_related"),
22029  "Create Permission", new_permission);
22030 
22031  CHECK_PARAM (permission, "Create Permission", new_permission);
22032  CHECK_PARAM (comment, "Create Permission", new_permission);
22033  CHECK_PARAM (resource_id, "Create Permission", new_permission);
22034  CHECK_PARAM (subject_type, "Create Permission", new_permission);
22035  CHECK_PARAM (resource_type, "Create Permission", new_permission);
22036 
22037  if (params_given (params, "subject_name"))
22038  {
22039  CHECK_PARAM (subject_name, "Create Permission", new_permission);
22040  subject_id = NULL;
22041  ret = ompf (credentials,
22042  &subject_response,
22043  &get_subject_entity,
22044  response_data,
22045  "<get_%ss filter=\"rows=1 name=%s\">"
22046  "</get_%ss>",
22047  subject_type,
22048  subject_name,
22049  subject_type);
22050 
22051  switch (ret)
22052  {
22053  case 0:
22054  case -1:
22055  break;
22056  case 1:
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 while getting"
22061  " the subject for a permission. "
22062  "The permission was not created. "
22063  "Diagnostics: Failure to send command"
22064  " to manager daemon.",
22065  "/omp?cmd=get_permissions", response_data);
22066  case 2:
22067  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22068  return gsad_message (credentials,
22069  "Internal error", __FUNCTION__, __LINE__,
22070  "An internal error occurred while getting"
22071  " the subject for a permission. "
22072  "The permission was not created. "
22073  "Diagnostics: Failure to receive response"
22074  " from manager daemon.",
22075  "/omp?cmd=get_permissions", response_data);
22076  default:
22077  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22078  return gsad_message (credentials,
22079  "Internal error", __FUNCTION__, __LINE__,
22080  "An internal error occurred while getting"
22081  " the subject for a permission. "
22082  "The permission was not created. "
22083  "Diagnostics: Internal Error.",
22084  "/omp?cmd=get_permissions", response_data);
22085  }
22086 
22087  subject_entity = entity_child (get_subject_entity, subject_type);
22088 
22089  if (subject_entity)
22090  subject_id = entity_attribute (subject_entity, "id");
22091 
22092  if (subject_id == NULL)
22093  {
22094  gchar *msg;
22095  msg = g_strdup_printf ("<gsad_msg status_text=\"Subject not found\""
22096  " operation=\"create_permission\">"
22097  "Could not find a %s with name '%s'."
22098  "</gsad_msg>",
22099  subject_type,
22100  subject_name ? subject_name : "");
22101  return next_page (credentials, params, msg, response_data);
22102  }
22103  }
22104  else if (strcmp (subject_type, "user") == 0)
22105  subject_id = params_value (params, "permission_user_id");
22106  else if (strcmp (subject_type, "group") == 0)
22107  subject_id = params_value (params, "permission_group_id");
22108  else if (strcmp (subject_type, "role") == 0)
22109  subject_id = params_value (params, "permission_role_id");
22110  else
22111  subject_id = NULL;
22112  CHECK_PARAM (subject_id, "Create Permission", new_permission);
22113 
22114  successes = 0;
22115 
22116  /* Create the permission(s). */
22117 
22118  // Main resource permissions
22119  if (include_related != 2)
22120  {
22121  if (strcmp (permission, "read") == 0
22122  || strcmp (permission, "proxy") == 0)
22123  {
22124  response = NULL;
22125  entity = NULL;
22126  ret = ompf (credentials,
22127  &response,
22128  &entity,
22129  response_data,
22130  "<create_permission>"
22131  "<name>get_%ss</name>"
22132  "<comment>%s</comment>"
22133  "<resource id=\"%s\">"
22134  "</resource>"
22135  "<subject id=\"%s\"><type>%s</type></subject>"
22136  "</create_permission>",
22137  resource_type,
22138  comment ? comment : "",
22139  resource_id,
22140  subject_id,
22141  subject_type);
22142 
22144  }
22145 
22146  if ((strcmp (permission, "proxy") == 0)
22147  && strcmp (resource_type, "result")
22148  && strcmp (resource_type, "report"))
22149  {
22150  response = NULL;
22151  entity = NULL;
22152  ret = ompf (credentials,
22153  &response,
22154  &entity,
22155  response_data,
22156  "<create_permission>"
22157  "<name>modify_%s</name>"
22158  "<comment>%s</comment>"
22159  "<resource id=\"%s\">"
22160  "</resource>"
22161  "<subject id=\"%s\"><type>%s</type></subject>"
22162  "</create_permission>",
22163  resource_type,
22164  comment ? comment : "",
22165  resource_id,
22166  subject_id,
22167  subject_type);
22168 
22170 
22171  if (strcmp (resource_type, "task") == 0)
22172  {
22173  response = NULL;
22174  entity = NULL;
22175  ret = ompf (credentials,
22176  &response,
22177  &entity,
22178  response_data,
22179  "<create_permission>"
22180  "<name>start_%s</name>"
22181  "<comment>%s</comment>"
22182  "<resource id=\"%s\">"
22183  "</resource>"
22184  "<subject id=\"%s\"><type>%s</type></subject>"
22185  "</create_permission>",
22186  resource_type,
22187  comment ? comment : "",
22188  resource_id,
22189  subject_id,
22190  subject_type);
22191 
22193 
22194  response = NULL;
22195  entity = NULL;
22196  ret = ompf (credentials,
22197  &response,
22198  &entity,
22199  response_data,
22200  "<create_permission>"
22201  "<name>stop_%s</name>"
22202  "<comment>%s</comment>"
22203  "<resource id=\"%s\">"
22204  "</resource>"
22205  "<subject id=\"%s\"><type>%s</type></subject>"
22206  "</create_permission>",
22207  resource_type,
22208  comment ? comment : "",
22209  resource_id,
22210  subject_id,
22211  subject_type);
22212 
22214 
22215  response = NULL;
22216  entity = NULL;
22217  ret = ompf (credentials,
22218  &response,
22219  &entity,
22220  response_data,
22221  "<create_permission>"
22222  "<name>resume_%s</name>"
22223  "<comment>%s</comment>"
22224  "<resource id=\"%s\">"
22225  "</resource>"
22226  "<subject id=\"%s\"><type>%s</type></subject>"
22227  "</create_permission>",
22228  resource_type,
22229  comment ? comment : "",
22230  resource_id,
22231  subject_id,
22232  subject_type);
22233 
22235  }
22236 
22237  if (strcmp (resource_type, "alert") == 0)
22238  {
22239  response = NULL;
22240  entity = NULL;
22241  ret = ompf (credentials,
22242  &response,
22243  &entity,
22244  response_data,
22245  "<create_permission>"
22246  "<name>test_%s</name>"
22247  "<comment>%s</comment>"
22248  "<resource id=\"%s\">"
22249  "</resource>"
22250  "<subject id=\"%s\"><type>%s</type></subject>"
22251  "</create_permission>",
22252  resource_type,
22253  comment ? comment : "",
22254  resource_id,
22255  subject_id,
22256  subject_type);
22257 
22259  }
22260 
22261  if (strcmp (resource_type, "agent") == 0
22262  || strcmp (resource_type, "report_format") == 0
22263  || strcmp (resource_type, "scanner") == 0)
22264  {
22265  response = NULL;
22266  entity = NULL;
22267  ret = ompf (credentials,
22268  &response,
22269  &entity,
22270  response_data,
22271  "<create_permission>"
22272  "<name>verify_%s</name>"
22273  "<comment>%s</comment>"
22274  "<resource id=\"%s\">"
22275  "</resource>"
22276  "<subject id=\"%s\"><type>%s</type></subject>"
22277  "</create_permission>",
22278  resource_type,
22279  comment ? comment : "",
22280  resource_id,
22281  subject_id,
22282  subject_type);
22283 
22285  }
22286  }
22287  }
22288 
22289  // Related permissions
22290  if (include_related)
22291  {
22292  params_t *related;
22293  related = params_values (params, "related:");
22294  if (related)
22295  {
22296  params_iterator_t iter;
22297  char *name;
22298  param_t *param;
22299 
22300  params_iterator_init (&iter, related);
22301  while (params_iterator_next (&iter, &name, &param))
22302  {
22303  char *related_id = name;
22304  char *related_type = param->value;
22305 
22306  if (strcmp (permission, "read") == 0
22307  || strcmp (permission, "proxy") == 0)
22308  {
22309  response = NULL;
22310  entity = NULL;
22311  ret = ompf (credentials,
22312  &response,
22313  &entity,
22314  response_data,
22315  "<create_permission>"
22316  "<name>get_%ss</name>"
22317  "<comment>%s</comment>"
22318  "<resource id=\"%s\">"
22319  "</resource>"
22320  "<subject id=\"%s\"><type>%s</type></subject>"
22321  "</create_permission>",
22322  related_type,
22323  comment ? comment : "",
22324  related_id,
22325  subject_id,
22326  subject_type);
22327 
22329  }
22330 
22331  if ((strcmp (permission, "proxy") == 0)
22332  && strcmp (related_type, "result")
22333  && strcmp (related_type, "report"))
22334  {
22335  response = NULL;
22336  entity = NULL;
22337  ret = ompf (credentials,
22338  &response,
22339  &entity,
22340  response_data,
22341  "<create_permission>"
22342  "<name>modify_%s</name>"
22343  "<comment>%s</comment>"
22344  "<resource id=\"%s\">"
22345  "</resource>"
22346  "<subject id=\"%s\"><type>%s</type></subject>"
22347  "</create_permission>",
22348  related_type,
22349  comment ? comment : "",
22350  related_id,
22351  subject_id,
22352  subject_type);
22353 
22355 
22356  if (strcmp (related_type, "task") == 0)
22357  {
22358  response = NULL;
22359  entity = NULL;
22360  ret = ompf (credentials,
22361  &response,
22362  &entity,
22363  response_data,
22364  "<create_permission>"
22365  "<name>start_%s</name>"
22366  "<comment>%s</comment>"
22367  "<resource id=\"%s\">"
22368  "</resource>"
22369  "<subject id=\"%s\"><type>%s</type></subject>"
22370  "</create_permission>",
22371  related_type,
22372  comment ? comment : "",
22373  related_id,
22374  subject_id,
22375  subject_type);
22376 
22378 
22379  response = NULL;
22380  entity = NULL;
22381  ret = ompf (credentials,
22382  &response,
22383  &entity,
22384  response_data,
22385  "<create_permission>"
22386  "<name>stop_%s</name>"
22387  "<comment>%s</comment>"
22388  "<resource id=\"%s\">"
22389  "</resource>"
22390  "<subject id=\"%s\"><type>%s</type></subject>"
22391  "</create_permission>",
22392  related_type,
22393  comment ? comment : "",
22394  related_id,
22395  subject_id,
22396  subject_type);
22397 
22399 
22400  response = NULL;
22401  entity = NULL;
22402  ret = ompf (credentials,
22403  &response,
22404  &entity,
22405  response_data,
22406  "<create_permission>"
22407  "<name>resume_%s</name>"
22408  "<comment>%s</comment>"
22409  "<resource id=\"%s\">"
22410  "</resource>"
22411  "<subject id=\"%s\"><type>%s</type></subject>"
22412  "</create_permission>",
22413  related_type,
22414  comment ? comment : "",
22415  related_id,
22416  subject_id,
22417  subject_type);
22418 
22420  }
22421 
22422  if (strcmp (related_type, "alert") == 0)
22423  {
22424  response = NULL;
22425  entity = NULL;
22426  ret = ompf (credentials,
22427  &response,
22428  &entity,
22429  response_data,
22430  "<create_permission>"
22431  "<name>test_%s</name>"
22432  "<comment>%s</comment>"
22433  "<resource id=\"%s\">"
22434  "</resource>"
22435  "<subject id=\"%s\"><type>%s</type></subject>"
22436  "</create_permission>",
22437  related_type,
22438  comment ? comment : "",
22439  related_id,
22440  subject_id,
22441  subject_type);
22442 
22444  }
22445 
22446  if (strcmp (related_type, "agent") == 0
22447  || strcmp (related_type, "report_format") == 0
22448  || strcmp (related_type, "scanner") == 0)
22449  {
22450  response = NULL;
22451  entity = NULL;
22452  ret = ompf (credentials,
22453  &response,
22454  &entity,
22455  response_data,
22456  "<create_permission>"
22457  "<name>verify_%s</name>"
22458  "<comment>%s</comment>"
22459  "<resource id=\"%s\">"
22460  "</resource>"
22461  "<subject id=\"%s\"><type>%s</type></subject>"
22462  "</create_permission>",
22463  related_type,
22464  comment ? comment : "",
22465  related_id,
22466  subject_id,
22467  subject_type);
22468 
22470  }
22471  }
22472  }
22473  }
22474  }
22475 
22476  if (get_subject_entity)
22477  free_entity (get_subject_entity);
22478 
22479  summary_response = g_strdup_printf("Successfully created %i permissions",
22480  successes);
22481 
22482  next_url = next_page_url (credentials, params,
22483  NULL, "new_permissions",
22484  "Create Permissions",
22485  G_STRINGIFY (MHD_HTTP_CREATED),
22486  summary_response);
22487 
22488  if (no_redirect)
22489  {
22490  html = action_result_page (credentials, response_data,
22491  "Create Permissions",
22492  G_STRINGIFY (MHD_HTTP_CREATED),
22493  summary_response,
22494  next_url);
22495  g_free (next_url);
22496  }
22497  else
22498  {
22499  html = NULL;
22500  response_data->redirect = next_url;
22501  }
22502  return html;
22503 }
22504 
22505 #undef CHECK_OMPF_RET
22506 
22517 char *
22518 edit_permission (credentials_t * credentials, params_t *params,
22519  const char *extra_xml, cmd_response_data_t* response_data)
22520 {
22521  gchar *html;
22522  GString *extra;
22523 
22524  extra = g_string_new ("");
22525 
22526  if (command_enabled (credentials, "GET_USERS"))
22527  {
22528  gchar *response;
22529  entity_t entity;
22530 
22531  response = NULL;
22532  entity = NULL;
22533  switch (omp (credentials, &response, &entity, response_data,
22534  "<get_users filter=\"rows=-1\"/>"))
22535  {
22536  case 0:
22537  case -1:
22538  break;
22539  case 1:
22540  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22541  return gsad_message (credentials,
22542  "Internal error", __FUNCTION__, __LINE__,
22543  "An internal error occurred getting the user list. "
22544  "No new user was created. "
22545  "Diagnostics: Failure to send command to manager daemon.",
22546  "/omp?cmd=get_users", response_data);
22547  case 2:
22548  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22549  return gsad_message (credentials,
22550  "Internal error", __FUNCTION__, __LINE__,
22551  "An internal error occurred getting the user list. "
22552  "No new user was created. "
22553  "Diagnostics: Failure to receive response from manager daemon.",
22554  "/omp?cmd=get_users", response_data);
22555  default:
22556  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22557  return gsad_message (credentials,
22558  "Internal error", __FUNCTION__, __LINE__,
22559  "An internal error occurred getting the user list. "
22560  "No new user was created. "
22561  "Diagnostics: Internal Error.",
22562  "/omp?cmd=get_users", response_data);
22563  }
22564 
22565  g_string_append (extra, response);
22566 
22567  free_entity (entity);
22568  g_free (response);
22569  }
22570 
22571  if (command_enabled (credentials, "GET_GROUPS"))
22572  {
22573  gchar *response;
22574  entity_t entity;
22575 
22576  response = NULL;
22577  entity = NULL;
22578  switch (omp (credentials, &response, &entity, response_data,
22579  "<get_groups filter=\"rows=-1\"/>"))
22580  {
22581  case 0:
22582  case -1:
22583  break;
22584  case 1:
22585  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22586  return gsad_message (credentials,
22587  "Internal error", __FUNCTION__, __LINE__,
22588  "An internal error occurred getting the group list. "
22589  "No new user was created. "
22590  "Diagnostics: Failure to send command to manager daemon.",
22591  "/omp?cmd=get_users", response_data);
22592  case 2:
22593  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22594  return gsad_message (credentials,
22595  "Internal error", __FUNCTION__, __LINE__,
22596  "An internal error occurred getting the group list. "
22597  "No new user was created. "
22598  "Diagnostics: Failure to receive response from manager daemon.",
22599  "/omp?cmd=get_users", response_data);
22600  default:
22601  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22602  return gsad_message (credentials,
22603  "Internal error", __FUNCTION__, __LINE__,
22604  "An internal error occurred getting the group list. "
22605  "No new user was created. "
22606  "Diagnostics: Internal Error.",
22607  "/omp?cmd=get_users", response_data);
22608  }
22609 
22610  g_string_append (extra, response);
22611 
22612  free_entity (entity);
22613  g_free (response);
22614  }
22615 
22616  if (command_enabled (credentials, "GET_ROLES"))
22617  {
22618  gchar *response;
22619  entity_t entity;
22620 
22621  response = NULL;
22622  entity = NULL;
22623  switch (omp (credentials, &response, &entity, response_data,
22624  "<get_roles filter=\"rows=-1\"/>"))
22625  {
22626  case 0:
22627  case -1:
22628  break;
22629  case 1:
22630  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22631  return gsad_message (credentials,
22632  "Internal error", __FUNCTION__, __LINE__,
22633  "An internal error occurred getting the role list. "
22634  "No new user was created. "
22635  "Diagnostics: Failure to send command to manager daemon.",
22636  "/omp?cmd=get_users", response_data);
22637  case 2:
22638  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22639  return gsad_message (credentials,
22640  "Internal error", __FUNCTION__, __LINE__,
22641  "An internal error occurred getting the role list. "
22642  "No new user was created. "
22643  "Diagnostics: Failure to receive response from manager daemon.",
22644  "/omp?cmd=get_users", response_data);
22645  default:
22646  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22647  return gsad_message (credentials,
22648  "Internal error", __FUNCTION__, __LINE__,
22649  "An internal error occurred getting the role list. "
22650  "No new user was created. "
22651  "Diagnostics: Internal Error.",
22652  "/omp?cmd=get_users", response_data);
22653  }
22654 
22655  g_string_append (extra, response);
22656 
22657  free_entity (entity);
22658  g_free (response);
22659  }
22660 
22661  if (extra_xml)
22662  g_string_append (extra, extra_xml);
22663  html = edit_resource ("permission", credentials, params, NULL, extra->str,
22664  response_data);
22665  g_string_free (extra, TRUE);
22666  return html;
22667 }
22668 
22678 char *
22680  cmd_response_data_t* response_data)
22681 {
22682  return edit_permission (credentials, params, NULL, response_data);
22683 }
22684 
22697 char *
22699  enum content_type * content_type,
22700  char **content_disposition, gsize *content_length,
22701  cmd_response_data_t* response_data)
22702 {
22703  return export_resource ("permission", credentials, params, content_type,
22704  content_disposition, content_length, response_data);
22705 }
22706 
22720 char *
22722  enum content_type * content_type,
22723  char **content_disposition, gsize *content_length,
22724  cmd_response_data_t* response_data)
22725 {
22726  return export_many ("permission", credentials, params, content_type,
22727  content_disposition, content_length, response_data);
22728 }
22729 
22739 char *
22741  cmd_response_data_t* response_data)
22742 {
22743  gchar *html, *response;
22744  const char *no_redirect;
22745  const char *permission_id, *name, *comment, *resource_id, *resource_type;
22746  const char *subject_id, *subject_type;
22747  entity_t entity;
22748  int ret;
22749 
22750  no_redirect = params_value (params, "no_redirect");
22751  permission_id = params_value (params, "permission_id");
22752  name = params_value (params, "permission");
22753  comment = params_value (params, "comment");
22754  subject_type = params_value (params, "subject_type");
22755  resource_id = params_value (params, "id_or_empty");
22756  resource_type = params_value (params, "optional_resource_type");
22757 
22758  CHECK_PARAM_INVALID (permission_id, "Save Permission", "edit_permission");
22759  CHECK_PARAM_INVALID (name, "Save Permission", "edit_permission");
22760  CHECK_PARAM_INVALID (comment, "Save Permission", "edit_permission");
22761  CHECK_PARAM_INVALID (resource_id, "Save Permission", "edit_permission");
22762  CHECK_PARAM_INVALID (subject_type, "Save Permission", "edit_permission");
22763  if (params_given (params, "optional_resource_type"))
22764  CHECK_PARAM_INVALID (resource_type, "Save Permission", "edit_permission");
22765 
22766  if (strcmp (subject_type, "user") == 0)
22767  subject_id = params_value (params, "user_id");
22768  else if (strcmp (subject_type, "group") == 0)
22769  subject_id = params_value (params, "group_id");
22770  else if (strcmp (subject_type, "role") == 0)
22771  subject_id = params_value (params, "role_id");
22772  else
22773  subject_id = NULL;
22774  CHECK_PARAM_INVALID (subject_id, "Save Permission", "edit_permission");
22775 
22776  /* Modify the permission. */
22777 
22778  response = NULL;
22779  entity = NULL;
22780  ret = ompf (credentials,
22781  &response,
22782  &entity,
22783  response_data,
22784  "<modify_permission permission_id=\"%s\">"
22785  "<name>%s</name>"
22786  "<comment>%s</comment>"
22787  "<subject id=\"%s\">"
22788  "<type>%s</type>"
22789  "</subject>"
22790  "<resource id=\"%s\">"
22791  "<type>%s</type>"
22792  "</resource>"
22793  "</modify_permission>",
22794  permission_id,
22795  name,
22796  comment,
22797  subject_id,
22798  subject_type,
22799  (resource_id && strlen (resource_id)) ? resource_id : "0",
22800  resource_type ? resource_type : "");
22801  switch (ret)
22802  {
22803  case 0:
22804  case -1:
22805  break;
22806  case 1:
22807  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22808  return gsad_message (credentials,
22809  "Internal error", __FUNCTION__, __LINE__,
22810  "An internal error occurred while modifying a permission. "
22811  "The permission was not modified. "
22812  "Diagnostics: Failure to send command to manager daemon.",
22813  "/omp?cmd=get_permissions", response_data);
22814  case 2:
22815  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22816  return gsad_message (credentials,
22817  "Internal error", __FUNCTION__, __LINE__,
22818  "An internal error occurred while modifying a permission. "
22819  "It is unclear whether the permission has been modified or not. "
22820  "Diagnostics: Failure to receive response from manager daemon.",
22821  "/omp?cmd=get_permissions", response_data);
22822  default:
22823  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22824  return gsad_message (credentials,
22825  "Internal error", __FUNCTION__, __LINE__,
22826  "An internal error occurred while modifying a permission. "
22827  "It is unclear whether the permission has been modified or not. "
22828  "Diagnostics: Internal Error.",
22829  "/omp?cmd=get_permissions", response_data);
22830  }
22831 
22832  html = response_from_entity (credentials, params, entity,
22833  (no_redirect && strcmp (no_redirect, "0")),
22834  NULL, "get_permissions",
22835  NULL, "edit_permission",
22836  "Save Permission", response_data);
22837  free_entity (entity);
22838  g_free (response);
22839  return html;
22840 }
22841 
22842 
22843 /* Port lists. */
22844 
22855 static char *
22856 new_port_list (credentials_t *credentials, params_t *params,
22857  const char *extra_xml, cmd_response_data_t* response_data)
22858 {
22859  GString *xml;
22860  xml = g_string_new ("<new_port_list>");
22861  if (extra_xml)
22862  g_string_append (xml, extra_xml);
22863  g_string_append (xml, "</new_port_list>");
22864  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
22865  response_data);
22866 }
22867 
22878 static char *
22879 upload_port_list (credentials_t *credentials, params_t *params,
22880  const char *extra_xml, cmd_response_data_t* response_data)
22881 {
22882  GString *xml;
22883 
22884  xml = g_string_new ("<upload_port_list>");
22885  if (extra_xml)
22886  g_string_append (xml, extra_xml);
22887  g_string_append (xml, "</upload_port_list>");
22888 
22889  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
22890  response_data);
22891 }
22892 
22902 char *
22904  cmd_response_data_t* response_data)
22905 {
22906  return upload_port_list (credentials, params, NULL, response_data);
22907 }
22908 
22918 char *
22920  cmd_response_data_t* response_data)
22921 {
22922  gchar *html, *response;
22923  const char *no_redirect, *name, *comment, *port_range, *from_file;
22924  entity_t entity;
22925 
22926  no_redirect = params_value (params, "no_redirect");
22927  name = params_value (params, "name");
22928  comment = params_value (params, "comment");
22929  port_range = params_value (params, "port_range");
22930  from_file = params_value (params, "from_file");
22931 
22932  CHECK_PARAM_INVALID (name, "Create Port List", "new_port_list");
22933  CHECK_PARAM_INVALID (comment, "Create Port List", "new_port_list");
22934  CHECK_PARAM_INVALID (port_range, "Create Port List", "new_port_list");
22935  CHECK_PARAM_INVALID (from_file, "Create Port List", "new_port_list");
22936 
22937  /* Create the port_list. */
22938 
22939  switch (ompf (credentials,
22940  &response,
22941  &entity,
22942  response_data,
22943  "<create_port_list>"
22944  "<name>%s</name>"
22945  "<port_range>%s</port_range>"
22946  "<comment>%s</comment>"
22947  "</create_port_list>",
22948  name,
22949  strcmp (from_file, "0")
22950  ? params_value (params, "file")
22951  : port_range,
22952  comment ? comment : ""))
22953  {
22954  case 0:
22955  case -1:
22956  break;
22957  case 1:
22958  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22959  return gsad_message (credentials,
22960  "Internal error", __FUNCTION__, __LINE__,
22961  "An internal error occurred while creating a new port list. "
22962  "No new port list was created. "
22963  "Diagnostics: Failure to send command to manager daemon.",
22964  "/omp?cmd=get_port_lists", response_data);
22965  case 2:
22966  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22967  return gsad_message (credentials,
22968  "Internal error", __FUNCTION__, __LINE__,
22969  "An internal error occurred while creating a new port list. "
22970  "It is unclear whether the port list has been created or not. "
22971  "Diagnostics: Failure to receive response from manager daemon.",
22972  "/omp?cmd=get_port_lists", response_data);
22973  default:
22974  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
22975  return gsad_message (credentials,
22976  "Internal error", __FUNCTION__, __LINE__,
22977  "An internal error occurred while creating a new port list. "
22978  "It is unclear whether the port list has been created or not. "
22979  "Diagnostics: Internal Error.",
22980  "/omp?cmd=get_port_lists", response_data);
22981  }
22982 
22983  if (entity_attribute (entity, "id"))
22984  params_add (params, "port_list_id", entity_attribute (entity, "id"));
22985  html = response_from_entity (credentials, params, entity,
22986  (no_redirect && strcmp (no_redirect, "0")),
22987  NULL, "get_port_lists",
22988  NULL, "new_port_list",
22989  "Create Port List", response_data);
22990  free_entity (entity);
22991  g_free (response);
22992  return html;
22993 }
22994 
23005 static char *
23006 new_port_range (credentials_t *credentials, params_t *params,
23007  const char *extra_xml, cmd_response_data_t* response_data)
23008 {
23009  GString *xml;
23010  xml = g_string_new ("<new_port_range>");
23011  if (extra_xml)
23012  g_string_append (xml, extra_xml);
23013  g_string_append (xml, "</new_port_range>");
23014  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
23015  response_data);
23016 }
23017 
23027 char *
23029  cmd_response_data_t* response_data)
23030 {
23031  return new_port_range (credentials, params, NULL, response_data);
23032 }
23033 
23043 char *
23045  cmd_response_data_t* response_data)
23046 {
23047  int ret;
23048  gchar *html, *response;
23049  const char *no_redirect, *port_list_id, *start, *end, *type;
23050  entity_t entity;
23051 
23052  no_redirect = params_value (params, "no_redirect");
23053  port_list_id = params_value (params, "port_list_id");
23054  start = params_value (params, "port_range_start");
23055  end = params_value (params, "port_range_end");
23056  type = params_value (params, "port_type");
23057 
23058  CHECK_PARAM_INVALID (port_list_id, "Create Port Range", "edit_port_list");
23059  CHECK_PARAM_INVALID (start, "Create Port Range", "edit_port_list");
23060  CHECK_PARAM_INVALID (end, "Create Port Range", "edit_port_list");
23061  CHECK_PARAM_INVALID (type, "Create Port Range", "edit_port_list");
23062 
23063  /* Create the port range. */
23064 
23065  response = NULL;
23066  entity = NULL;
23067  ret = ompf (credentials,
23068  &response,
23069  &entity,
23070  response_data,
23071  "<create_port_range>"
23072  "<port_list id=\"%s\"/>"
23073  "<start>%s</start>"
23074  "<end>%s</end>"
23075  "<type>%s</type>"
23076  "</create_port_range>",
23077  port_list_id,
23078  start,
23079  end,
23080  type);
23081 
23082  switch (ret)
23083  {
23084  case 0:
23085  case -1:
23086  break;
23087  case 1:
23088  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23089  return gsad_message (credentials,
23090  "Internal error", __FUNCTION__, __LINE__,
23091  "An internal error occurred while creating a Port Range. "
23092  "The Port Range was not created. "
23093  "Diagnostics: Failure to send command to manager daemon.",
23094  "/omp?cmd=get_port_lists", response_data);
23095  case 2:
23096  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23097  return gsad_message (credentials,
23098  "Internal error", __FUNCTION__, __LINE__,
23099  "An internal error occurred while creating a Port Range. "
23100  "It is unclear whether the Port Range has been created or not. "
23101  "Diagnostics: Failure to receive response from manager daemon.",
23102  "/omp?cmd=get_port_lists", response_data);
23103  default:
23104  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23105  return gsad_message (credentials,
23106  "Internal error", __FUNCTION__, __LINE__,
23107  "An internal error occurred while creating a Port Range. "
23108  "It is unclear whether the Port Range has been created or not. "
23109  "Diagnostics: Internal Error.",
23110  "/omp?cmd=get_port_lists", response_data);
23111  }
23112 
23113  html = response_from_entity (credentials, params, entity,
23114  (no_redirect && strcmp (no_redirect, "0")),
23115  NULL, "edit_port_list",
23116  NULL, "edit_port_list",
23117  "Create Port Range", response_data);
23118  free_entity (entity);
23119  g_free (response);
23120  return html;
23121 }
23122 
23133 static char *
23134 get_port_list (credentials_t * credentials, params_t *params,
23135  const char * extra_xml, cmd_response_data_t* response_data)
23136 {
23137  return get_one ("port_list", credentials, params, extra_xml,
23138  "targets=\"1\" details=\"1\"", response_data);
23139 }
23140 
23150 char *
23151 get_port_list_omp (credentials_t * credentials, params_t *params,
23152  cmd_response_data_t* response_data)
23153 {
23154  return get_port_list (credentials, params, NULL, response_data);
23155 }
23156 
23167 static char *
23168 get_port_lists (credentials_t * credentials, params_t *params,
23169  const char *extra_xml, cmd_response_data_t* response_data)
23170 {
23171  return get_many ("port_list", credentials, params, extra_xml, NULL,
23172  response_data);
23173 }
23174 
23184 char *
23186  cmd_response_data_t* response_data)
23187 {
23188  return get_port_lists (credentials, params, NULL, response_data);
23189 }
23190 
23200 char *
23202  cmd_response_data_t* response_data)
23203 {
23204  return new_port_list (credentials, params, NULL, response_data);
23205 }
23206 
23217 char *
23218 edit_port_list (credentials_t * credentials, params_t *params,
23219  const char *extra_xml, cmd_response_data_t* response_data)
23220 {
23221  return edit_resource ("port_list", credentials, params, NULL, extra_xml,
23222  response_data);
23223 }
23224 
23234 char *
23236  cmd_response_data_t* response_data)
23237 {
23238  return edit_port_list (credentials, params, NULL, response_data);
23239 }
23240 
23250 char *
23252  cmd_response_data_t* response_data)
23253 {
23254  int ret;
23255  gchar *html, *response;
23256  const char *no_redirect, *port_list_id, *name, *comment;
23257  entity_t entity;
23258 
23259  no_redirect = params_value (params, "no_redirect");
23260  port_list_id = params_value (params, "port_list_id");
23261  name = params_value (params, "name");
23262  comment = params_value (params, "comment");
23263 
23264  CHECK_PARAM_INVALID (port_list_id, "Save Port List", "edit_port_list");
23265  CHECK_PARAM_INVALID (name, "Save Port List", "edit_port_list");
23266  CHECK_PARAM_INVALID (comment, "Save Port List", "edit_port_list");
23267 
23268  /* Modify the Port List. */
23269 
23270  response = NULL;
23271  entity = NULL;
23272  ret = ompf (credentials,
23273  &response,
23274  &entity,
23275  response_data,
23276  "<modify_port_list port_list_id=\"%s\">"
23277  "<name>%s</name>"
23278  "<comment>%s</comment>"
23279  "</modify_port_list>",
23280  port_list_id,
23281  name,
23282  comment);
23283 
23284  switch (ret)
23285  {
23286  case 0:
23287  case -1:
23288  break;
23289  case 1:
23290  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23291  return gsad_message (credentials,
23292  "Internal error", __FUNCTION__, __LINE__,
23293  "An internal error occurred while saving a Port List. "
23294  "The Port List was not saved. "
23295  "Diagnostics: Failure to send command to manager daemon.",
23296  "/omp?cmd=get_port_lists", response_data);
23297  case 2:
23298  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23299  return gsad_message (credentials,
23300  "Internal error", __FUNCTION__, __LINE__,
23301  "An internal error occurred while saving a Port List. "
23302  "It is unclear whether the Port List has been saved or not. "
23303  "Diagnostics: Failure to receive response from manager daemon.",
23304  "/omp?cmd=get_port_lists", response_data);
23305  default:
23306  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23307  return gsad_message (credentials,
23308  "Internal error", __FUNCTION__, __LINE__,
23309  "An internal error occurred while saving a Port List. "
23310  "It is unclear whether the Port List has been saved or not. "
23311  "Diagnostics: Internal Error.",
23312  "/omp?cmd=get_port_lists", response_data);
23313  }
23314 
23315  html = response_from_entity (credentials, params, entity,
23316  (no_redirect && strcmp (no_redirect, "0")),
23317  NULL, "get_port_lists",
23318  NULL, "edit_port_list",
23319  "Save Port List", response_data);
23320  free_entity (entity);
23321  g_free (response);
23322  return html;
23323 }
23324 
23334 char *
23336  cmd_response_data_t* response_data)
23337 {
23338  return delete_resource ("port_list", credentials, params, 0, "get_port_lists",
23339  response_data);
23340 }
23341 
23351 char *
23353  cmd_response_data_t* response_data)
23354 {
23355  return delete_resource ("port_list", credentials, params, 1, "get_trash",
23356  response_data);
23357 }
23358 
23368 char *
23370  cmd_response_data_t* response_data)
23371 {
23372  return delete_resource ("port_range", credentials, params, 1,
23373  "edit_port_list", response_data);
23374 }
23375 
23385 char *
23387  cmd_response_data_t* response_data)
23388 {
23389  const char *no_redirect;
23390  gchar *command, *html, *response;
23391  entity_t entity;
23392  int ret;
23393 
23394  no_redirect = params_value (params, "no_redirect");
23395 
23396  /* Create the port list. */
23397 
23398  response = NULL;
23399  entity = NULL;
23400  command = g_strdup_printf ("<create_port_list>"
23401  "%s"
23402  "</create_port_list>",
23403  params_value (params, "xml_file"));
23404  ret = omp (credentials, &response, &entity, response_data, command);
23405  g_free (command);
23406  switch (ret)
23407  {
23408  case 0:
23409  case -1:
23410  break;
23411  case 1:
23412  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23413  return gsad_message (credentials,
23414  "Internal error", __FUNCTION__, __LINE__,
23415  "An internal error occurred while importing a port_list. "
23416  "The schedule remains the same. "
23417  "Diagnostics: Failure to send command to manager daemon.",
23418  "/omp?cmd=get_port_lists",
23419  response_data);
23420  case 2:
23421  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23422  return gsad_message (credentials,
23423  "Internal error", __FUNCTION__, __LINE__,
23424  "An internal error occurred while importing a port_list. "
23425  "It is unclear whether the schedule has been saved or not. "
23426  "Diagnostics: Failure to receive response from manager daemon.",
23427  "/omp?cmd=get_port_lists",
23428  response_data);
23429  default:
23430  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23431  return gsad_message (credentials,
23432  "Internal error", __FUNCTION__, __LINE__,
23433  "An internal error occurred while importing a port_list. "
23434  "It is unclear whether the schedule has been saved or not. "
23435  "Diagnostics: Internal Error.",
23436  "/omp?cmd=get_port_lists",
23437  response_data);
23438  }
23439 
23440  /* Cleanup, and return transformed XML. */
23441 
23442  html = response_from_entity (credentials, params, entity,
23443  (no_redirect && strcmp (no_redirect, "0")),
23444  NULL, "get_port_lists",
23445  NULL, "new_port_list",
23446  "Import Port List", response_data);
23447  free_entity (entity);
23448  g_free (response);
23449  return html;
23450 }
23451 
23452 
23453 /* Roles. */
23454 
23465 static char *
23466 new_role (credentials_t *credentials, params_t *params, const char *extra_xml,
23467  cmd_response_data_t* response_data)
23468 {
23469  GString *xml;
23470  xml = g_string_new ("<new_role>");
23471  if (extra_xml)
23472  g_string_append (xml, extra_xml);
23473  g_string_append (xml, "</new_role>");
23474  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
23475  response_data);
23476 }
23477 
23487 char *
23488 new_role_omp (credentials_t *credentials, params_t *params,
23489  cmd_response_data_t* response_data)
23490 {
23491  return new_role (credentials, params, NULL, response_data);
23492 }
23493 
23503 char *
23505  cmd_response_data_t* response_data)
23506 {
23507  return delete_resource ("role", credentials, params, 1, "get_trash",
23508  response_data);
23509 }
23510 
23520 char *
23521 delete_role_omp (credentials_t * credentials, params_t *params,
23522  cmd_response_data_t* response_data)
23523 {
23524  return delete_resource ("role", credentials, params, 0, "get_roles",
23525  response_data);
23526 }
23527 
23537 char *
23538 create_role_omp (credentials_t *credentials, params_t *params,
23539  cmd_response_data_t* response_data)
23540 {
23541  char *ret;
23542  gchar *response;
23543  const char *no_redirect, *name, *comment, *users;
23544  entity_t entity;
23545 
23546  no_redirect = params_value (params, "no_redirect");
23547  name = params_value (params, "name");
23548  comment = params_value (params, "comment");
23549  users = params_value (params, "users");
23550 
23551  CHECK_PARAM_INVALID (name, "Create Role", "new_role");
23552  CHECK_PARAM_INVALID (comment, "Create Role", "new_role");
23553  CHECK_PARAM_INVALID (users, "Create Role", "new_role");
23554 
23555  response = NULL;
23556  entity = NULL;
23557  switch (ompf (credentials,
23558  &response,
23559  &entity,
23560  response_data,
23561  "<create_role>"
23562  "<name>%s</name>"
23563  "<comment>%s</comment>"
23564  "<users>%s</users>"
23565  "</create_role>",
23566  name,
23567  comment,
23568  users))
23569  {
23570  case 0:
23571  case -1:
23572  break;
23573  case 1:
23574  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23575  return gsad_message (credentials,
23576  "Internal error", __FUNCTION__, __LINE__,
23577  "An internal error occurred while creating a new role. "
23578  "No new role was created. "
23579  "Diagnostics: Failure to send command to manager daemon.",
23580  "/omp?cmd=get_targets", response_data);
23581  case 2:
23582  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23583  return gsad_message (credentials,
23584  "Internal error", __FUNCTION__, __LINE__,
23585  "An internal error occurred while creating a new role. "
23586  "It is unclear whether the role has been created or not. "
23587  "Diagnostics: Failure to receive response from manager daemon.",
23588  "/omp?cmd=get_roles", response_data);
23589  default:
23590  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23591  return gsad_message (credentials,
23592  "Internal error", __FUNCTION__, __LINE__,
23593  "An internal error occurred while creating a new role. "
23594  "It is unclear whether the role has been created or not. "
23595  "Diagnostics: Internal Error.",
23596  "/omp?cmd=get_roles", response_data);
23597  }
23598 
23599  if (entity_attribute (entity, "id"))
23600  params_add (params, "role_id", entity_attribute (entity, "id"));
23601  ret = response_from_entity (credentials, params, entity,
23602  (no_redirect && strcmp (no_redirect, "0")),
23603  NULL, "get_roles",
23604  NULL, "new_role",
23605  "Create Role", response_data);
23606  free_entity (entity);
23607  g_free (response);
23608  return ret;
23609 }
23610 
23621 char *
23622 edit_role (credentials_t * credentials, params_t *params,
23623  const char *extra_xml, cmd_response_data_t* response_data)
23624 {
23625  gchar *html;
23626  GString *extra;
23627 
23628  extra = g_string_new ("");
23629 
23630  if (command_enabled (credentials, "GET_PERMISSIONS"))
23631  {
23632  gchar *response;
23633  entity_t entity;
23634 
23635  response = NULL;
23636  entity = NULL;
23637  switch (ompf (credentials, &response, &entity, response_data,
23638  "<get_permissions"
23639  " filter=\"rows=-1 subject_type=role and subject_uuid=%s\"/>",
23640  params_value (params, "role_id")))
23641  {
23642  case 0:
23643  case -1:
23644  break;
23645  case 1:
23646  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23647  return gsad_message (credentials,
23648  "Internal error", __FUNCTION__, __LINE__,
23649  "An internal error occurred getting the permission list. "
23650  "Diagnostics: Failure to send command to manager daemon.",
23651  "/omp?cmd=get_roles", response_data);
23652  case 2:
23653  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23654  return gsad_message (credentials,
23655  "Internal error", __FUNCTION__, __LINE__,
23656  "An internal error occurred getting the permission list. "
23657  "Diagnostics: Failure to receive response from manager daemon.",
23658  "/omp?cmd=get_roles", response_data);
23659  default:
23660  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23661  return gsad_message (credentials,
23662  "Internal error", __FUNCTION__, __LINE__,
23663  "An internal error occurred getting the permission list. "
23664  "Diagnostics: Internal Error.",
23665  "/omp?cmd=get_roles", response_data);
23666  }
23667 
23668  g_string_append (extra, response);
23669 
23670  free_entity (entity);
23671  g_free (response);
23672  }
23673 
23674  if (command_enabled (credentials, "GET_GROUPS"))
23675  {
23676  gchar *response;
23677  entity_t entity;
23678 
23679  response = NULL;
23680  entity = NULL;
23681  switch (ompf (credentials, &response, &entity, response_data,
23682  "<get_groups"
23683  " filter=\"rows=-1 owner=%s\"/>",
23684  credentials->username))
23685  {
23686  case 0:
23687  case -1:
23688  break;
23689  case 1:
23690  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23691  return gsad_message (credentials,
23692  "Internal error", __FUNCTION__, __LINE__,
23693  "An internal error occurred getting the group list. "
23694  "Diagnostics: Failure to send command to manager daemon.",
23695  "/omp?cmd=get_roles", response_data);
23696  case 2:
23697  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23698  return gsad_message (credentials,
23699  "Internal error", __FUNCTION__, __LINE__,
23700  "An internal error occurred getting the group list. "
23701  "Diagnostics: Failure to receive response from manager daemon.",
23702  "/omp?cmd=get_roles", response_data);
23703  default:
23704  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23705  return gsad_message (credentials,
23706  "Internal error", __FUNCTION__, __LINE__,
23707  "An internal error occurred getting the group list. "
23708  "Diagnostics: Internal Error.",
23709  "/omp?cmd=get_roles", response_data);
23710  }
23711 
23712  g_string_append (extra, response);
23713 
23714  free_entity (entity);
23715  g_free (response);
23716  }
23717 
23718  if (extra_xml)
23719  g_string_append (extra, extra_xml);
23720  html = edit_resource ("role", credentials, params, NULL, extra->str,
23721  response_data);
23722  g_string_free (extra, TRUE);
23723  return html;
23724 }
23725 
23735 char *
23736 edit_role_omp (credentials_t * credentials, params_t *params,
23737  cmd_response_data_t* response_data)
23738 {
23739  return edit_role (credentials, params, NULL, response_data);
23740 }
23741 
23752 static char *
23753 get_role (credentials_t * credentials, params_t *params,
23754  const char *extra_xml, cmd_response_data_t* response_data)
23755 {
23756  return get_one ("role", credentials, params, extra_xml, NULL, response_data);
23757 }
23758 
23768 char *
23769 get_role_omp (credentials_t * credentials, params_t *params,
23770  cmd_response_data_t* response_data)
23771 {
23772  return get_role (credentials, params, NULL, response_data);
23773 }
23774 
23785 static char *
23786 get_roles (credentials_t * credentials, params_t *params,
23787  const char *extra_xml, cmd_response_data_t* response_data)
23788 {
23789  return get_many ("role", credentials, params, extra_xml, NULL, response_data);
23790 }
23791 
23801 char *
23802 get_roles_omp (credentials_t * credentials, params_t *params,
23803  cmd_response_data_t* response_data)
23804 {
23805  return get_roles (credentials, params, NULL, response_data);
23806 }
23807 
23820 char *
23821 export_role_omp (credentials_t * credentials, params_t *params,
23822  enum content_type * content_type, char **content_disposition,
23823  gsize *content_length, cmd_response_data_t* response_data)
23824 {
23825  return export_resource ("role", credentials, params, content_type,
23826  content_disposition, content_length, response_data);
23827 }
23828 
23842 char *
23843 export_roles_omp (credentials_t * credentials, params_t *params,
23844  enum content_type * content_type, char **content_disposition,
23845  gsize *content_length, cmd_response_data_t* response_data)
23846 {
23847  return export_many ("role", credentials, params, content_type,
23848  content_disposition, content_length, response_data);
23849 }
23850 
23860 char *
23861 save_role_omp (credentials_t * credentials, params_t *params,
23862  cmd_response_data_t* response_data)
23863 {
23864  int ret;
23865  gchar *html, *response;
23866  const char *no_redirect, *role_id, *name, *comment, *users;
23867  entity_t entity;
23868 
23869  no_redirect = params_value (params, "no_redirect");
23870  role_id = params_value (params, "role_id");
23871  name = params_value (params, "name");
23872  comment = params_value (params, "comment");
23873  users = params_value (params, "users");
23874 
23875  CHECK_PARAM_INVALID (role_id, "Save Role", "edit_role");
23876  CHECK_PARAM_INVALID (name, "Save Role", "edit_role");
23877  CHECK_PARAM_INVALID (comment, "Save Role", "edit_role");
23878  CHECK_PARAM_INVALID (users, "Save Role", "edit_role");
23879 
23880  /* Modify the Role. */
23881 
23882  response = NULL;
23883  entity = NULL;
23884  ret = ompf (credentials,
23885  &response,
23886  &entity,
23887  response_data,
23888  "<modify_role role_id=\"%s\">"
23889  "<name>%s</name>"
23890  "<comment>%s</comment>"
23891  "<users>%s</users>"
23892  "</modify_role>",
23893  role_id,
23894  name,
23895  comment,
23896  users);
23897 
23898  switch (ret)
23899  {
23900  case 0:
23901  case -1:
23902  break;
23903  case 1:
23904  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23905  return gsad_message (credentials,
23906  "Internal error", __FUNCTION__, __LINE__,
23907  "An internal error occurred while saving a role. "
23908  "The role was not saved. "
23909  "Diagnostics: Failure to send command to manager daemon.",
23910  "/omp?cmd=get_roles", response_data);
23911  case 2:
23912  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23913  return gsad_message (credentials,
23914  "Internal error", __FUNCTION__, __LINE__,
23915  "An internal error occurred while saving a role. "
23916  "It is unclear whether the role has been saved or not. "
23917  "Diagnostics: Failure to receive response from manager daemon.",
23918  "/omp?cmd=get_roles", response_data);
23919  default:
23920  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23921  return gsad_message (credentials,
23922  "Internal error", __FUNCTION__, __LINE__,
23923  "An internal error occurred while saving a role. "
23924  "It is unclear whether the role has been saved or not. "
23925  "Diagnostics: Internal Error.",
23926  "/omp?cmd=get_roles", response_data);
23927  }
23928 
23929  html = response_from_entity (credentials, params, entity,
23930  (no_redirect && strcmp (no_redirect, "0")),
23931  NULL, "get_roles",
23932  NULL, "edit_role",
23933  "Save Role", response_data);
23934  free_entity (entity);
23935  g_free (response);
23936  return html;
23937 }
23938 
23939 
23940 /* Feeds. */
23941 
23951 char *
23952 get_feeds_omp (credentials_t * credentials, params_t *params,
23953  cmd_response_data_t* response_data)
23954 {
23955  entity_t entity;
23956  char *text = NULL;
23957  openvas_connection_t connection;
23958  gchar *html, *response;
23959  time_t now;
23960  struct tm *tm;
23961  gchar current_timestamp[30];
23962 
23963  switch (manager_connect (credentials, &connection, &html,
23964  response_data))
23965  {
23966  case -1:
23967  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23968  if (html)
23969  return html;
23970  return gsad_message (credentials,
23971  "Internal error", __FUNCTION__, __LINE__,
23972  "An internal error occurred while getting the feed list. "
23973  "The current list of feeds is not available. "
23974  "Diagnostics: Failure to connect to manager daemon.",
23975  "/omp?cmd=get_tasks", response_data);
23976  case -2:
23977  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23978  return xsl_transform_omp (credentials,
23979  g_strdup
23980  ("<gsad_msg status_text=\"Access refused.\""
23981  " operation=\"List Feeds\">"
23982  "Only users given the Administrator role"
23983  " may access Feed Administration."
23984  "</gsad_msg>"),
23985  response_data);
23986  }
23987 
23988  if (openvas_connection_sendf (&connection,
23989  "<commands>"
23990  "<get_feeds/>"
23991  "</commands>")
23992  == -1)
23993  {
23994  openvas_connection_close (&connection);
23995  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
23996  return gsad_message (credentials,
23997  "Internal error", __FUNCTION__, __LINE__,
23998  "An internal error occurred while getting the feed list. "
23999  "The current list of feeds is not available. "
24000  "Diagnostics: Failure to send command to manager daemon.",
24001  "/omp?cmd=get_tasks", response_data);
24002  }
24003 
24004  if (read_entity_and_text_c (&connection, &entity, &text))
24005  {
24006  openvas_connection_close (&connection);
24007  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24008  return gsad_message (credentials,
24009  "Internal error", __FUNCTION__, __LINE__,
24010  "An internal error occurred while getting the feed. "
24011  "The current list of feeds is not available. "
24012  "Diagnostics: Failure to receive response from manager daemon.",
24013  "/omp?cmd=get_tasks", response_data);
24014  }
24015 
24016  time (&now);
24017  tm = gmtime (&now);
24018  if (tm == NULL
24019  || (strftime (current_timestamp,
24020  29,
24021  "%Y-%m-%dT%H:%M:%S",
24022  tm)
24023  == 0))
24024  {
24025  current_timestamp[0] = '\0';
24026  }
24027 
24028  response = g_strdup_printf ("<get_feeds>"
24029  "%s"
24030  "<current_time_utc>%s</current_time_utc>"
24031  "</get_feeds>",
24032  text,
24033  current_timestamp);
24034 
24035  g_free (text);
24036 
24037  openvas_connection_close (&connection);
24038  return xsl_transform_omp (credentials, response, response_data);
24039 }
24040 
24053 static char*
24054 sync_feed (credentials_t * credentials, params_t *params,
24055  const char *sync_cmd, const char *action,
24056  const char *feed_name,
24057  cmd_response_data_t* response_data)
24058 {
24059  const char *no_redirect;
24060  entity_t entity;
24061  char *text = NULL;
24062  openvas_connection_t connection;
24063  gchar *html, *msg;
24064 
24065  no_redirect = params_value (params, "no_redirect");
24066 
24067  switch (manager_connect (credentials, &connection, &html,
24068  response_data))
24069  {
24070  case -1:
24071  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24072  if (html)
24073  return html;
24074  msg = g_strdup_printf
24075  ("An internal error occurred while synchronizing with %s. "
24076  "Feed synchronization is currently not available. "
24077  "Diagnostics: Failure to connect to manager daemon.",
24078  feed_name);
24079  html = gsad_message (credentials,
24080  "Internal error", __FUNCTION__, __LINE__,
24081  msg, "/omp?cmd=get_tasks", response_data);
24082  g_free (msg);
24083  return html;
24084  case -2:
24085  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24086  return xsl_transform_omp (credentials,
24087  g_strdup_printf
24088  ("<gsad_msg status_text=\"Access refused.\""
24089  " operation=\"%s\">"
24090  "Only users given the Administrator role"
24091  " may access Feed Administration."
24092  "</gsad_msg>",
24093  action),
24094  response_data);
24095  }
24096 
24097  if (openvas_connection_sendf (&connection,
24098  "<%s/>",
24099  sync_cmd)
24100  == -1)
24101  {
24102  openvas_connection_close (&connection);
24103  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24104 
24105  msg = g_strdup_printf
24106  ("An internal error occurred while synchronizing with %s. "
24107  "Feed synchronization is currently not available. "
24108  "Diagnostics: Failure to send command to manager daemon.",
24109  feed_name);
24110  html = gsad_message (credentials,
24111  "Internal error", __FUNCTION__, __LINE__,
24112  msg, "/omp?cmd=get_tasks", response_data);
24113  g_free (msg);
24114  return html;
24115  }
24116 
24117  if (read_entity_and_text_c (&connection, &entity, &text))
24118  {
24119  openvas_connection_close (&connection);
24120  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24121 
24122  msg = g_strdup_printf
24123  ("An internal error occurred while synchronizing with %s. "
24124  "Feed synchronization is currently not available. "
24125  "Diagnostics: Failure to receive response from manager daemon.",
24126  feed_name);
24127  html = gsad_message (credentials,
24128  "Internal error", __FUNCTION__, __LINE__,
24129  msg, "/omp?cmd=get_tasks", response_data);
24130  g_free (msg);
24131  return html;
24132  }
24133 
24134  openvas_connection_close (&connection);
24135  html = response_from_entity (credentials, params, entity,
24136  (no_redirect && strcmp (no_redirect, "0")),
24137  NULL, "get_feeds",
24138  NULL, "get_feeds",
24139  action, response_data);
24140 
24141  return html;
24142 }
24143 
24153 char *
24154 sync_feed_omp (credentials_t * credentials, params_t *params,
24155  cmd_response_data_t* response_data)
24156 {
24157  return sync_feed (credentials, params,
24158  "sync_feed", "Synchronize Feed", "the NVT feed",
24159  response_data);
24160 }
24161 
24171 char *
24172 sync_scap_omp (credentials_t * credentials, params_t *params,
24173  cmd_response_data_t* response_data)
24174 {
24175  return sync_feed (credentials, params,
24176  "sync_scap", "Synchronize Feed", "the SCAP feed",
24177  response_data);
24178 }
24179 
24189 char *
24190 sync_cert_omp (credentials_t * credentials, params_t *params,
24191  cmd_response_data_t* response_data)
24192 {
24193  return sync_feed (credentials, params,
24194  "sync_cert", "Synchronize CERT Feed", "the CERT feed",
24195  response_data);
24196 }
24197 
24198 
24199 /* Filters. */
24200 
24211 char *
24212 get_filter (credentials_t * credentials, params_t *params,
24213  const char *extra_xml, cmd_response_data_t* response_data)
24214 {
24215  return get_one ("filter", credentials, params, extra_xml, "alerts=\"1\"",
24216  response_data);
24217 }
24218 
24228 char *
24229 get_filter_omp (credentials_t * credentials, params_t *params,
24230  cmd_response_data_t* response_data)
24231 {
24232  return get_filter (credentials, params, NULL, response_data);
24233 }
24234 
24245 char *
24246 get_filters (credentials_t * credentials, params_t *params,
24247  const char *extra_xml, cmd_response_data_t* response_data)
24248 {
24249  return get_many ("filter", credentials, params, extra_xml, NULL,
24250  response_data);
24251 }
24252 
24262 char *
24263 get_filters_omp (credentials_t * credentials, params_t *params,
24264  cmd_response_data_t* response_data)
24265 {
24266  return get_filters (credentials, params, NULL, response_data);
24267 }
24268 
24279 static char *
24280 new_filter (credentials_t *credentials, params_t *params, const char *extra_xml,
24281  cmd_response_data_t* response_data)
24282 {
24283  GString *xml;
24284  xml = g_string_new ("<new_filter>");
24285  if (extra_xml)
24286  g_string_append (xml, extra_xml);
24287  g_string_append (xml, "</new_filter>");
24288  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
24289  response_data);
24290 }
24291 
24301 char *
24303  cmd_response_data_t* response_data)
24304 {
24305  gchar *html, *response;
24306  const char *no_redirect, *name, *comment, *term, *type;
24307  entity_t entity;
24308 
24309  no_redirect = params_value (params, "no_redirect");
24310  name = params_value (params, "name");
24311  comment = params_value (params, "comment");
24312  term = params_value (params, "term");
24313  type = params_value (params, "optional_resource_type");
24314 
24315  CHECK_PARAM_INVALID (name, "Create Filter", "new_filter");
24316  CHECK_PARAM_INVALID (comment, "Create Filter", "new_filter");
24317  CHECK_PARAM_INVALID (term, "Create Filter", "new_filter");
24318  CHECK_PARAM_INVALID (type, "Create Filter", "new_filter");
24319 
24320  switch (ompf (credentials,
24321  &response,
24322  &entity,
24323  response_data,
24324  "<create_filter>"
24325  "<name>%s</name>"
24326  "<comment>%s</comment>"
24327  "<term>%s</term>"
24328  "<type>%s</type>"
24329  "</create_filter>",
24330  name,
24331  comment,
24332  term,
24333  type))
24334  {
24335  case 0:
24336  case -1:
24337  break;
24338  case 1:
24339  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24340  return gsad_message (credentials,
24341  "Internal error", __FUNCTION__, __LINE__,
24342  "An internal error occurred while creating a new alert. "
24343  "No new alert was created. "
24344  "Diagnostics: Failure to send command to manager daemon.",
24345  "/omp?cmd=get_alerts", response_data);
24346  case 2:
24347  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24348  return gsad_message (credentials,
24349  "Internal error", __FUNCTION__, __LINE__,
24350  "An internal error occurred while creating a new alert. "
24351  "It is unclear whether the alert has been created or not. "
24352  "Diagnostics: Failure to receive response from manager daemon.",
24353  "/omp?cmd=get_alerts", response_data);
24354  default:
24355  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24356  return gsad_message (credentials,
24357  "Internal error", __FUNCTION__, __LINE__,
24358  "An internal error occurred while creating a new alert. "
24359  "It is unclear whether the alert has been created or not. "
24360  "Diagnostics: Internal Error.",
24361  "/omp?cmd=get_alerts", response_data);
24362  }
24363 
24364  if (omp_success (entity))
24365  {
24366  const char *filter_id;
24367 
24368  filter_id = entity_attribute (entity, "id");
24369  if (filter_id && strlen (filter_id))
24370  {
24371  param_t *param;
24372  param = params_add (params, "filt_id", filter_id);
24373  param->valid = 1;
24374  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
24375  }
24376  }
24377 
24378  if (entity_attribute (entity, "id"))
24379  params_add (params, "filter_id", entity_attribute (entity, "id"));
24380  html = response_from_entity (credentials, params, entity,
24381  (no_redirect && strcmp (no_redirect, "0")),
24382  NULL, "get_filters",
24383  NULL, "new_filter",
24384  "Create Filter", response_data);
24385  free_entity (entity);
24386  g_free (response);
24387  return html;
24388 }
24389 
24399 char *
24401  cmd_response_data_t* response_data)
24402 {
24403  return delete_resource ("filter", credentials, params, 1, "get_trash",
24404  response_data);
24405 }
24406 
24416 char *
24417 delete_filter_omp (credentials_t * credentials, params_t *params,
24418  cmd_response_data_t* response_data)
24419 {
24420  param_t *filt_id, *id;
24421  GList *list;
24422 
24423  filt_id = params_get (params, "filt_id");
24424  id = params_get (params, "filter_id");
24425  if (id && id->value && filt_id && filt_id->value
24426  && (strcmp (id->value, filt_id->value) == 0))
24427  // TODO: Add params_remove.
24428  filt_id->value = NULL;
24429 
24430  /* remove to be deleted key from the user credentials */
24431  if (id && id->value)
24432  {
24433  find_by_value_t find;
24434 
24435  init_find_by_value (&find, id->value);
24436 
24437  g_tree_foreach (credentials->last_filt_ids, (GTraverseFunc)find_by_value,
24438  &find);
24439  if (find.keys != NULL)
24440  {
24441  list = g_list_first (find.keys);
24442 
24443  while (list != NULL)
24444  {
24445  g_debug ("%s removing filter from last filter ids for %s\n",
24446  __FUNCTION__, (char *)list->data);
24447  g_tree_remove (credentials->last_filt_ids, list->data);
24448  list = g_list_next (find.keys);
24449  }
24450  }
24451 
24452  free_find_by_value(&find);
24453  }
24454 
24455  return delete_resource ("filter", credentials, params, 0, "get_filters",
24456  response_data);
24457 }
24458 
24469 char *
24470 edit_filter (credentials_t * credentials, params_t *params,
24471  const char *extra_xml, cmd_response_data_t* response_data)
24472 {
24473  return edit_resource ("filter", credentials, params, NULL, extra_xml,
24474  response_data);
24475 }
24476 
24486 char *
24487 edit_filter_omp (credentials_t * credentials, params_t *params,
24488  cmd_response_data_t* response_data)
24489 {
24490  return edit_filter (credentials, params, NULL, response_data);
24491 }
24492 
24505 char *
24506 export_filter_omp (credentials_t * credentials, params_t *params,
24507  enum content_type * content_type, char **content_disposition,
24508  gsize *content_length, cmd_response_data_t* response_data)
24509 {
24510  return export_resource ("filter", credentials, params, content_type,
24511  content_disposition, content_length, response_data);
24512 }
24513 
24527 char *
24529  enum content_type * content_type, char **content_disposition,
24530  gsize *content_length, cmd_response_data_t* response_data)
24531 {
24532  return export_many ("filter", credentials, params, content_type,
24533  content_disposition, content_length, response_data);
24534 }
24535 
24545 char *
24546 new_filter_omp (credentials_t *credentials, params_t *params,
24547  cmd_response_data_t* response_data)
24548 {
24549  return new_filter (credentials, params, NULL, response_data);
24550 }
24551 
24561 char *
24562 save_filter_omp (credentials_t * credentials, params_t *params,
24563  cmd_response_data_t* response_data)
24564 {
24565  openvas_connection_t connection;
24566  entity_t entity;
24567  gchar *html, *response;
24568  const char *no_redirect, *filter_id, *name, *comment, *term, *type;
24569 
24570  no_redirect = params_value (params, "no_redirect");
24571  filter_id = params_value (params, "filter_id");
24572  name = params_value (params, "name");
24573  comment = params_value (params, "comment");
24574  term = params_value (params, "term");
24575  type = params_value (params, "optional_resource_type");
24576 
24577  CHECK_PARAM_INVALID (filter_id, "Save Filter", "edit_filter");
24578  CHECK_PARAM_INVALID (name, "Save Filter", "edit_filter");
24579  CHECK_PARAM_INVALID (comment, "Save Filter", "edit_filter");
24580  CHECK_PARAM_INVALID (term, "Save Filter", "edit_filter");
24581  CHECK_PARAM_INVALID (type, "Save Filter", "edit_filter");
24582 
24583  switch (manager_connect (credentials, &connection, &html,
24584  response_data))
24585  {
24586  case 0:
24587  break;
24588  case -1:
24589  if (html)
24590  return html;
24591  /* Fall through. */
24592  default:
24593  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24594  return gsad_message (credentials,
24595  "Internal error", __FUNCTION__, __LINE__,
24596  "An internal error occurred while saving a filter. "
24597  "The filter was not saved. "
24598  "Diagnostics: Failure to connect to manager daemon.",
24599  "/omp?cmd=get_filters", response_data);
24600  }
24601 
24602  {
24603  int ret;
24604 
24605  /* Modify the filter. */
24606 
24607  ret = openvas_connection_sendf_xml (&connection,
24608  "<modify_filter filter_id=\"%s\">"
24609  "<name>%s</name>"
24610  "<comment>%s</comment>"
24611  "<term>%s</term>"
24612  "<type>%s</type>"
24613  "</modify_filter>",
24614  filter_id,
24615  name,
24616  comment,
24617  term,
24618  type);
24619 
24620  if (ret == -1)
24621  {
24622  openvas_connection_close (&connection);
24623  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24624  return gsad_message (credentials,
24625  "Internal error", __FUNCTION__, __LINE__,
24626  "An internal error occurred while modifying a filter. "
24627  "The filter was not modified. "
24628  "Diagnostics: Failure to send command to manager daemon.",
24629  "/omp?cmd=get_filters", response_data);
24630  }
24631 
24632  entity = NULL;
24633  if (read_entity_and_text_c (&connection, &entity, &response))
24634  {
24635  openvas_connection_close (&connection);
24636  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24637  return gsad_message (credentials,
24638  "Internal error", __FUNCTION__, __LINE__,
24639  "An internal error occurred while modifying a filter. "
24640  "It is unclear whether the filter has been modified or not. "
24641  "Diagnostics: Failure to receive response from manager daemon.",
24642  "/omp?cmd=get_filters", response_data);
24643  }
24644 
24645  }
24646 
24647  openvas_connection_close (&connection);
24648 
24649  /* Pass response to handler of following page. */
24650 
24651  html = response_from_entity (credentials, params, entity,
24652  (no_redirect && strcmp (no_redirect, "0")),
24653  NULL, "get_filters",
24654  NULL, "edit_filter",
24655  "Save Filter", response_data);
24656 
24657  free_entity (entity);
24658  g_free (response);
24659  return html;
24660 }
24661 
24662 
24663 /* Schedules. */
24664 
24675 char *
24676 edit_schedule (credentials_t * credentials, params_t *params,
24677  const char *extra_xml, cmd_response_data_t* response_data)
24678 {
24679  return edit_resource ("schedule", credentials, params,
24680  "tasks=\"1\"", extra_xml, response_data);
24681 }
24682 
24692 char *
24693 edit_schedule_omp (credentials_t * credentials, params_t *params,
24694  cmd_response_data_t* response_data)
24695 {
24696  return edit_schedule (credentials, params, NULL, response_data);
24697 }
24698 
24711 char *
24713  enum content_type * content_type,
24714  char **content_disposition, gsize *content_length,
24715  cmd_response_data_t* response_data)
24716 {
24717  return export_resource ("schedule", credentials, params, content_type,
24718  content_disposition, content_length, response_data);
24719 }
24720 
24733 char *
24735  enum content_type * content_type,
24736  char **content_disposition, gsize *content_length,
24737  cmd_response_data_t* response_data)
24738 {
24739  return export_many ("schedule", credentials, params, content_type,
24740  content_disposition, content_length, response_data);
24741 }
24742 
24752 char *
24753 save_schedule_omp (credentials_t * credentials, params_t *params,
24754  cmd_response_data_t* response_data)
24755 {
24756  gchar *response;
24757  entity_t entity;
24758  const char *no_redirect, *schedule_id, *name, *comment;
24759  const char *hour, *minute, *day_of_month, *month, *year, *timezone;
24760  const char *period, *period_unit, *duration, *duration_unit;
24761  char *ret;
24762 
24763  no_redirect = params_value (params, "no_redirect");
24764  schedule_id = params_value (params, "schedule_id");
24765  name = params_value (params, "name");
24766  comment = params_value (params, "comment");
24767  hour = params_value (params, "hour");
24768  minute = params_value (params, "minute");
24769  day_of_month = params_value (params, "day_of_month");
24770  duration = params_value (params, "duration");
24771  duration_unit = params_value (params, "duration_unit");
24772  month = params_value (params, "month");
24773  period = params_value (params, "period");
24774  period_unit = params_value (params, "period_unit");
24775  year = params_value (params, "year");
24776  timezone = params_value (params, "timezone");
24777 
24778  CHECK_PARAM_INVALID (schedule_id, "Save Schedule", "edit_schedule");
24779  CHECK_PARAM_INVALID (name, "Save Schedule", "edit_schedule");
24780  CHECK_PARAM_INVALID (comment, "Save Schedule", "edit_schedule");
24781  CHECK_PARAM_INVALID (hour, "Save Schedule", "edit_schedule");
24782  CHECK_PARAM_INVALID (minute, "Save Schedule", "edit_schedule");
24783  CHECK_PARAM_INVALID (day_of_month, "Save Schedule", "edit_schedule");
24784  CHECK_PARAM_INVALID (duration, "Save Schedule", "edit_schedule");
24785  CHECK_PARAM_INVALID (duration_unit, "Save Schedule", "edit_schedule");
24786  CHECK_PARAM_INVALID (month, "Save Schedule", "edit_schedule");
24787  CHECK_PARAM_INVALID (period, "Save Schedule", "edit_schedule");
24788  CHECK_PARAM_INVALID (period_unit, "Save Schedule", "edit_schedule");
24789  CHECK_PARAM_INVALID (year, "Save Schedule", "edit_schedule");
24790  CHECK_PARAM_INVALID (timezone, "Save Schedule", "edit_schedule");
24791 
24792  response = NULL;
24793  entity = NULL;
24794  switch (ompf (credentials,
24795  &response,
24796  &entity,
24797  response_data,
24798  "<modify_schedule schedule_id=\"%s\">"
24799  "<name>%s</name>"
24800  "<comment>%s</comment>"
24801  "<first_time>"
24802  "<hour>%s</hour>"
24803  "<minute>%s</minute>"
24804  "<day_of_month>%s</day_of_month>"
24805  "<month>%s</month>"
24806  "<year>%s</year>"
24807  "</first_time>"
24808  "<timezone>%s</timezone>"
24809  "<period>"
24810  "<unit>%s</unit>"
24811  "%s"
24812  "</period>"
24813  "<duration>"
24814  "<unit>%s</unit>"
24815  "%s"
24816  "</duration>"
24817  "</modify_schedule>",
24818  schedule_id,
24819  name ? name : "",
24820  comment ? comment : "",
24821  hour,
24822  minute,
24823  day_of_month,
24824  month,
24825  year,
24826  timezone,
24827  (strcmp (period_unit, "")
24828  ? period_unit
24829  : "second"),
24830  period,
24831  (strcmp (duration_unit, "")
24832  ? duration_unit
24833  : "second"),
24834  duration))
24835  {
24836  case 0:
24837  case -1:
24838  break;
24839  case 1:
24840  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24841  return gsad_message (credentials,
24842  "Internal error", __FUNCTION__, __LINE__,
24843  "An internal error occurred while saving a schedule. "
24844  "The schedule remains the same. "
24845  "Diagnostics: Failure to send command to manager daemon.",
24846  "/omp?cmd=get_schedules", response_data);
24847  case 2:
24848  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24849  return gsad_message (credentials,
24850  "Internal error", __FUNCTION__, __LINE__,
24851  "An internal error occurred while saving a schedule. "
24852  "It is unclear whether the schedule has been saved or not. "
24853  "Diagnostics: Failure to receive response from manager daemon.",
24854  "/omp?cmd=get_schedules", response_data);
24855  default:
24856  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24857  return gsad_message (credentials,
24858  "Internal error", __FUNCTION__, __LINE__,
24859  "An internal error occurred while saving a schedule. "
24860  "It is unclear whether the schedule has been saved or not. "
24861  "Diagnostics: Internal Error.",
24862  "/omp?cmd=get_schedules", response_data);
24863  }
24864 
24865  ret = response_from_entity (credentials, params, entity,
24866  (no_redirect && strcmp (no_redirect, "0")),
24867  NULL, "get_schedules",
24868  NULL, "edit_schedule",
24869  "Save Schedule", response_data);
24870  free_entity (entity);
24871  g_free (response);
24872  return ret;
24873 }
24874 
24875 
24876 /* Users. */
24877 
24888 static char *
24889 new_user (credentials_t *credentials, params_t *params, const char *extra_xml,
24890  cmd_response_data_t* response_data)
24891 {
24892  GString *xml;
24893 
24894  xml = g_string_new ("<new_user>");
24895  if (extra_xml)
24896  g_string_append (xml, extra_xml);
24897 
24898  if (command_enabled (credentials, "DESCRIBE_AUTH"))
24899  {
24900  gchar *response;
24901  entity_t entity;
24902 
24903  response = NULL;
24904  entity = NULL;
24905  switch (omp (credentials, &response, &entity, response_data,
24906  "<describe_auth/>"))
24907  {
24908  case 0:
24909  case -1:
24910  break;
24911  case 1:
24912  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24913  return gsad_message (credentials,
24914  "Internal error", __FUNCTION__, __LINE__,
24915  "An internal error occurred getting the auth list. "
24916  "No new user was created. "
24917  "Diagnostics: Failure to send command to manager daemon.",
24918  "/omp?cmd=get_users", response_data);
24919  case 2:
24920  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24921  return gsad_message (credentials,
24922  "Internal error", __FUNCTION__, __LINE__,
24923  "An internal error occurred getting the auth list. "
24924  "No new user was created. "
24925  "Diagnostics: Failure to receive response from manager daemon.",
24926  "/omp?cmd=get_users", response_data);
24927  default:
24928  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24929  return gsad_message (credentials,
24930  "Internal error", __FUNCTION__, __LINE__,
24931  "An internal error occurred getting the auth list. "
24932  "No new user was created. "
24933  "Diagnostics: Internal Error.",
24934  "/omp?cmd=get_users", response_data);
24935  }
24936 
24937  g_string_append (xml, response);
24938 
24939  free_entity (entity);
24940  g_free (response);
24941  }
24942 
24943  if (command_enabled (credentials, "GET_GROUPS"))
24944  {
24945  gchar *response;
24946  entity_t entity;
24947 
24948  response = NULL;
24949  entity = NULL;
24950  switch (omp (credentials, &response, &entity, response_data,
24951  "<get_groups/>"))
24952  {
24953  case 0:
24954  case -1:
24955  break;
24956  case 1:
24957  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24958  return gsad_message (credentials,
24959  "Internal error", __FUNCTION__, __LINE__,
24960  "An internal error occurred getting the group list. "
24961  "No new user was created. "
24962  "Diagnostics: Failure to send command to manager daemon.",
24963  "/omp?cmd=get_users", response_data);
24964  case 2:
24965  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24966  return gsad_message (credentials,
24967  "Internal error", __FUNCTION__, __LINE__,
24968  "An internal error occurred getting the group list. "
24969  "No new user was created. "
24970  "Diagnostics: Failure to receive response from manager daemon.",
24971  "/omp?cmd=get_users", response_data);
24972  default:
24973  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
24974  return gsad_message (credentials,
24975  "Internal error", __FUNCTION__, __LINE__,
24976  "An internal error occurred getting the group list. "
24977  "No new user was created. "
24978  "Diagnostics: Internal Error.",
24979  "/omp?cmd=get_users", response_data);
24980  }
24981 
24982  g_string_append (xml, response);
24983 
24984  free_entity (entity);
24985  g_free (response);
24986  }
24987 
24988  if (command_enabled (credentials, "GET_ROLES"))
24989  {
24990  gchar *response;
24991  entity_t entity;
24992 
24993  response = NULL;
24994  entity = NULL;
24995  switch (omp (credentials, &response, &entity, response_data,
24996  "<get_roles/>"))
24997  {
24998  case 0:
24999  case -1:
25000  break;
25001  case 1:
25002  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25003  return gsad_message (credentials,
25004  "Internal error", __FUNCTION__, __LINE__,
25005  "An internal error occurred getting the role list. "
25006  "No new user was created. "
25007  "Diagnostics: Failure to send command to manager daemon.",
25008  "/omp?cmd=get_users", response_data);
25009  case 2:
25010  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25011  return gsad_message (credentials,
25012  "Internal error", __FUNCTION__, __LINE__,
25013  "An internal error occurred getting the role list. "
25014  "No new user was created. "
25015  "Diagnostics: Failure to receive response from manager daemon.",
25016  "/omp?cmd=get_users", response_data);
25017  default:
25018  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25019  return gsad_message (credentials,
25020  "Internal error", __FUNCTION__, __LINE__,
25021  "An internal error occurred getting the role list. "
25022  "No new user was created. "
25023  "Diagnostics: Internal Error.",
25024  "/omp?cmd=get_users", response_data);
25025  }
25026 
25027  g_string_append (xml, response);
25028 
25029  free_entity (entity);
25030  g_free (response);
25031  }
25032 
25033  g_string_append (xml, "</new_user>");
25034  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
25035  response_data);
25036 }
25037 
25047 char *
25048 new_user_omp (credentials_t *credentials, params_t *params,
25049  cmd_response_data_t* response_data)
25050 {
25051  return new_user (credentials, params, NULL, response_data);
25052 }
25053 
25063 char *
25064 delete_user_omp (credentials_t * credentials, params_t *params,
25065  cmd_response_data_t* response_data)
25066 {
25067  return delete_resource ("user", credentials, params, 0, "get_users",
25068  response_data);
25069 }
25070 
25081 static char *
25082 delete_user_confirm (credentials_t *credentials, params_t *params,
25083  const char *extra_xml, cmd_response_data_t* response_data)
25084 {
25085  GString *xml;
25086  gchar *response = NULL;
25087  entity_t entity = NULL;
25088 
25089  if (command_enabled (credentials, "GET_USERS"))
25090  {
25091  switch (omp (credentials, &response, &entity, response_data,
25092  "<get_users filter=\"first=1 rows=-1\"/>"))
25093  {
25094  case 0:
25095  case -1:
25096  break;
25097  case 1:
25098  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25099  return gsad_message (credentials,
25100  "Internal error", __FUNCTION__, __LINE__,
25101  "An internal error occurred getting the user list. "
25102  "Diagnostics: Failure to send command to manager daemon.",
25103  "/omp?cmd=get_users", response_data);
25104  case 2:
25105  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25106  return gsad_message (credentials,
25107  "Internal error", __FUNCTION__, __LINE__,
25108  "An internal error occurred getting the user list. "
25109  "Diagnostics: Failure to receive response from manager daemon.",
25110  "/omp?cmd=get_users", response_data);
25111  default:
25112  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25113  return gsad_message (credentials,
25114  "Internal error", __FUNCTION__, __LINE__,
25115  "An internal error occurred getting the user list. "
25116  "Diagnostics: Internal Error.",
25117  "/omp?cmd=get_users", response_data);
25118  }
25119  }
25120 
25121  xml = g_string_new ("<delete_user>");
25122  if (extra_xml)
25123  g_string_append (xml, extra_xml);
25124 
25125  if (response)
25126  g_string_append (xml, response);
25127  free_entity (entity);
25128  g_free (response);
25129 
25130  if (extra_xml)
25131  g_string_append (xml, extra_xml);
25132 
25133  g_string_append (xml, "</delete_user>");
25134  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
25135  response_data);
25136 }
25137 
25147 char *
25149  cmd_response_data_t* response_data)
25150 {
25151  return delete_user_confirm (credentials, params, NULL, response_data);
25152 }
25153 
25164 char *
25165 get_user (credentials_t * credentials, params_t *params, const char *extra_xml,
25166  cmd_response_data_t* response_data)
25167 {
25168  gchar *html;
25169  GString *extra;
25170 
25171  extra = g_string_new ("");
25172  if (extra_xml)
25173  g_string_append (extra, extra_xml);
25174  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25175  {
25176  gchar *response;
25177  entity_t entity;
25178 
25179  response = NULL;
25180  entity = NULL;
25181  switch (omp (credentials, &response, &entity, response_data,
25182  "<describe_auth/>"))
25183  {
25184  case 0:
25185  case -1:
25186  break;
25187  case 1:
25188  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25189  return gsad_message (credentials,
25190  "Internal error", __FUNCTION__, __LINE__,
25191  "An internal error occurred getting the auth list. "
25192  "Diagnostics: Failure to send command to manager daemon.",
25193  "/omp?cmd=get_users", response_data);
25194  case 2:
25195  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25196  return gsad_message (credentials,
25197  "Internal error", __FUNCTION__, __LINE__,
25198  "An internal error occurred getting the auth list. "
25199  "Diagnostics: Failure to receive response from manager daemon.",
25200  "/omp?cmd=get_users", response_data);
25201  default:
25202  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25203  return gsad_message (credentials,
25204  "Internal error", __FUNCTION__, __LINE__,
25205  "An internal error occurred getting the auth list. "
25206  "Diagnostics: Internal Error.",
25207  "/omp?cmd=get_users", response_data);
25208  }
25209 
25210  g_string_append (extra, response);
25211 
25212  free_entity (entity);
25213  g_free (response);
25214  }
25215  html = get_one ("user", credentials, params, extra->str, NULL, response_data);
25216  g_string_free (extra, TRUE);
25217  return html;
25218 }
25219 
25229 char *
25230 get_user_omp (credentials_t * credentials, params_t *params,
25231  cmd_response_data_t* response_data)
25232 {
25233  return get_user (credentials, params, NULL, response_data);
25234 }
25235 
25246 static char *
25247 get_users (credentials_t * credentials, params_t *params,
25248  const char *extra_xml, cmd_response_data_t* response_data)
25249 {
25250  gchar *html;
25251  GString *extra;
25252 
25253  extra = g_string_new ("");
25254  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25255  {
25256  gchar *response;
25257  entity_t entity;
25258 
25259  response = NULL;
25260  entity = NULL;
25261  switch (omp (credentials, &response, &entity, response_data,
25262  "<describe_auth/>"))
25263  {
25264  case 0:
25265  case -1:
25266  break;
25267  case 1:
25268  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25269  return gsad_message (credentials,
25270  "Internal error", __FUNCTION__, __LINE__,
25271  "An internal error occurred getting the auth list. "
25272  "Diagnostics: Failure to send command to manager daemon.",
25273  "/omp?cmd=get_users", response_data);
25274  case 2:
25275  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25276  return gsad_message (credentials,
25277  "Internal error", __FUNCTION__, __LINE__,
25278  "An internal error occurred getting the auth list. "
25279  "Diagnostics: Failure to receive response from manager daemon.",
25280  "/omp?cmd=get_users", response_data);
25281  default:
25282  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25283  return gsad_message (credentials,
25284  "Internal error", __FUNCTION__, __LINE__,
25285  "An internal error occurred getting the auth list. "
25286  "Diagnostics: Internal Error.",
25287  "/omp?cmd=get_users", response_data);
25288  }
25289 
25290  g_string_append (extra, response);
25291 
25292  free_entity (entity);
25293  g_free (response);
25294  }
25295  if (extra_xml)
25296  g_string_append (extra, extra_xml);
25297  html = get_many ("user", credentials, params, extra->str, NULL,
25298  response_data);
25299  g_string_free (extra, TRUE);
25300  return html;
25301 }
25302 
25312 char *
25313 get_users_omp (credentials_t * credentials, params_t *params,
25314  cmd_response_data_t* response_data)
25315 {
25316  return get_users (credentials, params, NULL, response_data);
25317 }
25318 
25328 char *
25329 create_user_omp (credentials_t * credentials, params_t *params,
25330  cmd_response_data_t* response_data)
25331 {
25332  const char *no_redirect;
25333  const char *name, *password, *hosts, *hosts_allow, *ifaces, *ifaces_allow;
25334  const char *auth_method;
25335  int ret;
25336  params_t *groups, *roles;
25337  GString *group_elements, *role_elements, *string;
25338  gchar *buf, *response, *html;
25339  entity_t entity;
25340 
25341  no_redirect = params_value (params, "no_redirect");
25342  name = params_value (params, "login");
25343  password = params_value (params, "password");
25344  hosts = params_value (params, "access_hosts");
25345  hosts_allow = params_value (params, "hosts_allow");
25346  ifaces = params_value (params, "access_ifaces");
25347  ifaces_allow = params_value (params, "ifaces_allow");
25348  auth_method = params_value (params, "auth_method");
25349 
25350  CHECK_PARAM_INVALID (name, "Create User", "new_user");
25351  CHECK_PARAM_INVALID (password, "Create User", "new_user");
25352  CHECK_PARAM_INVALID (hosts, "Create User", "new_user");
25353  CHECK_PARAM_INVALID (hosts_allow, "Create User", "new_user");
25354  CHECK_PARAM_INVALID (ifaces, "Create User", "new_user");
25355  CHECK_PARAM_INVALID (ifaces_allow, "Create User", "new_user");
25356 
25357  /* Create the user. */
25358 
25359  string = g_string_new ("<create_user>");
25360  buf = g_markup_printf_escaped ("<name>%s</name>"
25361  "<password>%s</password>",
25362  name,
25363  password);
25364 
25365  g_string_append (string, buf);
25366  g_free (buf);
25367 
25368  group_elements = g_string_new ("<groups>");
25369  if (params_given (params, "group_id_optional:"))
25370  groups = params_values (params, "group_id_optional:");
25371  else
25372  groups = params_values (params, "group_ids:");
25373 
25374  if (groups)
25375  {
25376  params_iterator_t iter;
25377  char *name;
25378  param_t *param;
25379 
25380  params_iterator_init (&iter, groups);
25381  while (params_iterator_next (&iter, &name, &param))
25382  {
25383  if (param->value && strcmp (param->value, "--"))
25384  g_string_append_printf (group_elements,
25385  "<group id=\"%s\"/>",
25386  param->value ? param->value : "");
25387  }
25388  }
25389  g_string_append (string, group_elements->str);
25390  g_string_free (group_elements, TRUE);
25391  g_string_append (string, "</groups>");
25392 
25393  role_elements = g_string_new ("");
25394  if (params_given (params, "role_id_optional:"))
25395  roles = params_values (params, "role_id_optional:");
25396  else
25397  roles = params_values (params, "role_ids:");
25398 
25399  if (roles)
25400  {
25401  params_iterator_t iter;
25402  char *name;
25403  param_t *param;
25404 
25405  params_iterator_init (&iter, roles);
25406  while (params_iterator_next (&iter, &name, &param))
25407  {
25408  if (param->value && strcmp (param->value, "--"))
25409  g_string_append_printf (role_elements,
25410  "<role id=\"%s\"/>",
25411  param->value ? param->value : "");
25412  }
25413  }
25414  g_string_append (string, role_elements->str);
25415  g_string_free (role_elements, TRUE);
25416 
25417  buf = g_markup_printf_escaped ("<hosts allow=\"%s\">%s</hosts>"
25418  "<ifaces allow=\"%s\">%s</ifaces>",
25419  hosts_allow, hosts, ifaces_allow, ifaces);
25420  g_string_append (string, buf);
25421  g_free (buf);
25422  if (auth_method && !strcmp (auth_method, "1"))
25423  g_string_append
25424  (string, "<sources><source>ldap_connect</source></sources>");
25425  else if (auth_method && !strcmp (auth_method, "2"))
25426  g_string_append
25427  (string, "<sources><source>radius_connect</source></sources>");
25428  g_string_append (string, "</create_user>");
25429 
25430  buf = g_string_free (string, FALSE);
25431 
25432  response = NULL;
25433  entity = NULL;
25434  ret = omp (credentials, &response, &entity, response_data, buf);
25435  g_free (buf);
25436  switch (ret)
25437  {
25438  case 0:
25439  case -1:
25440  break;
25441  case 1:
25442  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25443  return gsad_message (credentials,
25444  "Internal error", __FUNCTION__, __LINE__,
25445  "An internal error occurred while creating a new user. "
25446  "No new user was created. "
25447  "Diagnostics: Failure to send command to manager daemon.",
25448  "/omp?cmd=get_users", response_data);
25449  case 2:
25450  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25451  return gsad_message (credentials,
25452  "Internal error", __FUNCTION__, __LINE__,
25453  "An internal error occurred while creating a new user. "
25454  "It is unclear whether the user has been created or not. "
25455  "Diagnostics: Failure to receive response from manager daemon.",
25456  "/omp?cmd=get_users", response_data);
25457  default:
25458  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25459  return gsad_message (credentials,
25460  "Internal error", __FUNCTION__, __LINE__,
25461  "An internal error occurred while creating a new user. "
25462  "It is unclear whether the user has been created or not. "
25463  "Diagnostics: Internal Error.",
25464  "/omp?cmd=get_users", response_data);
25465  }
25466 
25467  if (entity_attribute (entity, "id"))
25468  params_add (params, "user_id", entity_attribute (entity, "id"));
25469  html = response_from_entity (credentials, params, entity,
25470  (no_redirect && strcmp (no_redirect, "0")),
25471  NULL, "get_users",
25472  NULL, "new_user",
25473  "Create User", response_data);
25474  free_entity (entity);
25475  g_free (response);
25476  return html;
25477 }
25478 
25489 char *
25490 edit_user (credentials_t * credentials, params_t *params,
25491  const char *extra_xml, cmd_response_data_t* response_data)
25492 {
25493  gchar *html;
25494  GString *extra;
25495 
25496  extra = g_string_new ("");
25497  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25498  {
25499  gchar *response;
25500  entity_t entity;
25501 
25502  response = NULL;
25503  entity = NULL;
25504  switch (omp (credentials, &response, &entity, response_data,
25505  "<describe_auth/>"))
25506  {
25507  case 0:
25508  case -1:
25509  break;
25510  case 1:
25511  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25512  return gsad_message (credentials,
25513  "Internal error", __FUNCTION__, __LINE__,
25514  "An internal error occurred getting the auth list. "
25515  "Diagnostics: Failure to send command to manager daemon.",
25516  "/omp?cmd=get_users", response_data);
25517  case 2:
25518  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25519  return gsad_message (credentials,
25520  "Internal error", __FUNCTION__, __LINE__,
25521  "An internal error occurred getting the auth list. "
25522  "Diagnostics: Failure to receive response from manager daemon.",
25523  "/omp?cmd=get_users", response_data);
25524  default:
25525  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25526  return gsad_message (credentials,
25527  "Internal error", __FUNCTION__, __LINE__,
25528  "An internal error occurred getting the auth list. "
25529  "Diagnostics: Internal Error.",
25530  "/omp?cmd=get_users", response_data);
25531  }
25532 
25533  g_string_append (extra, response);
25534 
25535  free_entity (entity);
25536  g_free (response);
25537  }
25538 
25539  if (command_enabled (credentials, "GET_GROUPS"))
25540  {
25541  gchar *response;
25542  entity_t entity;
25543 
25544  response = NULL;
25545  entity = NULL;
25546  switch (omp (credentials, &response, &entity, response_data,
25547  "<get_groups/>"))
25548  {
25549  case 0:
25550  case -1:
25551  break;
25552  case 1:
25553  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25554  return gsad_message (credentials,
25555  "Internal error", __FUNCTION__, __LINE__,
25556  "An internal error occurred getting the group list. "
25557  "Diagnostics: Failure to send command to manager daemon.",
25558  "/omp?cmd=get_users", response_data);
25559  case 2:
25560  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25561  return gsad_message (credentials,
25562  "Internal error", __FUNCTION__, __LINE__,
25563  "An internal error occurred getting the group list. "
25564  "Diagnostics: Failure to receive response from manager daemon.",
25565  "/omp?cmd=get_users", response_data);
25566  default:
25567  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25568  return gsad_message (credentials,
25569  "Internal error", __FUNCTION__, __LINE__,
25570  "An internal error occurred getting the group list. "
25571  "Diagnostics: Internal Error.",
25572  "/omp?cmd=get_users", response_data);
25573  }
25574 
25575  g_string_append (extra, response);
25576 
25577  free_entity (entity);
25578  g_free (response);
25579  }
25580 
25581  if (command_enabled (credentials, "GET_ROLES"))
25582  {
25583  gchar *response;
25584  entity_t entity;
25585 
25586  response = NULL;
25587  entity = NULL;
25588  switch (omp (credentials, &response, &entity, response_data,
25589  "<get_roles/>"))
25590  {
25591  case 0:
25592  case -1:
25593  break;
25594  case 1:
25595  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25596  return gsad_message (credentials,
25597  "Internal error", __FUNCTION__, __LINE__,
25598  "An internal error occurred getting the role list. "
25599  "No new user was created. "
25600  "Diagnostics: Failure to send command to manager daemon.",
25601  "/omp?cmd=get_users", response_data);
25602  case 2:
25603  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25604  return gsad_message (credentials,
25605  "Internal error", __FUNCTION__, __LINE__,
25606  "An internal error occurred getting the role list. "
25607  "No new user was created. "
25608  "Diagnostics: Failure to receive response from manager daemon.",
25609  "/omp?cmd=get_users", response_data);
25610  default:
25611  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25612  return gsad_message (credentials,
25613  "Internal error", __FUNCTION__, __LINE__,
25614  "An internal error occurred getting the role list. "
25615  "No new user was created. "
25616  "Diagnostics: Internal Error.",
25617  "/omp?cmd=get_users", response_data);
25618  }
25619 
25620  g_string_append (extra, response);
25621 
25622  free_entity (entity);
25623  g_free (response);
25624  }
25625 
25626  if (extra_xml)
25627  g_string_append (extra, extra_xml);
25628  html = edit_resource ("user", credentials, params, NULL, extra->str,
25629  response_data);
25630  g_string_free (extra, TRUE);
25631  return html;
25632 }
25633 
25643 char *
25644 edit_user_omp (credentials_t * credentials, params_t *params,
25645  cmd_response_data_t* response_data)
25646 {
25647  return edit_user (credentials, params, NULL, response_data);
25648 }
25649 
25650 char *
25651 auth_settings_omp (credentials_t * credentials, params_t *params,
25652  cmd_response_data_t* response_data)
25653 {
25654  GString * xml;
25655  gchar * buf;
25656  const char *name;
25657 
25658  name = params_value (params, "name");
25659 
25660  CHECK_PARAM_INVALID (name, "Auth settings", "auth_settings");
25661 
25662  xml = g_string_new ("");
25663  buf = g_markup_printf_escaped ("<auth_settings name=\"%s\">", name);
25664  g_string_append (xml, buf);
25665  g_free (buf);
25666 
25667  if (command_enabled (credentials, "DESCRIBE_AUTH"))
25668  {
25669  gchar * response = NULL;
25670  entity_t entity = NULL;
25671 
25672  switch (omp (credentials, &response, &entity, response_data,
25673  "<describe_auth/>"))
25674  {
25675  case 0:
25676  case -1:
25677  break;
25678  case 1:
25679  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25680  return gsad_message (credentials,
25681  "Internal error", __FUNCTION__, __LINE__,
25682  "An internal error occurred getting the auth list. "
25683  "Diagnostics: Failure to send command to manager daemon.",
25684  "/omp?cmd=get_users", response_data);
25685  case 2:
25686  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25687  return gsad_message (credentials,
25688  "Internal error", __FUNCTION__, __LINE__,
25689  "An internal error occurred getting the auth list. "
25690  "Diagnostics: Failure to receive response from manager daemon.",
25691  "/omp?cmd=get_users", response_data);
25692  default:
25693  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25694  return gsad_message (credentials,
25695  "Internal error", __FUNCTION__, __LINE__,
25696  "An internal error occurred getting the auth list. "
25697  "Diagnostics: Internal Error.",
25698  "/omp?cmd=get_users", response_data);
25699  }
25700 
25701 
25702  g_string_append (xml, response);
25703  free_entity (entity);
25704  g_free (response);
25705  }
25706 
25707  g_string_append (xml, "</auth_settings>");
25708 
25709  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
25710  response_data);
25711 }
25712 
25722 static char *
25723 logout (credentials_t *credentials, const gchar *message,
25724  cmd_response_data_t *response_data)
25725 {
25726  time_t now;
25727  gchar *xml;
25728  char *res;
25729  char ctime_now[200];
25730  int ret;
25731  const char* xml_flag;
25732  xml_flag = params_value (credentials->params, "xml");
25733 
25734  if (credentials->token == NULL)
25735  return NULL;
25736 
25737  ret = token_user_remove (credentials->token);
25738  if (ret)
25739  return NULL;
25740 
25741  now = time (NULL);
25742  ctime_r_strip_newline (&now, ctime_now);
25743 
25744  xml = login_xml (message, NULL, ctime_now, NULL, NULL, NULL);
25745 
25746  if (xml_flag && strcmp (xml_flag, "0"))
25747  return xml;
25748 
25749  res = xsl_transform (xml, response_data);
25750  g_free (xml);
25751  return res;
25752 }
25753 
25766 char *
25767 save_user_omp (credentials_t * credentials, params_t *params,
25768  char **password_return, char **modified_user,
25769  int *logout_user, cmd_response_data_t* response_data)
25770 {
25771  int ret;
25772  gchar *html, *response, *buf;
25773  const char *no_redirect;
25774  const char *user_id, *login, *old_login, *modify_password, *password;
25775  const char *hosts, *hosts_allow, *ifaces, *ifaces_allow;
25776  entity_t entity;
25777  GString *command, *group_elements, *role_elements;
25778  params_t *groups, *roles;
25779  const char *status;
25780 
25781  *password_return = NULL;
25782  *modified_user = NULL;
25783  *logout_user = 0;
25784 
25785  no_redirect = params_value (params, "no_redirect");
25786  /* List of hosts user has/lacks access rights. */
25787  hosts = params_value (params, "access_hosts");
25788  /* Whether hosts grants ("1") or forbids ("0") access. "2" for all
25789  * access. */
25790  hosts_allow = params_value (params, "hosts_allow");
25791  ifaces = params_value (params, "access_ifaces");
25792  ifaces_allow = params_value (params, "ifaces_allow");
25793  login = params_value (params, "login");
25794  old_login = params_value (params, "old_login");
25795  modify_password = params_value (params, "modify_password");
25796  password = params_value (params, "password");
25797  user_id = params_value (params, "user_id");
25798 
25799  CHECK_PARAM_INVALID (user_id, "Edit User", "edit_user");
25800  CHECK_PARAM_INVALID (modify_password, "Edit User", "edit_user");
25801  CHECK_PARAM_INVALID (password, "Edit User", "edit_user");
25802  CHECK_PARAM_INVALID (hosts, "Edit User", "edit_user");
25803  CHECK_PARAM_INVALID (hosts_allow, "Edit User", "edit_user");
25804  CHECK_PARAM_INVALID (ifaces, "Save User", "edit_user");
25805  CHECK_PARAM_INVALID (ifaces_allow, "Save User", "edit_user");
25806 
25807  if (params_given (params, "login")
25808  && !(params_given (params, "current_user")))
25809  {
25810  CHECK_PARAM_INVALID (login, "Save User", "edit_user");
25811  CHECK_PARAM_INVALID (old_login, "Save User", "edit_user");
25812  }
25813 
25814  /* Modify the user. */
25815 
25816  command = g_string_new ("");
25817  buf = g_markup_printf_escaped ("<modify_user user_id=\"%s\">"
25818  "<password modify=\"%s\">"
25819  "%s</password>",
25820  user_id,
25821  modify_password,
25822  password);
25823  g_string_append (command, buf);
25824  g_free (buf);
25825 
25826  if (login)
25827  {
25828  buf = g_markup_printf_escaped ("<new_name>%s</new_name>",
25829  login);
25830  g_string_append (command, buf);
25831  g_free (buf);
25832  }
25833 
25834  buf = g_markup_printf_escaped ("<hosts allow=\"%s\">%s</hosts>"
25835  "<ifaces allow=\"%s\">%s</ifaces>",
25836  hosts_allow, hosts, ifaces_allow, ifaces);
25837  g_string_append (command, buf);
25838  g_free (buf);
25839 
25840  if (modify_password && !strcmp (modify_password, "2"))
25841  g_string_append (command, "<sources><source>ldap_connect</source></sources>");
25842  else if (modify_password && !strcmp (modify_password, "3"))
25843  g_string_append (command, "<sources><source>radius_connect</source></sources>");
25844  else
25845  g_string_append (command, "<sources><source>file</source></sources>");
25846 
25847  group_elements = g_string_new ("<groups>");
25848  if (params_given (params, "group_id_optional:"))
25849  groups = params_values (params, "group_id_optional:");
25850  else
25851  groups = params_values (params, "group_ids:");
25852 
25853  if (groups)
25854  {
25855  params_iterator_t iter;
25856  char *name;
25857  param_t *param;
25858 
25859  params_iterator_init (&iter, groups);
25860  while (params_iterator_next (&iter, &name, &param))
25861  {
25862  if (param->value && strcmp (param->value, "--"))
25863  g_string_append_printf (group_elements,
25864  "<group id=\"%s\"/>",
25865  param->value ? param->value : "");
25866  }
25867  }
25868  g_string_append (command, group_elements->str);
25869  g_string_free (group_elements, TRUE);
25870  g_string_append (command, "</groups>");
25871 
25872  role_elements = g_string_new ("");
25873  if (params_given (params, "role_id_optional:"))
25874  roles = params_values (params, "role_id_optional:");
25875  else
25876  roles = params_values (params, "role_ids:");
25877 
25878  if (roles)
25879  {
25880  params_iterator_t iter;
25881  char *name;
25882  param_t *param;
25883 
25884  params_iterator_init (&iter, roles);
25885  while (params_iterator_next (&iter, &name, &param))
25886  {
25887  if (param->value && strcmp (param->value, "--"))
25888  g_string_append_printf (role_elements,
25889  "<role id=\"%s\"/>",
25890  param->value ? param->value : "");
25891  }
25892  }
25893  else
25894  g_string_append_printf (role_elements, "<role id=\"0\"/>");
25895 
25896  g_string_append (command, role_elements->str);
25897  g_string_free (role_elements, TRUE);
25898 
25899  g_string_append (command, "</modify_user>");
25900 
25901  response = NULL;
25902  entity = NULL;
25903  ret = omp (credentials,
25904  &response,
25905  &entity,
25906  response_data,
25907  command->str);
25908  g_string_free (command, TRUE);
25909  switch (ret)
25910  {
25911  case 0:
25912  status = entity_attribute (entity, "status");
25913  if (status && (strlen (status) > 0) && (status[0] == '2'))
25914  {
25915  *modified_user
25916  = g_strdup (old_login ? old_login : credentials->username);
25917 
25918  if (strcmp (modify_password, "0")
25919  || (login && strcmp (old_login, login)))
25920  *logout_user = 1;
25921 
25922  if (strcmp (modify_password, "0")
25923  && strcmp (modify_password, "2")
25924  && strcmp (modify_password, "3")
25925  && params_given (params, "current_user"))
25926  {
25927  g_free (credentials->password);
25928  credentials->password = g_strdup (password);
25929  *password_return = g_strdup (password);
25930  }
25931  }
25932  break;
25933  case -1:
25934  break;
25935  case 1:
25936  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25937  return gsad_message (credentials,
25938  "Internal error", __FUNCTION__, __LINE__,
25939  "An internal error occurred while saving a user. "
25940  "The user was not saved. "
25941  "Diagnostics: Failure to send command to manager daemon.",
25942  "/omp?cmd=get_users", response_data);
25943  case 2:
25944  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25945  return gsad_message (credentials,
25946  "Internal error", __FUNCTION__, __LINE__,
25947  "An internal error occurred while saving a user. "
25948  "It is unclear whether the user has been saved or not. "
25949  "Diagnostics: Failure to receive response from manager daemon.",
25950  "/omp?cmd=get_users", response_data);
25951  default:
25952  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
25953  return gsad_message (credentials,
25954  "Internal error", __FUNCTION__, __LINE__,
25955  "An internal error occurred while saving a user. "
25956  "It is unclear whether the user has been saved or not. "
25957  "Diagnostics: Internal Error.",
25958  "/omp?cmd=get_users", response_data);
25959  }
25960 
25961  if (omp_success (entity)
25962  && (!strcmp (modify_password, "2")
25963  || !strcmp (modify_password, "3"))
25964  && params_given (params, "current_user"))
25965  {
25966  free_entity (entity);
25967  g_free (response);
25968  html = logout (credentials,
25969  "Authentication method changed."
25970  " Please login with LDAP password.",
25971  response_data);
25972  }
25973  else
25974  html = response_from_entity (credentials, params, entity,
25975  (no_redirect && strcmp (no_redirect, "0")),
25976  NULL, "get_users",
25977  NULL, "edit_user",
25978  "Save User", response_data);
25979  free_entity (entity);
25980  g_free (response);
25981  return html;
25982 }
25983 
25996 char *
25997 export_user_omp (credentials_t * credentials, params_t *params,
25998  enum content_type * content_type, char **content_disposition,
25999  gsize *content_length, cmd_response_data_t* response_data)
26000 {
26001  return export_resource ("user", credentials, params, content_type,
26002  content_disposition, content_length, response_data);
26003 }
26004 
26018 char *
26019 export_users_omp (credentials_t * credentials, params_t *params,
26020  enum content_type * content_type, char **content_disposition,
26021  gsize *content_length, cmd_response_data_t* response_data)
26022 {
26023  return export_many ("user", credentials, params, content_type,
26024  content_disposition, content_length, response_data);
26025 }
26026 
26027 char *
26028 cvss_calculator (credentials_t * credentials, params_t *params,
26029  cmd_response_data_t* response_data)
26030 {
26031  GString *xml;
26032  const char *cvss_av, *cvss_au, *cvss_ac, *cvss_c, *cvss_i, *cvss_a;
26033  const char *cvss_vector, *name;
26034 
26035  cvss_av = params_value (params, "cvss_av");
26036  cvss_au = params_value (params, "cvss_au");
26037  cvss_ac = params_value (params, "cvss_ac");
26038  cvss_c = params_value (params, "cvss_c");
26039  cvss_i = params_value (params, "cvss_i");
26040  cvss_a = params_value (params, "cvss_a");
26041  cvss_vector = params_value (params, "cvss_vector");
26042  name = params_value (params, "name");
26043 
26044  xml = g_string_new ("<cvss_calculator>");
26045 
26046  /* Calculate base score */
26047  if (cvss_av && cvss_au && cvss_ac && cvss_c && cvss_i && cvss_a)
26048  {
26049  char *vector = g_strdup_printf
26050  ("AV:%c/AC:%c/Au:%c/C:%c/I:%c/A:%c",
26051  *cvss_av, *cvss_ac, *cvss_au, *cvss_c, *cvss_i, *cvss_a);
26052 
26053  g_string_append_printf (xml,
26054  "<cvss_vector>%s</cvss_vector>"
26055  "<cvss_score>%.1f</cvss_score>",
26056  vector,
26057  get_cvss_score_from_base_metrics (vector));
26058 
26059  g_string_append_printf
26060  (xml,
26061  "<cvss_av>%c</cvss_av><cvss_au>%c</cvss_au>"
26062  "<cvss_ac>%c</cvss_ac><cvss_c>%c</cvss_c>"
26063  "<cvss_i>%c</cvss_i><cvss_a>%c</cvss_a>",
26064  *cvss_av, *cvss_au, *cvss_ac, *cvss_c, *cvss_i, *cvss_a);
26065 
26066  g_free (vector);
26067  }
26068  else if (cvss_vector)
26069  {
26070  double cvss_score = get_cvss_score_from_base_metrics (cvss_vector);
26071 
26072  g_string_append_printf (xml,
26073  "<cvss_vector>%s</cvss_vector>"
26074  "<cvss_score>%.1f</cvss_score>",
26075  cvss_vector,
26076  cvss_score);
26077 
26078  if (cvss_score != -1.0)
26079  {
26080  cvss_av = strstr (cvss_vector, "AV:");
26081  cvss_ac = strstr (cvss_vector, "/AC:");
26082  cvss_au = strstr (cvss_vector, "/Au:");
26083  if (cvss_au == NULL)
26084  cvss_au = strstr (cvss_vector, "/AU:");
26085  cvss_c = strstr (cvss_vector, "/C:");
26086  cvss_i = strstr (cvss_vector, "/I:");
26087  cvss_a = strstr (cvss_vector, "/A:");
26088 
26089  if (cvss_av && cvss_ac && cvss_au && cvss_c
26090  && cvss_i && cvss_a)
26091  g_string_append_printf
26092  (xml,
26093  "<cvss_av>%c</cvss_av><cvss_ac>%c</cvss_ac>"
26094  "<cvss_au>%c</cvss_au><cvss_c>%c</cvss_c>"
26095  "<cvss_i>%c</cvss_i><cvss_a>%c</cvss_a>",
26096  *(cvss_av + 3), *(cvss_ac + 4), *(cvss_au + 4),
26097  *(cvss_c + 3), *(cvss_i + 3), *(cvss_a + 3));
26098  }
26099  }
26100  else if (name && !strcmp ("vector", name))
26101  {
26102  g_string_append_printf (xml,
26103  "<cvss_score>%.1f</cvss_score>",
26104  -1.0);
26105  }
26106 
26107  g_string_append (xml, "</cvss_calculator>");
26108  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
26109  response_data);
26110 }
26111 
26121 char *
26122 dashboard (credentials_t * credentials, params_t *params,
26123  cmd_response_data_t* response_data)
26124 {
26125  GString *xml;
26126  const char *name;
26127  gchar* response;
26128  entity_t entity;
26129  int ret;
26130 
26131  name = params_value (params, "dashboard_name");
26132  if (name == NULL)
26133  {
26134  if (credentials->guest)
26135  name = "secinfo";
26136  else
26137  name = "main";
26138  }
26139 
26140  xml = g_string_new ("<dashboard>");
26141  g_string_append_printf (xml, "<name>%s</name>", name);
26142 
26143  response = NULL;
26144  entity = NULL;
26145 
26146  // Test if SCAP and CERT databases are available
26147  if (strcasecmp (name, "") == 0
26148  || strcasecmp (name, "Main") == 0
26149  || strcasecmp (name, "SecInfo") == 0)
26150  {
26151  ret = ompf (credentials,
26152  &response,
26153  &entity,
26154  response_data,
26155  "<get_info type=\"cve\" info_id=\"--\"/>");
26156 
26157  switch (ret)
26158  {
26159  case 0:
26160  case -1:
26161  break;
26162  case 1:
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 "
26168  "testing SecInfo database availability. "
26169  "Diagnostics: Failure to send command to "
26170  "manager daemon.",
26171  "/omp?cmd=dashboard", response_data);
26172  case 2:
26173  g_string_free (xml, TRUE);
26174  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26175  return gsad_message (credentials,
26176  "Internal error", __FUNCTION__, __LINE__,
26177  "An internal error occurred while "
26178  "testing SecInfo database availability. "
26179  "Diagnostics: Failure to receive response from "
26180  "manager daemon.",
26181  "/omp?cmd=dashboard", response_data);
26182  default:
26183  g_string_free (xml, TRUE);
26184  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26185  return gsad_message (credentials,
26186  "Internal error", __FUNCTION__, __LINE__,
26187  "An internal error occurred while "
26188  "testing SecInfo database availability. "
26189  "Diagnostics: Internal Error.",
26190  "/omp?cmd=dashboard", response_data);
26191  }
26192  g_string_append_printf (xml,
26193  "<secinfo_test>%s</secinfo_test>",
26194  response);
26195  g_free (response);
26196  free_entity (entity);
26197  }
26198 
26199  if (strcasecmp (name, "SecInfo") == 0)
26200  ret = ompf (credentials,
26201  &response,
26202  &entity,
26203  response_data,
26204  "<get_filters filter=\"type=info or type= first=1 rows=-1\"/>");
26205  else
26206  ret = ompf (credentials,
26207  &response,
26208  &entity,
26209  response_data,
26210  "<get_filters filter=\"first=1 rows=-1\"/>");
26211 
26212  switch (ret)
26213  {
26214  case 0:
26215  case -1:
26216  break;
26217  case 1:
26218  g_string_free (xml, TRUE);
26219  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26220  return gsad_message (credentials,
26221  "Internal error", __FUNCTION__, __LINE__,
26222  "An internal error occurred while getting the "
26223  "filters list. "
26224  "Diagnostics: Failure to send command to "
26225  "manager daemon.",
26226  "/omp?cmd=dashboard", response_data);
26227  case 2:
26228  g_string_free (xml, TRUE);
26229  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26230  return gsad_message (credentials,
26231  "Internal error", __FUNCTION__, __LINE__,
26232  "An internal error occurred while getting the "
26233  "filters list. "
26234  "Diagnostics: Failure to receive response from "
26235  "manager daemon.",
26236  "/omp?cmd=dashboard", response_data);
26237  default:
26238  g_string_free (xml, TRUE);
26239  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26240  return gsad_message (credentials,
26241  "Internal error", __FUNCTION__, __LINE__,
26242  "An internal error occurred while getting the "
26243  "filters list. "
26244  "Diagnostics: Internal Error.",
26245  "/omp?cmd=dashboard", response_data);
26246  }
26247 
26248  g_string_append (xml, response);
26249  g_free (response);
26250  free_entity (entity);
26251 
26252  g_string_append (xml, "</dashboard>");
26253  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
26254  response_data);
26255 }
26256 
26260 #define AUTH_CONF_SETTING(key, value) \
26261  "<auth_conf_setting>" \
26262  "<key>" key "</key>" \
26263  "<value>" value "</value>" \
26264  "</auth_conf_setting>"
26265 
26275 char*
26276 save_auth_omp (credentials_t* credentials, params_t *params,
26277  cmd_response_data_t* response_data)
26278 {
26279  int ret;
26280  entity_t entity = NULL;
26281  char *html, *response = NULL, *truefalse;
26282  const char *no_redirect, *method;
26283 
26284  if (params_value (params, "enable")
26285  && (strcmp (params_value (params, "enable"), "1") == 0))
26286  truefalse = "true";
26287  else
26288  truefalse = "false";
26289 
26290  no_redirect = params_value (params, "no_redirect");
26291  method = params_value (params, "group");
26292  CHECK_PARAM_INVALID (method, "Save Authentication", "get_users");
26293  if (!strcmp (method, "method:ldap_connect"))
26294  {
26295  const char *ldaphost, *authdn, *certificate;
26296  ldaphost = params_value (params, "ldaphost");
26297  authdn = params_value (params, "authdn");
26298  certificate = params_value (params, "certificate");
26299 
26300  CHECK_PARAM_INVALID (ldaphost, "Save Authentication", "get_users");
26301  CHECK_PARAM_INVALID (authdn, "Save Authentication", "get_users");
26302  if (params_given (params, "certificate") && strcmp (certificate, ""))
26303  {
26304  CHECK_PARAM_INVALID (certificate, "Save Authentication", "get_users");
26306  ret = ompf (credentials, &response, &entity, response_data,
26307  "<modify_auth>"
26308  "<group name=\"%s\">"
26309  AUTH_CONF_SETTING ("enable", "%s")
26310  AUTH_CONF_SETTING ("ldaphost", "%s")
26311  AUTH_CONF_SETTING ("authdn", "%s")
26312  AUTH_CONF_SETTING ("cacert", "%s")
26313  "</group>"
26314  "</modify_auth>", method, truefalse, ldaphost, authdn,
26315  certificate);
26316  }
26317  else
26319  ret = ompf (credentials, &response, &entity, response_data,
26320  "<modify_auth>"
26321  "<group name=\"%s\">"
26322  AUTH_CONF_SETTING ("enable", "%s")
26323  AUTH_CONF_SETTING ("ldaphost", "%s")
26324  AUTH_CONF_SETTING ("authdn", "%s")
26325  "</group>"
26326  "</modify_auth>", method, truefalse, ldaphost, authdn);
26327  }
26328  else if (!strcmp (method, "method:radius_connect"))
26329  {
26330  const char *radiushost, *radiuskey;
26331  radiushost = params_value (params, "radiushost");
26332  radiuskey = params_value (params, "radiuskey");
26333 
26334  CHECK_PARAM_INVALID (radiushost, "Save Authentication", "get_users");
26335  CHECK_PARAM_INVALID (radiuskey, "Save Authentication", "get_users");
26337  ret = ompf (credentials, &response, &entity, response_data,
26338  "<modify_auth>"
26339  "<group name=\"%s\">"
26340  AUTH_CONF_SETTING ("enable", "%s")
26341  AUTH_CONF_SETTING ("radiushost", "%s")
26342  AUTH_CONF_SETTING ("radiuskey", "%s")
26343  "</group>"
26344  "</modify_auth>", method, truefalse, radiushost, radiuskey);
26345  }
26346  else
26347  return get_users (credentials, params,
26349  ("Save Authentication Configuration"),
26350  response_data);
26351 
26352  switch (ret)
26353  {
26354  case 0:
26355  case -1:
26356  break;
26357  case 1:
26358  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26359  return gsad_message (credentials,
26360  "Internal error", __FUNCTION__, __LINE__,
26361  "An internal error occurred while saving the auth settings. "
26362  "The settings were not saved. "
26363  "Diagnostics: Failure to send command to manager daemon.",
26364  "/omp?cmd=get_users", response_data);
26365  case 2:
26366  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26367  return gsad_message (credentials,
26368  "Internal error", __FUNCTION__, __LINE__,
26369  "An internal error occurred while saving the auth settings. "
26370  "It is unclear whether the settings have been saved or not. "
26371  "Diagnostics: Failure to receive response from manager daemon.",
26372  "/omp?cmd=get_users", response_data);
26373  default:
26374  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26375  return gsad_message (credentials,
26376  "Internal error", __FUNCTION__, __LINE__,
26377  "An internal error occurred while saving the auth settings. "
26378  "It is unclear whether the settings have been saved or not. "
26379  "Diagnostics: Internal Error.",
26380  "/omp?cmd=get_users", response_data);
26381  }
26382 
26383  html = response_from_entity (credentials, params, entity,
26384  (no_redirect && strcmp (no_redirect, "0")),
26385  NULL, NULL,
26386  NULL, "modify_auth",
26387  "Save Authentication Configuration",
26388  response_data);
26389  free_entity (entity);
26390  g_free (response);
26391  return html;
26392 }
26393 
26405 char*
26407  gchar **pref_id, gchar **pref_value,
26408  cmd_response_data_t* response_data)
26409 {
26410  *pref_id = g_strdup (params_value (params, "chart_preference_id"));
26411  *pref_value = g_strdup (params_value (params, "chart_preference_value"));
26412 
26413  gchar* value_64 = g_base64_encode ((guchar*)*pref_value,
26414  strlen (*pref_value));
26415  gchar* response;
26416  entity_t entity;
26417  int ret;
26418 
26419  if (*pref_id == NULL)
26420  {
26421  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26422  return ("<save_chart_preference_response"
26423  " status=\"400\" status_text=\"Invalid or missing name\"/>");
26424  }
26425  if (*pref_value == NULL)
26426  {
26427  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26428  return ("<save_chart_preference_response"
26429  " status=\"400\" status_text=\"Invalid or missing value\"/>");
26430  }
26431 
26432  response = NULL;
26433  entity = NULL;
26434  ret = ompf (credentials, &response, &entity, response_data,
26435  "<modify_setting setting_id=\"%s\">"
26436  "<value>%s</value>"
26437  "</modify_setting>",
26438  *pref_id, value_64);
26439  g_free (value_64);
26440  switch (ret)
26441  {
26442  case 0:
26443  case -1:
26444  break;
26445  case 1:
26446  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26447  return gsad_message (credentials,
26448  "Internal error", __FUNCTION__, __LINE__,
26449  "An internal error occurred while saving settings. "
26450  "It is unclear whether all the settings were saved. "
26451  "Diagnostics: Failure to send command to manager daemon.",
26452  "/omp?cmd=get_my_settings", response_data);
26453  case 2:
26454  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26455  return gsad_message (credentials,
26456  "Internal error", __FUNCTION__, __LINE__,
26457  "An internal error occurred while saving settings. "
26458  "It is unclear whether all the settings were saved. "
26459  "Diagnostics: Failure to receive response from manager daemon.",
26460  "/omp?cmd=get_my_settings", response_data);
26461  default:
26462  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26463  return gsad_message (credentials,
26464  "Internal error", __FUNCTION__, __LINE__,
26465  "An internal error occurred while saving settings. "
26466  "It is unclear whether all the settings were saved. "
26467  "Diagnostics: Internal Error.",
26468  "/omp?cmd=get_my_settings", response_data);
26469  }
26470 
26471  if (omp_success (entity))
26472  {
26473  free_entity (entity);
26474  g_free (response);
26475  return ("<save_chart_preference_response"
26476  " status=\"200\" status_text=\"OK\"/>");
26477  }
26478  else
26479  {
26480  set_http_status_from_entity (entity, response_data);
26481  gchar* ret_response
26482  = g_strdup_printf("<save_chart_preference_response"
26483  " status=\"%s\" status_text=\"%s\"/>",
26484  entity_attribute (entity, "status"),
26485  entity_attribute (entity, "status_text"));
26486  free_entity (entity);
26487  g_free (response);
26488  return ret_response;
26489  }
26490 }
26491 
26492 
26493 /* Wizards. */
26494 
26505 char *
26506 wizard (credentials_t *credentials, params_t *params, const char *extra_xml,
26507  cmd_response_data_t* response_data)
26508 {
26509  GString *xml;
26510  openvas_connection_t connection;
26511  gchar *html;
26512  const char* name = params_value (params, "name");
26513 
26514  switch (manager_connect (credentials, &connection, &html,
26515  response_data))
26516  {
26517  case 0:
26518  break;
26519  case -1:
26520  if (html)
26521  return html;
26522  /* Fall through. */
26523  default:
26524  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26525  return gsad_message (credentials,
26526  "Internal error", __FUNCTION__, __LINE__,
26527  "An internal error occurred while getting the wizard. "
26528  "Diagnostics: Failure to connect to manager daemon.",
26529  "/omp?cmd=get_tasks", response_data);
26530  }
26531 
26532  if (name == NULL)
26533  {
26534  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26535  return gsad_message (credentials,
26536  "Internal error", __FUNCTION__, __LINE__,
26537  "An internal error occurred while getting the wizard. "
26538  "Given name was invalid",
26539  "/omp?cmd=get_tasks", response_data);
26540  }
26541 
26542  xml = g_string_new ("");
26543  g_string_append_printf (xml,
26544  "<wizard>%s<%s/>",
26545  extra_xml ? extra_xml : "",
26546  name);
26547 
26548  /* Try to run init mode of the wizard */
26549  if (openvas_connection_sendf_xml (&connection,
26550  "<run_wizard read_only=\"1\">"
26551  "<name>%s</name>"
26552  "<mode>init</mode>"
26553  "</run_wizard>",
26554  name)
26555  == -1)
26556  {
26557  g_string_free (xml, TRUE);
26558  openvas_connection_close (&connection);
26559  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26560  return gsad_message (credentials,
26561  "Internal error", __FUNCTION__, __LINE__,
26562  "An internal error occurred while getting the wizard. "
26563  "Diagnostics: Failure to send command to manager daemon.",
26564  "/omp?cmd=get_tasks", response_data);
26565  }
26566 
26567  if (read_string_c (&connection, &xml))
26568  {
26569  g_string_free (xml, TRUE);
26570  openvas_connection_close (&connection);
26571  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26572  return gsad_message (credentials,
26573  "Internal error", __FUNCTION__, __LINE__,
26574  "An internal error occurred while getting the"
26575  " wizard."
26576  "Diagnostics: Failure to receive response from"
26577  " manager daemon.",
26578  "/omp?cmd=get_tasks", response_data);
26579  }
26580 
26581  /* Get the setting. */
26582 
26583  if (openvas_connection_sendf_xml (&connection,
26584  "<get_settings"
26585  " setting_id=\"20f3034c-e709-11e1-87e7-406186ea4fc5\"/>")
26586  == -1)
26587  {
26588  g_string_free (xml, TRUE);
26589  openvas_connection_close (&connection);
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 the wizard. "
26594  "Diagnostics: Failure to send command to manager daemon.",
26595  "/omp?cmd=get_tasks", response_data);
26596  }
26597 
26598  if (read_string_c (&connection, &xml))
26599  {
26600  g_string_free (xml, TRUE);
26601  openvas_connection_close (&connection);
26602  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26603  return gsad_message (credentials,
26604  "Internal error", __FUNCTION__, __LINE__,
26605  "An internal error occurred while the wizard. "
26606  "Diagnostics: Failure to receive response from manager daemon.",
26607  "/omp?cmd=get_tasks", response_data);
26608  }
26609 
26610  /* Cleanup, and return transformed XML. */
26611 
26612  g_string_append_printf (xml, "</wizard>");
26613  openvas_connection_close (&connection);
26614  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
26615  response_data);
26616 }
26617 
26627 char *
26628 wizard_omp (credentials_t *credentials, params_t *params,
26629  cmd_response_data_t* response_data)
26630 {
26631  return wizard (credentials, params, NULL, response_data);
26632 }
26633 
26644 char *
26645 wizard_get (credentials_t *credentials, params_t *params, const char *extra_xml,
26646  cmd_response_data_t* response_data)
26647 {
26648  const char *name;
26649  int ret;
26650  GString *run;
26651  param_t *param;
26652  gchar *param_name, *response;
26653  params_iterator_t iter;
26654  params_t *wizard_params;
26655  entity_t entity;
26656  gchar *wizard_xml;
26657 
26658  /* The naming is a bit subtle here, because the HTTP request
26659  * parameters are called "param"s and so are the OMP wizard
26660  * parameters. */
26661 
26662  name = params_value (params, "get_name");
26663  if (name == NULL)
26664  {
26665  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26666  return gsad_message (credentials,
26667  "Internal error", __FUNCTION__, __LINE__,
26668  "An internal error occurred while trying to start a wizard. "
26669  "Diagnostics: Required parameter 'get_name' was NULL.",
26670  "/omp?cmd=get_tasks", response_data);
26671  }
26672 
26673  run = g_string_new ("<run_wizard read_only=\"1\">");
26674 
26675  g_string_append_printf (run,
26676  "<name>%s</name>"
26677  "<params>",
26678  name);
26679 
26680  wizard_params = params_values (params, "event_data:");
26681  if (wizard_params)
26682  {
26683  params_iterator_init (&iter, wizard_params);
26684  while (params_iterator_next (&iter, &param_name, &param))
26685  xml_string_append (run,
26686  "<param>"
26687  "<name>%s</name>"
26688  "<value>%s</value>"
26689  "</param>",
26690  param_name,
26691  param->value);
26692  }
26693 
26694  g_string_append (run, "</params></run_wizard>");
26695 
26696  response = NULL;
26697  entity = NULL;
26698  ret = omp (credentials, &response, &entity, response_data, run->str);
26699  g_string_free (run, TRUE);
26700  switch (ret)
26701  {
26702  case 0:
26703  case -1:
26704  break;
26705  case 1:
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 running a wizard. "
26710  "The wizard did not start. "
26711  "Diagnostics: Failure to send command to manager daemon.",
26712  "/omp?cmd=get_tasks", response_data);
26713  case 2:
26714  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26715  return gsad_message (credentials,
26716  "Internal error", __FUNCTION__, __LINE__,
26717  "An internal error occurred while running a wizard. "
26718  "It is unclear whether the wizard started or not. "
26719  "Diagnostics: Failure to receive response from manager daemon.",
26720  "/omp?cmd=get_tasks", response_data);
26721  default:
26722  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26723  return gsad_message (credentials,
26724  "Internal error", __FUNCTION__, __LINE__,
26725  "An internal error occurred while running a wizard. "
26726  "It is unclear whether the wizard started or not. "
26727  "Diagnostics: Internal Error.",
26728  "/omp?cmd=get_tasks", response_data);
26729  }
26730 
26731  wizard_xml = g_strdup_printf ("<wizard><%s/>%s%s</wizard>",
26732  name,
26733  extra_xml ? extra_xml : "",
26734  response);
26735 
26736  return xsl_transform_omp (credentials, wizard_xml, response_data);
26737 }
26738 
26748 char *
26749 wizard_get_omp (credentials_t *credentials, params_t *params,
26750  cmd_response_data_t* response_data)
26751 {
26752  return wizard_get (credentials, params, NULL, response_data);
26753 }
26754 
26767 char *
26768 process_bulk_omp (credentials_t *credentials, params_t *params,
26769  enum content_type *content_type,
26770  char **content_disposition, gsize *content_length,
26771  cmd_response_data_t* response_data)
26772 {
26773  GString *bulk_string;
26774  const char *type, *subtype, *action;
26775  char *param_name;
26776  params_t *selected_ids;
26777  param_t *param;
26778  params_iterator_t iter;
26779 
26780  type = params_value (params, "resource_type");
26781  if (type == NULL)
26782  {
26783  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26784  return gsad_message (credentials,
26785  "Internal error", __FUNCTION__, __LINE__,
26786  "An internal error occurred while performing a bulk action. "
26787  "Diagnostics: Required parameter 'resource_type' was NULL.",
26788  "/omp?cmd=get_tasks", response_data);
26789  }
26790  if (strcmp (type, "info") == 0)
26791  {
26792  subtype = params_value (params, "info_type");
26793  if (subtype == NULL)
26794  {
26795  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26796  return gsad_message (credentials,
26797  "Internal error", __FUNCTION__, __LINE__,
26798  "An internal error occurred while performing a bulk action. "
26799  "Diagnostics: Required parameter 'info_type' was NULL.",
26800  "/omp?cmd=get_tasks", response_data);
26801  }
26802  }
26803  else
26804  subtype = NULL;
26805 
26806  if (params_valid (params, "bulk_create.x"))
26807  action = "create";
26808  else if (params_valid (params, "bulk_delete.x"))
26809  action = "delete";
26810  else if (params_valid (params, "bulk_export.x"))
26811  action = "export";
26812  else if (params_value (params, "bulk_trash.x"))
26813  action = "trash";
26814  else
26815  {
26816  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
26817  return gsad_message (credentials,
26818  "Internal error", __FUNCTION__, __LINE__,
26819  "An internal error occurred while performing a bulk action. "
26820  "Diagnostics: Could not determine the action.",
26821  "/omp?cmd=get_tasks", response_data);
26822  }
26823 
26824  if (strcmp (action, "create") == 0)
26825  {
26826  if (params_value (params, "bulk_select")
26827  && strcmp (params_value (params, "bulk_select"), "1") == 0)
26828  {
26829  bulk_string = g_string_new ("first=1 rows=-1");
26830 
26831  selected_ids = params_values (params, "bulk_selected:");
26832  if (selected_ids)
26833  {
26834  params_iterator_init (&iter, selected_ids);
26835  while (params_iterator_next (&iter, &param_name, &param))
26836  xml_string_append (bulk_string,
26837  " uuid=%s",
26838  param_name);
26839  }
26840  }
26841  else if (params_value (params, "bulk_select")
26842  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26843  {
26844  bulk_string = g_string_new ("first=1 rows=-1 ");
26845  g_string_append (bulk_string, params_value (params, "filter") ? : "");
26846  }
26847  else
26848  {
26849  bulk_string = g_string_new (params_value (params, "filter") ? : "");
26850  }
26851  param = params_add (params, "hosts_filter", bulk_string->str);
26852  param->valid = 1;
26853  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
26854  g_string_free (bulk_string, TRUE);
26855 
26856  return new_target (credentials, params, NULL, response_data);
26857  }
26858 
26859  if (strcmp (action, "export") == 0)
26860  {
26861  if (params_value (params, "bulk_select")
26862  && strcmp (params_value (params, "bulk_select"), "1") == 0)
26863  {
26864  bulk_string
26865  = g_string_new ("first=1 rows=-1 uuid=");
26866 
26867  selected_ids = params_values (params, "bulk_selected:");
26868  if (selected_ids)
26869  {
26870  params_iterator_init (&iter, selected_ids);
26871  while (params_iterator_next (&iter, &param_name, &param))
26872  xml_string_append (bulk_string,
26873  " uuid=%s",
26874  param_name);
26875  }
26876  }
26877  else if (params_value (params, "bulk_select")
26878  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26879  {
26880  bulk_string
26881  = g_string_new ("first=1 rows=-1 ");
26882  g_string_append (bulk_string, params_value (params, "filter") ? : "");
26883  }
26884  else
26885  {
26886  bulk_string
26887  = g_string_new (params_value (params, "filter") ? : "");
26888  }
26889  params_add (params, "filter", g_string_free (bulk_string, FALSE));
26890 
26891  return export_many (type, credentials, params, content_type,
26892  content_disposition, content_length, response_data);
26893  }
26894 
26895  bulk_string = g_string_new ("<process_bulk>");
26896 
26897  g_string_append_printf (bulk_string,
26898  "<type>%s</type>"
26899  "<action>%s</action>",
26900  type,
26901  action);
26902 
26903  g_string_append (bulk_string, "<selections>");
26904 
26905  if (params_value (params, "bulk_select")
26906  && strcmp (params_value (params, "bulk_select"), "2") == 0)
26907  {
26908  int ret;
26909  entity_t entity;
26910  gchar *response;
26911 
26912  ret = ompf (credentials, &response, &entity, response_data,
26913  "<get_%ss filter=\"first=1 rows=-1 %s\"/>",
26914  type,
26915  params_value (params, "filter") ? : "");
26916 
26917  if (ret)
26918  {
26919  free_entity (entity);
26920  g_free (response);
26921  g_string_free (bulk_string, TRUE);
26922  }
26923  switch (ret)
26924  {
26925  case 0:
26926  case -1:
26927  break;
26928  case 1:
26929  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26930  return gsad_message (credentials,
26931  "Internal error", __FUNCTION__, __LINE__,
26932  "An internal error occurred while getting a"
26933  " resources list. "
26934  "Diagnostics: Failure to send command to"
26935  " manager daemon.",
26936  "/omp?cmd=get_my_settings", response_data);
26937  case 2:
26938  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26939  return gsad_message (credentials,
26940  "Internal error", __FUNCTION__, __LINE__,
26941  "An internal error occurred while getting a"
26942  " resources list. "
26943  "Diagnostics: Failure to receive response from"
26944  " manager daemon.",
26945  "/omp?cmd=get_my_settings", response_data);
26946  default:
26947  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
26948  return gsad_message (credentials,
26949  "Internal error", __FUNCTION__, __LINE__,
26950  "An internal error occurred while getting a"
26951  " resources list. "
26952  "Diagnostics: Internal Error.",
26953  "/omp?cmd=get_my_settings", response_data);
26954  }
26955 
26956  entities_t entities = entity->entities;
26957  entity_t child_entity;
26958 
26959  while ((child_entity = first_entity (entities)))
26960  {
26961  if (strcmp (entity_name (child_entity), type) == 0)
26962  {
26963  const char *resource_id
26964  = entity_attribute (child_entity, "id");
26965 
26966  if (resource_id)
26967  xml_string_append (bulk_string,
26968  "<selection id=\"%s\" />",
26969  resource_id);
26970  }
26971  entities = next_entities (entities);
26972  }
26973  }
26974  else
26975  {
26976  selected_ids = params_values (params, "bulk_selected:");
26977  if (selected_ids)
26978  {
26979  params_iterator_init (&iter, selected_ids);
26980  while (params_iterator_next (&iter, &param_name, &param))
26981  xml_string_append (bulk_string,
26982  "<selection id=\"%s\" />",
26983  param_name);
26984  }
26985  }
26986  g_string_append (bulk_string, "</selections>");
26987 
26988  if (strcmp (action, "delete") == 0 && strcmp (type, "user") == 0)
26989  {
26990  int ret;
26991  entity_t entity;
26992  gchar *response;
26993 
26994  ret = ompf (credentials, &response, &entity, response_data,
26995  "<get_users filter=\"first=1 rows=-1\"/>",
26996  type,
26997  params_value (params, "filter") ? : "");
26998 
26999  if (ret)
27000  {
27001  free_entity (entity);
27002  g_free (response);
27003  g_string_free (bulk_string, TRUE);
27004  }
27005  switch (ret)
27006  {
27007  case 0:
27008  case -1:
27009  break;
27010  case 1:
27011  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27012  return gsad_message (credentials,
27013  "Internal error", __FUNCTION__, __LINE__,
27014  "An internal error occurred while getting a"
27015  " resources list. "
27016  "Diagnostics: Failure to send command to"
27017  " manager daemon.",
27018  "/omp?cmd=get_my_settings", response_data);
27019  case 2:
27020  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27021  return gsad_message (credentials,
27022  "Internal error", __FUNCTION__, __LINE__,
27023  "An internal error occurred while getting a"
27024  " resources list. "
27025  "Diagnostics: Failure to receive response from"
27026  " manager daemon.",
27027  "/omp?cmd=get_my_settings", response_data);
27028  default:
27029  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27030  return gsad_message (credentials,
27031  "Internal error", __FUNCTION__, __LINE__,
27032  "An internal error occurred while getting a"
27033  " resources list. "
27034  "Diagnostics: Internal Error.",
27035  "/omp?cmd=get_my_settings", response_data);
27036  }
27037 
27038  g_string_append (bulk_string, response);
27039  free_entity (entity);
27040  g_free (response);
27041  }
27042 
27043  g_string_append (bulk_string, "</process_bulk>");
27044 
27045  return xsl_transform_omp (credentials, g_string_free (bulk_string, FALSE),
27046  response_data);
27047 }
27048 
27058 char *
27059 bulk_delete_omp (credentials_t * credentials, params_t *params,
27060  cmd_response_data_t* response_data)
27061 {
27062  openvas_connection_t connection;
27063  const char *no_redirect, *type;
27064  GString *commands_xml;
27065  params_t *selected_ids;
27066  params_iterator_t iter;
27067  param_t *param;
27068  gchar *param_name;
27069  gchar *html, *response;
27070  entity_t entity;
27071  gchar *extra_attribs;
27072 
27073  no_redirect = params_value (params, "no_redirect");
27074  type = params_value (params, "resource_type");
27075  if (type == NULL)
27076  {
27077  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27078  return gsad_message (credentials,
27079  "Internal error", __FUNCTION__, __LINE__,
27080  "An internal error occurred while deleting resources. "
27081  "The resources were not deleted. "
27082  "Diagnostics: Required parameter 'resource_type' was NULL.",
27083  "/omp?cmd=get_tasks", response_data);
27084  }
27085 
27086  /* Extra attributes */
27087  extra_attribs = NULL;
27088 
27089  /* Inheritor of user's resource */
27090  if (strcmp (type, "user") == 0)
27091  {
27092  const char* inheritor_id;
27093  inheritor_id = params_value (params, "inheritor_id");
27094  if (inheritor_id)
27095  extra_attribs = g_strdup_printf ("inheritor_id=\"%s\"", inheritor_id);
27096  }
27097 
27098  commands_xml = g_string_new ("<commands>");
27099 
27100  selected_ids = params_values (params, "bulk_selected:");
27101  if (selected_ids)
27102  {
27103  params_iterator_init (&iter, selected_ids);
27104  while (params_iterator_next (&iter, &param_name, &param))
27105  {
27106  xml_string_append (commands_xml,
27107  "<delete_%s %s_id=\"%s\" ultimate=\"0\"",
27108  type,
27109  type,
27110  param_name);
27111  if (extra_attribs)
27112  g_string_append_printf (commands_xml, " %s/>", extra_attribs);
27113  else
27114  g_string_append (commands_xml, "/>");
27115  }
27116  }
27117 
27118  g_string_append (commands_xml, "</commands>");
27119 
27120  switch (manager_connect (credentials, &connection, &html,
27121  response_data))
27122  {
27123  case 0:
27124  break;
27125  case -1:
27126  if (html)
27127  return html;
27128  /* Fall through. */
27129  default:
27130  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27131  return gsad_message (credentials,
27132  "Internal error", __FUNCTION__, __LINE__,
27133  "An internal error occurred while deleting resources. "
27134  "The resources were not deleted. "
27135  "Diagnostics: Failure to connect to manager daemon.",
27136  "/omp?cmd=get_tasks", response_data);
27137  }
27138 
27139  /* Delete the resources and get all resources. */
27140 
27141  if (openvas_connection_sendf_xml (&connection,
27142  commands_xml->str)
27143  == -1)
27144  {
27145  g_string_free (commands_xml, TRUE);
27146  openvas_connection_close (&connection);
27147  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27148  return gsad_message (credentials,
27149  "Internal error", __FUNCTION__, __LINE__,
27150  "An internal error occurred while deleting resources. "
27151  "The resources were not deleted. "
27152  "Diagnostics: Failure to send command to manager daemon.",
27153  "/omp?cmd=get_tasks", response_data);
27154  }
27155  g_string_free (commands_xml, TRUE);
27156 
27157  entity = NULL;
27158  if (read_entity_and_text_c (&connection, &entity, &response))
27159  {
27160  openvas_connection_close (&connection);
27161  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27162  return gsad_message (credentials,
27163  "Internal error", __FUNCTION__, __LINE__,
27164  "An internal error occurred while deleting resources. "
27165  "It is unclear whether the resources have been deleted or not. "
27166  "Diagnostics: Failure to read response from manager daemon.",
27167  "/omp?cmd=get_tasks", response_data);
27168  }
27169 
27170  openvas_connection_close (&connection);
27171 
27172  /* Cleanup, and return transformed XML. */
27173 
27174  if (params_given (params, "next") == 0)
27175  {
27176  gchar *next;
27177  next = g_strdup_printf ("get_%ss", type);
27178  params_add (params, "next", next);
27179  g_free (next);
27180  }
27181  html = response_from_entity (credentials, params, entity,
27182  (no_redirect && strcmp (no_redirect, "0")),
27183  NULL, NULL,
27184  NULL, NULL,
27185  "Bulk Delete", response_data);
27186  g_free (response);
27187  free_entity (entity);
27188 
27189  return html;
27190 }
27191 
27192 
27193 /* Assets. */
27194 
27205 static char *
27206 new_host (credentials_t *credentials, params_t *params,
27207  const char *extra_xml, cmd_response_data_t* response_data)
27208 {
27209  GString *xml;
27210  xml = g_string_new ("<new_host>");
27211  if (extra_xml)
27212  g_string_append (xml, extra_xml);
27213  g_string_append (xml, "</new_host>");
27214  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
27215  response_data);
27216 }
27217 
27227 char *
27228 new_host_omp (credentials_t *credentials, params_t *params,
27229  cmd_response_data_t* response_data)
27230 {
27231  return new_host (credentials, params, NULL, response_data);
27232 }
27233 
27243 char *
27244 create_host_omp (credentials_t * credentials, params_t *params,
27245  cmd_response_data_t* response_data)
27246 {
27247  int ret;
27248  gchar *html, *response;
27249  const char *no_redirect, *name, *comment;
27250  entity_t entity;
27251  GString *xml;
27252 
27253  no_redirect = params_value (params, "no_redirect");
27254 
27255  name = params_value (params, "name");
27256  CHECK_PARAM_INVALID (name, "Create Host", "new_host");
27257 
27258  comment = params_value (params, "comment");
27259  CHECK_PARAM_INVALID (comment, "Create Host", "new_host");
27260 
27261  /* Create the host. */
27262 
27263  xml = g_string_new ("");
27264 
27265  xml_string_append (xml,
27266  "<create_asset>"
27267  "<asset>"
27268  "<type>host</type>"
27269  "<name>%s</name>"
27270  "<comment>%s</comment>"
27271  "</asset>"
27272  "</create_asset>",
27273  name,
27274  comment);
27275 
27276  ret = omp (credentials,
27277  &response,
27278  &entity,
27279  response_data,
27280  xml->str);
27281  g_string_free (xml, TRUE);
27282  switch (ret)
27283  {
27284  case 0:
27285  case -1:
27286  break;
27287  case 1:
27288  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27289  return gsad_message (credentials,
27290  "Internal error", __FUNCTION__, __LINE__,
27291  "An internal error occurred while creating a new host. "
27292  "No new host was created. "
27293  "Diagnostics: Failure to send command to manager daemon.",
27294  "/omp?cmd=get_assets", response_data);
27295  case 2:
27296  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27297  return gsad_message (credentials,
27298  "Internal error", __FUNCTION__, __LINE__,
27299  "An internal error occurred while creating a new host. "
27300  "It is unclear whether the host has been created or not. "
27301  "Diagnostics: Failure to receive response from manager daemon.",
27302  "/omp?cmd=get_assets", response_data);
27303  default:
27304  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27305  return gsad_message (credentials,
27306  "Internal error", __FUNCTION__, __LINE__,
27307  "An internal error occurred while creating a new host. "
27308  "It is unclear whether the host has been created or not. "
27309  "Diagnostics: Internal Error.",
27310  "/omp?cmd=get_assets", response_data);
27311  }
27312 
27313  if (entity_attribute (entity, "id"))
27314  params_add (params, "asset_id", entity_attribute (entity, "id"));
27315  html = response_from_entity (credentials, params, entity,
27316  (no_redirect && strcmp (no_redirect, "0")),
27317  NULL, "get_assets",
27318  NULL, "new_host",
27319  "Create Host", response_data);
27320  free_entity (entity);
27321  g_free (response);
27322  return html;
27323 }
27324 
27335 char *
27336 get_asset (credentials_t *credentials, params_t *params, const char *extra_xml,
27337  cmd_response_data_t* response_data)
27338 {
27339  char *ret;
27340  GString *extra_attribs, *extra_response;
27341  const char *asset_type;
27342 
27343  asset_type = params_value (params, "asset_type");
27344  if (asset_type == NULL)
27345  {
27346  param_t *param;
27347  param = params_add (params, "asset_type", "host");
27348  param->valid = 1;
27349  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
27350  asset_type = params_value (params, "asset_type");
27351  }
27352 
27353  if (strcmp (asset_type, "host")
27354  && strcmp (asset_type, "os"))
27355  {
27356  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27357  return gsad_message (credentials,
27358  "Internal error", __FUNCTION__, __LINE__,
27359  "An internal error occurred while getting an asset. "
27360  "Diagnostics: Invalid asset_type parameter value",
27361  "/omp?cmd=get_asset", response_data);
27362  }
27363 
27364  if (params_value (params, "asset_name")
27365  && params_value (params, "asset_id"))
27366  {
27367  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27368  return gsad_message (credentials,
27369  "Internal error", __FUNCTION__, __LINE__,
27370  "An internal error occurred while getting an asset. "
27371  "Diagnostics: Both ID and Name set.",
27372  "/omp?cmd=get_asset", response_data);
27373  }
27374 
27375  extra_response = g_string_new (extra_xml ? extra_xml : "");
27376 
27377  extra_attribs = g_string_new ("");
27378  g_string_append_printf (extra_attribs, "type=\"%s\"",
27379  params_value (params, "asset_type"));
27380  if (params_value (params, "asset_name"))
27381  g_string_append_printf (extra_attribs,
27382  " name=\"%s\"",
27383  params_value (params, "asset_name"));
27384  else if (params_value (params, "asset_id"))
27385  g_string_append_printf (extra_attribs,
27386  " asset_id=\"%s\"",
27387  params_value (params, "asset_id"));
27388  if (params_value (params, "details"))
27389  g_string_append_printf (extra_attribs,
27390  " details=\"%s\"",
27391  params_value (params, "details"));
27392 
27393  ret = get_one ("asset", credentials, params, extra_response->str,
27394  extra_attribs->str, response_data);
27395 
27396  g_string_free (extra_response, TRUE);
27397  g_string_free (extra_attribs, TRUE);
27398 
27399  return ret;
27400 }
27401 
27411 char *
27412 get_asset_omp (credentials_t * credentials, params_t *params,
27413  cmd_response_data_t* response_data)
27414 {
27415  return get_asset (credentials, params, NULL, response_data);
27416 }
27417 
27428 char *
27429 get_assets (credentials_t *credentials, params_t *params, const char *extra_xml,
27430  cmd_response_data_t* response_data)
27431 {
27432  char *ret;
27433  GString *extra_attribs, *extra_response;
27434  const char *asset_type;
27435 
27436  asset_type = params_value (params, "asset_type");
27437  if (asset_type == NULL)
27438  {
27439  param_t *param;
27440  param = params_add (params, "asset_type", "host");
27441  param->valid = 1;
27442  param->valid_utf8 = g_utf8_validate (param->value, -1, NULL);
27443  asset_type = params_value (params, "asset_type");
27444  }
27445 
27446  if (strcmp (asset_type, "host")
27447  && strcmp (asset_type, "os"))
27448  {
27449  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27450  return gsad_message (credentials,
27451  "Internal error", __FUNCTION__, __LINE__,
27452  "An internal error occurred while getting Assets. "
27453  "Diagnostics: Invalid asset_type parameter value",
27454  "/omp?cmd=get_assets", response_data);
27455  }
27456 
27457  extra_response = g_string_new (extra_xml ? extra_xml : "");
27458 
27459  extra_attribs = g_string_new("");
27460  g_string_append_printf (extra_attribs, "type=\"%s\" ignore_pagination=\"%s\"",
27461  params_value (params, "asset_type"),
27462  params_value (params, "ignore_pagination")
27463  ? params_value (params, "ignore_pagination")
27464  : "0");
27465  if (params_value (params, "details"))
27466  g_string_append_printf (extra_attribs,
27467  " details=\"%s\"",
27468  params_value (params, "details"));
27469  ret = get_many ("asset", credentials, params, extra_response->str,
27470  extra_attribs->str, response_data);
27471 
27472  g_string_free (extra_response, TRUE);
27473  g_string_free (extra_attribs, TRUE);
27474 
27475  return ret;
27476 }
27477 
27487 char *
27488 get_assets_omp (credentials_t * credentials, params_t *params,
27489  cmd_response_data_t* response_data)
27490 {
27491  return get_assets (credentials, params, NULL, response_data);
27492 }
27493 
27503 char *
27504 create_asset_omp (credentials_t *credentials, params_t *params,
27505  cmd_response_data_t* response_data)
27506 {
27507  char *ret;
27508  gchar *response;
27509  const char *no_redirect, *report_id, *filter;
27510  entity_t entity;
27511 
27512  no_redirect = params_value (params, "no_redirect");
27513  report_id = params_value (params, "report_id");
27514  filter = params_value (params, "filter");
27515 
27516  CHECK_PARAM_INVALID (report_id, "Create Asset", "get_report_section");
27517  CHECK_PARAM_INVALID (filter, "Create Asset", "get_report_section");
27518 
27519  response = NULL;
27520  entity = NULL;
27521  switch (ompf (credentials,
27522  &response,
27523  &entity,
27524  response_data,
27525  "<create_asset>"
27526  "<report id=\"%s\">"
27527  "<filter><term>%s</term></filter>"
27528  "</report>"
27529  "</create_asset>",
27530  report_id,
27531  filter))
27532  {
27533  case 0:
27534  case -1:
27535  break;
27536  case 1:
27537  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27538  return gsad_message (credentials,
27539  "Internal error", __FUNCTION__, __LINE__,
27540  "An internal error occurred while creating an asset. "
27541  "No new asset was created. "
27542  "Diagnostics: Failure to send command to manager daemon.",
27543  "/omp?cmd=get_tasks", response_data);
27544  case 2:
27545  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27546  return gsad_message (credentials,
27547  "Internal error", __FUNCTION__, __LINE__,
27548  "An internal error occurred while creating an asset. "
27549  "It is unclear whether the asset has been created or not. "
27550  "Diagnostics: Failure to receive response from manager daemon.",
27551  "/omp?cmd=get_tasks", response_data);
27552  default:
27553  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27554  return gsad_message (credentials,
27555  "Internal error", __FUNCTION__, __LINE__,
27556  "An internal error occurred while creating an asset. "
27557  "It is unclear whether the asset has been created or not. "
27558  "Diagnostics: Internal Error.",
27559  "/omp?cmd=get_tasks", response_data);
27560  }
27561 
27562  ret = response_from_entity (credentials, params, entity,
27563  (no_redirect && strcmp (no_redirect, "0")),
27564  NULL, "get_report_section",
27565  NULL, "get_report_section",
27566  "Create Asset", response_data);
27567  free_entity (entity);
27568  g_free (response);
27569  return ret;
27570 }
27571 
27581 char *
27582 delete_asset_omp (credentials_t * credentials, params_t *params,
27583  cmd_response_data_t* response_data)
27584 {
27585  openvas_connection_t connection;
27586  gchar *html, *response, *resource_id;
27587  const char *next_id, *no_redirect;
27588  entity_t entity;
27589  gchar *next_url;
27590 
27591  if (params_value (params, "asset_id"))
27592  resource_id = g_strdup (params_value (params, "asset_id"));
27593  else if (params_value (params, "report_id"))
27594  resource_id = g_strdup (params_value (params, "report_id"));
27595  else
27596  {
27597  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27598  return gsad_message (credentials,
27599  "Internal error", __FUNCTION__, __LINE__,
27600  "An internal error occurred while deleting an asset. "
27601  "The asset was not deleted. "
27602  "Diagnostics: Required parameter was NULL.",
27603  "/omp?cmd=get_tasks", response_data);
27604  }
27605 
27606  /* This is a hack, needed because asset_id is the param name used for
27607  * both the asset being deleted and the asset on the next page. */
27608  next_id = params_value (params, "next_id");
27609  if (next_id
27610  && params_value (params, "asset_id"))
27611  {
27612  param_t *param;
27613  param = params_get (params, "asset_id");
27614  g_free (param->value);
27615  param->value = g_strdup (next_id);
27616  param->value_size = strlen (param->value);
27617  }
27618 
27619  switch (manager_connect (credentials, &connection, &html,
27620  response_data))
27621  {
27622  case 0:
27623  break;
27624  case -1:
27625  if (html)
27626  {
27627  g_free (resource_id);
27628  return html;
27629  }
27630  /* Fall through. */
27631  default:
27632  g_free (resource_id);
27633  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27634  return gsad_message (credentials,
27635  "Internal error", __FUNCTION__, __LINE__,
27636  "An internal error occurred while deleting an asset. "
27637  "The asset is not deleted. "
27638  "Diagnostics: Failure to connect to manager daemon.",
27639  "/omp?cmd=get_tasks", response_data);
27640  }
27641 
27642  /* Delete the resource and get all resources. */
27643 
27644  if (openvas_connection_sendf (&connection,
27645  "<delete_asset %s_id=\"%s\"/>",
27646  params_value (params, "asset_id")
27647  ? "asset" : "report",
27648  resource_id)
27649  == -1)
27650  {
27651  openvas_connection_close (&connection);
27652  g_free (resource_id);
27653  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27654  return gsad_message (credentials,
27655  "Internal error", __FUNCTION__, __LINE__,
27656  "An internal error occurred while deleting an asset. "
27657  "The asset is not deleted. "
27658  "Diagnostics: Failure to send command to manager daemon.",
27659  "/omp?cmd=get_tasks", response_data);
27660  }
27661 
27662  g_free (resource_id);
27663 
27664  entity = NULL;
27665  if (read_entity_and_text_c (&connection, &entity, &response))
27666  {
27667  openvas_connection_close (&connection);
27668  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27669  return gsad_message (credentials,
27670  "Internal error", __FUNCTION__, __LINE__,
27671  "An internal error occurred while deleting an asset. "
27672  "It is unclear whether the asset has been deleted or not. "
27673  "Diagnostics: Failure to read response from manager daemon.",
27674  "/omp?cmd=get_tasks", response_data);
27675  }
27676 
27677  openvas_connection_close (&connection);
27678 
27679  /* Cleanup, and return transformed XML. */
27680 
27681  if (params_given (params, "next") == 0)
27682  params_add (params, "next", "get_asset");
27683 
27684  no_redirect = params_value (params, "no_redirect");
27685  if (no_redirect && strcmp (no_redirect, "0"))
27686  {
27687  html = next_page (credentials, params, response, response_data);
27688  if (html == NULL)
27689  {
27690  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27691  html = gsad_message (credentials,
27692  "Internal error", __FUNCTION__, __LINE__,
27693  "An internal error occurred while deleting an "
27694  "asset. Diagnostics: Error in parameter next.",
27695  "/omp?cmd=get_tasks", response_data);
27696  }
27697  }
27698  else
27699  {
27700  next_url = next_page_url (credentials, params,
27701  NULL, "get_asset", "delete_asset",
27702  entity_attribute (entity, "status"),
27703  entity_attribute (entity, "status_text"));
27704  response_data->redirect = next_url;
27705 
27706  html = NULL;
27707  }
27708 
27709  g_free (response);
27710  free_entity (entity);
27711  return html;
27712 }
27713 
27726 char *
27727 export_asset_omp (credentials_t * credentials, params_t *params,
27728  enum content_type * content_type, char **content_disposition,
27729  gsize *content_length, cmd_response_data_t* response_data)
27730 {
27731  return export_resource ("asset", credentials, params, content_type,
27732  content_disposition, content_length, response_data);
27733 }
27734 
27748 char *
27749 export_assets_omp (credentials_t * credentials, params_t *params,
27750  enum content_type * content_type, char **content_disposition,
27751  gsize *content_length, cmd_response_data_t* response_data)
27752 {
27753  return export_many ("asset", credentials, params, content_type,
27754  content_disposition, content_length, response_data);
27755 }
27756 
27767 char *
27768 edit_asset (credentials_t *credentials, params_t *params, const char *extra_xml,
27769  cmd_response_data_t* response_data)
27770 {
27771  GString *xml;
27772  const char *asset_id;
27773  gchar *response;
27774  entity_t entity;
27775 
27776  asset_id = params_value (params, "asset_id");
27777  if (asset_id == NULL)
27778  {
27779  response_data->http_status_code = MHD_HTTP_BAD_REQUEST;
27780  return gsad_message (credentials,
27781  "Internal error", __FUNCTION__, __LINE__,
27782  "An internal error occurred while editing a asset. "
27783  "The asset remains as it was. "
27784  "Diagnostics: Required ID parameter was NULL.",
27785  "/omp?cmd=get_tasks", response_data);
27786  }
27787 
27788  xml = g_string_new ("");
27789 
27790  g_string_append_printf (xml, "<edit_asset>");
27791 
27792  if (extra_xml)
27793  g_string_append (xml, extra_xml);
27794 
27795  response = NULL;
27796  entity = NULL;
27797  switch (ompf (credentials,
27798  &response,
27799  &entity,
27800  response_data,
27801  "<get_assets"
27802  " type=\"host\""
27803  " asset_id=\"%s\""
27804  " details=\"1\"/>",
27805  asset_id))
27806  {
27807  case 0:
27808  case -1:
27809  break;
27810  case 1:
27811  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27812  g_string_free (xml, TRUE);
27813  return gsad_message (credentials,
27814  "Internal error", __FUNCTION__, __LINE__,
27815  "An internal error occurred while getting the asset. "
27816  "Diagnostics: Failure to send command to manager daemon.",
27817  "/omp?cmd=get_assets", response_data);
27818  case 2:
27819  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27820  g_string_free (xml, TRUE);
27821  return gsad_message (credentials,
27822  "Internal error", __FUNCTION__, __LINE__,
27823  "An internal error occurred while getting the asset. "
27824  "Diagnostics: Failure to receive response from manager daemon.",
27825  "/omp?cmd=get_assets", response_data);
27826  default:
27827  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27828  g_string_free (xml, TRUE);
27829  return gsad_message (credentials,
27830  "Internal error", __FUNCTION__, __LINE__,
27831  "An internal error occurred while getting the asset. "
27832  "Diagnostics: Internal Error.",
27833  "/omp?cmd=get_assets", response_data);
27834  }
27835 
27836  g_string_append (xml, response);
27837  g_string_append_printf (xml, "</edit_asset>");
27838  free_entity (entity);
27839  g_free (response);
27840  return xsl_transform_omp (credentials, g_string_free (xml, FALSE),
27841  response_data);
27842 }
27843 
27853 char *
27854 edit_asset_omp (credentials_t * credentials, params_t *params,
27855  cmd_response_data_t* response_data)
27856 {
27857  return edit_asset (credentials, params, NULL, response_data);
27858 }
27859 
27869 char *
27870 save_asset_omp (credentials_t * credentials, params_t *params,
27871  cmd_response_data_t* response_data)
27872 {
27873  int ret;
27874  gchar *html, *response;
27875  const char *no_redirect, *asset_id, *comment;
27876  entity_t entity;
27877 
27878  no_redirect = params_value (params, "no_redirect");
27879  asset_id = params_value (params, "asset_id");
27880  comment = params_value (params, "comment");
27881 
27882  CHECK_PARAM_INVALID (asset_id, "Save Asset", "edit_asset");
27883  CHECK_PARAM_INVALID (comment, "Save Asset", "edit_asset");
27884 
27885  /* Modify the asset. */
27886 
27887  response = NULL;
27888  entity = NULL;
27889  ret = ompf (credentials,
27890  &response,
27891  &entity,
27892  response_data,
27893  "<modify_asset asset_id=\"%s\">"
27894  "<comment>%s</comment>"
27895  "</modify_asset>",
27896  asset_id,
27897  comment);
27898 
27899  switch (ret)
27900  {
27901  case 0:
27902  case -1:
27903  break;
27904  case 1:
27905  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27906  return gsad_message (credentials,
27907  "Internal error", __FUNCTION__, __LINE__,
27908  "An internal error occurred while saving an asset. "
27909  "The asset was not saved. "
27910  "Diagnostics: Failure to send command to manager daemon.",
27911  "/omp?cmd=get_assets", response_data);
27912  case 2:
27913  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27914  return gsad_message (credentials,
27915  "Internal error", __FUNCTION__, __LINE__,
27916  "An internal error occurred while saving an asset. "
27917  "It is unclear whether the asset has been saved or not. "
27918  "Diagnostics: Failure to receive response from manager daemon.",
27919  "/omp?cmd=get_assets", response_data);
27920  default:
27921  response_data->http_status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
27922  return gsad_message (credentials,
27923  "Internal error", __FUNCTION__, __LINE__,
27924  "An internal error occurred while saving an asset. "
27925  "It is unclear whether the asset has been saved or not. "
27926  "Diagnostics: Internal Error.",
27927  "/omp?cmd=get_assets", response_data);
27928  }
27929 
27930  html = response_from_entity (credentials, params, entity,
27931  (no_redirect && strcmp (no_redirect, "0")),
27932  NULL, "get_assets",
27933  NULL, "edit_asset",
27934  "Save Asset", response_data);
27935  free_entity (entity);
27936  g_free (response);
27937  return html;
27938 }
27939 
27950 static char *
27951 get_assets_chart (credentials_t *credentials, params_t *params,
27952  const char *extra_xml, cmd_response_data_t* response_data)
27953 {
27954  return xsl_transform_omp (credentials, g_strdup ("<get_assets_chart/>"),
27955  response_data);
27956 }
27957 
27967 char *
27969  cmd_response_data_t* response_data)
27970 {
27971  return get_assets_chart (credentials, params, NULL, response_data);
27972 }
27973 
27974 
27975 /* Manager communication. */
27976 
27984 int
27985 connect_unix (const gchar *path)
27986 {
27987  struct sockaddr_un address;
27988  int sock;
27989 
27990  /* Make socket. */
27991 
27992  sock = socket (AF_UNIX, SOCK_STREAM, 0);
27993  if (sock == -1)
27994  {
27995  g_warning ("Failed to create server socket");
27996  return -1;
27997  }
27998 
27999  /* Connect to server. */
28000 
28001  address.sun_family = AF_UNIX;
28002  strncpy (address.sun_path, path, sizeof (address.sun_path) - 1);
28003  if (connect (sock, (struct sockaddr *) &address, sizeof (address)) == -1)
28004  {
28005  g_warning ("Failed to connect to server: %s", strerror (errno));
28006  close (sock);
28007  return -1;
28008  }
28009 
28010  return sock;
28011 }
28012 
28022 int
28023 openvas_connection_open (openvas_connection_t *connection,
28024  const gchar *address,
28025  int port)
28026 {
28027  if (address == NULL)
28028  return -1;
28029 
28030  connection->tls = manager_use_tls;
28031 
28032  if (manager_use_tls)
28033  {
28034  connection->socket = openvas_server_open (&connection->session,
28035  address,
28036  port);
28037  connection->credentials = NULL;
28038  }
28039  else
28040  connection->socket = connect_unix (address);
28041 
28042  if (connection->socket == -1)
28043  return -1;
28044 
28045  return 0;
28046 }
28047 
28064 int
28065 authenticate_omp (const gchar * username, const gchar * password,
28066  gchar **role, gchar **timezone, gchar **severity,
28067  gchar **capabilities, gchar **language, gchar **pw_warning,
28068  GTree **chart_prefs, gchar **autorefresh)
28069 {
28070  openvas_connection_t connection;
28071  int auth;
28072  omp_authenticate_info_opts_t auth_opts;
28073 
28074  if (openvas_connection_open (&connection,
28076  manager_port))
28077  {
28078  g_debug ("%s failed to acquire socket!\n", __FUNCTION__);
28079  return 2;
28080  }
28081 
28082 #ifdef DEBUG_AUTH
28083  /* Enable this if you need the CGI to sleep after launch. This can be useful
28084  * if you need to attach to manager process the CGI is talking to for
28085  * debugging purposes.
28086  *
28087  * It's probably easier to run gsad in the foreground under gdb and
28088  * set a break point here.
28089  */
28090  g_debug ("Sleeping!");
28091  sleep (20);
28092 #endif
28093 
28094  auth_opts = omp_authenticate_info_opts_defaults;
28095  auth_opts.username = username;
28096  auth_opts.password = password;
28097  auth_opts.role = role;
28098  auth_opts.severity = severity;
28099  auth_opts.timezone = timezone;
28100  auth_opts.pw_warning = pw_warning;
28101 
28102  auth = omp_authenticate_info_ext_c (&connection, auth_opts);
28103  if (auth == 0)
28104  {
28105  entity_t entity;
28106  const char* status;
28107  char first;
28108  gchar *response;
28109  int ret;
28110 
28111  /* Get language setting. */
28112 
28113  ret = setting_get_value (&connection,
28114  "6765549a-934e-11e3-b358-406186ea4fc5",
28115  language,
28116  NULL);
28117 
28118  switch (ret)
28119  {
28120  case 0:
28121  break;
28122  case 1:
28123  case 2:
28124  openvas_connection_close (&connection);
28125  return 2;
28126  default:
28127  openvas_connection_close (&connection);
28128  return -1;
28129  }
28130 
28131  /* Request help. */
28132 
28133  ret = openvas_connection_sendf (&connection,
28134  "<help format=\"XML\" type=\"brief\"/>");
28135  if (ret)
28136  {
28137  openvas_connection_close (&connection);
28138  return 2;
28139  }
28140 
28141  /* Read the response. */
28142 
28143  entity = NULL;
28144  if (read_entity_and_text_c (&connection, &entity, &response))
28145  {
28146  openvas_connection_close (&connection);
28147  return 2;
28148  }
28149 
28150  /* Check the response. */
28151 
28152  status = entity_attribute (entity, "status");
28153  if (status == NULL
28154  || strlen (status) == 0)
28155  {
28156  g_free (response);
28157  free_entity (entity);
28158  return -1;
28159  }
28160  first = status[0];
28161  free_entity (entity);
28162  if (first == '2')
28163  {
28164  *capabilities = response;
28165  }
28166  else
28167  {
28168  openvas_connection_close (&connection);
28169  g_free (response);
28170  return -1;
28171  }
28172 
28173  /* Get the chart preferences */
28174 
28175  ret = openvas_connection_sendf (&connection,
28176  "<get_settings"
28177  " filter='name~\"Dashboard\"'/>");
28178  if (ret)
28179  {
28180  openvas_connection_close (&connection);
28181  return 2;
28182  }
28183 
28184  /* Read the response */
28185  entity = NULL;
28186  if (read_entity_and_text_c (&connection, &entity, &response))
28187  {
28188  openvas_connection_close (&connection);
28189  return 2;
28190  }
28191 
28192  /* Check the response. */
28193  status = entity_attribute (entity, "status");
28194  if (status == NULL
28195  || strlen (status) == 0)
28196  {
28197  g_free (response);
28198  free_entity (entity);
28199  return -1;
28200  }
28201  first = status[0];
28202  if (first == '2')
28203  {
28204  entities_t entities = entity->entities;
28205  entity_t child_entity;
28206  *chart_prefs = g_tree_new_full ((GCompareDataFunc) g_strcmp0,
28207  NULL, g_free, g_free);
28208 
28209  while ((child_entity = first_entity (entities)))
28210  {
28211  if (strcmp (entity_name (child_entity), "setting") == 0)
28212  {
28213  const char *setting_id
28214  = entity_attribute (child_entity, "id");
28215  const char *setting_value
28216  = entity_text (entity_child (child_entity, "value"));
28217 
28218  if (setting_id && setting_value)
28219  g_tree_insert (*chart_prefs,
28220  g_strdup (setting_id),
28221  g_strdup (setting_value));
28222  }
28223  entities = next_entities (entities);
28224  }
28225  free_entity (entity);
28226  g_free (response);
28227  }
28228  else
28229  {
28230  free_entity (entity);
28231  g_free (response);
28232  openvas_connection_close (&connection);
28233  return -1;
28234  }
28235 
28236  /* Get autorefresh setting. */
28237 
28238  ret = setting_get_value (&connection,
28239  "578a1c14-e2dc-45ef-a591-89d31391d007",
28240  autorefresh,
28241  NULL);
28242 
28243  switch (ret)
28244  {
28245  case 0:
28246  break;
28247  case 1:
28248  case 2:
28249  openvas_connection_close (&connection);
28250  return 2;
28251  default:
28252  openvas_connection_close (&connection);
28253  return -1;
28254  }
28255 
28256  openvas_connection_close (&connection);
28257  return 0;
28258  }
28259  else
28260  {
28261  openvas_connection_close (&connection);
28262  return 1;
28263  }
28264 }
28265 
28278 int
28279 manager_connect (credentials_t *credentials, openvas_connection_t *connection,
28280  gchar **html, cmd_response_data_t *response_data)
28281 {
28282  omp_authenticate_info_opts_t auth_opts;
28283 
28284  if (html)
28285  *html = NULL; /* Keep compiler quiet. */
28286 
28287  if (openvas_connection_open (connection,
28289  manager_port))
28290  {
28291  response_data->http_status_code = MHD_HTTP_SERVICE_UNAVAILABLE;
28292  if (html)
28293  *html = logout (credentials, "Logged out. OMP service is down.",
28294  response_data);
28295  return -1;
28296  }
28297 
28298 #if 0
28299  g_debug ("in manager_connect: Trying to authenticate with %s/%s\n",
28300  credentials->username,
28301  credentials->password);
28302 #endif
28303 
28304 
28305  auth_opts = omp_authenticate_info_opts_defaults;
28306  auth_opts.username = credentials->username;
28307  auth_opts.password = credentials->password;
28308  if (omp_authenticate_info_ext_c (connection, auth_opts))
28309  {
28310  g_debug ("authenticate failed!\n");
28311  openvas_connection_close (connection);
28312  return -2;
28313  }
28314 
28315 #ifdef DEBUG
28316  /* Enable this if you need the CGI to sleep after launch. This can be useful
28317  * if you need to attach to manager process the CGI is talking to for
28318  * debugging purposes.
28319  *
28320  * An easier method is to run gsad under gdb and set a breakpoint here.
28321  */
28322  g_debug ("Sleeping!");
28323  sleep (10);
28324 #endif
28325  return 0;
28326 }
char * export_notes_omp(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:13371
char * get_report_omp(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:15060
char * get_agents_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all agents, XSL transform the result.
Definition: gsad_omp.c:7582
char * edit_schedule_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_schedule XML, XSL transform the result.
Definition: gsad_omp.c:24693
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:879
char * get_protocol_doc_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get OMP doc.
Definition: gsad_omp.c:20469
char * run_wizard_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Run a wizard and XSL transform the result.
Definition: gsad_omp.c:19089
char * save_filter_omp(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:24562
int params_value_size(params_t *params, const char *name)
Get the size of the value of param.
Definition: gsad_base.c:698
char * get_reports_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all reports, XSL transform the result.
Definition: gsad_omp.c:15109
char * export_scanners_omp(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:17217
char * edit_config_nvt_omp(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:13024
params_t * params_values(params_t *params, const char *name)
Get values of param.
Definition: gsad_base.c:748
char * export_permission_omp(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:22698
char * get_asset_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get asset, XSL transform the result.
Definition: gsad_omp.c:27412
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:28065
char * get_credential_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one credential, XSL transform the result.
Definition: gsad_omp.c:6435
char * get_override_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an override, XSL transform the result.
Definition: gsad_omp.c:16419
char * export_role_omp(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:23821
char * save_tag_omp(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:10602
char * save_task_omp(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:4754
char * export_result_omp(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:15365
char * new_filter_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new filter.
Definition: gsad_omp.c:24546
char * process_bulk_omp(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:26768
gchar * value
Definition: gsad_base.h:146
char * get_one(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:1766
char * get_report_formats_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all Report Formats, XSL transform the result.
Definition: gsad_omp.c:18497
char * get_filter_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one filter, XSL transform the result.
Definition: gsad_omp.c:24229
char * edit_schedule(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:24676
char * get_info_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get info, XSL transform the result.
Definition: gsad_omp.c:5527
char * export_users_omp(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:26019
void cmd_response_data_init(cmd_response_data_t *data)
Initializes a cmd_response_data_t struct.
Definition: gsad_omp.c:347
char * create_port_range_omp(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:23044
char * save_override_omp(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:16982
char * delete_group_omp(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:20797
char * edit_note(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:16136
char * create_agent_omp(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:7061
char * capabilities
Capabilites of manager.
Definition: gsad_base.h:76
param_t * params_get(params_t *params, const char *name)
Get param.
Definition: gsad_base.c:650
char * timezone
User's timezone.
Definition: gsad_base.h:72
char * get_permissions_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all permissions, XSL transform the result.
Definition: gsad_omp.c:21137
char * create_config_omp(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:11648
content_type
Content types.
Definition: gsad_base.h:118
char * get_credentials_omp(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:6734
char * edit_scanner_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_scanner XML, XSL transform the result.
Definition: gsad_omp.c:17650
char * edit_note_omp(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:16221
char * edit_target_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_target XML, XSL transform the result.
Definition: gsad_omp.c:11113
char * save_group_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a group, return the next page.
Definition: gsad_omp.c:20994
char * get_roles_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all roles, XSL transform the result.
Definition: gsad_omp.c:23802
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:440
char * edit_group_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_group XML, XSL transform the result.
Definition: gsad_omp.c:20935
char * edit_filter_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_filter XML, XSL transform the result.
Definition: gsad_omp.c:24487
char * dashboard(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Show a dashboard.
Definition: gsad_omp.c:26122
char * export_preference_file_omp(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:13484
char * get_schedule_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one schedule, XSL transform the result.
Definition: gsad_omp.c:17830
char * clone_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Clone a resource, XSL transform the result.
Definition: gsad_omp.c:9795
char * get_aggregate_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an aggregate of resources.
Definition: gsad_omp.c:7735
char * edit_override(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:16881
char * delete_report_format_omp(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:18552
char * save_config_omp(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:12257
char * edit_asset_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_asset XML, XSL transform the result.
Definition: gsad_omp.c:27854
char * export_alert_omp(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:9331
#define CHECK_OMPF_RET
Definition: gsad_omp.c:21647
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:2595
char * new_user_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new user.
Definition: gsad_omp.c:25048
char * download_ssl_cert(credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get an SSL Certificate.
Definition: gsad_omp.c:15254
char * edit_config_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a config, XSL transform the result.
Definition: gsad_omp.c:12090
int params_given(params_t *params, const char *name)
Get whether a param was given at all.
Definition: gsad_base.c:666
char * create_task_omp(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:4217
struct sockaddr_storage address
The IP address of this program, "the GSAD".
Definition: gsad.c:255
char * delete_filter_omp(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:24417
#define CHECK_PARAM(name, op_name, ret_func)
Check a param.
Definition: gsad_omp.c:1184
char * resume_task_omp(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:5160
char * get_note_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a note, XSL transform the result.
Definition: gsad_omp.c:15705
char * edit_resource(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:2490
char * save_role_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Modify a role, return the next page.
Definition: gsad_omp.c:23861
char * edit_credential_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_credential XML, XSL transform the result.
Definition: gsad_omp.c:6801
char * new_target_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new target.
Definition: gsad_omp.c:9559
char * save_note_omp(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:16237
char * toggle_tag_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Set tag enabled status.
Definition: gsad_omp.c:10816
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 * stop_task_omp(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:5144
char * get_tags_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all tags, XSL transform the result.
Definition: gsad_omp.c:10800
char * delete_trash_group_omp(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:20780
char * export_port_lists_omp(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:13462
char * export_tag_omp(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:10710
char * edit_target(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:10931
char * sync_config_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Sync config, get configs, XSL transform the result.
Definition: gsad_omp.c:12106
Response information for commands.
Definition: gsad_base.h:92
char * edit_permission(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:22518
char * export_tags_omp(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:10732
char * edit_user(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:25490
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:318
char * new_container_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new task.
Definition: gsad_omp.c:3898
int valid_utf8
Definition: gsad_base.h:151
char * export_credentials_omp(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:6696
char * get_alert_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one alert, XSL transform the result.
Definition: gsad_omp.c:8673
const char * params_filename(params_t *params, const char *name)
Get filename of param.
Definition: gsad_base.c:732
char * delete_trash_schedule_omp(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:10055
char * delete_trash_task_omp(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:10089
int download_credential_omp(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:6455
char * export_agent_omp(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:7695
void set_language_code(gchar **lang, const gchar *language)
Set language code of user.
Definition: gsad_base.c:191
char * save_scanner_omp(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:17666
char * export_override_omp(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:13392
char * delete_trash_agent_omp(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:9970
int token_user_remove(const char *)
Remove a user from the session "database", releasing the user_t too.
Definition: gsad.c:944
char * delete_trash_tag_omp(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:10471
char * upload_config_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload scan config page.
Definition: gsad_omp.c:11632
char * edit_my_settings_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page with user's settings, for editing.
Definition: gsad_omp.c:19638
char * create_note_omp(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:15942
#define RESULTS_PER_PAGE
Initial filtered results per page on the report summary.
Definition: gsad_base.h:41
char * delete_user_omp(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:25064
char * export_schedule_omp(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:24712
char * export_config_omp(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:13306
char * resource_action(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:3224
void cmd_response_data_reset(cmd_response_data_t *data)
Clears a cmd_response_data_t struct.
Definition: gsad_omp.c:359
char * edit_port_list_omp(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:23235
char * save_config_family_omp(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:12716
char * delete_task_omp(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:4573
char * get_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a task, XSL transform the result.
Definition: gsad_omp.c:6071
char * get_report_section_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a report section, XSL transform the result.
Definition: gsad_omp.c:15237
char * new_config_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new scan config page.
Definition: gsad_omp.c:11591
char * create_alert_omp(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:8347
Structure of credential related information.
Definition: gsad_base.h:66
char * create_filter_omp(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:24302
char * delete_trash_filter_omp(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:24400
char * export_groups_omp(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:20976
int manager_use_tls
Whether to use TLS for Manager connections.
Definition: gsad_omp.c:91
char * new_agent_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new agent page.
Definition: gsad_omp.c:7045
const char * params_original_value(params_t *params, const char *name)
Get original value of param, before validation.
Definition: gsad_base.c:716
char * get_agent_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one agent, XSL transform the result.
Definition: gsad_omp.c:7548
char * delete_schedule_omp(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:18072
#define CHECK_PARAM_INVALID(name, op_name, next_cmd)
Check a param using the direct response method.
Definition: gsad_omp.c:1204
char * get_group_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one group, XSL transform the result.
Definition: gsad_omp.c:20691
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 * username
Name of user.
Definition: gsad_base.h:69
char * export_assets_omp(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:27749
char * export_filters_omp(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:24528
char * get_assets_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get assets, XSL transform the result.
Definition: gsad_omp.c:27488
char * import_config_omp(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:11735
char * new_role_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new role.
Definition: gsad_omp.c:23488
char * delete_port_list_omp(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:23335
char * create_user_omp(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:25329
char * new_override(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:16436
int user_set_autorefresh(const gchar *token, const gchar *autorefresh)
Set default autorefresh interval of user.
Definition: gsad.c:816
char * bulk_delete_omp(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:27059
char * new_host_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new host page.
Definition: gsad_omp.c:27228
char * autorefresh
Auto-refresh interval.
Definition: gsad_base.h:82
char * create_permission_omp(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:21365
GTree * last_filt_ids
Last filter ids.
Definition: gsad_base.h:83
char * client_address
Client's address.
Definition: gsad_base.h:80
char * get_config_family_omp(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:12684
char * export_user_omp(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:25997
char * get_filters_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all filters, XSL transform the result.
Definition: gsad_omp.c:24263
#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:3418
char * new_schedule_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new schedule page.
Definition: gsad_omp.c:17925
char * save_config_nvt_omp(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:13040
char * edit_agent_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_agent XML, XSL transform the result.
Definition: gsad_omp.c:7431
void init_find_by_value(find_by_value_t *find, gchar *value)
Definition: gsad_omp.c:395
char * edit_agent(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:7414
char * delete_trash_note_omp(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:16118
char * get_system_reports_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all system reports, XSL transform the result.
Definition: gsad_omp.c:18089
Headers/structs used generally in GSA.
char * wizard_get_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns a wizard_get page.
Definition: gsad_omp.c:26749
int value_size
Definition: gsad_base.h:152
#define CHECK(name)
Check a param.
Definition: gsad_omp.c:9772
char * delete_trash_scanner_omp(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:17528
char * delete_report_omp(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:13651
char * edit_override_omp(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:16966
Structure to search a key by value.
Definition: gsad_omp.c:388
char * edit_role_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_role XML, XSL transform the result.
Definition: gsad_omp.c:23736
char * delete_role_omp(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:23521
char * edit_config_family_omp(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:12700
char * edit_tag(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:10489
char * edit_scanner(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:17546
char * delete_override_omp(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:16846
char * export_roles_omp(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:23843
char * cvss_calculator(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Definition: gsad_omp.c:26028
char * new_permission(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:21188
char * delete_user_confirm_omp(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:25148
char * export_results_omp(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:15387
char * export_tasks_omp(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:5126
char * save_credential_omp(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:6817
char * wizard_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns a wizard page.
Definition: gsad_omp.c:26628
struct timeval cmd_start
Seconds since command page handler started.
Definition: gsad_base.h:68
char * save_user_omp(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:25767
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:557
char * export_overrides_omp(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:13415
char * create_override_omp(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:16658
char * new_permissions_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup new_permission XML, XSL transform the result.
Definition: gsad_omp.c:21987
char * new_alert_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new alert.
Definition: gsad_omp.c:8124
char * save_permission_omp(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:22740
char * new_report_format_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new report format page.
Definition: gsad_omp.c:18536
char * start_task_omp(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:5176
char * export_schedules_omp(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:24734
char * get_config_nvt_omp(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:13008
char * get_config_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a config, XSL transform the result.
Definition: gsad_omp.c:12058
char * get_scanner_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one scanner, XSL transform the result.
Definition: gsad_omp.c:17176
char * export_configs_omp(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:13328
char * save_asset_omp(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:27870
char * new_port_range_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new Port Range page.
Definition: gsad_omp.c:23028
char * get_feeds_omp(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:23952
char * get_configs_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all scan configs, XSL transform the result.
Definition: gsad_omp.c:11826
char * get_user_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one user, XSL transform the result.
Definition: gsad_omp.c:25230
char * create_permissions_omp(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:22003
char * delete_port_range_omp(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:23369
char * delete_trash_target_omp(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:10072
#define params_t
Definition: gsad_base.h:61
char * export_alerts_omp(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:9353
char * get_target_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one target, XSL transform the result.
Definition: gsad_omp.c:11147
char * edit_alert(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:8828
char * create_target_omp(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:9575
gchar * value
Definition: gsad_omp.c:390
char * export_report_formats_omp(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:13632
char * delete_trash_role_omp(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:23504
char * edit_filter(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:24470
char * edit_asset(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:27768
char * new_override_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new overrides page.
Definition: gsad_omp.c:16642
char * delete_credential_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete credential, get all credentials, XSL transform result.
Definition: gsad_omp.c:6750
char * new_note(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:15722
char * get_assets_chart_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get an assets chart, XSL transform the result.
Definition: gsad_omp.c:27968
char * create_role_omp(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:23538
Request parameter.
Definition: gsad_base.h:144
#define GET_TRASH_RESOURCE(capability, command, name)
Definition: gsad_omp.c:19184
char * upload_report_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload report page.
Definition: gsad_omp.c:3956
char * export_scanner_omp(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:17195
char * get_my_settings_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page with user's settings.
Definition: gsad_omp.c:19414
char * new_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new task.
Definition: gsad_omp.c:3850
GPtrArray * users
User session data.
Definition: gsad.c:309
char * sync_cert_omp(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:24190
char * auth_settings_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Definition: gsad_omp.c:25651
char * download_ca_pub(credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get a Scanner's CA Certificate.
Definition: gsad_omp.c:15295
char * edit_tag_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_tag XML, XSL transform the result.
Definition: gsad_omp.c:10586
char * create_schedule_omp(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:17941
char * export_agents_omp(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:7717
char * create_tag_omp(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:10357
char * get_permission_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one permission, XSL transform the result.
Definition: gsad_omp.c:21103
int guest
Whether the user is a guest user.
Definition: gsad_base.h:86
char * create_group_omp(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:20814
char * get_report_format_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one report format, XSL transform the result.
Definition: gsad_omp.c:18463
char * new_permissions(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:21708
char * create_asset_omp(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:27504
int manager_port
The port the manager is on.
Definition: gsad_omp.c:101
char * create_container_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a container task, serve next page.
Definition: gsad_omp.c:4134
char * export_credential_omp(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:6673
char * export_omp_doc_omp(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:20550
char * delete_trash_config_omp(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:9987
char * delete_trash_port_list_omp(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:23352
char * token
Session token.
Definition: gsad_base.h:73
char * get_scanners_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all scanners, XSL transform the result.
Definition: gsad_omp.c:17142
const char * params_value(params_t *params, const char *name)
Get value of param.
Definition: gsad_base.c:682
char * new_tag_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new target.
Definition: gsad_omp.c:10341
int member1(params_t *params, const char *string)
Look for param with value 1 and name equal to given string.
Definition: gsad_omp.c:721
char * new_permission_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup new_permission XML, XSL transform the result.
Definition: gsad_omp.c:21349
char * import_report_omp(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:4115
char * export_report_format_omp(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:13609
gboolean params_iterator_next(params_iterator_t *iterator, char **name, param_t **param)
Increment a params iterator.
Definition: gsad_base.c:858
char * role
User's role.
Definition: gsad_base.h:71
#define GSAD_MESSAGE_INVALID
Answer for invalid input.
Definition: gsad_base.h:44
char * verify_scanner_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Verify scanner, get scanners, XSL transform the result.
Definition: gsad_omp.c:17323
char * delete_permission_omp(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:21170
char * delete_note_omp(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:16102
char * get_groups_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all groups, XSL transform the result.
Definition: gsad_omp.c:20725
char * get_trash_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all trash, XSL transform the result.
Definition: gsad_omp.c:19317
char * get_users_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all users, XSL transform the result.
Definition: gsad_omp.c:25313
char * get_system_report_omp(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:18276
char * download_key_pub(credentials_t *credentials, params_t *params, gsize *response_size, cmd_response_data_t *response_data)
Get a Scanner's Certificate.
Definition: gsad_omp.c:15328
char * get_tasks_chart_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get a tasks chart, XSL transform the result.
Definition: gsad_omp.c:5687
const gchar * vendor_version_get()
Get the vendor version.
Definition: gsad_base.c:148
int charts
Whether to show charts for this user.
Definition: gsad_base.h:85
char * export_task_omp(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:5104
char * pw_warning
Password policy warning message.
Definition: gsad_base.h:79
char * sync_feed_omp(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:24154
char * get_info(credentials_t *credentials, params_t *params, const char *extra_xml, cmd_response_data_t *response_data)
Requests SecInfo.
Definition: gsad_omp.c:5403
gchar * redirect
HTTP status code.
Definition: gsad_base.h:94
GList * keys
Definition: gsad_omp.c:391
char * get_results_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all results, XSL transform the result.
Definition: gsad_omp.c:15431
char * edit_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_task XML, XSL transform the result.
Definition: gsad_omp.c:4738
char * delete_trash_report_format_omp(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:10038
char * get_tasks_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all tasks, XSL transform the result.
Definition: gsad_omp.c:5653
char * edit_report_format_omp(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:18605
char * get_schedules_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all schedules, XSL transform the result.
Definition: gsad_omp.c:17864
char * empty_trashcan_omp(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:10206
char * xsl_transform(const char *xml_text, cmd_response_data_t *response_data)
XSL Transformation.
Definition: gsad_base.c:418
char * delete_trash_alert_omp(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:10004
char * get_nvts_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Requests NVT details, accepting extra commands.
Definition: gsad_omp.c:5543
char * delete_target_omp(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:9953
GTree * chart_prefs
Chart preferences.
Definition: gsad_base.h:81
char * new_scanner_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new scanner page.
Definition: gsad_omp.c:17307
char * import_port_list_omp(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:23386
char * export_group_omp(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:20954
char * severity
Severity class.
Definition: gsad_base.h:78
char * get_targets_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all targets, XSL transform the result.
Definition: gsad_omp.c:11181
char * create_port_list_omp(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:22919
char * delete_scanner_omp(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:17511
char * restore_omp(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:10106
char * get_role_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one role, XSL transform the result.
Definition: gsad_omp.c:23769
char * get_tag_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one tag, XSL transform the result.
Definition: gsad_omp.c:10767
char * save_container_task_omp(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:5000
void free_find_by_value(find_by_value_t *find)
Definition: gsad_omp.c:402
int download_agent_omp(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:7219
char * delete_agent_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete agent, get all agents, XSL transform result.
Definition: gsad_omp.c:7198
#define GSAD_MESSAGE_INVALID_PARAM(op)
Answer for invalid input.
Definition: gsad_base.h:53
gchar * message_invalid(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 * create_credential_omp(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:6110
char * create_host_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Create a host, serve next page.
Definition: gsad_omp.c:27244
char * save_alert_omp(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:9095
char * get_port_list_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one port_list, XSL transform the result.
Definition: gsad_omp.c:23151
int manager_connect(credentials_t *, openvas_connection_t *, gchar **, cmd_response_data_t *)
Connect to OpenVAS Manager daemon.
Definition: gsad_omp.c:28279
char * save_schedule_omp(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:24753
char * delete_tag_omp(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:10455
#define params_iterator_init
Definition: gsad_base.h:187
char * current_page
Current page URL, for refresh.
Definition: gsad_base.h:75
char * test_alert_omp(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:9238
char * move_task_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Reassign a task to a new OMP slave.
Definition: gsad_omp.c:5192
char * new_port_list_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new Port List page.
Definition: gsad_omp.c:23201
char * get_port_lists_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all port_lists, XSL transform the result.
Definition: gsad_omp.c:23185
char * upload_port_list_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the upload port list page.
Definition: gsad_omp.c:22903
char * export_port_list_omp(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:13438
char * language
Accept-Language browser header.
Definition: gsad_base.h:77
char * edit_user_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_user XML, XSL transform the result.
Definition: gsad_omp.c:25644
char * edit_alert_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_alert XML, XSL transform the result.
Definition: gsad_omp.c:9079
char * sync_scap_omp(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:24172
char * export_targets_omp(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:11508
char * save_my_settings_omp(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's settings, for editing.
Definition: gsad_omp.c:19731
char * verify_agent_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Verify agent, get agents, XSL transform the result.
Definition: gsad_omp.c:7598
char * save_target_omp(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:11197
char * export_note_omp(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:13349
#define AUTH_CONF_SETTING(key, value)
Generate AUTH_CONF_SETTING element for save_auth_omp.
Definition: gsad_omp.c:26260
char * export_target_omp(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:11486
char * new_group_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new group.
Definition: gsad_omp.c:20764
int openvas_connection_open(openvas_connection_t *connection, const gchar *address, int port)
Connect to an address.
Definition: gsad_omp.c:28023
int valid
Definition: gsad_base.h:150
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:214
char * edit_group(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:20918
char * get_alerts_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all alerts, XSL transform the result.
Definition: gsad_omp.c:8811
char * new_credential_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Returns page to create a new Credential.
Definition: gsad_omp.c:6767
char * get_result_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get one result, XSL transform the result.
Definition: gsad_omp.c:15603
#define params_iterator_t
Definition: gsad_base.h:185
char * get_notes_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all notes, XSL transform the result.
Definition: gsad_omp.c:15672
Headers for GSA's OMP communication module.
char * save_port_list_omp(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:23251
char * export_asset_omp(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:27727
char * delete_config_omp(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:13286
void buffer_languages_xml(GString *buffer)
Write the list of installed languages to a buffer as XML.
Definition: xslt_i18n.c:718
char * delete_resource(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:3073
char * delete_trash_credential_omp(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:10021
int command_enabled(credentials_t *credentials, const gchar *name)
Init the GSA OMP library.
Definition: gsad_omp.c:304
char * save_chart_preference_omp(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:26406
char * save_agent_omp(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:7447
char * get_overrides_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Get all overrides, XSL transform the result.
Definition: gsad_omp.c:16385
char * new_note_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Return the new notes page.
Definition: gsad_omp.c:15926
char * delete_trash_override_omp(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:16863
params_t * values
Definition: gsad_base.h:149
char * password
User's password.
Definition: gsad_base.h:70
char * export_filter_omp(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:24506
char * verify_report_format_omp(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:18992
char * export_permissions_omp(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:22721
param_t * params_add(params_t *params, const char *name, const char *value)
Add a param.
Definition: gsad_base.c:779
char * delete_trash_permission_omp(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:21153
char * import_report_format_omp(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:18621
char * save_report_format_omp(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:18697
int params_valid(params_t *params, const char *name)
Get whether a param is valid.
Definition: gsad_base.c:764
char * save_auth_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Save authentication settings.
Definition: gsad_omp.c:26276
char * delete_asset_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Delete an asset, go to the next page.
Definition: gsad_omp.c:27582
char * delete_alert_omp(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:8488
char * create_report_omp(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:3972
char * edit_permission_omp(credentials_t *credentials, params_t *params, cmd_response_data_t *response_data)
Setup edit_permission XML, XSL transform the result.
Definition: gsad_omp.c:22679
char * create_scanner_omp(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:17414
int connect_unix(const gchar *path)
Connect to OpenVAS Manager daemon.
Definition: gsad_omp.c:27985
char * export_resource(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:2684