Greenbone Security Assistant  8.0.0
gsad_http.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-2018 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #include "gsad_http.h"
26 
27 #include "gsad_base.h" /* for ctime_r_strip_newline */
28 #include "gsad_i18n.h" /* for accept_language_to_env_fmt */
29 #include "gsad_settings.h"
30 #include "utils.h" /* for str_equal */
31 
32 #include <assert.h> /* for asset */
33 #include <gvm/base/networking.h> /* for sockaddr_as_str */
34 #include <gvm/util/xmlutils.h> /* for xml_string_append */
35 #include <locale.h> /* for setlocale */
36 #include <stdlib.h> /* for abort */
37 #include <string.h> /* for strcmp */
38 #include <sys/stat.h> /* struct stat */
39 #include <sys/types.h>
40 #include <unistd.h>
41 
42 #undef G_LOG_DOMAIN
43 
46 #define G_LOG_DOMAIN "gsad http"
47 
56 guess_content_type (const gchar *path)
57 {
58  /* Guess content type. */
59  if (g_str_has_suffix (path, ".png"))
61  else if (g_str_has_suffix (path, ".svg"))
63  else if (g_str_has_suffix (path, ".html"))
65  else if (g_str_has_suffix (path, ".css"))
67  else if (g_str_has_suffix (path, ".js"))
69  else if (g_str_has_suffix (path, ".gif"))
71  else if (g_str_has_suffix (path, ".json"))
73  else if (g_str_has_suffix (path, ".txt"))
75  else
77 }
78 
88 void
90 {
91  if (!response)
92  return;
93 
94  switch (*ct)
95  {
97  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
98  "application/deb");
99  break;
101  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
102  "application/exe");
103  break;
105  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
106  "application/xhtml+xml");
107  break;
109  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
110  "application/key");
111  break;
113  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
114  "application/nbe");
115  break;
117  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
118  "application/pdf");
119  break;
121  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
122  "application/rpm");
123  break;
125  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
126  "application/xml; charset=utf-8");
127  break;
129  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
130  "image/png");
131  break;
133  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
134  "image/svg+xml");
135  break;
137  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
138  "image/gif");
139  break;
141  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
142  "application/octet-stream");
143  break;
145  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
146  "text/css");
147  break;
149  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
150  "text/html; charset=utf-8");
151  break;
153  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
154  "text/javascript");
155  break;
157  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
158  "text/plain; charset=utf-8");
159  break;
161  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
162  "application/json; charset=utf-8");
163  break;
165  break;
166  default:
167  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
168  "text/plain; charset=utf-8");
169  break;
170  }
171 }
172 
182 int
183 send_redirect_to_uri (http_connection_t *connection, const char *uri,
184  const gchar *sid)
185 {
186  int ret;
187  http_response_t *response;
188  char *body;
189 
190  /* Some libmicrohttp versions get into an endless loop in https mode
191  if an empty body is passed. As a workaround and because it is
192  anyway suggested by the HTTP specs, we provide a short body. We
193  assume that uri does not need to be quoted. */
194  body = g_strdup_printf ("<html><body>Code 303 - Redirecting to"
195  " <a href=\"%s\">%s<a/></body></html>\n",
196  uri, uri);
197  response = MHD_create_response_from_buffer (strlen (body), body,
198  MHD_RESPMEM_MUST_FREE);
199 
200  if (!response)
201  {
202  g_warning ("%s: failed to create response, dropping request",
203  __FUNCTION__);
204  return MHD_NO;
205  }
206  ret = MHD_add_response_header (response, MHD_HTTP_HEADER_LOCATION, uri);
207  if (!ret)
208  {
209  MHD_destroy_response (response);
210  g_warning ("%s: failed to add location header, dropping request",
211  __FUNCTION__);
212  return MHD_NO;
213  }
214 
215  if (attach_remove_sid (response, sid) == MHD_NO)
216  {
217  MHD_destroy_response (response);
218  g_warning ("%s: failed to attach SID, dropping request", __FUNCTION__);
219  return MHD_NO;
220  }
221 
222  add_forbid_caching_headers (response);
223  add_security_headers (response);
224  add_cors_headers (response);
225  ret = MHD_queue_response (connection, MHD_HTTP_SEE_OTHER, response);
226  MHD_destroy_response (response);
227  return ret;
228 }
229 
243 int
244 send_response (http_connection_t *connection, const char *content,
245  int status_code, const gchar *sid, content_type_t content_type,
246  const char *content_disposition, size_t content_length)
247 {
248  http_response_t *response;
249  size_t size;
250  int ret;
251 
252  if (content)
253  size = (content_length ? content_length : strlen (content));
254  else
255  {
256  g_warning ("%s: content is NULL", __FUNCTION__);
257  status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
258  size = 0;
259  }
260  response = MHD_create_response_from_buffer (
261  size, (void *) (content ? content : ""), MHD_RESPMEM_MUST_COPY);
262 
264 
265  if (content_disposition)
266  MHD_add_response_header (response, "Content-Disposition",
267  content_disposition);
268 
269  if (attach_remove_sid (response, sid) == MHD_NO)
270  {
271  return MHD_NO;
272  }
273 
274  add_forbid_caching_headers (response);
275  add_security_headers (response);
276  add_cors_headers (response);
277  ret = MHD_queue_response (connection, status_code, response);
278  MHD_destroy_response (response);
279  return ret;
280 }
281 
294 int
296  cmd_response_data_t *response_data, const gchar *sid)
297 {
298  int ret;
299  const gchar *content_disposition;
301  int status_code;
302 
303  if (attach_remove_sid (response, sid) == MHD_NO)
304  {
305  cmd_response_data_free (response_data);
306  MHD_destroy_response (response);
307  return MHD_NO;
308  }
309 
311 
313  {
314  MHD_add_response_header (
315  response, MHD_HTTP_HEADER_CONTENT_TYPE,
317  }
318  else
319  {
321  }
322 
323  content_disposition =
325 
326  if (content_disposition != NULL)
327  {
328  MHD_add_response_header (response, "Content-Disposition",
329  content_disposition);
330  }
331 
332  if (cmd_response_data_is_allow_caching (response_data) == FALSE)
333  add_forbid_caching_headers (response);
334  add_security_headers (response);
335  add_cors_headers (response);
336 
337  status_code = cmd_response_data_get_status_code (response_data);
338 
339  cmd_response_data_free (response_data);
340 
341  ret = MHD_queue_response (connection, status_code, response);
342  if (ret == MHD_NO)
343  {
344  /* Assume this was due to a bad request, to keep the MHD "Internal
345  * application error" out of the log. */
347  NULL, GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
348  MHD_destroy_response (response);
349  return MHD_YES;
350  }
351  MHD_destroy_response (response);
352  return ret;
353 }
354 
367 int
368 handler_create_response (http_connection_t *connection, gchar *data,
369  cmd_response_data_t *response_data, const gchar *sid)
370 {
371  http_response_t *response;
372  gsize len = 0;
373 
374  len = cmd_response_data_get_content_length (response_data);
375  if (len == 0 && data)
376  {
377  len = strlen (data);
378  }
379 
380  response = MHD_create_response_from_buffer (len, data, MHD_RESPMEM_MUST_FREE);
381  return handler_send_response (connection, response, response_data, sid);
382 }
383 
393 create_not_found_response (const gchar *url, cmd_response_data_t *response_data)
394 {
395  http_response_t *response;
396  int len;
397 
398  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
399 
400  gchar *msg = g_strdup_printf (
401  "<!DOCTYPE html>"
402  "<html>"
403  "<head>"
404  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
405  "<link rel=\"icon\" href=\"/img/favicon.gif\" type=\"image/gif\"></link>"
406  "<title>Greenbone Security Assistant</title>"
407  "</head>"
408  "<body>"
409  "<h1>URL not found</h1>"
410  "<p>"
411  "The requested URL %s is not available"
412  "</p>"
413  "</body>"
414  "</html>",
415  url
416  );
417 
418  cmd_response_data_set_content_type (response_data,
420 
421  cmd_response_data_set_content_length (response_data, strlen (msg));
422 
423  len = cmd_response_data_get_content_length (response_data);
424  response = MHD_create_response_from_buffer (len, msg, MHD_RESPMEM_MUST_COPY);
425  g_free (msg);
426  return response;
427 }
428 
437 int
438 handler_send_not_found (http_connection_t *connection, const gchar *url)
439 {
440  http_response_t *response;
441  cmd_response_data_t *response_data;
442 
443  response_data = cmd_response_data_new ();
444 
445  response = create_not_found_response (url, response_data);
446  return handler_send_response (connection, response, response_data, NULL);
447 }
448 
458 int
460  int http_status_code,
462 {
463  const char *msg;
464 
465  switch (reason)
466  {
467  case LOGIN_FAILED:
468  msg = "Login failed.";
469  break;
470  case LOGIN_ERROR:
471  msg = "Login failed. Error during authentication.";
472  break;
473  case GMP_SERVICE_DOWN:
474  msg = "Login failed. GMP Service is down.";
475  break;
476  case SESSION_EXPIRED:
477  msg = "Session expired. Please login again.";
478  break;
479  case LOGOUT_ALREADY:
480  msg = "Already logged out.";
481  break;
482  case BAD_MISSING_TOKEN:
483  msg = "Token missing or bad. Please login again.";
484  break;
485  case BAD_MISSING_COOKIE:
486  msg = "Cookie missing or bad. Please login again.";
487  break;
488  case LOGOUT:
489  msg = "Successfully logged out.";
490  break;
491  default:
492  msg = "";
493  }
494 
495  cmd_response_data_t *response_data = cmd_response_data_new ();
496  cmd_response_data_set_status_code (response_data, http_status_code);
497 
498  gchar *xml = gsad_message (NULL, "Authentication required", __FUNCTION__,
499  __LINE__, msg, response_data);
500 
501  return handler_create_response (connection, xml, response_data, REMOVE_SID);
502 }
503 
511 int
513 {
514  int ret;
515  gchar *value;
516  gchar *locale;
517  char expires[EXPIRES_LENGTH + 1];
518  struct tm expire_time_broken;
519  time_t expire_time;
520 
521  /* Set up the expires param. */
522  locale = g_strdup (setlocale (LC_ALL, NULL));
523  setlocale (LC_ALL, "C");
524 
525  expire_time = time (NULL);
526  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
527  abort ();
528  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
529  &expire_time_broken);
530  if (ret == 0)
531  abort ();
532 
533  setlocale (LC_ALL, locale);
534  g_free (locale);
535 
536  /* Add the cookie.
537  *
538  * Tim Brown's suggested cookie included a domain attribute. How would
539  * we get the domain in here? Maybe a --domain option. */
540 
541  value =
542  g_strdup_printf (SID_COOKIE_NAME "=0; expires=%s; path=/; %sHTTPonly",
543  expires, (is_use_secure_cookie () ? "secure; " : ""));
544  ret = MHD_add_response_header (response, "Set-Cookie", value);
545  g_free (value);
546  return ret;
547 }
548 
557 int
558 attach_sid (http_response_t *response, const char *sid)
559 {
560  int ret, timeout;
561  gchar *value;
562  gchar *locale;
563  char expires[EXPIRES_LENGTH + 1];
564  struct tm expire_time_broken;
565  time_t now, expire_time;
566  gchar *tz;
567 
568  /* Set up the expires param. */
569 
570  /* Store current TZ, switch to GMT. */
571  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
572  if (setenv ("TZ", "GMT", 1) == -1)
573  {
574  g_critical ("%s: failed to set TZ\n", __FUNCTION__);
575  g_free (tz);
576  exit (EXIT_FAILURE);
577  }
578  tzset ();
579 
580  locale = g_strdup (setlocale (LC_ALL, NULL));
581  setlocale (LC_ALL, "C");
582 
583  timeout = get_session_timeout () * 60 + 30;
584 
585  now = time (NULL);
586  expire_time = now + timeout;
587  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
588  abort ();
589  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
590  &expire_time_broken);
591  if (ret == 0)
592  abort ();
593 
594  setlocale (LC_ALL, locale);
595  g_free (locale);
596 
597  /* Revert to stored TZ. */
598  if (tz)
599  {
600  if (setenv ("TZ", tz, 1) == -1)
601  {
602  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
603  g_free (tz);
604  exit (EXIT_FAILURE);
605  }
606  }
607  else
608  unsetenv ("TZ");
609  g_free (tz);
610 
611  /* Add the cookie.
612  *
613  * Tim Brown's suggested cookie included a domain attribute. How would
614  * we get the domain in here? Maybe a --domain option. */
615 
616  value = g_strdup_printf (
617  SID_COOKIE_NAME "=%s; expires=%s; max-age=%d; path=/; %sHTTPonly", sid,
618  expires, timeout, (is_use_secure_cookie () ? "secure; " : ""));
619  ret = MHD_add_response_header (response, "Set-Cookie", value);
620  g_free (value);
621  return ret;
622 }
623 
635 int
636 attach_remove_sid (http_response_t *response, const gchar *sid)
637 {
638  if (sid)
639  {
640  if (str_equal (sid, REMOVE_SID))
641  {
642  if (remove_sid (response) == MHD_NO)
643  {
644  MHD_destroy_response (response);
645  g_warning ("%s: failed to remove SID, dropping request",
646  __FUNCTION__);
647  return MHD_NO;
648  }
649  }
650  else
651  {
652  if (attach_sid (response, sid) == MHD_NO)
653  {
654  MHD_destroy_response (response);
655  g_warning ("%s: failed to attach SID, dropping request",
656  __FUNCTION__);
657  return MHD_NO;
658  }
659  }
660  }
661  return MHD_YES;
662 }
673 static int
674 file_reader (void *cls, uint64_t pos, char *buf, int max)
675 {
676  FILE *file = cls;
677 
678  fseek (file, pos, SEEK_SET);
679  return fread (buf, 1, max, file);
680 }
681 
694 file_content_response (http_connection_t *connection, const char *url,
695  const char *path, cmd_response_data_t *response_data)
696 {
697  char date_2822[DATE_2822_LEN];
698  struct tm *mtime;
699  time_t next_week;
700  http_response_t *response;
701  FILE *file;
702  struct stat buf;
703 
704  cmd_response_data_set_status_code (response_data, MHD_HTTP_OK);
705 
706  if (!g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
707  {
708  /* path does not exists or is not a file */
709  /* return index.html to show page not found via js */
710  g_debug ("File %s not found. Return index.html", path);
711 
712  path = "index.html";
713  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
714  }
715 
716  file = fopen (path, "r"); /* this file is just read and sent */
717 
718  if (file == NULL)
719  {
720  g_debug ("File %s failed, ", path);
721  return create_not_found_response (url, response_data);
722  }
723 
724  /* Guess content type. */
726 
727  if (stat (path, &buf))
728  {
729  /* File information could not be retrieved. */
730  g_critical ("%s: file <%s> can not be stat'ed.\n", __FUNCTION__, path);
731  fclose (file);
732  return create_not_found_response (url, response_data);
733  }
734 
735  /* Make sure the requested path really is a file. */
736  if ((buf.st_mode & S_IFMT) != S_IFREG)
737  {
738  fclose (file);
739  return create_not_found_response (url, response_data);
740  }
741 
742  response = MHD_create_response_from_callback (
743  buf.st_size, 32 * 1024, (MHD_ContentReaderCallback) &file_reader, file,
744  (MHD_ContentReaderFreeCallback) &fclose);
745 
746  mtime = localtime (&buf.st_mtime);
747  if (mtime
748  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
749  {
750  MHD_add_response_header (response, "Last-Modified", date_2822);
751  }
752 
753  next_week = time (NULL) + 7 * 24 * 60 * 60;
754  mtime = localtime (&next_week);
755  if (mtime
756  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
757  {
758  MHD_add_response_header (response, "Expires", date_2822);
759  }
760 
761  return response;
762 }
763 
774 static int
775 append_param (void *string, enum MHD_ValueKind kind, const char *key,
776  const char *value)
777 {
778  if (value == NULL)
779  /* http://foo/bar?key */
780  return MHD_YES;
781  if (key == NULL)
782  {
783  assert (0);
784  return MHD_YES;
785  }
786  /* http://foo/bar?key=value */
787  if (strcmp (key, "token") && strcmp (key, "r"))
788  {
789  g_string_append ((GString *) string, key);
790  g_string_append ((GString *) string, "=");
791  g_string_append ((GString *) string, value);
792  g_string_append ((GString *) string, "&");
793  }
794  return MHD_YES;
795 }
796 
805 gchar *
806 reconstruct_url (http_connection_t *connection, const char *url)
807 {
808  GString *full_url;
809 
810  full_url = g_string_new (url);
811  /* To simplify appending the token later, ensure there is at least
812  * one param. */
813  g_string_append (full_url, "?r=1&");
814 
815  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, append_param,
816  full_url);
817 
818  if (full_url->str[strlen (full_url->str) - 1] == '&')
819  full_url->str[strlen (full_url->str) - 1] = '\0';
820 
821  return g_string_free (full_url, FALSE);
822 }
823 
827 void
829 {
831  const gchar *http_content_security_policy =
833  const gchar *http_strict_transport_security =
835 
836  if (strcmp (http_x_frame_options, ""))
837  MHD_add_response_header (response, "X-Frame-Options", http_x_frame_options);
838  if (strcmp (http_content_security_policy, ""))
839  MHD_add_response_header (response, "Content-Security-Policy",
842  MHD_add_response_header (response, "Strict-Transport-Security",
844 }
845 
849 void
851 {
852  if (strcmp (get_http_x_frame_options (), ""))
853  MHD_add_response_header (response, "X-Frame-Options",
855  if (strcmp (get_http_content_security_policy (), ""))
856  MHD_add_response_header (response, "Content-Security-Policy",
858 }
859 
860 void
862 {
863  const gchar *http_cors_origin = get_http_cors_origin ();
864 
865  if (strcmp (http_cors_origin, ""))
866  {
867  MHD_add_response_header (response, "Access-Control-Allow-Origin",
869  MHD_add_response_header (response, "Access-Control-Allow-Credentials",
870  "true");
871  }
872 }
873 
880 void
882 {
883  MHD_add_response_header (response, MHD_HTTP_HEADER_EXPIRES, "-1");
884  MHD_add_response_header (response, MHD_HTTP_HEADER_CACHE_CONTROL,
885  "no-cache, no-store");
886  MHD_add_response_header (response, MHD_HTTP_HEADER_PRAGMA, "no-cache");
887 }
888 
898 int
899 get_client_address (http_connection_t *conn, char *client_address)
900 {
901  const char *x_real_ip;
902 
903  /* First try X-Real-IP header (unless told to ignore), then MHD connection. */
904 
905  x_real_ip = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Real-IP");
906 
907  if (!is_ignore_http_x_real_ip () && x_real_ip
908  && g_utf8_validate (x_real_ip, -1, NULL) == FALSE)
909  return 1;
910  else if (!is_ignore_http_x_real_ip () && x_real_ip != NULL)
911  strncpy (client_address, x_real_ip, INET6_ADDRSTRLEN);
912  else if (is_unix_socket ())
913  strncpy (client_address, "unix_socket", INET6_ADDRSTRLEN);
914  else
915  {
916  const union MHD_ConnectionInfo *info;
917 
918  info = MHD_get_connection_info (conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
919  sockaddr_as_str ((struct sockaddr_storage *) info->client_addr,
920  client_address);
921  }
922  return 0;
923 }
924 
947 int
948 serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
949  const char *filename, const char *content_type,
950  const char *transfer_encoding, const char *data, uint64_t off,
951  size_t size)
952 {
953  gsad_connection_info_t *con_info = (gsad_connection_info_t *) coninfo_cls;
954 
955  if (NULL != key)
956  {
957  params_append_mhd (con_info->params, key, filename, data, size, off);
958  return MHD_YES;
959  }
960  return MHD_NO;
961 }
962 
977 char *
979  const char *function, int line, const char *msg,
980  cmd_response_data_t *response_data)
981 {
982  gchar *xml, *xmltitle;
983 
984  if (function)
985  {
986  xmltitle = g_strdup_printf ("<title>%s: %s:%i (GSA %s)</title>", title,
987  function, line, GSAD_VERSION);
988  }
989  else
990  {
991  xmltitle =
992  g_strdup_printf ("<title>%s (GSA %s)</title>", title, GSAD_VERSION);
993  }
994 
995  if (credentials)
996  {
997  gchar *pre;
998  time_t now;
999  char ctime_now[200];
1001 
1002  now = time (NULL);
1003  ctime_r_strip_newline (&now, ctime_now);
1004 
1005  pre = g_markup_printf_escaped (
1006  "<envelope>"
1007  "<version>%s</version>"
1008  "<vendor_version>%s</vendor_version>"
1009  "<token>%s</token>"
1010  "<time>%s</time>"
1011  "<login>%s</login>"
1012  "<role>%s</role>"
1013  "<i18n>%s</i18n>"
1014  "<client_address>%s</client_address>",
1015  GSAD_VERSION, vendor_version_get (), user_get_token (user), ctime_now,
1018 
1019  xml = g_strdup_printf ("%s"
1020  "<gsad_response>"
1021  "%s"
1022  "<message>%s</message>"
1023  "</gsad_response>"
1024  "<capabilities>%s</capabilities>"
1025  "</envelope>",
1026  pre, xmltitle, msg ? msg : "",
1028 
1029  g_free (pre);
1030  }
1031  else
1032  {
1033  xml =
1034  g_strdup_printf ("<envelope>"
1035  "<version>%s</version>"
1036  "<vendor_version>%s</vendor_version>"
1037  "<gsad_response>"
1038  "%s"
1039  "<message>%s</message>"
1040  "<token></token>"
1041  "</gsad_response>"
1042  "</envelope>",
1043  GSAD_VERSION, vendor_version_get (),
1044  xmltitle, msg ? msg : "");
1045  }
1046 
1047  g_free (xmltitle);
1048 
1050  cmd_response_data_set_content_length (response_data, strlen (xml));
1051 
1052  return xml;
1053 }
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:636
int handler_send_reauthentication(http_connection_t *connection, int http_status_code, authentication_reason_t reason)
Allow for reauthentication of a user.
Definition: gsad_http.c:459
int cmd_response_data_get_status_code(cmd_response_data_t *data)
Get http status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:174
Structure of credential related information.
const gchar * cmd_response_data_get_content_disposition(cmd_response_data_t *data)
Get content disposition of cmd_response_data_t struct.
Definition: gsad_cmd.c:226
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:558
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
static int file_reader(void *cls, uint64_t pos, char *buf, int max)
Reads from a file.
Definition: gsad_http.c:674
I18n support for Greenbone Security Assistant.
const gchar * get_http_strict_transport_security()
params_t * params
Request parameters.
Definition: gsad_http.h:128
const gchar * vendor_version_get()
Get the vendor version.
int handler_send_not_found(http_connection_t *connection, const gchar *url)
Send a 404 response for a request.
Definition: gsad_http.c:438
const gchar * get_http_cors_origin()
gboolean is_unix_socket()
const gchar * get_http_x_frame_options()
int get_session_timeout()
const gchar * user_get_role(user_t *user)
Definition: gsad_user.c:211
HTTP handling of GSA.
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:861
const gchar * cmd_response_data_get_content_type_string(cmd_response_data_t *data)
Get a content type string if set.
Definition: gsad_cmd.c:255
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:157
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
Definition: gsad_cmd.c:65
Global settings for GSA.
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:899
struct MHD_Connection http_connection_t
Definition: gsad_http.h:135
Special marker for using content type string.
Headers/structs for utility functions in GSAD.
gboolean cmd_response_data_is_allow_caching(cmd_response_data_t *data)
Get allow_caching flag of cmd_response_data_t struct.
Definition: gsad_cmd.c:122
const gchar * credentials_get_language(credentials_t *cred)
Response information for commands.
Definition: gsad_cmd.c:33
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:175
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:171
gboolean is_ignore_http_x_real_ip()
int params_append_mhd(params_t *params, const char *name, const char *filename, const char *chunk_data, int chunk_size, int chunk_offset)
Connection information.
Definition: gsad_http.h:125
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:56
Headers/structs used generally in GSA.
void cmd_response_data_set_content_type(cmd_response_data_t *data, content_type_t content_type)
Set content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:134
http_response_t * create_not_found_response(const gchar *url, cmd_response_data_t *response_data)
Create a default 404 (not found) http response.
Definition: gsad_http.c:393
content_type_t cmd_response_data_get_content_type(cmd_response_data_t *data)
Get content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:148
void gsad_add_content_type_header(http_response_t *response, content_type_t *ct)
Adds content-type header fields to a response.
Definition: gsad_http.c:89
enum content_type content_type_t
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:806
http_response_t * file_content_response(http_connection_t *connection, const char *url, const char *path, cmd_response_data_t *response_data)
Create a response to serve a file from a path.
Definition: gsad_http.c:694
int send_response(http_connection_t *connection, const char *content, int status_code, const gchar *sid, content_type_t content_type, const char *content_disposition, size_t content_length)
Sends a HTTP response.
Definition: gsad_http.c:244
const gchar * http_strict_transport_security
Current value of for HTTP header "Strict-Transport-Security".
Definition: gsad_settings.c:72
void cmd_response_data_free(cmd_response_data_t *data)
Frees the memory of a cmd_response_data_t struct.
Definition: gsad_cmd.c:81
char * gsad_message(credentials_t *credentials, const char *title, const char *function, int line, const char *msg, cmd_response_data_t *response_data)
Handles fatal errors.
Definition: gsad_http.c:978
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:881
const gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
Definition: gsad_settings.c:52
const gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
Definition: gsad_settings.c:57
const gchar * get_http_content_security_policy()
const gchar * user_get_capabilities(user_t *user)
Definition: gsad_user.c:181
const gchar * get_http_guest_chart_content_security_policy()
void add_guest_chart_content_security_headers(http_response_t *response)
Add guest chart content security headers to a MHD response.
Definition: gsad_http.c:850
const gchar * get_http_guest_chart_x_frame_options()
int handler_send_response(http_connection_t *connection, http_response_t *response, cmd_response_data_t *response_data, const gchar *sid)
Send response for handle_request.
Definition: gsad_http.c:295
int serve_post(void *coninfo_cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size)
Serves part of a POST request.
Definition: gsad_http.c:948
gboolean is_use_secure_cookie()
int handler_create_response(http_connection_t *connection, gchar *data, cmd_response_data_t *response_data, const gchar *sid)
Create and send a response.
Definition: gsad_http.c:368
#define MHD_HTTP_NOT_ACCEPTABLE
The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.
Definition: gsad_http.h:108
void cmd_response_data_set_content_length(cmd_response_data_t *data, gsize content_length)
Set response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:186
User information structure, for sessions.
Definition: gsad_user.c:42
#define BAD_REQUEST_PAGE
Bad request error HTML.
Definition: gsad_http.h:80
static int append_param(void *string, enum MHD_ValueKind kind, const char *key, const char *value)
Append a request param to a string.
Definition: gsad_http.c:775
struct MHD_Response http_response_t
Definition: gsad_http.h:137
#define EXPIRES_LENGTH
Max length of cookie expires param.
Definition: gsad_http.h:44
#define DATE_2822_LEN
At least maximum length of rfc2822 format date.
Definition: gsad_http.h:39
const gchar * http_cors_origin
Current value of for HTTP header "Access-Control-Allow-Origin".
Definition: gsad_settings.c:77
#define REMOVE_SID
Definition: gsad_http.h:60
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:512
void cmd_response_data_set_status_code(cmd_response_data_t *data, int http_status_code)
Set status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:160
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
Definition: gsad_http.h:58
char * ctime_r_strip_newline(time_t *time, char *string)
Return string from ctime_r with newline replaces with terminator.
Definition: gsad_base.c:111
int send_redirect_to_uri(http_connection_t *connection, const char *uri, const gchar *sid)
Sends a HTTP redirection to an uri.
Definition: gsad_http.c:183
user_t * credentials_get_user(credentials_t *cred)
const gchar * user_get_client_address(user_t *user)
Definition: gsad_user.c:199
content_type
Content types.
gsize cmd_response_data_get_content_length(cmd_response_data_t *data)
Get response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:200
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:828