Update copyright for 2022
[pgsql.git] / contrib / auth_delay / auth_delay.c
blob38f4276db39660dc8551e715671d4c02ebbb285e
1 /* -------------------------------------------------------------------------
3 * auth_delay.c
5 * Copyright (c) 2010-2022, PostgreSQL Global Development Group
7 * IDENTIFICATION
8 * contrib/auth_delay/auth_delay.c
10 * -------------------------------------------------------------------------
12 #include "postgres.h"
14 #include <limits.h>
16 #include "libpq/auth.h"
17 #include "port.h"
18 #include "utils/guc.h"
19 #include "utils/timestamp.h"
21 PG_MODULE_MAGIC;
23 void _PG_init(void);
25 /* GUC Variables */
26 static int auth_delay_milliseconds;
28 /* Original Hook */
29 static ClientAuthentication_hook_type original_client_auth_hook = NULL;
32 * Check authentication
34 static void
35 auth_delay_checks(Port *port, int status)
38 * Any other plugins which use ClientAuthentication_hook.
40 if (original_client_auth_hook)
41 original_client_auth_hook(port, status);
44 * Inject a short delay if authentication failed.
46 if (status != STATUS_OK)
48 pg_usleep(1000L * auth_delay_milliseconds);
53 * Module Load Callback
55 void
56 _PG_init(void)
58 /* Define custom GUC variables */
59 DefineCustomIntVariable("auth_delay.milliseconds",
60 "Milliseconds to delay before reporting authentication failure",
61 NULL,
62 &auth_delay_milliseconds,
64 0, INT_MAX / 1000,
65 PGC_SIGHUP,
66 GUC_UNIT_MS,
67 NULL,
68 NULL,
69 NULL);
71 EmitWarningsOnPlaceholders("auth_delay");
73 /* Install Hooks */
74 original_client_auth_hook = ClientAuthentication_hook;
75 ClientAuthentication_hook = auth_delay_checks;