Lines 16-22
Link Here
|
16 |
#include <ulogd/ulogd.h> |
16 |
#include <ulogd/ulogd.h> |
17 |
#include <ulogd/conffile.h> |
17 |
#include <ulogd/conffile.h> |
18 |
#include <libpq-fe.h> |
18 |
#include <libpq-fe.h> |
19 |
|
19 |
#include <inttypes.h> |
20 |
|
20 |
|
21 |
#ifdef DEBUG_PGSQL |
21 |
#ifdef DEBUG_PGSQL |
22 |
#define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
22 |
#define DEBUGP(x, args...) fprintf(stderr, x, ## args) |
Lines 27-32
Link Here
|
27 |
struct _field { |
27 |
struct _field { |
28 |
char name[ULOGD_MAX_KEYLEN]; |
28 |
char name[ULOGD_MAX_KEYLEN]; |
29 |
unsigned int id; |
29 |
unsigned int id; |
|
|
30 |
unsigned int str; |
30 |
struct _field *next; |
31 |
struct _field *next; |
31 |
}; |
32 |
}; |
32 |
|
33 |
|
Lines 39-44
Link Here
|
39 |
/* buffer for our insert statement */ |
40 |
/* buffer for our insert statement */ |
40 |
static char *stmt; |
41 |
static char *stmt; |
41 |
|
42 |
|
|
|
43 |
/* size of our insert statement buffer */ |
44 |
static size_t stmt_siz; |
45 |
|
42 |
/* pointer to the beginning of the "VALUES" part */ |
46 |
/* pointer to the beginning of the "VALUES" part */ |
43 |
static char *stmt_val; |
47 |
static char *stmt_val; |
44 |
|
48 |
|
Lines 97-102
Link Here
|
97 |
|
101 |
|
98 |
static unsigned char pgsql_have_schemas; |
102 |
static unsigned char pgsql_have_schemas; |
99 |
|
103 |
|
|
|
104 |
#define STMT_ADD(pos,fmt...) \ |
105 |
do { \ |
106 |
if ((pos) >= stmt && stmt_siz > (pos) - stmt) \ |
107 |
snprintf((pos), stmt_siz-((pos)-stmt), ##fmt); \ |
108 |
} while(0) |
109 |
|
100 |
/* our main output function, called by ulogd */ |
110 |
/* our main output function, called by ulogd */ |
101 |
static int pgsql_output(ulog_iret_t *result) |
111 |
static int pgsql_output(ulog_iret_t *result) |
102 |
{ |
112 |
{ |
Lines 107-112
Link Here
|
107 |
char *tmpstr; /* need this for --log-ip-as-string */ |
117 |
char *tmpstr; /* need this for --log-ip-as-string */ |
108 |
struct in_addr addr; |
118 |
struct in_addr addr; |
109 |
#endif |
119 |
#endif |
|
|
120 |
size_t esclen; |
121 |
|
122 |
if( stmt_val == NULL) |
123 |
return 1; |
110 |
|
124 |
|
111 |
stmt_ins = stmt_val; |
125 |
stmt_ins = stmt_val; |
112 |
|
126 |
|
Lines 120-181
Link Here
|
120 |
|
134 |
|
121 |
if (!res || !IS_VALID((*res))) { |
135 |
if (!res || !IS_VALID((*res))) { |
122 |
/* no result, we have to fake something */ |
136 |
/* no result, we have to fake something */ |
123 |
sprintf(stmt_ins, "NULL,"); |
137 |
STMT_ADD(stmt_ins, "NULL,"); |
124 |
stmt_ins = stmt + strlen(stmt); |
138 |
stmt_ins = stmt + strlen(stmt); |
125 |
continue; |
139 |
continue; |
126 |
} |
140 |
} |
127 |
|
141 |
|
128 |
switch (res->type) { |
142 |
switch (res->type) { |
129 |
case ULOGD_RET_INT8: |
143 |
case ULOGD_RET_INT8: |
130 |
sprintf(stmt_ins, "%d,", res->value.i8); |
144 |
STMT_ADD(stmt_ins, "%d,", res->value.i8); |
131 |
break; |
145 |
break; |
132 |
case ULOGD_RET_INT16: |
146 |
case ULOGD_RET_INT16: |
133 |
sprintf(stmt_ins, "%d,", res->value.i16); |
147 |
STMT_ADD(stmt_ins, "%d,", res->value.i16); |
134 |
break; |
148 |
break; |
135 |
case ULOGD_RET_INT32: |
149 |
case ULOGD_RET_INT32: |
136 |
sprintf(stmt_ins, "%d,", res->value.i32); |
150 |
STMT_ADD(stmt_ins, "%d,", res->value.i32); |
137 |
break; |
151 |
break; |
138 |
case ULOGD_RET_INT64: |
152 |
case ULOGD_RET_INT64: |
139 |
sprintf(stmt_ins, "%lld,", res->value.i64); |
153 |
STMT_ADD(stmt_ins, "%"PRId64",",res->value.i64); |
140 |
break; |
154 |
break; |
141 |
case ULOGD_RET_UINT8: |
155 |
case ULOGD_RET_UINT8: |
142 |
sprintf(stmt_ins, "%u,", res->value.ui8); |
156 |
STMT_ADD(stmt_ins, "%u,", res->value.ui8); |
143 |
break; |
157 |
break; |
144 |
case ULOGD_RET_UINT16: |
158 |
case ULOGD_RET_UINT16: |
145 |
sprintf(stmt_ins, "%u,", res->value.ui16); |
159 |
STMT_ADD(stmt_ins, "%u,", res->value.ui16); |
146 |
break; |
160 |
break; |
147 |
case ULOGD_RET_IPADDR: |
161 |
case ULOGD_RET_IPADDR: |
148 |
#ifdef IP_AS_STRING |
162 |
#ifdef IP_AS_STRING |
149 |
*stmt_ins++ = '\''; |
163 |
if (f->str) { |
150 |
memset(&addr, 0, sizeof(addr)); |
164 |
addr.s_addr = ntohl(res->value.ui32); |
151 |
addr.s_addr = ntohl(res->value.ui32); |
165 |
tmpstr = (char *)inet_ntoa(addr); |
152 |
tmpstr = (char *)inet_ntoa(addr); |
166 |
esclen = (strlen(tmpstr)*2) + 4; |
153 |
PQescapeString(stmt_ins,tmpstr,strlen(tmpstr)); |
167 |
if (stmt_siz <= (stmt_ins-stmt)+esclen) |
154 |
stmt_ins = stmt + strlen(stmt); |
168 |
{ |
155 |
sprintf(stmt_ins, "',"); |
169 |
STMT_ADD(stmt_ins,"'',"); |
156 |
break; |
170 |
break; |
|
|
171 |
} |
172 |
*stmt_ins++ = '\''; |
173 |
PQescapeString(stmt_ins,tmpstr, |
174 |
strlen(tmpstr)); |
175 |
stmt_ins = stmt + strlen(stmt); |
176 |
STMT_ADD(stmt_ins, "',"); |
177 |
break; |
178 |
} |
157 |
#endif /* IP_AS_STRING */ |
179 |
#endif /* IP_AS_STRING */ |
158 |
/* EVIL: fallthrough when logging IP as |
180 |
/* EVIL: fallthrough when logging IP as |
159 |
* u_int32_t */ |
181 |
* u_int32_t */ |
160 |
|
182 |
|
161 |
case ULOGD_RET_UINT32: |
183 |
case ULOGD_RET_UINT32: |
162 |
sprintf(stmt_ins, "%u,", res->value.ui32); |
184 |
STMT_ADD(stmt_ins, "%u,", res->value.ui32); |
163 |
break; |
185 |
break; |
164 |
case ULOGD_RET_UINT64: |
186 |
case ULOGD_RET_UINT64: |
165 |
sprintf(stmt_ins, "%llu,", res->value.ui64); |
187 |
STMT_ADD(stmt_ins,"%"PRIu64",",res->value.ui64); |
166 |
break; |
188 |
break; |
167 |
case ULOGD_RET_BOOL: |
189 |
case ULOGD_RET_BOOL: |
168 |
sprintf(stmt_ins, "'%d',", res->value.b); |
190 |
STMT_ADD(stmt_ins, "'%d',", res->value.b); |
169 |
break; |
191 |
break; |
170 |
case ULOGD_RET_STRING: |
192 |
case ULOGD_RET_STRING: |
|
|
193 |
esclen = (strlen(res->value.ptr)*2) + 4; |
194 |
if (stmt_siz <= (stmt_ins-stmt) + esclen) { |
195 |
STMT_ADD(stmt_ins, "'',"); |
196 |
break; |
197 |
} |
171 |
*stmt_ins++ = '\''; |
198 |
*stmt_ins++ = '\''; |
172 |
PQescapeString(stmt_ins,res->value.ptr,strlen(res->value.ptr)); |
199 |
PQescapeString(stmt_ins,res->value.ptr, |
|
|
200 |
strlen(res->value.ptr)); |
173 |
stmt_ins = stmt + strlen(stmt); |
201 |
stmt_ins = stmt + strlen(stmt); |
174 |
sprintf(stmt_ins, "',"); |
202 |
STMT_ADD(stmt_ins, "',"); |
175 |
break; |
203 |
break; |
176 |
case ULOGD_RET_RAW: |
204 |
case ULOGD_RET_RAW: |
177 |
ulogd_log(ULOGD_NOTICE,"%s: pgsql doesn't support type RAW\n",res->key); |
205 |
ulogd_log(ULOGD_NOTICE, |
178 |
sprintf(stmt_ins, "NULL,"); |
206 |
"%s: pgsql doesn't support type RAW\n", |
|
|
207 |
res->key); |
208 |
STMT_ADD(stmt_ins, "NULL,"); |
179 |
break; |
209 |
break; |
180 |
default: |
210 |
default: |
181 |
ulogd_log(ULOGD_NOTICE, |
211 |
ulogd_log(ULOGD_NOTICE, |
Lines 186-191
Link Here
|
186 |
stmt_ins = stmt + strlen(stmt); |
216 |
stmt_ins = stmt + strlen(stmt); |
187 |
} |
217 |
} |
188 |
*(stmt_ins - 1) = ')'; |
218 |
*(stmt_ins - 1) = ')'; |
|
|
219 |
|
189 |
DEBUGP("stmt=#%s#\n", stmt); |
220 |
DEBUGP("stmt=#%s#\n", stmt); |
190 |
|
221 |
|
191 |
/* now we have created our statement, insert it */ |
222 |
/* now we have created our statement, insert it */ |
Lines 202-218
Link Here
|
202 |
return 0; |
233 |
return 0; |
203 |
} |
234 |
} |
204 |
|
235 |
|
205 |
#define PGSQL_HAVE_NAMESPACE_TEMPLATE "SELECT nspname FROM pg_namespace n WHERE n.nspname='%s'" |
236 |
#define PGSQL_HAVE_NAMESPACE_TEMPLATE \ |
|
|
237 |
"SELECT nspname FROM pg_namespace n WHERE n.nspname='%s'" |
206 |
|
238 |
|
207 |
/* Determine if server support schemas */ |
239 |
/* Determine if server support schemas */ |
208 |
static int pgsql_namespace(void) { |
240 |
static int pgsql_namespace(void) { |
209 |
PGresult *result; |
241 |
PGresult *result; |
210 |
char pgbuf[strlen(PGSQL_HAVE_NAMESPACE_TEMPLATE)+strlen(schema_ce.u.string)+1]; |
242 |
char pgbuf[strlen(PGSQL_HAVE_NAMESPACE_TEMPLATE)+ |
|
|
243 |
strlen(schema_ce.u.string)+1]; |
211 |
|
244 |
|
212 |
if (!dbh) |
245 |
if (!dbh) |
213 |
return 1; |
246 |
return 1; |
214 |
|
247 |
|
215 |
sprintf(pgbuf, PGSQL_HAVE_NAMESPACE_TEMPLATE, schema_ce.u.string); |
248 |
snprintf(pgbuf, sizeof(pgbuf), PGSQL_HAVE_NAMESPACE_TEMPLATE, |
|
|
249 |
schema_ce.u.string); |
216 |
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf); |
250 |
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf); |
217 |
|
251 |
|
218 |
result = PQexec(dbh, pgbuf); |
252 |
result = PQexec(dbh, pgbuf); |
Lines 240-246
Link Here
|
240 |
static int pgsql_createstmt(void) |
274 |
static int pgsql_createstmt(void) |
241 |
{ |
275 |
{ |
242 |
struct _field *f; |
276 |
struct _field *f; |
243 |
unsigned int size; |
|
|
244 |
char buf[ULOGD_MAX_KEYLEN]; |
277 |
char buf[ULOGD_MAX_KEYLEN]; |
245 |
char *underscore; |
278 |
char *underscore; |
246 |
|
279 |
|
Lines 251-291
Link Here
|
251 |
} |
284 |
} |
252 |
|
285 |
|
253 |
/* caclulate the size for the insert statement */ |
286 |
/* caclulate the size for the insert statement */ |
254 |
size = strlen(PGSQL_INSERTTEMPL) + strlen(table_ce.u.string) + strlen(schema_ce.u.string) + 1; |
287 |
stmt_siz = strlen(PGSQL_INSERTTEMPL) + |
|
|
288 |
strlen(table_ce.u.string) + |
289 |
strlen(schema_ce.u.string) + 1; |
255 |
|
290 |
|
256 |
for (f = fields; f; f = f->next) { |
291 |
for (f = fields; f; f = f->next) { |
257 |
/* we need space for the key and a comma, as well as |
292 |
/* we need space for the key and a comma, as well as |
258 |
* enough space for the values */ |
293 |
* enough space for the values */ |
259 |
size += strlen(f->name) + 1 + PGSQL_VALSIZE; |
294 |
stmt_siz += strlen(f->name) + 1 + PGSQL_VALSIZE; |
260 |
} |
295 |
} |
261 |
|
296 |
|
262 |
ulogd_log(ULOGD_DEBUG, "allocating %u bytes for statement\n", size); |
297 |
ulogd_log(ULOGD_DEBUG, "allocating %u bytes for statement\n", stmt_siz); |
263 |
|
298 |
|
264 |
stmt = (char *) malloc(size); |
299 |
stmt = (char *) malloc(stmt_siz); |
265 |
|
300 |
|
266 |
if (!stmt) { |
301 |
if (!stmt) { |
|
|
302 |
stmt_siz = 0; |
267 |
ulogd_log(ULOGD_ERROR, "OOM!\n"); |
303 |
ulogd_log(ULOGD_ERROR, "OOM!\n"); |
268 |
return 1; |
304 |
return 1; |
269 |
} |
305 |
} |
270 |
|
306 |
|
271 |
if (pgsql_have_schemas) { |
307 |
if (pgsql_have_schemas) { |
272 |
sprintf(stmt, "insert into %s.%s (", schema_ce.u.string, table_ce.u.string); |
308 |
snprintf(stmt, stmt_siz, "insert into %s.%s (", |
|
|
309 |
schema_ce.u.string, table_ce.u.string); |
273 |
} else { |
310 |
} else { |
274 |
sprintf(stmt, "insert into %s (", table_ce.u.string); |
311 |
snprintf(stmt, stmt_siz, "insert into %s (", |
|
|
312 |
table_ce.u.string); |
275 |
} |
313 |
} |
276 |
|
314 |
|
277 |
stmt_val = stmt + strlen(stmt); |
315 |
stmt_val = stmt + strlen(stmt); |
278 |
|
316 |
|
279 |
for (f = fields; f; f = f->next) { |
317 |
for (f = fields; f; f = f->next) { |
280 |
strncpy(buf, f->name, ULOGD_MAX_KEYLEN); |
318 |
strncpy(buf, f->name, ULOGD_MAX_KEYLEN-1); |
|
|
319 |
buf[ULOGD_MAX_KEYLEN-1] = '\0'; |
281 |
while ((underscore = strchr(buf, '.'))) |
320 |
while ((underscore = strchr(buf, '.'))) |
282 |
*underscore = '_'; |
321 |
*underscore = '_'; |
283 |
sprintf(stmt_val, "%s,", buf); |
322 |
STMT_ADD(stmt_val, "%s,", buf); |
284 |
stmt_val = stmt + strlen(stmt); |
323 |
stmt_val = stmt + strlen(stmt); |
285 |
} |
324 |
} |
286 |
*(stmt_val - 1) = ')'; |
325 |
*(stmt_val - 1) = ')'; |
287 |
|
326 |
|
288 |
sprintf(stmt_val, " values ("); |
327 |
STMT_ADD(stmt_val, " values ("); |
289 |
stmt_val = stmt + strlen(stmt); |
328 |
stmt_val = stmt + strlen(stmt); |
290 |
|
329 |
|
291 |
ulogd_log(ULOGD_DEBUG, "stmt='%s'\n", stmt); |
330 |
ulogd_log(ULOGD_DEBUG, "stmt='%s'\n", stmt); |
Lines 293-320
Link Here
|
293 |
return 0; |
332 |
return 0; |
294 |
} |
333 |
} |
295 |
|
334 |
|
296 |
#define PGSQL_GETCOLUMN_TEMPLATE "SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname ='%s' AND a.attnum>0 AND a.attrelid=c.oid ORDER BY a.attnum" |
335 |
#define PGSQL_GETCOLUMN_TEMPLATE \ |
297 |
|
336 |
"SELECT a.attname,t.typname FROM pg_class c, pg_attribute a, "\ |
298 |
#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA "SELECT a.attname FROM pg_attribute a, pg_class c LEFT JOIN pg_namespace n ON c.relnamespace=n.oid WHERE c.relname ='%s' AND n.nspname='%s' AND a.attnum>0 AND a.attrelid=c.oid AND a.attisdropped=FALSE ORDER BY a.attnum" |
337 |
"pg_type t WHERE c.relname ='%s' AND a.attnum>0 AND a.attrelid="\ |
|
|
338 |
"c.oid AND a.atttypid=t.oid ORDER BY a.attnum" |
339 |
|
340 |
#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA "SELECT a.attname,t.typname FROM "\ |
341 |
"pg_attribute a, pg_type t, pg_class c LEFT JOIN pg_namespace n ON "\ |
342 |
"c.relnamespace=n.oid WHERE c.relname ='%s' AND n.nspname='%s' AND "\ |
343 |
"a.attnum>0 AND a.attrelid=c.oid AND a.atttypid=t.oid AND "\ |
344 |
"a.attisdropped=FALSE ORDER BY a.attnum" |
299 |
|
345 |
|
300 |
/* find out which columns the table has */ |
346 |
/* find out which columns the table has */ |
301 |
static int pgsql_get_columns(const char *table) |
347 |
static int pgsql_get_columns(const char *table) |
302 |
{ |
348 |
{ |
303 |
PGresult *result; |
349 |
PGresult *result; |
304 |
char buf[ULOGD_MAX_KEYLEN]; |
350 |
char buf[ULOGD_MAX_KEYLEN]; |
305 |
char pgbuf[strlen(PGSQL_GETCOLUMN_TEMPLATE_SCHEMA)+strlen(table)+strlen(schema_ce.u.string)+2]; |
351 |
char pgbuf[strlen(PGSQL_GETCOLUMN_TEMPLATE_SCHEMA)+ |
|
|
352 |
strlen(table)+strlen(schema_ce.u.string)+2]; |
306 |
char *underscore; |
353 |
char *underscore; |
307 |
struct _field *f; |
354 |
struct _field *f; |
308 |
int id; |
355 |
int id; |
309 |
int intaux; |
356 |
int intaux; |
|
|
357 |
char *typename; |
310 |
|
358 |
|
311 |
if (!dbh) |
359 |
if (!dbh) |
312 |
return 1; |
360 |
return 1; |
313 |
|
361 |
|
314 |
if (pgsql_have_schemas) { |
362 |
if (pgsql_have_schemas) { |
315 |
snprintf(pgbuf, sizeof(pgbuf)-1, PGSQL_GETCOLUMN_TEMPLATE_SCHEMA, table, schema_ce.u.string); |
363 |
snprintf(pgbuf, sizeof(pgbuf)-1, |
|
|
364 |
PGSQL_GETCOLUMN_TEMPLATE_SCHEMA, |
365 |
table, schema_ce.u.string); |
316 |
} else { |
366 |
} else { |
317 |
snprintf(pgbuf, sizeof(pgbuf)-1, PGSQL_GETCOLUMN_TEMPLATE, table); |
367 |
snprintf(pgbuf, sizeof(pgbuf)-1, |
|
|
368 |
PGSQL_GETCOLUMN_TEMPLATE, table); |
318 |
} |
369 |
} |
319 |
|
370 |
|
320 |
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf); |
371 |
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf); |
Lines 333-339
Link Here
|
333 |
for (intaux=0; intaux<PQntuples(result); intaux++) { |
384 |
for (intaux=0; intaux<PQntuples(result); intaux++) { |
334 |
|
385 |
|
335 |
/* replace all underscores with dots */ |
386 |
/* replace all underscores with dots */ |
336 |
strncpy(buf, PQgetvalue(result, intaux, 0), ULOGD_MAX_KEYLEN); |
387 |
strncpy(buf, PQgetvalue(result, intaux, 0), ULOGD_MAX_KEYLEN-1); |
|
|
388 |
buf[ULOGD_MAX_KEYLEN-1] = '\0'; |
337 |
while ((underscore = strchr(buf, '_'))) |
389 |
while ((underscore = strchr(buf, '_'))) |
338 |
*underscore = '.'; |
390 |
*underscore = '.'; |
339 |
|
391 |
|
Lines 352-359
Link Here
|
352 |
ulogd_log(ULOGD_ERROR, "OOM!\n"); |
404 |
ulogd_log(ULOGD_ERROR, "OOM!\n"); |
353 |
return 1; |
405 |
return 1; |
354 |
} |
406 |
} |
355 |
strncpy(f->name, buf, ULOGD_MAX_KEYLEN); |
407 |
strncpy(f->name, buf, ULOGD_MAX_KEYLEN-1); |
|
|
408 |
f->name[ULOGD_MAX_KEYLEN-1] = '\0'; |
356 |
f->id = id; |
409 |
f->id = id; |
|
|
410 |
f->str = 0; |
411 |
if( (typename = PQgetvalue(result, intaux, 1)) != NULL) |
412 |
{ |
413 |
if(strcmp(typename, "inet") == 0 || |
414 |
strstr(typename, "char") != NULL) |
415 |
f->str = 1; |
416 |
} |
357 |
f->next = fields; |
417 |
f->next = fields; |
358 |
fields = f; |
418 |
fields = f; |
359 |
} |
419 |
} |
Lines 386-419
Link Here
|
386 |
if (port) |
446 |
if (port) |
387 |
len += 20; |
447 |
len += 20; |
388 |
|
448 |
|
389 |
connstr = (char *) malloc(len); |
449 |
connstr = (char *) malloc(len+1); |
390 |
if (!connstr) |
450 |
if (!connstr) |
391 |
return 1; |
451 |
return 1; |
|
|
452 |
*connstr = '\0'; |
392 |
|
453 |
|
393 |
if (server) { |
454 |
if (server) { |
394 |
strcpy(connstr, " host="); |
455 |
strncat(connstr, " host=", len-strlen(connstr)); |
395 |
strcat(connstr, server); |
456 |
strncat(connstr, server, len-strlen(connstr)); |
396 |
} |
457 |
} |
397 |
|
458 |
|
398 |
if (port) { |
459 |
if (port) { |
399 |
char portbuf[20]; |
460 |
char portbuf[20]; |
400 |
snprintf(portbuf, sizeof(portbuf), " port=%u", port); |
461 |
snprintf(portbuf, sizeof(portbuf), " port=%u", port); |
401 |
strcat(connstr, portbuf); |
462 |
strncat(connstr, portbuf, len-strlen(connstr)); |
402 |
} |
463 |
} |
403 |
|
464 |
|
404 |
strcat(connstr, " dbname="); |
465 |
strncat(connstr, " dbname=", len-strlen(connstr)); |
405 |
strcat(connstr, db); |
466 |
strncat(connstr, db, len-strlen(connstr)); |
406 |
strcat(connstr, " user="); |
467 |
strncat(connstr, " user=", len-strlen(connstr)); |
407 |
strcat(connstr, user); |
468 |
strncat(connstr, user, len-strlen(connstr)); |
408 |
|
469 |
|
409 |
if (pass) { |
470 |
if (pass) { |
410 |
strcat(connstr, " password="); |
471 |
strncat(connstr, " password=", len-strlen(connstr)); |
411 |
strcat(connstr, pass); |
472 |
strncat(connstr, pass, len-strlen(connstr)); |
412 |
} |
473 |
} |
413 |
|
474 |
|
414 |
dbh = PQconnectdb(connstr); |
475 |
dbh = PQconnectdb(connstr); |
|
|
476 |
free(connstr); |
415 |
if (PQstatus(dbh)!=CONNECTION_OK) { |
477 |
if (PQstatus(dbh)!=CONNECTION_OK) { |
416 |
exit_nicely(dbh); |
478 |
exit_nicely(dbh); |
|
|
479 |
dbh = NULL; |
417 |
return 1; |
480 |
return 1; |
418 |
} |
481 |
} |
419 |
|
482 |
|
Lines 432-454
Link Here
|
432 |
} |
495 |
} |
433 |
|
496 |
|
434 |
if (pgsql_namespace()) { |
497 |
if (pgsql_namespace()) { |
435 |
return 1; |
498 |
PQfinish(dbh); |
|
|
499 |
dbh = NULL; |
436 |
ulogd_log(ULOGD_ERROR, "unable to test for pgsql schemas\n"); |
500 |
ulogd_log(ULOGD_ERROR, "unable to test for pgsql schemas\n"); |
|
|
501 |
return 1; |
437 |
} |
502 |
} |
438 |
|
503 |
|
439 |
/* read the fieldnames to know which values to insert */ |
504 |
/* read the fieldnames to know which values to insert */ |
440 |
if (pgsql_get_columns(table_ce.u.string)) { |
505 |
if (pgsql_get_columns(table_ce.u.string)) { |
|
|
506 |
PQfinish(dbh); |
507 |
dbh = NULL; |
441 |
ulogd_log(ULOGD_ERROR, "unable to get pgsql columns\n"); |
508 |
ulogd_log(ULOGD_ERROR, "unable to get pgsql columns\n"); |
442 |
return 1; |
509 |
return 1; |
443 |
} |
510 |
} |
444 |
pgsql_createstmt(); |
511 |
|
|
|
512 |
if (pgsql_createstmt()) { |
513 |
PQfinish(dbh); |
514 |
dbh = NULL; |
515 |
return 1; |
516 |
} |
445 |
|
517 |
|
446 |
return 0; |
518 |
return 0; |
447 |
} |
519 |
} |
448 |
|
520 |
|
449 |
static void pgsql_fini(void) |
521 |
static void pgsql_fini(void) |
450 |
{ |
522 |
{ |
451 |
PQfinish(dbh); |
523 |
if (dbh) |
|
|
524 |
PQfinish(dbh); |
525 |
if (stmt) |
526 |
{ |
527 |
free(stmt); |
528 |
stmt = NULL; |
529 |
stmt_val = NULL; |
530 |
} |
452 |
} |
531 |
} |
453 |
|
532 |
|
454 |
static ulog_output_t pgsql_plugin = { |
533 |
static ulog_output_t pgsql_plugin = { |