37 #include <gvm/base/networking.h> 38 #include <gvm/util/xmlutils.h> 46 #define G_LOG_DOMAIN "gsad http handler" 53 #define TOKEN_REGEXP "^[a-z0-9\\-]+$" 54 #define SLAVE_ID_REGEXP "^[a-z0-9\\-]+$" 103 while (handler->
next != NULL)
105 handler = handler->
next;
108 handler->
next = next;
122 return handler->
handle (connection, method, url, con_info, handler, data);
132 if (handler == NULL || handler->
next == NULL)
137 next = handler->
next;
138 return next->
handle (connection, method, url, con_info, next, data);
147 handler->
free = freefunc;
148 handler->
data = data;
149 handler->
next = NULL;
177 handler->
free (handler);
187 if (!strcmp (method,
"GET"))
189 g_debug (
"method router handling GET");
193 if (!strcmp (method,
"POST"))
195 g_debug (
"method router handling POST");
246 g_debug (
"checking url map for url %s against %s\n", url,
247 g_regex_get_pattern (map->
gregexp));
249 if (g_regex_match (map->
gregexp, url, 0, NULL))
251 g_debug (
"Found url handler for url %s\n", url);
264 map->
gregexp = g_regex_new (regexp, 0, 0, NULL);
302 g_debug (
"Validating url %s", url);
313 if (url && (url[0] ==
'/') && (url[1] ==
'/'))
319 if (url && (g_utf8_validate (url, -1, NULL) == FALSE))
343 if (strcmp (method,
"GET") && strcmp (method,
"POST"))
358 gchar client_address[INET6_ADDRSTRLEN];
362 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND,
"token");
374 MHD_lookup_connection_value (connection, MHD_COOKIE_KIND,
SID_COOKIE_NAME);
406 int http_response_code = MHD_HTTP_OK;
422 connection, MHD_HTTP_SERVICE_UNAVAILABLE, auth_reason);
431 http_response_code = MHD_HTTP_UNAUTHORIZED;
433 http_response_code = MHD_HTTP_BAD_REQUEST;
436 http_response_code = MHD_HTTP_UNAUTHORIZED;
463 const gchar *accept_language;
465 char client_address[INET6_ADDRSTRLEN];
474 accept_language = MHD_lookup_connection_value (
475 connection, MHD_HEADER_KIND,
"Accept-Language");
477 && g_utf8_validate (accept_language, -1, NULL) == FALSE)
538 const gchar *sid, *accept_language;
540 char client_address[INET6_ADDRSTRLEN];
543 MHD_lookup_connection_value (connection, MHD_COOKIE_KIND,
SID_COOKIE_NAME);
548 con_info->
cookie = g_strdup (sid);
550 accept_language = MHD_lookup_connection_value (connection, MHD_HEADER_KIND,
552 if (accept_language && g_utf8_validate (accept_language, -1, NULL) == FALSE)
584 const char *slave_id;
586 gvm_connection_t con;
589 g_debug (
"Request for system report url %s", url);
591 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
params_mhd_add,
597 MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND,
"slave_id");
602 g_warning (
"%s: failed to validate slave_id, dropping request",
614 &url[0] + strlen (
"/system_report/"), params,
616 gvm_connection_close (&con);
625 "An internal error occurred. " 626 "Diagnostics: Could not authenticate to manager " 632 "An internal error occurred. " 633 "Diagnostics: Failure to connect to manager daemon.",
641 g_warning (
"%s: failed to get system reports, dropping request",
675 char *default_file =
"index.html";
682 if (strstr (url,
".."))
683 path = g_strconcat (default_file, NULL);
687 const char *relative_url = url;
689 relative_url = url + 1;
690 path = g_strconcat (relative_url, NULL);
693 g_debug (
"Requesting url %s for static ng path %s", url, path);
715 const char *relative_url = url;
718 relative_url = url + 1;
720 path = g_strconcat (relative_url, NULL);
722 g_debug (
"Requesting url %s for static config path %s", url, path);
727 if (g_file_test (path, G_FILE_TEST_EXISTS))
806 g_debug (
"Cleaning up http handlers");
833 const char *method,
const char *version,
834 const char *upload_data,
size_t *upload_data_size,
845 if ((!strcmp (method,
"GET")) && *con_cls == NULL)
854 MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
859 *con_cls = (
void *) con_info;
863 if (!strcmp (method,
"POST"))
865 if (NULL == *con_cls)
874 if (NULL == con_info->postprocessor)
886 con_info->connectiontype = 1;
888 *con_cls = (
void *) con_info;
894 if (0 != *upload_data_size)
898 *upload_data_size = 0;
904 g_debug (
"============= url: %s\n", full_url);
int handler_send_reauthentication(http_connection_t *connection, int http_status_code, authentication_reason_t reason)
Allow for reauthentication of a user.
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.
http_handler_func_t handle
int handle_not_found(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
gchar * accept_language_to_env_fmt(const char *accept_language)
Convert an Accept-Language string to the LANGUAGE env variable form.
#define POST_BUFFER_SIZE
Buffer size for POST processor.
GSAD credentials handling.
Structure of credential related information.
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
http_handler_t * url_handler_add_func(http_handler_t *handlers, const gchar *regexp, http_handler_func_t handle)
I18n support for Greenbone Security Assistant.
http_handler_free_func_t free
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)
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)
params_t * params
Request parameters.
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 gvm_validate(validator_t validator, const char *name, const char *value)
Validate a string for a given rule.
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_index(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
int handler_send_not_found(http_connection_t *connection, const gchar *url)
Send a 404 response for a request.
void user_free(user_t *user)
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 handle_url(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *current, void *data)
const gchar * user_get_username(user_t *user)
int connectiontype
1=POST, 2=GET.
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
#define USER_BAD_MISSING_COOKIE
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
struct MHD_Connection http_connection_t
int exec_gmp_get(http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
validator_t http_validator
Validator instance for http params.
#define UTF8_ERROR_PAGE(location)
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)
http_handler_t * http_handler_add(http_handler_t *handlers, http_handler_t *next)
void cleanup_http_handlers()
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)
Headers/structs for a string validator.
Response information for commands.
enum authentication_reason authentication_reason_t
http_handler_t * init_http_handlers()
void gvm_validator_free(validator_t validator)
Free a validator.
Headers for GSA's GMP communication module.
Headers/structs used generally in GSA.
void(* http_handler_free_func_t)(http_handler_t *)
URL regexp to handler function mapping.
http_handler_t * url_handler_new(const gchar *regexp, http_handler_t *handler)
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
int get_user_from_connection(http_connection_t *connection, user_t **user)
params_t * params_new()
Make a params.
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)
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.
#define USER_GUEST_LOGIN_ERROR
#define USER_GUEST_LOGIN_FAILED
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.
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)
void cmd_response_data_free(cmd_response_data_t *data)
Frees the memory of a cmd_response_data_t struct.
struct MHD_PostProcessor * postprocessor
POST processor.
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.
credentials_t * credentials_new(user_t *user, const gchar *language)
#define ERROR_PAGE
Error page 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)
int exec_gmp_post(http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
http_handler_t * method_router_new()
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 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)
char * language
Language code e.g. en.
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)
void params_mhd_validate(void *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 MHD_HTTP_NOT_ACCEPTABLE
The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.
int manager_connect(credentials_t *credentials, gvm_connection_t *connection, cmd_response_data_t *response_data)
Connect to Greenbone Vulnerability Manager daemon.
#define USER_IP_ADDRESS_MISSMATCH
User information structure, for sessions.
#define BAD_REQUEST_PAGE
Bad request error HTML.
void credentials_free(credentials_t *creds)
struct MHD_Response http_response_t
int user_find(const gchar *cookie, const gchar *token, const char *address, user_t **user_return)
Find a user, given a token and cookie.
http_handler_t * handlers
GHashTable * validator_t
A set of name rule pairs.
void http_handler_free(http_handler_t *handler)
validator_t gvm_validator_new()
Create a new validator.
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)
void url_handler_free(http_handler_t *handler)
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_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 gvm_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
#define USER_BAD_MISSING_TOKEN
void http_handler_free_internal(http_handler_t *handler)
const gchar * user_get_language(user_t *user)
char * cookie
Value of SID cookie param.
int params_mhd_add(void *params, enum MHD_ValueKind kind, const char *name, const char *value)
http_handler_t * http_handler_new(http_handler_func_t func)
#define USER_EXPIRED_TOKEN
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.
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)
url_map_t * url_map_new(const gchar *regexp, http_handler_t *handler)
void method_router_set_get_handler(http_handler_t *router, http_handler_t *handler)
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)
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)
void method_router_set_post_handler(http_handler_t *router, http_handler_t *handler)
int user_logout(user_t *user)
Logout a user.