PostgreSQL/開発/フック

フック

Hookインターフェース説明
ClientAuthentication_hook(Port *port, int status)認証時に呼ばれる。具体的には、認証後にフックポイントがありログインイベントの記録などに利用される。

 参考  libpq/auth.c - on doxygen.postgresql.org

サンプル

 サンプル 

myext.c

#include "postgres.h"
#include "fmgr.h"
#include "libpq/auth.h"
#include "lib/stringinfo.h"

PG_MODULE_MAGIC;

extern void _PG_init(void);

extern ClientAuthentication_hook_type ClientAuthentication_hook;

static const char *logfile = "/tmp/postgresql-auth.log";

static void my_ClientAuthentication_hook(Port *port, int status);

void _PG_init(void)
{
  ClientAuthentication_hook = my_ClientAuthentication_hook;
}

static void my_ClientAuthentication_hook(Port *port, int status)
{
    FILE *fp;
    StringInfo strinfo = makeStringInfo();
    if ((fp = fopen(logfile, "ab")) == NULL) {
        elog(ERROR, "failed to open %s", logfile);
        return;
    }
    appendStringInfo(strinfo, "{\"username\":\"%s\",\"status\":\"%d\"}\n", port->user_name, status);
    fputs(strinfo->data, fp);
    fclose(fp);
}

postgresql.conf

shared_preload_libraries = 'myext'

実行例

$ psql -U guest -d postgres -c "\d"
$ cat /tmp/postgresql-auth.log
{"username":"guest","status":"0"}

参考リンク

コメント



トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP