All Projects → splendiddata → login_hook

splendiddata / login_hook

Licence: GPL-3.0 license
Postgres database extension to mimic a logon trigger

Programming Languages

c
50402 projects - #5 most used programming language
HTML
75241 projects
PLpgSQL
1095 projects
Makefile
30231 projects
CSS
56736 projects

Projects that are alternatives of or similar to login hook

Plgo
easily create postgresql extensions in golang; moved to gitlab.com/microo8/plgo
Stars: ✭ 286 (+886.21%)
Mutual labels:  postgresql-extension
Pg partman
Partition management extension for PostgreSQL
Stars: ✭ 1,085 (+3641.38%)
Mutual labels:  postgresql-extension
Agensgraph Extension
A graph database extension for PostgreSQL
Stars: ✭ 170 (+486.21%)
Mutual labels:  postgresql-extension
Plpgsql check
plpgsql_check is linter tool for language PL/pgSQL (native language for PostgreSQL store procedures).
Stars: ✭ 322 (+1010.34%)
Mutual labels:  postgresql-extension
Pg acoustid
PostgreSQL extension for working with AcoustID fingerprints
Stars: ✭ 6 (-79.31%)
Mutual labels:  postgresql-extension
Bgworker
Background Worker Processes for PostgreSQL written in Go
Stars: ✭ 77 (+165.52%)
Mutual labels:  postgresql-extension
Incubator Age
Graph database optimized for fast analysis and real-time data processing. It is provided as an extension to PostgreSQL.
Stars: ✭ 244 (+741.38%)
Mutual labels:  postgresql-extension
Wasmer Postgres
💽🕸 Postgres library to run WebAssembly binaries.
Stars: ✭ 245 (+744.83%)
Mutual labels:  postgresql-extension
Pgx
Build Postgres Extensions with Rust!
Stars: ✭ 903 (+3013.79%)
Mutual labels:  postgresql-extension
Pg hashids
Short unique id generator for PostgreSQL, using hashids
Stars: ✭ 164 (+465.52%)
Mutual labels:  postgresql-extension
Pgaudit
PostgreSQL Audit Extension
Stars: ✭ 532 (+1734.48%)
Mutual labels:  postgresql-extension
Pgrouting
Repository contains pgRouting library. Development branch is "develop", stable branch is "master"
Stars: ✭ 804 (+2672.41%)
Mutual labels:  postgresql-extension
Plsh
PL/sh is a procedural language handler for PostgreSQL that allows you to write stored procedures in a shell of your choice.
Stars: ✭ 111 (+282.76%)
Mutual labels:  postgresql-extension
Pggraphblas
High Performance Graph Processing with Postgres and GraphBLAS
Stars: ✭ 316 (+989.66%)
Mutual labels:  postgresql-extension
Pguri
uri type for PostgreSQL
Stars: ✭ 235 (+710.34%)
Mutual labels:  postgresql-extension
Orafce
The "orafce" project implements in Postgres some of the functions from the Oracle database that are missing (or behaving differently).Those functions were verified on Oracle 10g, and the module is useful for production work.
Stars: ✭ 274 (+844.83%)
Mutual labels:  postgresql-extension
Plv8
V8 Engine Javascript Procedural Language add-on for PostgreSQL
Stars: ✭ 1,195 (+4020.69%)
Mutual labels:  postgresql-extension
Pg similarity
set of functions and operators for executing similarity queries
Stars: ✭ 250 (+762.07%)
Mutual labels:  postgresql-extension
Tds fdw
A PostgreSQL foreign data wrapper to connect to TDS databases (Sybase and Microsoft SQL Server)
Stars: ✭ 238 (+720.69%)
Mutual labels:  postgresql-extension
Decoderbufs
INACTIVE: A PostgreSQL logical decoder output plugin to deliver data as Protocol Buffers
Stars: ✭ 116 (+300%)
Mutual labels:  postgresql-extension

login_hook

Postgres database extension to execute some code on user login, comparable to Oracle's after logon trigger.

Postgres versions

The login_hook database extension works well in Postgres versions 11, 12, 13, 14 and 15.

Installation

First you'll need to compile the database extension (Check the Postgres manual for more information):

  • Make sure pg_config points to the right places
  • execute: make
  • execute: sudo make install

After compilation, the login_hook.so library must be set to load at session start. So please alter the postgresql.conf file and add the login_hook.so library to the session_preload_libraries setting. For example:

      .
      .
      .

#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------

# Add settings for extensions here
#
session_preload_libraries = 'login_hook'

Restart the database to activate the setting.

Then logon to the database and execute:

create extension login_hook;

And create function login_hook.login() that is to be executed when a client logs in. For example:

CREATE OR REPLACE FUNCTION login_hook.login() RETURNS VOID LANGUAGE PLPGSQL AS $$
DECLARE
    ex_state   TEXT;
    ex_message TEXT;
    ex_detail  TEXT;
    ex_hint    TEXT;
    ex_context TEXT;
BEGIN
	IF NOT login_hook.is_executing_login_hook()
	THEN
	    RAISE EXCEPTION 'The login_hook.login() function should only be invoked by the login_hook code';
	END IF;
	
	BEGIN
	   -- 
	   -- Do whatever you need to do at login here.
	   -- For example:
	   RAISE NOTICE 'Hello %', current_user;
	EXCEPTION
	   WHEN OTHERS THEN
	       GET STACKED DIAGNOSTICS ex_state   = RETURNED_SQLSTATE
	                             , ex_message = MESSAGE_TEXT
	                             , ex_detail  = PG_EXCEPTION_DETAIL
	                             , ex_hint    = PG_EXCEPTION_HINT
	                             , ex_context = PG_EXCEPTION_CONTEXT;
	       RAISE LOG e'Error in login_hook.login()\nsqlstate: %\nmessage : %\ndetail  : %\nhint    : %\ncontext : %'
	               , ex_state
	               , ex_message
	               , ex_detail
	               , ex_hint
	               , ex_context;
    END	;       
END 
$$;
GRANT EXECUTE ON FUNCTION login_hook.login() TO PUBLIC;

Remarks:

the public execute permission is absolutely necessary because the function will be invoked for every body/thing that logs in to the database. In fact the function will be executed every time that a new process starts on behalf of a user session, so also if you are for example logged in with psql and use \c to reconnect. And also sessions started by dblink or fdw will trigger execution of the login() function.

Having public access granted to everybody might tempt people to execute the login_hook.login() function at any time. But of course it is intended to run only at login. The login_hook.is_executing_login_hook() function can be used to check if the function is invoked under the control of the login_hook code.

Make sure that all exceptions that occur in the login_hook.login() function are properly dealt with because otherwise logging in to the database might prove challenging. Superusers will get a warning, but are still allowed to log in to be able to correct the function. Normal users will be logged out immediately when the login() function fails.

The "make installcheck" will only pass if "session_preload_libraries = 'login_hook'" is added to the postgresql.conf file

BEWARE! there appears to be a problem with EDB databases. See issue #5.

Functions

login_hook.is_executing_login_hook() returns boolean

returns true when the login_hook.login() function is invoked under 
control of the login_hook code. When invoked during a normal
session, it will always return false.

login_hook.get_login_hook_version() returns text

returns the compiled version of the login_hook software.

login_hook.login() returns void

To be provided by you! 
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].