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