Greenbone Security Assistant  9.0.1
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 {
394  http_response_t *response;
395  int len;
396 
397  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
398 
399  gchar *msg =
400  "<!DOCTYPE html>"
401  "<html>"
402  "<head>"
403  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
404  "<link rel=\"icon\" href=\"/img/favicon.gif\" type=\"image/gif\"></link>"
405  "<title>Greenbone Security Assistant</title>"
406  "</head>"
407  "<body>"
408  "<h1>URL not found</h1>"
409  "<p>"
410  "The requested URL is not available"
411  "</p>"
412  "</body>"
413  "</html>";
414 
415  cmd_response_data_set_content_type (response_data,
417 
418  cmd_response_data_set_content_length (response_data, strlen (msg));
419 
420  len = cmd_response_data_get_content_length (response_data);
421  response = MHD_create_response_from_buffer (len, msg, MHD_RESPMEM_MUST_COPY);
422  return response;
423 }
424 
434 int
436  int http_status_code,
438 {
439  const char *msg;
440 
441  switch (reason)
442  {
443  case LOGIN_FAILED:
444  msg = "Login failed.";
445  break;
446  case LOGIN_ERROR:
447  msg = "Login failed. Error during authentication.";
448  break;
449  case GMP_SERVICE_DOWN:
450  msg = "Login failed. GMP Service is down.";
451  break;
452  case SESSION_EXPIRED:
453  msg = "Session expired. Please login again.";
454  break;
455  case LOGOUT_ALREADY:
456  msg = "Already logged out.";
457  break;
458  case BAD_MISSING_TOKEN:
459  msg = "Token missing or bad. Please login again.";
460  break;
461  case BAD_MISSING_COOKIE:
462  msg = "Cookie missing or bad. Please login again.";
463  break;
464  case LOGOUT:
465  msg = "Successfully logged out.";
466  break;
467  case UNKOWN_ERROR:
468  msg = "Unknown error.";
469  break;
470  default:
471  msg = "";
472  }
473 
474  cmd_response_data_t *response_data = cmd_response_data_new ();
475  cmd_response_data_set_status_code (response_data, http_status_code);
476 
477  gchar *xml = gsad_message (NULL, "Authentication required", __FUNCTION__,
478  __LINE__, msg, response_data);
479 
480  return handler_create_response (connection, xml, response_data, REMOVE_SID);
481 }
482 
490 int
492 {
493  int ret;
494  gchar *value;
495  gchar *locale;
496  char expires[EXPIRES_LENGTH + 1];
497  struct tm expire_time_broken;
498  time_t expire_time;
499 
500  /* Set up the expires param. */
501  locale = g_strdup (setlocale (LC_ALL, NULL));
502  setlocale (LC_ALL, "C");
503 
504  expire_time = time (NULL);
505  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
506  abort ();
507  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
508  &expire_time_broken);
509  if (ret == 0)
510  abort ();
511 
512  setlocale (LC_ALL, locale);
513  g_free (locale);
514 
515  /* Add the cookie.
516  *
517  * Tim Brown's suggested cookie included a domain attribute. How would
518  * we get the domain in here? Maybe a --domain option. */
519 
520  value =
521  g_strdup_printf (SID_COOKIE_NAME "=0; expires=%s; path=/; %sHTTPonly",
522  expires, (is_use_secure_cookie () ? "secure; " : ""));
523  ret = MHD_add_response_header (response, "Set-Cookie", value);
524  g_free (value);
525  return ret;
526 }
527 
536 int
537 attach_sid (http_response_t *response, const char *sid)
538 {
539  int ret, timeout;
540  gchar *value;
541  gchar *locale;
542  char expires[EXPIRES_LENGTH + 1];
543  struct tm expire_time_broken;
544  time_t now, expire_time;
545  gchar *tz;
546 
547  /* Set up the expires param. */
548 
549  /* Store current TZ, switch to GMT. */
550  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
551  if (setenv ("TZ", "GMT", 1) == -1)
552  {
553  g_critical ("%s: failed to set TZ\n", __FUNCTION__);
554  g_free (tz);
555  exit (EXIT_FAILURE);
556  }
557  tzset ();
558 
559  locale = g_strdup (setlocale (LC_ALL, NULL));
560  setlocale (LC_ALL, "C");
561 
562  timeout = get_session_timeout () * 60 + 30;
563 
564  now = time (NULL);
565  expire_time = now + timeout;
566  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
567  abort ();
568  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
569  &expire_time_broken);
570  if (ret == 0)
571  abort ();
572 
573  setlocale (LC_ALL, locale);
574  g_free (locale);
575 
576  /* Revert to stored TZ. */
577  if (tz)
578  {
579  if (setenv ("TZ", tz, 1) == -1)
580  {
581  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
582  g_free (tz);
583  exit (EXIT_FAILURE);
584  }
585  }
586  else
587  unsetenv ("TZ");
588  g_free (tz);
589 
590  /* Add the cookie.
591  *
592  * Tim Brown's suggested cookie included a domain attribute. How would
593  * we get the domain in here? Maybe a --domain option. */
594 
595  value = g_strdup_printf (
596  SID_COOKIE_NAME "=%s; expires=%s; max-age=%d; path=/; %sHTTPonly", sid,
597  expires, timeout, (is_use_secure_cookie () ? "secure; " : ""));
598  ret = MHD_add_response_header (response, "Set-Cookie", value);
599  g_free (value);
600  return ret;
601 }
602 
614 int
615 attach_remove_sid (http_response_t *response, const gchar *sid)
616 {
617  if (sid)
618  {
619  if (str_equal (sid, REMOVE_SID))
620  {
621  if (remove_sid (response) == MHD_NO)
622  {
623  MHD_destroy_response (response);
624  g_warning ("%s: failed to remove SID, dropping request",
625  __FUNCTION__);
626  return MHD_NO;
627  }
628  }
629  else
630  {
631  if (attach_sid (response, sid) == MHD_NO)
632  {
633  MHD_destroy_response (response);
634  g_warning ("%s: failed to attach SID, dropping request",
635  __FUNCTION__);
636  return MHD_NO;
637  }
638  }
639  }
640  return MHD_YES;
641 }
652 static int
653 file_reader (void *cls, uint64_t pos, char *buf, int max)
654 {
655  FILE *file = cls;
656 
657  fseek (file, pos, SEEK_SET);
658  return fread (buf, 1, max, file);
659 }
660 
673 file_content_response (http_connection_t *connection, const char *url,
674  const char *path, cmd_response_data_t *response_data)
675 {
676  char date_2822[DATE_2822_LEN];
677  struct tm *mtime;
678  time_t next_week;
679  http_response_t *response;
680  FILE *file;
681  struct stat buf;
682 
683  cmd_response_data_set_status_code (response_data, MHD_HTTP_OK);
684 
685  if (!g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
686  {
687  /* path does not exists or is not a file */
688  /* return index.html to show page not found via js */
689  g_debug ("File %s not found. Return index.html", path);
690 
691  path = "index.html";
692  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
693  }
694 
695  file = fopen (path, "r"); /* this file is just read and sent */
696 
697  if (file == NULL)
698  {
699  g_debug ("File %s failed, ", path);
700  return create_not_found_response (response_data);
701  }
702 
703  /* Guess content type. */
705 
706  if (stat (path, &buf))
707  {
708  /* File information could not be retrieved. */
709  g_critical ("%s: file <%s> can not be stat'ed.\n", __FUNCTION__, path);
710  fclose (file);
711  return create_not_found_response (response_data);
712  }
713 
714  /* Make sure the requested path really is a file. */
715  if ((buf.st_mode & S_IFMT) != S_IFREG)
716  {
717  fclose (file);
718  return create_not_found_response (response_data);
719  }
720 
721  response = MHD_create_response_from_callback (
722  buf.st_size, 32 * 1024, (MHD_ContentReaderCallback) &file_reader, file,
723  (MHD_ContentReaderFreeCallback) &fclose);
724 
725  mtime = localtime (&buf.st_mtime);
726  if (mtime
727  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
728  {
729  MHD_add_response_header (response, "Last-Modified", date_2822);
730  }
731 
732  next_week = time (NULL) + 7 * 24 * 60 * 60;
733  mtime = localtime (&next_week);
734  if (mtime
735  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
736  {
737  MHD_add_response_header (response, "Expires", date_2822);
738  }
739 
740  return response;
741 }
742 
753 static int
754 append_param (void *string, enum MHD_ValueKind kind, const char *key,
755  const char *value)
756 {
757  if (value == NULL)
758  /* http://foo/bar?key */
759  return MHD_YES;
760  if (key == NULL)
761  {
762  assert (0);
763  return MHD_YES;
764  }
765  /* http://foo/bar?key=value */
766  if (strcmp (key, "token") && strcmp (key, "r"))
767  {
768  g_string_append ((GString *) string, key);
769  g_string_append ((GString *) string, "=");
770  g_string_append ((GString *) string, value);
771  g_string_append ((GString *) string, "&");
772  }
773  return MHD_YES;
774 }
775 
784 gchar *
785 reconstruct_url (http_connection_t *connection, const char *url)
786 {
787  GString *full_url;
788 
789  full_url = g_string_new (url);
790  /* To simplify appending the token later, ensure there is at least
791  * one param. */
792  g_string_append (full_url, "?r=1&");
793 
794  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, append_param,
795  full_url);
796 
797  if (full_url->str[strlen (full_url->str) - 1] == '&')
798  full_url->str[strlen (full_url->str) - 1] = '\0';
799 
800  return g_string_free (full_url, FALSE);
801 }
802 
806 void
808 {
810  const gchar *http_content_security_policy =
812  const gchar *http_strict_transport_security =
814 
815  if (strcmp (http_x_frame_options, ""))
816  MHD_add_response_header (response, "X-Frame-Options", http_x_frame_options);
817  if (strcmp (http_content_security_policy, ""))
818  MHD_add_response_header (response, "Content-Security-Policy",
821  MHD_add_response_header (response, "Strict-Transport-Security",
823 }
824 
828 void
830 {
831  if (strcmp (get_http_x_frame_options (), ""))
832  MHD_add_response_header (response, "X-Frame-Options",
834  if (strcmp (get_http_content_security_policy (), ""))
835  MHD_add_response_header (response, "Content-Security-Policy",
837 }
838 
839 void
841 {
842  const gchar *http_cors_origin = get_http_cors_origin ();
843 
844  if (strcmp (http_cors_origin, ""))
845  {
846  MHD_add_response_header (response, "Access-Control-Allow-Origin",
848  MHD_add_response_header (response, "Access-Control-Allow-Credentials",
849  "true");
850  }
851 }
852 
859 void
861 {
862  MHD_add_response_header (response, MHD_HTTP_HEADER_EXPIRES, "-1");
863  MHD_add_response_header (response, MHD_HTTP_HEADER_CACHE_CONTROL,
864  "no-cache, no-store");
865  MHD_add_response_header (response, MHD_HTTP_HEADER_PRAGMA, "no-cache");
866 }
867 
877 int
878 get_client_address (http_connection_t *conn, char *client_address)
879 {
880  const char *x_real_ip;
881 
882  /* First try X-Real-IP header (unless told to ignore), then MHD connection. */
883 
884  x_real_ip = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Real-IP");
885 
886  if (!is_ignore_http_x_real_ip () && x_real_ip
887  && g_utf8_validate (x_real_ip, -1, NULL) == FALSE)
888  return 1;
889  else if (!is_ignore_http_x_real_ip () && x_real_ip != NULL)
890  strncpy (client_address, x_real_ip, INET6_ADDRSTRLEN);
891  else if (is_unix_socket ())
892  strncpy (client_address, "unix_socket", INET6_ADDRSTRLEN);
893  else
894  {
895  const union MHD_ConnectionInfo *info;
896 
897  info = MHD_get_connection_info (conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
898  sockaddr_as_str ((struct sockaddr_storage *) info->client_addr,
899  client_address);
900  }
901  return 0;
902 }
903 
926 int
927 serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
928  const char *filename, const char *content_type,
929  const char *transfer_encoding, const char *data, uint64_t off,
930  size_t size)
931 {
932  gsad_connection_info_t *con_info = (gsad_connection_info_t *) coninfo_cls;
933 
934  if (NULL != key)
935  {
936  params_append_mhd (con_info->params, key, filename, data, size, off);
937  return MHD_YES;
938  }
939  return MHD_NO;
940 }
941 
956 char *
958  const char *function, int line, const char *msg,
959  cmd_response_data_t *response_data)
960 {
961  gchar *xml, *xmltitle;
962 
963  if (function)
964  {
965  xmltitle = g_strdup_printf ("<title>%s: %s:%i (GSA %s)</title>", title,
966  function, line, GSAD_VERSION);
967  }
968  else
969  {
970  xmltitle =
971  g_strdup_printf ("<title>%s (GSA %s)</title>", title, GSAD_VERSION);
972  }
973 
974  if (credentials)
975  {
976  gchar *pre;
977  time_t now;
978  char ctime_now[200];
980 
981  now = time (NULL);
982  ctime_r_strip_newline (&now, ctime_now);
983 
984  pre = g_markup_printf_escaped (
985  "<envelope>"
986  "<version>%s</version>"
987  "<vendor_version>%s</vendor_version>"
988  "<token>%s</token>"
989  "<time>%s</time>"
990  "<login>%s</login>"
991  "<role>%s</role>"
992  "<i18n>%s</i18n>"
993  "<client_address>%s</client_address>",
994  GSAD_VERSION, vendor_version_get (), user_get_token (user), ctime_now,
997 
998  xml = g_strdup_printf ("%s"
999  "<gsad_response>"
1000  "%s"
1001  "<message>%s</message>"
1002  "</gsad_response>"
1003  "<capabilities>%s</capabilities>"
1004  "</envelope>",
1005  pre, xmltitle, msg ? msg : "",
1007 
1008  g_free (pre);
1009  }
1010  else
1011  {
1012  xml = g_strdup_printf ("<envelope>"
1013  "<version>%s</version>"
1014  "<vendor_version>%s</vendor_version>"
1015  "<gsad_response>"
1016  "%s"
1017  "<message>%s</message>"
1018  "<token></token>"
1019  "</gsad_response>"
1020  "</envelope>",
1021  GSAD_VERSION, vendor_version_get (), xmltitle,
1022  msg ? msg : "");
1023  }
1024 
1025  g_free (xmltitle);
1026 
1028  cmd_response_data_set_content_length (response_data, strlen (xml));
1029 
1030  return xml;
1031 }
gsad_connection_info::params
params_t * params
Request parameters.
Definition: gsad_http.h:128
EXPIRES_LENGTH
#define EXPIRES_LENGTH
Max length of cookie expires param.
Definition: gsad_http.h:44
is_unix_socket
gboolean is_unix_socket()
Definition: gsad_settings.c:276
handler_send_reauthentication
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:435
DATE_2822_LEN
#define DATE_2822_LEN
At least maximum length of rfc2822 format date.
Definition: gsad_http.h:39
GSAD_CONTENT_TYPE_APP_DEB
@ GSAD_CONTENT_TYPE_APP_DEB
Definition: gsad_content_type.h:33
cmd_response_data_get_content_length
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
cmd_response_data
Response information for commands.
Definition: gsad_cmd.c:33
gsad_settings.h
Global settings for GSA.
MHD_HTTP_NOT_ACCEPTABLE
#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
get_http_cors_origin
const gchar * get_http_cors_origin()
Definition: gsad_settings.c:201
user_get_username
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:154
content_type
content_type
Content types.
Definition: gsad_content_type.h:31
get_session_timeout
int get_session_timeout()
Definition: gsad_settings.c:153
ctime_r_strip_newline
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
user_get_client_address
const gchar * user_get_client_address(user_t *user)
Definition: gsad_user.c:196
user
User information structure, for sessions.
Definition: gsad_user.c:42
add_security_headers
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:807
UNKOWN_ERROR
@ UNKOWN_ERROR
Definition: gsad_http.h:166
cmd_response_data_set_content_type
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
cmd_response_data_set_status_code
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
credentials_get_language
const gchar * credentials_get_language(credentials_t *cred)
Definition: gsad_credentials.c:73
cmd_response_data_get_content_type_string
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
GSAD_CONTENT_TYPE_IMAGE_SVG
@ GSAD_CONTENT_TYPE_IMAGE_SVG
Definition: gsad_content_type.h:49
remove_sid
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:491
GSAD_CONTENT_TYPE_STRING
@ GSAD_CONTENT_TYPE_STRING
Special marker for using content type string.
Definition: gsad_content_type.h:42
credentials
Structure of credential related information.
Definition: gsad_credentials.c:34
is_ignore_http_x_real_ip
gboolean is_ignore_http_x_real_ip()
Definition: gsad_settings.c:249
GMP_SERVICE_DOWN
@ GMP_SERVICE_DOWN
Definition: gsad_http.h:162
cmd_response_data_set_content_length
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
cmd_response_data_get_content_disposition
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
GSAD_CONTENT_TYPE_TEXT_PLAIN
@ GSAD_CONTENT_TYPE_TEXT_PLAIN
Definition: gsad_content_type.h:47
create_not_found_response
http_response_t * create_not_found_response(cmd_response_data_t *response_data)
Create a default 404 (not found) http response.
Definition: gsad_http.c:392
GSAD_CONTENT_TYPE_IMAGE_GIF
@ GSAD_CONTENT_TYPE_IMAGE_GIF
Definition: gsad_content_type.h:50
cmd_response_data_is_allow_caching
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
GSAD_CONTENT_TYPE_APP_XHTML
@ GSAD_CONTENT_TYPE_APP_XHTML
Definition: gsad_content_type.h:35
gsad_message
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:957
content_type_t
enum content_type content_type_t
Definition: gsad_content_type.h:54
add_cors_headers
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:840
file_content_response
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:673
utils.h
Headers/structs for utility functions in GSAD.
http_strict_transport_security
const gchar * http_strict_transport_security
Current value of for HTTP header "Strict-Transport-Security".
Definition: gsad_settings.c:70
GSAD_CONTENT_TYPE_APP_KEY
@ GSAD_CONTENT_TYPE_APP_KEY
Definition: gsad_content_type.h:36
get_client_address
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:878
GSAD_CONTENT_TYPE_APP_PDF
@ GSAD_CONTENT_TYPE_APP_PDF
Definition: gsad_content_type.h:38
get_http_x_frame_options
const gchar * get_http_x_frame_options()
Definition: gsad_settings.c:189
get_http_guest_chart_x_frame_options
const gchar * get_http_guest_chart_x_frame_options()
Definition: gsad_settings.c:213
params_append_mhd
int params_append_mhd(params_t *params, const char *name, const char *filename, const char *chunk_data, int chunk_size, int chunk_offset)
http_connection_t
struct MHD_Connection http_connection_t
Definition: gsad_http.h:135
add_forbid_caching_headers
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:860
GSAD_CONTENT_TYPE_APP_RPM
@ GSAD_CONTENT_TYPE_APP_RPM
Definition: gsad_content_type.h:39
cmd_response_data_new
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
GSAD_CONTENT_TYPE_TEXT_JS
@ GSAD_CONTENT_TYPE_TEXT_JS
Definition: gsad_content_type.h:46
GSAD_CONTENT_TYPE_APP_XML
@ GSAD_CONTENT_TYPE_APP_XML
Definition: gsad_content_type.h:40
cmd_response_data_get_content_type
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
guess_content_type
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:56
BAD_MISSING_COOKIE
@ BAD_MISSING_COOKIE
Definition: gsad_http.h:164
authentication_reason_t
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:169
reconstruct_url
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:785
user_get_capabilities
const gchar * user_get_capabilities(user_t *user)
Definition: gsad_user.c:178
gsad_i18n.h
I18n support for Greenbone Security Assistant.
GSAD_CONTENT_TYPE_TEXT_CSS
@ GSAD_CONTENT_TYPE_TEXT_CSS
Definition: gsad_content_type.h:44
GSAD_CONTENT_TYPE_APP_EXE
@ GSAD_CONTENT_TYPE_APP_EXE
Definition: gsad_content_type.h:34
send_response
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
GSAD_CONTENT_TYPE_TEXT_HTML
@ GSAD_CONTENT_TYPE_TEXT_HTML
Definition: gsad_content_type.h:45
str_equal
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
http_cors_origin
const gchar * http_cors_origin
Current value of for HTTP header "Access-Control-Allow-Origin".
Definition: gsad_settings.c:75
BAD_MISSING_TOKEN
@ BAD_MISSING_TOKEN
Definition: gsad_http.h:165
LOGOUT_ALREADY
@ LOGOUT_ALREADY
Definition: gsad_http.h:161
SESSION_EXPIRED
@ SESSION_EXPIRED
Definition: gsad_http.h:163
send_redirect_to_uri
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
gsad_http.h
HTTP handling of GSA.
GSAD_CONTENT_TYPE_APP_NBE
@ GSAD_CONTENT_TYPE_APP_NBE
Definition: gsad_content_type.h:37
GSAD_CONTENT_TYPE_IMAGE_PNG
@ GSAD_CONTENT_TYPE_IMAGE_PNG
Definition: gsad_content_type.h:43
http_response_t
struct MHD_Response http_response_t
Definition: gsad_http.h:137
add_guest_chart_content_security_headers
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:829
SID_COOKIE_NAME
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
Definition: gsad_http.h:58
http_x_frame_options
const gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
Definition: gsad_settings.c:50
attach_remove_sid
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:615
attach_sid
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:537
BAD_REQUEST_PAGE
#define BAD_REQUEST_PAGE
Bad request error HTML.
Definition: gsad_http.h:80
LOGOUT
@ LOGOUT
Definition: gsad_http.h:160
http_content_security_policy
const gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
Definition: gsad_settings.c:55
get_http_content_security_policy
const gchar * get_http_content_security_policy()
Definition: gsad_settings.c:177
LOGIN_ERROR
@ LOGIN_ERROR
Definition: gsad_http.h:159
REMOVE_SID
#define REMOVE_SID
Definition: gsad_http.h:60
user_get_role
const gchar * user_get_role(user_t *user)
Definition: gsad_user.c:208
credentials_get_user
user_t * credentials_get_user(credentials_t *cred)
Definition: gsad_credentials.c:67
is_use_secure_cookie
gboolean is_use_secure_cookie()
Definition: gsad_settings.c:165
GSAD_CONTENT_TYPE_DONE
@ GSAD_CONTENT_TYPE_DONE
Special marker.
Definition: gsad_content_type.h:41
handler_create_response
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
gsad_add_content_type_header
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
cmd_response_data_get_status_code
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
append_param
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:754
get_http_guest_chart_content_security_policy
const gchar * get_http_guest_chart_content_security_policy()
Definition: gsad_settings.c:225
handler_send_response
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
cmd_response_data_free
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
GSAD_CONTENT_TYPE_APP_JSON
@ GSAD_CONTENT_TYPE_APP_JSON
Definition: gsad_content_type.h:51
gsad_base.h
Headers/structs used generally in GSA.
gsad_connection_info
Connection information.
Definition: gsad_http.h:125
LOGIN_FAILED
@ LOGIN_FAILED
Definition: gsad_http.h:158
get_http_strict_transport_security
const gchar * get_http_strict_transport_security()
Definition: gsad_settings.c:237
serve_post
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:927
GSAD_CONTENT_TYPE_OCTET_STREAM
@ GSAD_CONTENT_TYPE_OCTET_STREAM
Definition: gsad_content_type.h:48
file_reader
static int file_reader(void *cls, uint64_t pos, char *buf, int max)
Reads from a file.
Definition: gsad_http.c:653
vendor_version_get
const gchar * vendor_version_get()
Get the vendor version.
Definition: gsad_settings.c:117
user_get_token
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:172