Greenbone Security Assistant  8.0.0
gsad_http_handler.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_handler.h"
26 
27 #include "gsad_base.h" /* for ctime_r_strip_newline */
28 #include "gsad_credentials.h" /* for credentials_t */
29 #include "gsad_gmp.h" /* for get_system_report_gmp */
30 #include "gsad_i18n.h" /* for accept_language_to_env_fmt */
31 #include "gsad_settings.h" /* for get_guest_usernmae */
32 #include "gsad_user.h" /* for user_t */
33 #include "validator.h" /* for gvm_validate */
34 
35 #include <assert.h> /* for assert */
36 #include <glib.h> /* for g_file_test */
37 #include <gvm/base/networking.h> /* for INET6_ADDRSTRLEN */
38 #include <gvm/util/xmlutils.h> /* for find_element_in_xml_file */
39 #include <stdlib.h> /* for abort */
40 #include <string.h> /* for strcmp */
41 
42 #undef G_LOG_DOMAIN
43 
46 #define G_LOG_DOMAIN "gsad http handler"
47 
53 #define TOKEN_REGEXP "^[a-z0-9\\-]+$"
54 #define SLAVE_ID_REGEXP "^[a-z0-9\\-]+$"
55 
60 
67 struct url_map
68 {
69  GRegex *gregexp;
71 };
72 
73 typedef struct url_map url_map_t;
74 
76 {
80  void *data;
81 };
82 
84 {
87 };
88 
90 
92 
95 {
96  http_handler_t *handler = handlers;
97 
98  if (handler == NULL)
99  {
100  return next;
101  }
102 
103  while (handler->next != NULL)
104  {
105  handler = handler->next;
106  }
107 
108  handler->next = next;
109 
110  return handlers;
111 }
112 
113 int
114 http_handler_start (http_connection_t *connection, const char *method,
115  const char *url, gsad_connection_info_t *con_info,
116  http_handler_t *handler, void *data)
117 {
118  if (handler == NULL)
119  {
120  return MHD_NO;
121  }
122  return handler->handle (connection, method, url, con_info, handler, data);
123 }
124 
125 int
126 http_handler_next (http_connection_t *connection, const char *method,
127  const char *url, gsad_connection_info_t *con_info,
128  http_handler_t *handler, void *data)
129 {
130  http_handler_t *next;
131 
132  if (handler == NULL || handler->next == NULL)
133  {
134  return MHD_NO;
135  }
136 
137  next = handler->next;
138  return next->handle (connection, method, url, con_info, next, data);
139 }
140 
143  http_handler_free_func_t freefunc, void *data)
144 {
145  http_handler_t *handler = g_malloc0 (sizeof (http_handler_t));
146  handler->handle = func;
147  handler->free = freefunc;
148  handler->data = data;
149  handler->next = NULL;
150  return handler;
151 }
152 
153 void
155 {
156  g_free (handler);
157 }
158 
161 {
163 }
164 
165 void
167 {
168  if (!handler)
169  return;
170 
171  if (handler->next)
172  {
173  // free the chain
174  http_handler_free (handler->next);
175  }
176 
177  handler->free (handler);
178 }
179 
180 int
181 handle_get_post (http_connection_t *connection, const char *method,
182  const char *url, gsad_connection_info_t *con_info,
183  http_handler_t *handler, void *data)
184 {
185  method_router_t *routes = (method_router_t *) handler->data;
186 
187  if (!strcmp (method, "GET"))
188  {
189  g_debug ("method router handling GET");
190  return http_handler_start (connection, method, url, con_info, routes->get,
191  data);
192  }
193  if (!strcmp (method, "POST"))
194  {
195  g_debug ("method router handling POST");
196  return http_handler_start (connection, method, url, con_info,
197  routes->post, data);
198  }
199  return http_handler_next (connection, method, url, con_info, handler, data);
200 }
201 
202 void
204 {
205  method_router_t *routes = (method_router_t *) handler->data;
206 
207  http_handler_free (routes->get);
208  http_handler_free (routes->post);
209 
210  g_free (routes);
211 
212  http_handler_free_internal (handler);
213 }
214 
217 {
218  method_router_t *router = g_malloc0 (sizeof (method_router_t));
219  router->get = NULL;
220  router->post = NULL;
222  router);
223 }
224 
225 void
227 {
229  method_router->get = handler;
230 }
231 
232 void
234 {
236  method_router->post = handler;
237 }
238 
239 int
240 handle_url (http_connection_t *connection, const char *method, const char *url,
241  gsad_connection_info_t *con_info, http_handler_t *current,
242  void *data)
243 {
244  url_map_t *map = (url_map_t *) current->data;
245 
246  g_debug ("checking url map for url %s against %s\n", url,
247  g_regex_get_pattern (map->gregexp));
248 
249  if (g_regex_match (map->gregexp, url, 0, NULL))
250  {
251  g_debug ("Found url handler for url %s\n", url);
252 
253  return http_handler_start (connection, method, url, con_info,
254  map->handler, data);
255  }
256 
257  return http_handler_next (connection, method, url, con_info, current, data);
258 }
259 
260 url_map_t *
261 url_map_new (const gchar *regexp, http_handler_t *handler)
262 {
263  url_map_t *map = g_malloc0 (sizeof (url_map_t));
264  map->gregexp = g_regex_new (regexp, 0, 0, NULL);
265  map->handler = handler;
266  return map;
267 }
268 
269 void
271 {
272  url_map_t *map = (url_map_t *) handler->data;
273 
274  g_regex_unref (map->gregexp);
275  http_handler_free (map->handler); /* free the chain */
276  g_free (map);
277 
278  http_handler_free_internal (handler);
279 }
280 
282 url_handler_new (const gchar *regexp, http_handler_t *handler)
283 {
284  url_map_t *map = url_map_new (regexp, handler);
286 }
287 
290  http_handler_func_t handle)
291 {
292  http_handler_t *handler = http_handler_new (handle);
293  http_handler_t *url_handler = url_handler_new (regexp, handler);
294  return http_handler_add (handlers, url_handler);
295 }
296 
297 int
298 handle_validate (http_connection_t *connection, const char *method,
299  const char *url, gsad_connection_info_t *con_info,
300  http_handler_t *handler, void *data)
301 {
302  g_debug ("Validating url %s", url);
303 
304  /* If called with undefined URL, abort request handler. */
305  if (&url[0] == NULL)
306  {
308  NULL, GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
309  return MHD_YES;
310  }
311 
312  /* Prevent guest link from leading to URL redirection. */
313  if (url && (url[0] == '/') && (url[1] == '/'))
314  {
315  return handler_send_not_found (connection, url);
316  }
317 
318  /* Many Glib functions require valid UTF-8. */
319  if (url && (g_utf8_validate (url, -1, NULL) == FALSE))
320  {
321  send_response (connection, UTF8_ERROR_PAGE ("URL"), MHD_HTTP_BAD_REQUEST,
322  NULL, GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
323  return MHD_YES;
324  }
325 
326  return http_handler_next (connection, method, url, con_info, handler, data);
327 }
328 
329 int
330 handle_not_found (http_connection_t *connection, const char *method,
331  const char *url, gsad_connection_info_t *con_info,
332  http_handler_t *handler, void *data)
333 {
334  return handler_send_not_found (connection, url);
335 }
336 
337 int
338 handle_invalid_method (http_connection_t *connection, const char *method,
339  const char *url, gsad_connection_info_t *con_info,
340  http_handler_t *handler, void *data)
341 {
342  /* Only accept GET and POST methods and send ERROR_PAGE in other cases. */
343  if (strcmp (method, "GET") && strcmp (method, "POST"))
344  {
345  send_response (connection, ERROR_PAGE, MHD_HTTP_METHOD_NOT_ALLOWED, NULL,
346  GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
347  return MHD_YES;
348  }
349 
350  return http_handler_next (connection, method, url, con_info, handler, data);
351 }
352 
353 int
355 {
356  const gchar *cookie;
357  const gchar *token;
358  gchar client_address[INET6_ADDRSTRLEN];
359  int ret;
360 
361  token =
362  MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "token");
363  if (token == NULL)
364  {
365  return USER_BAD_MISSING_TOKEN;
366  }
367 
368  if (gvm_validate (http_validator, "token", token))
369  {
370  return USER_BAD_MISSING_TOKEN;
371  }
372 
373  cookie =
374  MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, SID_COOKIE_NAME);
375 
376  if (gvm_validate (http_validator, "token", cookie))
377  {
379  }
380 
381  ret = get_client_address (connection, client_address);
382  if (ret == 1)
383  {
385  }
386 
387  return user_find (cookie, token, client_address, user);
388 }
389 
390 int
391 handle_get_user (http_connection_t *connection, const char *method,
392  const char *url, gsad_connection_info_t *con_info,
393  http_handler_t *handler, void *data)
394 {
395  user_t *user = NULL;
396  get_user_from_connection (connection, &user);
397  return http_handler_next (connection, method, url, con_info, handler, user);
398 }
399 
400 int
401 handle_setup_user (http_connection_t *connection, const char *method,
402  const char *url, gsad_connection_info_t *con_info,
403  http_handler_t *handler, void *data)
404 {
405  int ret;
406  int http_response_code = MHD_HTTP_OK;
407  authentication_reason_t auth_reason;
408 
409  user_t *user;
410 
411  ret = get_user_from_connection (connection, &user);
412 
413  if (ret == USER_GUEST_LOGIN_FAILED || ret == USER_GMP_DOWN
414  || ret == USER_GUEST_LOGIN_ERROR)
415  {
416  auth_reason =
417  ret == USER_GMP_DOWN
420 
422  connection, MHD_HTTP_SERVICE_UNAVAILABLE, auth_reason);
423  }
424 
425  if ((ret == USER_EXPIRED_TOKEN) || (ret == USER_BAD_MISSING_COOKIE)
426  || (ret == USER_BAD_MISSING_TOKEN) || (ret == USER_IP_ADDRESS_MISSMATCH))
427  {
428  if (ret == USER_EXPIRED_TOKEN)
429  {
430  if (strncmp (url, LOGOUT_URL, strlen (LOGOUT_URL)))
431  http_response_code = MHD_HTTP_UNAUTHORIZED;
432  else
433  http_response_code = MHD_HTTP_BAD_REQUEST;
434  }
435  else
436  http_response_code = MHD_HTTP_UNAUTHORIZED;
437 
438  auth_reason =
439  (ret == USER_EXPIRED_TOKEN)
440  ? (strncmp (url, LOGOUT_URL, strlen (LOGOUT_URL)) ? SESSION_EXPIRED
441  : LOGOUT_ALREADY)
444 
445  return handler_send_reauthentication (connection, http_response_code,
446  auth_reason);
447  }
448 
449  if (ret)
450  abort ();
451 
452  g_debug ("Found user %s\n", user_get_username (user));
453 
454  return http_handler_next (connection, method, url, con_info, handler, user);
455 }
456 
457 int
458 handle_setup_credentials (http_connection_t *connection, const char *method,
459  const char *url, gsad_connection_info_t *con_info,
460  http_handler_t *handler, void *data)
461 {
462  user_t *user = (user_t *) data;
463  const gchar *accept_language;
465  char client_address[INET6_ADDRSTRLEN];
466 
467  get_client_address (connection, client_address);
468 
469  gchar *language = g_strdup (user_get_language (user));
470 
471  if (!language)
472  /* Accept-Language: de; q=1.0, en; q=0.5 */
473  {
474  accept_language = MHD_lookup_connection_value (
475  connection, MHD_HEADER_KIND, "Accept-Language");
476  if (accept_language
477  && g_utf8_validate (accept_language, -1, NULL) == FALSE)
478  {
479  send_response (
480  connection, UTF8_ERROR_PAGE ("'Accept-Language' header"),
481  MHD_HTTP_BAD_REQUEST, NULL, GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
482  return MHD_YES;
483  }
484  language = accept_language_to_env_fmt (accept_language);
485  credentials = credentials_new (user, language);
486  }
487  else
488  {
489  credentials = credentials_new (user, language);
490  }
491 
492  user_free (user);
493  g_free (language);
494 
495  return http_handler_next (connection, method, url, con_info, handler,
496  credentials);
497 }
498 
499 int
500 handle_logout (http_connection_t *connection, const char *method,
501  const char *url, gsad_connection_info_t *con_info,
502  http_handler_t *handler, void *data)
503 {
504  user_t *user = (user_t *) data;
505 
506  if (user != NULL)
507  {
508  user_logout (user);
509 
510  g_debug ("Logged out user %s\n", user_get_username (user));
511 
512  user_free (user);
513  }
514  return send_response (connection, "", MHD_HTTP_OK, REMOVE_SID,
515  GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
516 }
517 
518 int
519 handle_gmp_get (http_connection_t *connection, const char *method,
520  const char *url, gsad_connection_info_t *con_info,
521  http_handler_t *handler, void *data)
522 {
523  /* URL requests to run GMP command. */
524  int ret;
526 
527  ret = exec_gmp_get (connection, con_info, credentials);
528 
530  return ret;
531 }
532 
533 int
534 handle_gmp_post (http_connection_t *connection, const char *method,
535  const char *url, gsad_connection_info_t *con_info,
536  http_handler_t *handler, void *data)
537 {
538  const gchar *sid, *accept_language;
539  int ret;
540  char client_address[INET6_ADDRSTRLEN];
541 
542  sid =
543  MHD_lookup_connection_value (connection, MHD_COOKIE_KIND, SID_COOKIE_NAME);
544 
545  if (gvm_validate (http_validator, "token", sid))
546  con_info->cookie = NULL;
547  else
548  con_info->cookie = g_strdup (sid);
549 
550  accept_language = MHD_lookup_connection_value (connection, MHD_HEADER_KIND,
551  "Accept-Language");
552  if (accept_language && g_utf8_validate (accept_language, -1, NULL) == FALSE)
553  {
554  send_response (connection, UTF8_ERROR_PAGE ("'Accept-Language' header"),
555  MHD_HTTP_BAD_REQUEST, NULL, GSAD_CONTENT_TYPE_TEXT_HTML,
556  NULL, 0);
557  return MHD_YES;
558  }
559  con_info->language = accept_language_to_env_fmt (accept_language);
560 
561  get_client_address (connection, client_address);
562 
563  /* FIXME why is get_client_address called twice? it was already called twice
564  in gsad.c handle_request function */
565  ret = get_client_address (connection, client_address);
566  if (ret == 1)
567  {
568  send_response (connection, UTF8_ERROR_PAGE ("'X-Real-IP' header"),
569  MHD_HTTP_BAD_REQUEST, NULL, GSAD_CONTENT_TYPE_TEXT_HTML,
570  NULL, 0);
571  return MHD_YES;
572  }
573 
574  return exec_gmp_post (connection, con_info, client_address);
575 }
576 
577 int
578 handle_system_report (http_connection_t *connection, const char *method,
579  const char *url, gsad_connection_info_t *con_info,
580  http_handler_t *handler, void *data)
581 {
582  params_t *params = params_new ();
584  const char *slave_id;
585  char *res;
586  gvm_connection_t con;
587  cmd_response_data_t *response_data;
588 
589  g_debug ("Request for system report url %s", url);
590 
591  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, params_mhd_add,
592  params);
593 
594  params_mhd_validate (params);
595 
596  slave_id =
597  MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "slave_id");
598 
599  if (slave_id && gvm_validate (http_validator, "slave_id", slave_id))
600  {
602  g_warning ("%s: failed to validate slave_id, dropping request",
603  __FUNCTION__);
604  return MHD_NO;
605  }
606 
607  response_data = cmd_response_data_new ();
608 
609  /* Connect to manager */
610  switch (manager_connect (credentials, &con, response_data))
611  {
612  case 0:
613  res = get_system_report_gmp (&con, credentials,
614  &url[0] + strlen ("/system_report/"), params,
615  response_data);
616  gvm_connection_close (&con);
617  break;
618  case -1:
620  connection, MHD_HTTP_SERVICE_UNAVAILABLE, GMP_SERVICE_DOWN);
621 
622  break;
623  case -2:
624  res = gsad_message (credentials, "Internal error", __FUNCTION__, __LINE__,
625  "An internal error occurred. "
626  "Diagnostics: Could not authenticate to manager "
627  "daemon.",
628  response_data);
629  break;
630  default:
631  res = gsad_message (credentials, "Internal error", __FUNCTION__, __LINE__,
632  "An internal error occurred. "
633  "Diagnostics: Failure to connect to manager daemon.",
634  response_data);
635  break;
636  }
637 
638  if (res == NULL)
639  {
641  g_warning ("%s: failed to get system reports, dropping request",
642  __FUNCTION__);
643  cmd_response_data_free (response_data);
644  return MHD_NO;
645  }
646 
648 
649  return handler_create_response (connection, res, response_data, NULL);
650 }
651 
652 int
653 handle_index (http_connection_t *connection, const char *method,
654  const char *url, gsad_connection_info_t *con_info,
655  http_handler_t *handler, void *data)
656 {
657  http_response_t *response;
658  cmd_response_data_t *response_data;
659 
660  response_data = cmd_response_data_new ();
661  cmd_response_data_set_allow_caching (response_data, 1);
662 
663  response =
664  file_content_response (connection, url, "index.html", response_data);
665  return handler_send_response (connection, response, response_data, NULL);
666 }
667 
668 int
669 handle_static_file (http_connection_t *connection, const char *method,
670  const char *url, gsad_connection_info_t *con_info,
671  http_handler_t *handler, void *data)
672 {
673  gchar *path;
674  http_response_t *response;
675  char *default_file = "index.html";
676  cmd_response_data_t *response_data;
677 
681  /* Attempt to prevent disclosing non-gsa content. */
682  if (strstr (url, ".."))
683  path = g_strconcat (default_file, NULL);
684  else
685  {
686  /* Ensure that url is relative. */
687  const char *relative_url = url;
688  if (*url == '/')
689  relative_url = url + 1;
690  path = g_strconcat (relative_url, NULL);
691  }
692 
693  g_debug ("Requesting url %s for static ng path %s", url, path);
694 
695  response_data = cmd_response_data_new ();
696  cmd_response_data_set_allow_caching (response_data, 1);
697 
698  response = file_content_response (connection, url, path, response_data);
699 
700  g_free (path);
701 
702  return handler_send_response (connection, response, response_data, NULL);
703 }
704 
705 int
706 handle_static_config (http_connection_t *connection, const char *method,
707  const char *url, gsad_connection_info_t *con_info,
708  http_handler_t *handler, void *data)
709 {
710  gchar *path;
711  http_response_t *response;
712  cmd_response_data_t *response_data;
713 
714  /* Ensure that url is relative. */
715  const char *relative_url = url;
716  if (*url == '/')
717  {
718  relative_url = url + 1;
719  }
720  path = g_strconcat (relative_url, NULL);
721 
722  g_debug ("Requesting url %s for static config path %s", url, path);
723 
724  response_data = cmd_response_data_new ();
725  cmd_response_data_set_allow_caching (response_data, 1);
726 
727  if (g_file_test (path, G_FILE_TEST_EXISTS))
728  {
729  response = file_content_response (connection, url, path, response_data);
730  g_free (path);
731  return handler_send_response (connection, response, response_data, NULL);
732  }
733 
734  g_free (path);
735 
736  // send empty config
737  cmd_response_data_set_status_code (response_data, MHD_HTTP_OK);
738  return handler_create_response (connection, g_strdup (""), response_data,
739  NULL);
740 }
741 
744 {
746  http_handler_t *not_found_handler;
747  http_handler_t *gmp_post_handler;
748  http_handler_t *url_handlers;
749 
753 
755 
757  not_found_handler = http_handler_new (handle_not_found);
758  gmp_post_handler = http_handler_new (handle_gmp_post);
759 
761 
762  url_handlers = url_handler_new ("^/(img|js|css|locales)/.+$",
764  url_handler_add_func (url_handlers, "^/robots.txt$", handle_static_file);
765 
766  url_handler_add_func (url_handlers, "^/config.*js$", handle_static_config);
767  url_handler_add_func (url_handlers, "^/static/(img|js|css)/.+$",
769 
773  http_handler_t *gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler);
774 
775  http_handler_t *system_report_handler = http_handler_new (handle_setup_user);
776  http_handler_add (system_report_handler,
778  http_handler_add (system_report_handler,
780  http_handler_t *system_report_url_handler =
781  url_handler_new ("^/system_report/.+$", system_report_handler);
782 
783  http_handler_t *logout_handler = http_handler_new (handle_get_user);
784  http_handler_add (logout_handler, http_handler_new (handle_logout));
785  http_handler_t *logout_url_handler =
786  url_handler_new ("^/logout/?$", logout_handler);
787 
788  http_handler_add (url_handlers, gmp_url_handler);
789  http_handler_add (url_handlers, system_report_url_handler);
790  http_handler_add (url_handlers, logout_url_handler);
791 
792  url_handler_add_func (url_handlers, "^/.*$", handle_index);
793  http_handler_add (url_handlers, not_found_handler);
794 
796  method_router_set_post_handler (method_router, gmp_post_handler);
797 
799 
800  return handlers;
801 }
802 
803 void
805 {
806  g_debug ("Cleaning up http handlers");
807 
809 
811 }
812 
831 int
832 handle_request (void *cls, http_connection_t *connection, const char *url,
833  const char *method, const char *version,
834  const char *upload_data, size_t *upload_data_size,
835  void **con_cls)
836 {
837  gsad_connection_info_t *con_info;
839  char *full_url;
840 
841  handlers = (http_handler_t *) cls;
842  con_info = *con_cls;
843 
844  /* Never respond on first call of a GET. */
845  if ((!strcmp (method, "GET")) && *con_cls == NULL)
846  {
847  /* First call for this request, a GET. */
848 
849  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
850  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
851  con_info->params = params_new ();
852  con_info->connectiontype = 2;
853 
854  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
855  params_mhd_add, con_info->params);
856 
857  params_mhd_validate (con_info->params);
858 
859  *con_cls = (void *) con_info;
860  return MHD_YES;
861  }
862 
863  if (!strcmp (method, "POST"))
864  {
865  if (NULL == *con_cls)
866  {
867  /* First call for this request, a POST. */
868 
869  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
870  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
871 
872  con_info->postprocessor = MHD_create_post_processor (
873  connection, POST_BUFFER_SIZE, serve_post, (void *) con_info);
874  if (NULL == con_info->postprocessor)
875  {
876  g_free (con_info);
877  /* Both bad request or running out of memory will lead here, but
878  * we return the Bad Request page always, to prevent bad requests
879  * from leading to "Internal application error" in the log. */
880  send_response (connection, BAD_REQUEST_PAGE,
882  GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
883  return MHD_YES;
884  }
885  con_info->params = params_new ();
886  con_info->connectiontype = 1;
887 
888  *con_cls = (void *) con_info;
889  return MHD_YES;
890  }
891 
892  /* Second or later call for this request, a POST. */
893 
894  if (0 != *upload_data_size)
895  {
896  MHD_post_process (con_info->postprocessor, upload_data,
897  *upload_data_size);
898  *upload_data_size = 0;
899  return MHD_YES;
900  }
901  }
902 
903  full_url = reconstruct_url (connection, url);
904  g_debug ("============= url: %s\n", full_url);
905  g_free (full_url);
906 
907  if (handlers != NULL)
908  {
909  return http_handler_start (connection, method, url, con_info, handlers,
910  NULL);
911  }
912 
913  return MHD_NO;
914 }
915 
916 /* vim: set ts=2 sw=2 tw=80:*/
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:459
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.
Definition: gsad_i18n.c:62
#define POST_BUFFER_SIZE
Buffer size for POST processor.
Definition: gsad_http.h:101
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.
Definition: gsad_http.h:128
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.
Definition: gsad_gmp.c:12328
int gvm_validate(validator_t validator, const char *name, const char *value)
Validate a string for a given rule.
Definition: validator.c:197
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.
Definition: gsad_cmd.c:108
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.
Definition: gsad_http.c:438
void user_free(user_t *user)
Definition: gsad_user.c:102
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)
http_handler_t * get
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:157
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:131
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
Global settings for GSA.
#define USER_BAD_MISSING_COOKIE
Definition: gsad_user.h:35
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:899
struct MHD_Connection http_connection_t
Definition: gsad_http.h:135
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)
Definition: gsad_http.h:49
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.
Definition: gsad_cmd.c:33
#define USER_GMP_DOWN
Definition: gsad_user.h:38
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:171
http_handler_t * init_http_handlers()
void gvm_validator_free(validator_t validator)
Free a validator.
Definition: validator.c:266
Headers for GSA's GMP communication module.
Connection information.
Definition: gsad_http.h:125
Headers/structs used generally in GSA.
void(* http_handler_free_func_t)(http_handler_t *)
#define SLAVE_ID_REGEXP
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.
Definition: gsad_http.c:806
int get_user_from_connection(http_connection_t *connection, user_t **user)
params_t * params_new()
Make a params.
Definition: gsad_base.c:147
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.
Definition: gsad_http.c:694
#define USER_GUEST_LOGIN_ERROR
Definition: gsad_user.h:40
#define USER_GUEST_LOGIN_FAILED
Definition: gsad_user.h:37
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
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.
Definition: gsad_cmd.c:81
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:127
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:978
credentials_t * credentials_new(user_t *user, const gchar *language)
#define ERROR_PAGE
Error page HTML.
Definition: gsad_http.h:75
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.
Definition: gsad_http.c:295
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.
Definition: gsad_http.h:130
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:948
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.
Definition: gsad_http.c:368
#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
int manager_connect(credentials_t *credentials, gvm_connection_t *connection, cmd_response_data_t *response_data)
Connect to Greenbone Vulnerability Manager daemon.
Definition: gsad_gmp.c:19475
http_handler_t * next
#define USER_IP_ADDRESS_MISSMATCH
Definition: gsad_user.h:39
User information structure, for sessions.
Definition: gsad_user.c:42
GSAD user handling.
#define BAD_REQUEST_PAGE
Bad request error HTML.
Definition: gsad_http.h:80
void credentials_free(credentials_t *creds)
struct MHD_Response http_response_t
Definition: gsad_http.h:137
int user_find(const gchar *cookie, const gchar *token, const char *address, user_t **user_return)
Find a user, given a token and cookie.
Definition: gsad_user.c:411
http_handler_t * handlers
http_handler_t * post
GHashTable * validator_t
A set of name rule pairs.
Definition: validator.h:33
void http_handler_free(http_handler_t *handler)
validator_t gvm_validator_new()
Create a new validator.
Definition: validator.c:102
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)
#define params_t
Definition: gsad_params.h:31
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.
Definition: validator.c:116
#define USER_BAD_MISSING_TOKEN
Definition: gsad_user.h:36
void http_handler_free_internal(http_handler_t *handler)
#define TOKEN_REGEXP
#define REMOVE_SID
Definition: gsad_http.h:60
const gchar * user_get_language(user_t *user)
Definition: gsad_user.c:163
char * cookie
Value of SID cookie param.
Definition: gsad_http.h:129
#define LOGOUT_URL
Definition: gsad_http.h:96
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
Definition: gsad_user.h:34
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
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
Definition: gsad_http.h:58
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)
GRegex * gregexp
int user_logout(user_t *user)
Logout a user.
Definition: gsad_user.c:325