- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2017-08-16T21:37:14+09:00","default:haikikyou","haikikyou")
#author("2017-08-17T20:22:22+09:00","default:haikikyou","haikikyou")
[[PostgreSQL/開発/フック]]
#contents
* フック [#g09c3125]
|~Hookインターフェース|~説明|h
|ClientAuthentication_hook(Port *port, int status)|認証時に呼ばれる。具体的には、認証後にフックポイントがありログインイベントの記録などに利用される。|
|void 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){ サンプル };};
&size(12){&color(white,#800080){ 実験 };};
''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 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''
#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