Header And Logo

PostgreSQL
| The world's most advanced open source database.

auth_delay.c

Go to the documentation of this file.
00001 /* -------------------------------------------------------------------------
00002  *
00003  * auth_delay.c
00004  *
00005  * Copyright (C) 2010-2011, PostgreSQL Global Development Group
00006  *
00007  * IDENTIFICATION
00008  *      contrib/auth_delay/auth_delay.c
00009  *
00010  * -------------------------------------------------------------------------
00011  */
00012 #include "postgres.h"
00013 
00014 #include "libpq/auth.h"
00015 #include "port.h"
00016 #include "utils/guc.h"
00017 #include "utils/timestamp.h"
00018 
00019 PG_MODULE_MAGIC;
00020 
00021 void        _PG_init(void);
00022 
00023 /* GUC Variables */
00024 static int  auth_delay_milliseconds;
00025 
00026 /* Original Hook */
00027 static ClientAuthentication_hook_type original_client_auth_hook = NULL;
00028 
00029 /*
00030  * Check authentication
00031  */
00032 static void
00033 auth_delay_checks(Port *port, int status)
00034 {
00035     /*
00036      * Any other plugins which use ClientAuthentication_hook.
00037      */
00038     if (original_client_auth_hook)
00039         original_client_auth_hook(port, status);
00040 
00041     /*
00042      * Inject a short delay if authentication failed.
00043      */
00044     if (status != STATUS_OK)
00045     {
00046         pg_usleep(1000L * auth_delay_milliseconds);
00047     }
00048 }
00049 
00050 /*
00051  * Module Load Callback
00052  */
00053 void
00054 _PG_init(void)
00055 {
00056     /* Define custom GUC variables */
00057     DefineCustomIntVariable("auth_delay.milliseconds",
00058              "Milliseconds to delay before reporting authentication failure",
00059                             NULL,
00060                             &auth_delay_milliseconds,
00061                             0,
00062                             0, INT_MAX / 1000,
00063                             PGC_SIGHUP,
00064                             GUC_UNIT_MS,
00065                             NULL,
00066                             NULL,
00067                             NULL);
00068     /* Install Hooks */
00069     original_client_auth_hook = ClientAuthentication_hook;
00070     ClientAuthentication_hook = auth_delay_checks;
00071 }