33 #include <gvm/base/networking.h>
34 #include <gvm/util/xmlutils.h>
39 #include <sys/types.h>
46 #define G_LOG_DOMAIN "gsad http"
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"))
97 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
101 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
105 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
106 "application/xhtml+xml");
109 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
113 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
117 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
121 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
125 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
126 "application/xml; charset=utf-8");
129 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
133 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
137 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
141 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
142 "application/octet-stream");
145 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
149 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
150 "text/html; charset=utf-8");
153 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
157 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
158 "text/plain; charset=utf-8");
161 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
162 "application/json; charset=utf-8");
167 MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
168 "text/plain; charset=utf-8");
194 body = g_strdup_printf (
"<html><body>Code 303 - Redirecting to"
195 " <a href=\"%s\">%s<a/></body></html>\n",
197 response = MHD_create_response_from_buffer (strlen (body), body,
198 MHD_RESPMEM_MUST_FREE);
202 g_warning (
"%s: failed to create response, dropping request",
206 ret = MHD_add_response_header (response, MHD_HTTP_HEADER_LOCATION, uri);
209 MHD_destroy_response (response);
210 g_warning (
"%s: failed to add location header, dropping request",
217 MHD_destroy_response (response);
218 g_warning (
"%s: failed to attach SID, dropping request", __FUNCTION__);
225 ret = MHD_queue_response (connection, MHD_HTTP_SEE_OTHER, response);
226 MHD_destroy_response (response);
246 const char *content_disposition,
size_t content_length)
253 size = (content_length ? content_length : strlen (content));
256 g_warning (
"%s: content is NULL", __FUNCTION__);
257 status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
260 response = MHD_create_response_from_buffer (
261 size, (
void *) (content ? content :
""), MHD_RESPMEM_MUST_COPY);
265 if (content_disposition)
266 MHD_add_response_header (response,
"Content-Disposition",
267 content_disposition);
277 ret = MHD_queue_response (connection, status_code, response);
278 MHD_destroy_response (response);
299 const gchar *content_disposition;
306 MHD_destroy_response (response);
314 MHD_add_response_header (
315 response, MHD_HTTP_HEADER_CONTENT_TYPE,
323 content_disposition =
326 if (content_disposition != NULL)
328 MHD_add_response_header (response,
"Content-Disposition",
329 content_disposition);
341 ret = MHD_queue_response (connection, status_code, response);
348 MHD_destroy_response (response);
351 MHD_destroy_response (response);
375 if (len == 0 && data)
380 response = MHD_create_response_from_buffer (len, data, MHD_RESPMEM_MUST_FREE);
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>"
408 "<h1>URL not found</h1>"
410 "The requested URL is not available"
421 response = MHD_create_response_from_buffer (len, msg, MHD_RESPMEM_MUST_COPY);
436 int http_status_code,
444 msg =
"Login failed.";
447 msg =
"Login failed. Error during authentication.";
450 msg =
"Login failed. GMP Service is down.";
453 msg =
"Session expired. Please login again.";
456 msg =
"Already logged out.";
459 msg =
"Token missing or bad. Please login again.";
462 msg =
"Cookie missing or bad. Please login again.";
465 msg =
"Successfully logged out.";
468 msg =
"Unknown error.";
477 gchar *xml =
gsad_message (NULL,
"Authentication required", __FUNCTION__,
478 __LINE__, msg, response_data);
497 struct tm expire_time_broken;
501 locale = g_strdup (setlocale (LC_ALL, NULL));
502 setlocale (LC_ALL,
"C");
504 expire_time = time (NULL);
505 if (localtime_r (&expire_time, &expire_time_broken) == NULL)
508 &expire_time_broken);
512 setlocale (LC_ALL, locale);
523 ret = MHD_add_response_header (response,
"Set-Cookie", value);
543 struct tm expire_time_broken;
544 time_t now, expire_time;
550 tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
551 if (setenv (
"TZ",
"GMT", 1) == -1)
553 g_critical (
"%s: failed to set TZ\n", __FUNCTION__);
559 locale = g_strdup (setlocale (LC_ALL, NULL));
560 setlocale (LC_ALL,
"C");
565 expire_time = now + timeout;
566 if (localtime_r (&expire_time, &expire_time_broken) == NULL)
569 &expire_time_broken);
573 setlocale (LC_ALL, locale);
579 if (setenv (
"TZ", tz, 1) == -1)
581 g_warning (
"%s: Failed to switch to original TZ", __FUNCTION__);
595 value = g_strdup_printf (
596 SID_COOKIE_NAME "=%s; expires=%s; max-age=%d; path=/; %sHTTPonly", sid,
598 ret = MHD_add_response_header (response,
"Set-Cookie", value);
623 MHD_destroy_response (response);
624 g_warning (
"%s: failed to remove SID, dropping request",
633 MHD_destroy_response (response);
634 g_warning (
"%s: failed to attach SID, dropping request",
657 fseek (file, pos, SEEK_SET);
658 return fread (buf, 1, max, file);
685 if (!g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
689 g_debug (
"File %s not found. Return index.html", path);
695 file = fopen (path,
"r");
699 g_debug (
"File %s failed, ", path);
706 if (stat (path, &buf))
709 g_critical (
"%s: file <%s> can not be stat'ed.\n", __FUNCTION__, path);
715 if ((buf.st_mode & S_IFMT) != S_IFREG)
721 response = MHD_create_response_from_callback (
722 buf.st_size, 32 * 1024, (MHD_ContentReaderCallback) &
file_reader, file,
723 (MHD_ContentReaderFreeCallback) &fclose);
725 mtime = localtime (&buf.st_mtime);
727 && strftime (date_2822,
DATE_2822_LEN,
"%a, %d %b %Y %H:%M:%S %Z", mtime))
729 MHD_add_response_header (response,
"Last-Modified", date_2822);
732 next_week = time (NULL) + 7 * 24 * 60 * 60;
733 mtime = localtime (&next_week);
735 && strftime (date_2822,
DATE_2822_LEN,
"%a, %d %b %Y %H:%M:%S %Z", mtime))
737 MHD_add_response_header (response,
"Expires", date_2822);
766 if (strcmp (key,
"token") && strcmp (key,
"r"))
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,
"&");
789 full_url = g_string_new (url);
792 g_string_append (full_url,
"?r=1&");
794 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
append_param,
797 if (full_url->str[strlen (full_url->str) - 1] ==
'&')
798 full_url->str[strlen (full_url->str) - 1] =
'\0';
800 return g_string_free (full_url, FALSE);
818 MHD_add_response_header (response,
"Content-Security-Policy",
821 MHD_add_response_header (response,
"Strict-Transport-Security",
832 MHD_add_response_header (response,
"X-Frame-Options",
835 MHD_add_response_header (response,
"Content-Security-Policy",
846 MHD_add_response_header (response,
"Access-Control-Allow-Origin",
848 MHD_add_response_header (response,
"Access-Control-Allow-Credentials",
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");
880 const char *x_real_ip;
884 x_real_ip = MHD_lookup_connection_value (conn, MHD_HEADER_KIND,
"X-Real-IP");
887 && g_utf8_validate (x_real_ip, -1, NULL) == FALSE)
890 strncpy (client_address, x_real_ip, INET6_ADDRSTRLEN);
892 strncpy (client_address,
"unix_socket", INET6_ADDRSTRLEN);
895 const union MHD_ConnectionInfo *info;
897 info = MHD_get_connection_info (conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
898 sockaddr_as_str ((
struct sockaddr_storage *) info->client_addr,
927 serve_post (
void *coninfo_cls,
enum MHD_ValueKind kind,
const char *key,
929 const char *transfer_encoding,
const char *data, uint64_t off,
958 const char *
function,
int line,
const char *msg,
961 gchar *xml, *xmltitle;
965 xmltitle = g_strdup_printf (
"<title>%s: %s:%i (GSA %s)</title>", title,
966 function, line, GSAD_VERSION);
971 g_strdup_printf (
"<title>%s (GSA %s)</title>", title, GSAD_VERSION);
984 pre = g_markup_printf_escaped (
986 "<version>%s</version>"
987 "<vendor_version>%s</vendor_version>"
993 "<client_address>%s</client_address>",
998 xml = g_strdup_printf (
"%s"
1001 "<message>%s</message>"
1003 "<capabilities>%s</capabilities>"
1005 pre, xmltitle, msg ? msg :
"",
1012 xml = g_strdup_printf (
"<envelope>"
1013 "<version>%s</version>"
1014 "<vendor_version>%s</vendor_version>"
1017 "<message>%s</message>"