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);
400 gchar *msg = g_strdup_printf (
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>" 409 "<h1>URL not found</h1>" 411 "The requested URL %s is not available" 424 response = MHD_create_response_from_buffer (len, msg, MHD_RESPMEM_MUST_COPY);
460 int http_status_code,
468 msg =
"Login failed.";
471 msg =
"Login failed. Error during authentication.";
474 msg =
"Login failed. GMP Service is down.";
477 msg =
"Session expired. Please login again.";
480 msg =
"Already logged out.";
483 msg =
"Token missing or bad. Please login again.";
486 msg =
"Cookie missing or bad. Please login again.";
489 msg =
"Successfully logged out.";
498 gchar *xml =
gsad_message (NULL,
"Authentication required", __FUNCTION__,
499 __LINE__, msg, response_data);
518 struct tm expire_time_broken;
522 locale = g_strdup (setlocale (LC_ALL, NULL));
523 setlocale (LC_ALL,
"C");
525 expire_time = time (NULL);
526 if (localtime_r (&expire_time, &expire_time_broken) == NULL)
529 &expire_time_broken);
533 setlocale (LC_ALL, locale);
544 ret = MHD_add_response_header (response,
"Set-Cookie", value);
564 struct tm expire_time_broken;
565 time_t now, expire_time;
571 tz = getenv (
"TZ") ? g_strdup (getenv (
"TZ")) : NULL;
572 if (setenv (
"TZ",
"GMT", 1) == -1)
574 g_critical (
"%s: failed to set TZ\n", __FUNCTION__);
580 locale = g_strdup (setlocale (LC_ALL, NULL));
581 setlocale (LC_ALL,
"C");
586 expire_time = now + timeout;
587 if (localtime_r (&expire_time, &expire_time_broken) == NULL)
590 &expire_time_broken);
594 setlocale (LC_ALL, locale);
600 if (setenv (
"TZ", tz, 1) == -1)
602 g_warning (
"%s: Failed to switch to original TZ", __FUNCTION__);
616 value = g_strdup_printf (
617 SID_COOKIE_NAME "=%s; expires=%s; max-age=%d; path=/; %sHTTPonly", sid,
619 ret = MHD_add_response_header (response,
"Set-Cookie", value);
644 MHD_destroy_response (response);
645 g_warning (
"%s: failed to remove SID, dropping request",
654 MHD_destroy_response (response);
655 g_warning (
"%s: failed to attach SID, dropping request",
678 fseek (file, pos, SEEK_SET);
679 return fread (buf, 1, max, file);
706 if (!g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
710 g_debug (
"File %s not found. Return index.html", path);
716 file = fopen (path,
"r");
720 g_debug (
"File %s failed, ", path);
727 if (stat (path, &buf))
730 g_critical (
"%s: file <%s> can not be stat'ed.\n", __FUNCTION__, path);
736 if ((buf.st_mode & S_IFMT) != S_IFREG)
742 response = MHD_create_response_from_callback (
743 buf.st_size, 32 * 1024, (MHD_ContentReaderCallback) &
file_reader, file,
744 (MHD_ContentReaderFreeCallback) &fclose);
746 mtime = localtime (&buf.st_mtime);
748 && strftime (date_2822,
DATE_2822_LEN,
"%a, %d %b %Y %H:%M:%S %Z", mtime))
750 MHD_add_response_header (response,
"Last-Modified", date_2822);
753 next_week = time (NULL) + 7 * 24 * 60 * 60;
754 mtime = localtime (&next_week);
756 && strftime (date_2822,
DATE_2822_LEN,
"%a, %d %b %Y %H:%M:%S %Z", mtime))
758 MHD_add_response_header (response,
"Expires", date_2822);
787 if (strcmp (key,
"token") && strcmp (key,
"r"))
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,
"&");
810 full_url = g_string_new (url);
813 g_string_append (full_url,
"?r=1&");
815 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
append_param,
818 if (full_url->str[strlen (full_url->str) - 1] ==
'&')
819 full_url->str[strlen (full_url->str) - 1] =
'\0';
821 return g_string_free (full_url, FALSE);
839 MHD_add_response_header (response,
"Content-Security-Policy",
842 MHD_add_response_header (response,
"Strict-Transport-Security",
853 MHD_add_response_header (response,
"X-Frame-Options",
856 MHD_add_response_header (response,
"Content-Security-Policy",
867 MHD_add_response_header (response,
"Access-Control-Allow-Origin",
869 MHD_add_response_header (response,
"Access-Control-Allow-Credentials",
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");
901 const char *x_real_ip;
905 x_real_ip = MHD_lookup_connection_value (conn, MHD_HEADER_KIND,
"X-Real-IP");
908 && g_utf8_validate (x_real_ip, -1, NULL) == FALSE)
911 strncpy (client_address, x_real_ip, INET6_ADDRSTRLEN);
913 strncpy (client_address,
"unix_socket", INET6_ADDRSTRLEN);
916 const union MHD_ConnectionInfo *info;
918 info = MHD_get_connection_info (conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
919 sockaddr_as_str ((
struct sockaddr_storage *) info->client_addr,
948 serve_post (
void *coninfo_cls,
enum MHD_ValueKind kind,
const char *key,
950 const char *transfer_encoding,
const char *data, uint64_t off,
979 const char *
function,
int line,
const char *msg,
982 gchar *xml, *xmltitle;
986 xmltitle = g_strdup_printf (
"<title>%s: %s:%i (GSA %s)</title>", title,
987 function, line, GSAD_VERSION);
992 g_strdup_printf (
"<title>%s (GSA %s)</title>", title, GSAD_VERSION);
1005 pre = g_markup_printf_escaped (
1007 "<version>%s</version>" 1008 "<vendor_version>%s</vendor_version>" 1014 "<client_address>%s</client_address>",
1019 xml = g_strdup_printf (
"%s" 1022 "<message>%s</message>" 1024 "<capabilities>%s</capabilities>" 1026 pre, xmltitle, msg ? msg :
"",
1034 g_strdup_printf (
"<envelope>" 1035 "<version>%s</version>" 1036 "<vendor_version>%s</vendor_version>" 1039 "<message>%s</message>" 1044 xmltitle, msg ? msg :
"");
int attach_remove_sid(http_response_t *response, const gchar *sid)
int handler_send_reauthentication(http_connection_t *connection, int http_status_code, authentication_reason_t reason)
Allow for reauthentication of a user.
int cmd_response_data_get_status_code(cmd_response_data_t *data)
Get http status code of cmd_response_data_t struct.
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.
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
static int file_reader(void *cls, uint64_t pos, char *buf, int max)
Reads from a file.
I18n support for Greenbone Security Assistant.
const gchar * get_http_strict_transport_security()
params_t * params
Request parameters.
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.
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)
void add_cors_headers(http_response_t *response)
const gchar * cmd_response_data_get_content_type_string(cmd_response_data_t *data)
Get a content type string if set.
const gchar * user_get_username(user_t *user)
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
struct MHD_Connection http_connection_t
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.
const gchar * credentials_get_language(credentials_t *cred)
Response information for commands.
const gchar * user_get_token(user_t *user)
enum authentication_reason authentication_reason_t
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)
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
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.
http_response_t * create_not_found_response(const gchar *url, cmd_response_data_t *response_data)
Create a default 404 (not found) http response.
content_type_t cmd_response_data_get_content_type(cmd_response_data_t *data)
Get content type of cmd_response_data_t struct.
void gsad_add_content_type_header(http_response_t *response, content_type_t *ct)
Adds content-type header fields to a response.
enum content_type content_type_t
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
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.
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.
const gchar * http_strict_transport_security
Current value of for HTTP header "Strict-Transport-Security".
void cmd_response_data_free(cmd_response_data_t *data)
Frees the memory of a cmd_response_data_t struct.
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.
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
const gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
const gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
const gchar * get_http_content_security_policy()
const gchar * user_get_capabilities(user_t *user)
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.
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.
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.
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.
#define MHD_HTTP_NOT_ACCEPTABLE
The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.
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.
User information structure, for sessions.
#define BAD_REQUEST_PAGE
Bad request error HTML.
static int append_param(void *string, enum MHD_ValueKind kind, const char *key, const char *value)
Append a request param to a string.
struct MHD_Response http_response_t
#define EXPIRES_LENGTH
Max length of cookie expires param.
#define DATE_2822_LEN
At least maximum length of rfc2822 format date.
const gchar * http_cors_origin
Current value of for HTTP header "Access-Control-Allow-Origin".
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
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.
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
char * ctime_r_strip_newline(time_t *time, char *string)
Return string from ctime_r with newline replaces with terminator.
int send_redirect_to_uri(http_connection_t *connection, const char *uri, const gchar *sid)
Sends a HTTP redirection to an uri.
user_t * credentials_get_user(credentials_t *cred)
const gchar * user_get_client_address(user_t *user)
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.
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.