|
Greenbone Security Assistant
9.0.1
|
Go to the documentation of this file.
38 #include <gvm/base/networking.h>
39 #include <gvm/util/xmlutils.h>
47 #define G_LOG_DOMAIN "gsad http handler"
54 #define TOKEN_REGEXP "^[a-z0-9\\-]+$"
55 #define SLAVE_ID_REGEXP "^[a-z0-9\\-]+$"
104 while (handler->
next != NULL)
106 handler = handler->
next;
109 handler->
next = next;
123 return handler->
handle (connection, method, url, con_info, handler, data);
133 if (handler == NULL || handler->
next == NULL)
138 next = handler->
next;
139 return next->
handle (connection, method, url, con_info, next, data);
148 handler->
free = freefunc;
149 handler->
data = data;
150 handler->
next = NULL;
178 handler->
free (handler);
188 if (!strcmp (method,
"GET"))
190 g_debug (
"method router handling GET");
194 if (!strcmp (method,
"POST"))
196 g_debug (
"method router handling POST");
247 g_debug (
"checking url map for url %s against %s\n", url,
248 g_regex_get_pattern (map->
gregexp));
250 if (g_regex_match (map->
gregexp, url, 0, NULL))
252 g_debug (
"Found url handler for url %s\n", url);
265 map->
gregexp = g_regex_new (regexp, 0, 0, NULL);
303 g_debug (
"Validating url %s", url);
314 if (url && (url[0] ==
'/') && (url[1] ==
'/'))
322 if (url && (g_utf8_validate (url, -1, NULL) == FALSE))
338 if (strcmp (method,
"GET") && strcmp (method,
"POST"))
353 gchar client_address[INET6_ADDRSTRLEN];
357 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND,
"token");
369 MHD_lookup_connection_value (connection, MHD_COOKIE_KIND,
SID_COOKIE_NAME);
401 int http_response_code = MHD_HTTP_OK;
420 http_response_code = MHD_HTTP_UNAUTHORIZED;
422 http_response_code = MHD_HTTP_BAD_REQUEST;
425 http_response_code = MHD_HTTP_UNAUTHORIZED;
452 const gchar *accept_language;
454 char client_address[INET6_ADDRSTRLEN];
463 accept_language = MHD_lookup_connection_value (
464 connection, MHD_HEADER_KIND,
"Accept-Language");
466 && g_utf8_validate (accept_language, -1, NULL) == FALSE)
527 const gchar *sid, *accept_language;
529 char client_address[INET6_ADDRSTRLEN];
532 MHD_lookup_connection_value (connection, MHD_COOKIE_KIND,
SID_COOKIE_NAME);
537 con_info->
cookie = g_strdup (sid);
539 accept_language = MHD_lookup_connection_value (connection, MHD_HEADER_KIND,
541 if (accept_language && g_utf8_validate (accept_language, -1, NULL) == FALSE)
573 const char *slave_id;
575 gvm_connection_t con;
578 g_debug (
"Request for system report url %s", url);
580 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
params_mhd_add,
586 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND,
"slave_id");
591 g_warning (
"%s: failed to validate slave_id, dropping request",
603 &url[0] + strlen (
"/system_report/"), params,
605 gvm_connection_close (&con);
609 MHD_HTTP_INTERNAL_SERVER_ERROR);
611 "An internal error occurred. "
612 "Diagnostics: Failure to connect to manager daemon. "
613 "Manager daemon doesn't respond.",
625 MHD_HTTP_INTERNAL_SERVER_ERROR);
627 "An internal error occurred. "
628 "Diagnostics: Failure to connect to manager daemon. "
629 "Timeout while waiting for manager response.",
634 MHD_HTTP_INTERNAL_SERVER_ERROR);
636 "An internal error occurred. "
637 "Diagnostics: Failure to connect to manager daemon. "
638 "Could not open a connection.",
643 MHD_HTTP_INTERNAL_SERVER_ERROR);
645 "An internal error occurred. "
646 "Diagnostics: Failure to connect to manager daemon. "
655 g_warning (
"%s: failed to get system reports, dropping request",
689 char *default_file =
"index.html";
696 if (strstr (url,
".."))
697 path = g_strconcat (default_file, NULL);
701 const char *relative_url = url;
703 relative_url = url + 1;
704 path = g_strconcat (relative_url, NULL);
707 g_debug (
"Requesting url %s for static ng path %s", url, path);
729 const char *relative_url = url;
732 relative_url = url + 1;
734 path = g_strconcat (relative_url, NULL);
736 g_debug (
"Requesting url %s for static config path %s", url, path);
743 if (g_file_test (path, G_FILE_TEST_EXISTS))
820 g_debug (
"Cleaning up http handlers");
847 const char *method,
const char *version,
848 const char *upload_data,
size_t *upload_data_size,
854 gboolean new_connection;
858 new_connection = (*con_cls == NULL);
861 if ((
str_equal (method,
"GET")) && new_connection)
870 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
875 *con_cls = (
void *) con_info;
891 if (con_info->postprocessor == NULL)
904 con_info->connectiontype = 1;
906 *con_cls = (
void *) con_info;
912 if (*upload_data_size != 0)
916 *upload_data_size = 0;
922 g_debug (
"============= url: %s\n", full_url);
int handle_gmp_post(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
char * cookie
Value of SID cookie param.
params_t * params
Request parameters.
http_handler_t * handlers
int http_handler_next(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
void method_router_set_get_handler(http_handler_t *router, http_handler_t *handler)
int handler_send_reauthentication(http_connection_t *connection, int http_status_code, authentication_reason_t reason)
Allow for reauthentication of a user.
void http_handler_free(http_handler_t *handler)
Response information for commands.
#define MHD_HTTP_NOT_ACCEPTABLE
The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.
gchar * accept_language_to_env_fmt(const char *accept_language)
Convert an Accept-Language string to the LANGUAGE env variable form.
const gchar * user_get_username(user_t *user)
int params_mhd_add(void *params, enum MHD_ValueKind kind, const char *name, const char *value)
void method_router_set_post_handler(http_handler_t *router, http_handler_t *handler)
char * get_system_report_gmp(gvm_connection_t *connection, credentials_t *credentials, const char *url, params_t *params, cmd_response_data_t *response_data)
Return system report image.
int handle_setup_credentials(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
void gvm_validator_free(validator_t validator)
Free a validator.
User information structure, for sessions.
#define USER_IP_ADDRESS_MISSMATCH
int user_logout(user_t *user)
Logout a user.
url_map_t * url_map_new(const gchar *regexp, http_handler_t *handler)
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.
int handle_setup_user(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
http_handler_func_t handle
Structure of credential related information.
GSAD credentials handling.
int connectiontype
1=POST, 2=GET.
http_handler_t * url_handler_add_func(http_handler_t *handlers, const gchar *regexp, http_handler_func_t handle)
int exec_gmp_post(http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
http_handler_t * init_http_handlers()
#define POST_BUFFER_SIZE
Buffer size for POST processor.
validator_t gvm_validator_new()
Create a new validator.
int handle_invalid_method(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Headers for GSA's GMP communication module.
#define USER_BAD_MISSING_TOKEN
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.
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
http_handler_t * method_router_new()
int handle_validate(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
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.
Headers/structs for utility functions in GSAD.
int exec_gmp_get(http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
void params_mhd_validate(void *params)
int handle_static_config(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
#define UTF8_ERROR_PAGE(location)
void cmd_response_data_set_allow_caching(cmd_response_data_t *data, gboolean allow_caching)
Set allow_caching flag of cmd_response_data_t struct.
int handle_logout(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
#define USER_EXPIRED_TOKEN
int user_find(const gchar *cookie, const gchar *token, const char *address, user_t **user_return)
Find a user, given a token and cookie.
struct MHD_Connection http_connection_t
void gvm_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
validator_t http_validator
Validator instance for http params.
char * language
Language code e.g. en.
struct MHD_PostProcessor * postprocessor
POST processor.
http_handler_t * http_handler_add(http_handler_t *handlers, http_handler_t *next)
void url_handler_free(http_handler_t *handler)
int handle_url(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *current, void *data)
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
void cleanup_http_handlers()
int(* http_handler_func_t)(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
int handle_system_report(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
void http_handler_free_internal(http_handler_t *handler)
enum authentication_reason authentication_reason_t
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
credentials_t * credentials_new(user_t *user, const gchar *language)
I18n support for Greenbone Security Assistant.
void(* http_handler_free_func_t)(http_handler_t *)
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.
URL regexp to handler function mapping.
@ GSAD_CONTENT_TYPE_TEXT_HTML
int handle_get_post(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
void credentials_free(credentials_t *creds)
void user_free(user_t *user)
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
const gchar * user_get_language(user_t *user)
int gvm_validate(validator_t validator, const char *name, const char *value)
Validate a string for a given rule.
int get_user_from_connection(http_connection_t *connection, user_t **user)
int http_handler_start(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
struct MHD_Response http_response_t
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
#define USER_BAD_MISSING_COOKIE
int handle_gmp_get(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
#define BAD_REQUEST_PAGE
Bad request error HTML.
int handle_get_user(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
http_handler_t * url_handler_new(const gchar *regexp, http_handler_t *handler)
int manager_connect(credentials_t *credentials, gvm_connection_t *connection, cmd_response_data_t *response_data)
Connect to Greenbone Vulnerability Manager daemon.
params_t * params_new()
Make a params.
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 ERROR_PAGE
Error page HTML.
int handle_request(void *cls, http_connection_t *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
HTTP request handler for GSAD.
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.
void cmd_response_data_free(cmd_response_data_t *data)
Frees the memory of a cmd_response_data_t struct.
Headers/structs used generally in GSA.
GHashTable * validator_t
A set of name rule pairs.
Headers/structs for a string validator.
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.
void method_router_free(http_handler_t *handler)
int handle_static_file(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
int handle_index(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
http_handler_free_func_t free
http_handler_t * http_handler_new(http_handler_func_t func)