You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth_delay.c 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* -------------------------------------------------------------------------
  2. *
  3. * auth_delay.c
  4. *
  5. * Copyright (c) 2010-2019, PostgreSQL Global Development Group
  6. *
  7. * IDENTIFICATION
  8. * contrib/auth_delay/auth_delay.c
  9. *
  10. * -------------------------------------------------------------------------
  11. */
  12. #include "postgres.h"
  13. #include <limits.h>
  14. #include "libpq/auth.h"
  15. #include "port.h"
  16. #include "utils/guc.h"
  17. #include "utils/timestamp.h"
  18. PG_MODULE_MAGIC;
  19. void _PG_init(void);
  20. /* GUC Variables */
  21. static int auth_delay_milliseconds;
  22. /* Original Hook */
  23. static ClientAuthentication_hook_type original_client_auth_hook = NULL;
  24. /*
  25. * Check authentication
  26. */
  27. static void
  28. auth_delay_checks(Port *port, int status)
  29. {
  30. /*
  31. * Any other plugins which use ClientAuthentication_hook.
  32. */
  33. if (original_client_auth_hook)
  34. original_client_auth_hook(port, status);
  35. /*
  36. * Inject a short delay if authentication failed.
  37. */
  38. if (status != STATUS_OK)
  39. {
  40. pg_usleep(1000L * auth_delay_milliseconds);
  41. }
  42. }
  43. /*
  44. * Module Load Callback
  45. */
  46. void
  47. _PG_init(void)
  48. {
  49. /* Define custom GUC variables */
  50. DefineCustomIntVariable("auth_delay.milliseconds",
  51. "Milliseconds to delay before reporting authentication failure",
  52. NULL,
  53. &auth_delay_milliseconds,
  54. 0,
  55. 0, INT_MAX / 1000,
  56. PGC_SIGHUP,
  57. GUC_UNIT_MS,
  58. NULL,
  59. NULL,
  60. NULL);
  61. /* Install Hooks */
  62. original_client_auth_hook = ClientAuthentication_hook;
  63. ClientAuthentication_hook = auth_delay_checks;
  64. }