File: logtofile_csv.c

package info (click to toggle)
pgauditlogtofile 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 288 kB
  • sloc: ansic: 1,211; makefile: 21; sql: 11; sh: 7
file content (272 lines) | stat: -rw-r--r-- 8,246 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-------------------------------------------------------------------------
 *
 * logtofile_csv.c
 *      Functions to create a csv audit record
 *
 * Copyright (c) 2020-2025, Francisco Miguel Biete Banon
 *
 * This code is released under the PostgreSQL licence, as given at
 *  http://www.postgresql.org/about/licence/
 *-------------------------------------------------------------------------
 */
#include "logtofile_csv.h"

#include "logtofile_string_format.h"

#include <access/xact.h>
#include <miscadmin.h>
#include <libpq/libpq-be.h>
#include <storage/proc.h>
#include <tcop/tcopprot.h>
#include <utils/json.h>
#include <utils/ps_status.h>

#include <stdarg.h>

/* forward declaration private functions */

inline static void pgauditlogtofile_append_csv_value(StringInfo buf, const char *value)
    __attribute__((always_inline));
static void pgauditlogtofile_append_csv_fmt(StringInfo buf, const char *fmt, ...)
    __attribute__((format(gnu_printf, 2, 3)));
inline static void pgauditlogtofile_pgaudit_escape(StringInfo buf, char *line)
    __attribute__((always_inline));

/**
 * @brief Creates a csv audit record
 * @param buf: buffer to write the csv line
 * @param edata: error data
 * @param exclude_nchars: number of characters to exclude from the pgaudit message
 * @return void
 */
