Skip to content

Commit

Permalink
Support 64-bit PostgreSQL 13.2, 12.6, 11.11 and both 64-bit and 32-bi…
Browse files Browse the repository at this point in the history
…t PostgreSQL 10.16, 9.6.21, 9.5.25

Plugin now provides PostgreSQL SQLSTATE (postgres_sqlstate) in events, where available.
  • Loading branch information
pwrpw committed Jun 1, 2021
1 parent 7b42321 commit d786e3d
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 98 deletions.
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ AC_DEFUN([CHECK_DEBUG], [
if test "$enable_debug" = "yes"
then
CPPFLAGS="$CPPFLAGS -g -D_DEBUG"
CPPFLAGS="$CPPFLAGS -DENABLE_NLS -g -D_DEBUG"
AC_MSG_RESULT([yes])
else
CPPFLAGS="$CPPFLAGS -g -O2"
CPPFLAGS="$CPPFLAGS -DENABLE_NLS -g -O2"
AC_MSG_RESULT([no])
fi
Expand Down
7 changes: 7 additions & 0 deletions include/audit_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ extern "C" {
*/

// Revised to use C++ conventions - struct xxx instead of typedef struct xxx{ ... } xxx;
struct AuditError {
int sqlerrcode; /* encoded ERRSTATE */
const char* message; /* primary error message (translated) */
};

struct AuditEvent
{
int64 statementId; /* Simple counter */
Expand All @@ -29,6 +34,8 @@ struct AuditEvent
const char *className; /* Class of item being logged */
List *objectList; /* List of objects, e.g. in a DROP statement */
bool useLastInList; /* True if last element in list is the object name */

List* errorList;
};

/*
Expand Down
46 changes: 29 additions & 17 deletions include/audit_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "pgsql_inc.h"
#include "audit_event.h"

#include <vector>
#include <string>

#define AUDIT_PROTOCOL_VERSION "1.0"

/*
Expand All @@ -51,6 +54,11 @@ class IWriter {
virtual void close() = 0;
};

struct ProcError {
int sqlerrcode; /* encoded ERRSTATE */
std::string message; /* primary error message (translated) */
};

struct PostgreSQL_proc {
pid_t pid;
const char *db_name;
Expand All @@ -62,9 +70,12 @@ struct PostgreSQL_proc {
const char *appname;
unsigned int query_id;
bool connected;
bool initialized;
int auth_status;
std::vector<ProcError> error_list;

PostgreSQL_proc()
: pid(getpid()),
: pid(),
db_name(""),
user(""),
priv_user(""),
Expand All @@ -73,7 +84,9 @@ struct PostgreSQL_proc {
os_user(""),
appname(""),
query_id(0),
connected(false)
connected(false),
initialized(),
auth_status()
{
}
};
Expand All @@ -91,7 +104,7 @@ class Audit_formatter {
*
* @return -1 on a failure
*/
virtual ssize_t event_format(PostgreSQL_proc * proc, AuditEventStackItem *pItem, IWriter *writer) = 0;
virtual ssize_t event_format(const PostgreSQL_proc *proc, AuditEventStackItem *pItem, IWriter *writer) = 0;

/**
* Format a message when handler is started
Expand All @@ -110,7 +123,7 @@ class Audit_formatter {
* Format a generic message
* @return -1 on a failure
*/
virtual ssize_t command_format(PostgreSQL_proc *proc, const char *command, const char *query, IWriter *writer) = 0;
virtual ssize_t command_format(const PostgreSQL_proc *proc, const char *command, const char *query, IWriter *writer) = 0;
};


Expand Down Expand Up @@ -141,9 +154,9 @@ class Audit_json_formatter : public Audit_formatter {
}
}

virtual ssize_t event_format(PostgreSQL_proc * proc, AuditEventStackItem *pItem, IWriter *writer);
virtual ssize_t event_format(const PostgreSQL_proc *proc, AuditEventStackItem *pItem, IWriter *writer);
virtual ssize_t start_msg_format(IWriter *writer);
virtual ssize_t command_format(PostgreSQL_proc *proc, const char *command, const char *query, IWriter *writer);
virtual ssize_t command_format(const PostgreSQL_proc *proc, const char *command, const char *query, IWriter *writer);

/**
* Utility method used to compile a regex program.
Expand Down Expand Up @@ -228,12 +241,13 @@ class Audit_handler {
*/
static void stop_all();

Audit_handler() :
Audit_handler(const PostgreSQL_proc& proc) :
m_initialized(false),
m_enabled(false),
m_formatter(NULL),
m_failed(false),
m_log_io_errors(true),
m_proc(proc),
m_handler_type("unknown")
{
}
Expand All @@ -249,10 +263,9 @@ class Audit_handler {
* destruction of this object)
* @return 0 on success
*/
int init(Audit_formatter *formatter, PostgreSQL_proc *proc)
int init(Audit_formatter *formatter)
{
m_formatter = formatter;
m_proc = *proc;
if (m_initialized)
{
// elog(LOG, "pid = %d, %s Audit_handler::init - initialized!", getpid(), m_handler_type);
Expand Down Expand Up @@ -304,7 +317,6 @@ class Audit_handler {
/**
* Allow updating the proc info.
*/
void set_proc(const PostgreSQL_proc& proc) { m_proc = proc; }

protected:
virtual void handler_start();
Expand All @@ -320,7 +332,7 @@ class Audit_handler {
bool m_failed;
bool m_log_io_errors;
time_t m_last_retry_sec_ts;
PostgreSQL_proc m_proc;
const PostgreSQL_proc& m_proc;

inline void set_failed()
{
Expand Down Expand Up @@ -353,8 +365,8 @@ class Audit_handler {
*/
class Audit_io_handler: public Audit_handler, public IWriter {
public:
Audit_io_handler()
: m_io_dest(NULL), m_io_type(NULL)
Audit_io_handler(const PostgreSQL_proc& proc)
: Audit_handler(proc), m_io_dest(NULL), m_io_type(NULL)
{
set_handler_type("io");
}
Expand Down Expand Up @@ -389,8 +401,8 @@ class Audit_io_handler: public Audit_handler, public IWriter {
class Audit_file_handler: public Audit_io_handler {
public:

Audit_file_handler() :
m_sync_period(0), m_bufsize(0), m_log_file(NULL), m_sync_counter(0)
Audit_file_handler(const PostgreSQL_proc& proc) :
Audit_io_handler(proc) ,m_sync_period(0), m_bufsize(0), m_log_file(NULL), m_sync_counter(0)
{
m_io_type = "file";
set_handler_type("file");
Expand Down Expand Up @@ -440,8 +452,8 @@ class Audit_file_handler: public Audit_io_handler {
class Audit_unix_socket_handler: public Audit_io_handler {
public:

Audit_unix_socket_handler() :
m_connect_timeout(1), m_fd(-1)
Audit_unix_socket_handler(const PostgreSQL_proc& proc) :
Audit_io_handler(proc) ,m_connect_timeout(1), m_fd(-1)
{
m_io_type = "socket";
set_handler_type("socket");
Expand Down
1 change: 1 addition & 0 deletions include/pgsql_inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {
#include "miscadmin.h"
#include "libpq/auth.h"
#include "nodes/nodes.h"
#include "nodes/pg_list.h"
#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/builtins.h"
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.pg.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ REGRESS = audit
REGRESS_OPTS = --temp-config=$(top_srcdir)/contrib/postgresql-audit/audit.conf

PG_CPPFLAGS += -Werror -I../include -I../yajl/include -I../pcre -fPIC \
-DENABLE_NLS \
-DPOSTGRESQL_AUDIT_PLUGIN_REVISION='"@POSTGRESQL_AUDIT_PLUGIN_REVISION@"' \
-DPOSTGRESQL_AUDIT_PLUGIN_VERSION='"@POSTGRESQL_AUDIT_PLUGIN_VERSION@"'

Expand Down
Loading

0 comments on commit d786e3d

Please sign in to comment.