#author("2017-08-16T21:25:58+09:00","default:haikikyou","haikikyou")
[[PostgreSQL/開発/フック]]

#contents

* フック [#g09c3125]

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


&size(12){&color(white,orange){ 参考 };}; [[libpq/auth.c>https://doxygen.postgresql.org/auth_8c.html]] - &size(11){&color(gray){on [[doxygen.postgresql.org>https://doxygen.postgresql.org]]};};

* サンプル [#f6f3509a]

&size(12){&color(white,#00afcc){ サンプル };};

''myext.c''

#geshi(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 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''

#geshi{{{
shared_preload_libraries = 'myext'
}}}

''実行例''

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

* 参考リンク [#b06e8b3a]

- [[libpq/auth.c>https://doxygen.postgresql.org/auth_8c.html]] - &size(11){&color(gray){on [[doxygen.postgresql.org>https://doxygen.postgresql.org]]};};

* コメント [#c8252353]

#comment


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