void PgAuditLogToFile_csv_audit(StringInfo buf, const ErrorData *edata, int exclude_nchars)
{
  bool print_stmt = false;
  char *formatted_log_time;

  /* timestamp with milliseconds */

  formatted_log_time = PgAuditLogToFile_format_now_timestamp();
  pgauditlogtofile_append_csv_value(buf, formatted_log_time);
  pfree(formatted_log_time);
  appendStringInfoCharMacro(buf, ',');

  /* username */
  if (MyProcPort && MyProcPort->user_name)
    pgauditlogtofile_append_csv_value(buf, MyProcPort->user_name);
  appendStringInfoCharMacro(buf, ',');

  /* database name */
  if (MyProcPort && MyProcPort->database_name)
    pgauditlogtofile_append_csv_value(buf, MyProcPort->database_name);
  appendStringInfoCharMacro(buf, ',');

  /* Process id  */
  pgauditlogtofile_append_csv_fmt(buf, "%d", MyProcPid);
  appendStringInfoCharMacro(buf, ',');

  /* Remote host and port */
  if (MyProcPort && MyProcPort->remote_host)
  {
    if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0')
      pgauditlogtofile_append_csv_fmt(buf, "%s:%s", MyProcPort->remote_host, MyProcPort->remote_host);
    else
      pgauditlogtofile_append_csv_value(buf, MyProcPort->remote_host);
  }
  appendStringInfoCharMacro(buf, ',');

  /* session id - hex representation of start time . session process id */
  pgauditlogtofile_append_csv_fmt(buf, "%lx.%x", (long)MyStartTime, MyProcPid);
  appendStringInfoCharMacro(buf, ',');

  /* PS display */
  if (MyProcPort)
  {
    StringInfoData msgbuf;
    const char *psdisp;
    int displen;

    initStringInfo(&msgbuf);

    psdisp = get_ps_display(&displen);
    appendBinaryStringInfo(&msgbuf, psdisp, displen);
    pgauditlogtofile_append_csv_value(buf, msgbuf.data);

    pfree(msgbuf.data);
  }
  appendStringInfoCharMacro(buf, ',');

  /* Virtual transaction id */
  /* keep VXID format in sync with lockfuncs.c */
#if (PG_VERSION_NUM >= 170000)
  if (MyProc != NULL && MyProc->vxid.procNumber != INVALID_PROC_NUMBER)
    pgauditlogtofile_append_csv_fmt(buf, "%d/%u", MyProc->vxid.procNumber, MyProc->vxid.lxid);
#else
  if (MyProc != NULL && MyProc->backendId != InvalidBackendId)
    pgauditlogtofile_append_csv_fmt(buf, "%d/%u", MyProc->backendId, MyProc->lxid);
#endif
  appendStringInfoCharMacro(buf, ',');

  /* Transaction id */
  pgauditlogtofile_append_csv_fmt(buf, "%u", GetTopTransactionIdIfAny());
  appendStringInfoCharMacro(buf, ',');

  /* SQL state code */
  pgauditlogtofile_append_csv_value(buf, unpack_sql_state(edata->sqlerrcode));
  appendStringInfoCharMacro(buf, ',');

  /* errmessage - PGAUDIT formatted text, +7 exclude "AUDIT: " prefix */
  if (exclude_nchars > 0)
    pgauditlogtofile_pgaudit_escape(buf, edata->message + exclude_nchars);
  else
    pgauditlogtofile_append_csv_value(buf, edata->message);
  appendStringInfoCharMacro(buf, ',');

  /* errdetail or errdetail_log */
  if (edata->detail_log)
    pgauditlogtofile_append_csv_value(buf, edata->detail_log);
  else if (edata->detail)
    pgauditlogtofile_append_csv_value(buf, edata->detail);
  appendStringInfoCharMacro(buf, ',');

  /* errhint */
  if (edata->hint)
    pgauditlogtofile_append_csv_value(buf, edata->hint);
  appendStringInfoCharMacro(buf, ',');

  /* internal query */
  if (edata->internalquery)
    pgauditlogtofile_append_csv_value(buf, edata->internalquery);
  appendStringInfoCharMacro(buf, ',');

  /* if printed internal query, print internal pos too */
  if (edata->internalpos > 0 && edata->internalquery != NULL)
    pgauditlogtofile_append_csv_fmt(buf, "%d", edata->internalpos);
  appendStringInfoCharMacro(buf, ',');

  /* errcontext */
  if (edata->context)
    pgauditlogtofile_append_csv_value(buf, edata->context);
  appendStringInfoCharMacro(buf, ',');

  /* user query --- only reported if not disabled by the caller */
  if (debug_query_string != NULL && !edata->hide_stmt)
    print_stmt = true;
  if (print_stmt)
    pgauditlogtofile_append_csv_value(buf, debug_query_string);
  appendStringInfoCharMacro(buf, ',');
  if (print_stmt && edata->cursorpos > 0)
    pgauditlogtofile_append_csv_fmt(buf, "%d", edata->cursorpos);
  appendStringInfoCharMacro(buf, ',');

  /* file error location */
  if (Log_error_verbosity >= PGERROR_VERBOSE)
  {
    StringInfoData msgbuf;

    initStringInfo(&msgbuf);

    if (edata->funcname && edata->filename)
      appendStringInfo(&msgbuf, "%s, %s:%d", edata->funcname, edata->filename,
                       edata->lineno);
    else if (edata->filename)
      appendStringInfo(&msgbuf, "%s:%d", edata->filename, edata->lineno);
    pgauditlogtofile_append_csv_value(buf, msgbuf.data);
    pfree(msgbuf.data);
  }
  appendStringInfoCharMacro(buf, ',');

  /* application name */
  if (application_name)
    pgauditlogtofile_append_csv_value(buf, application_name);

  appendStringInfoCharMacro(buf, '\n');
}

/**
 * @brief Writes a CSV value quoted and escaped.
 * @param buf where to write
 * @param value value
 */
static void
pgauditlogtofile_append_csv_value(StringInfo buf, const char *value)
{
  if (value == NULL)
    return;

  escape_json(buf, value);
}

/**
 * @brief Writes a CSV value quoted and escaped.
 * @param buf where to write
 * @param fmt sprintf like formatting string
 * @param any format parameters
 */
static void
pgauditlogtofile_append_csv_fmt(StringInfo buf, const char *fmt, ...)
{
  StringInfoData formatted_value;
  va_list args;

  if (fmt == NULL)
    return;

  initStringInfo(&formatted_value);

  va_start(args, fmt);
  appendStringInfoVA(&formatted_value, fmt, args);
  va_end(args);

  pgauditlogtofile_append_csv_value(buf, formatted_value.data);

  pfree(formatted_value.data);
}

/**
 * @brief Split and escapes each piece on pgaudit original message and writes it as CSV value.
 * @param buf Where to write
 * @param line original pgaudit message, it's modified in this function
 */
static void
pgauditlogtofile_pgaudit_escape(StringInfo buf, char *line)
{
  char *token;

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  token = strsep(&line, ",");
  if (token)
    pgauditlogtofile_append_csv_value(buf, token);
  appendStringInfoCharMacro(buf, ',');

  /*
   * writes as one filed the statement and the params, but the statement and parameters
   * can contain comma we cannot split them easily
   * */
  if (line)
    pgauditlogtofile_append_csv_value(buf, line + 1);
}