From patchwork Fri Dec 21 14:01:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Leblond X-Patchwork-Id: 207819 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 985F52C0082 for ; Sat, 22 Dec 2012 01:02:55 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751484Ab2LUOCy (ORCPT ); Fri, 21 Dec 2012 09:02:54 -0500 Received: from ks28632.kimsufi.com ([91.121.96.152]:46339 "EHLO ks28632.kimsufi.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751363Ab2LUOCy (ORCPT ); Fri, 21 Dec 2012 09:02:54 -0500 Received: from ip-37-24-162-232.unitymediagroup.de ([37.24.162.232] helo=localhost.localdomain) by ks28632.kimsufi.com with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1Tm3Bd-0000ou-38; Fri, 21 Dec 2012 15:02:53 +0100 From: Eric Leblond To: netfilter-devel Cc: Bob Hockney , Eric Leblond Subject: [PATCH 2/3] Handle postgresql schemas correctly Date: Fri, 21 Dec 2012 15:01:52 +0100 Message-Id: <1356098513-18684-3-git-send-email-eric@regit.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1356098513-18684-1-git-send-email-eric@regit.org> References: <1356098513-18684-1-git-send-email-eric@regit.org> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org From: Bob Hockney Add 'schema' variable to look into corresponding schema. Signed-off-by: Eric Leblond --- output/pgsql/ulogd_output_PGSQL.c | 35 +++++++++++++++++++++++++++++++---- ulogd.conf.in | 4 ++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/output/pgsql/ulogd_output_PGSQL.c b/output/pgsql/ulogd_output_PGSQL.c index cd793ca..f246153 100644 --- a/output/pgsql/ulogd_output_PGSQL.c +++ b/output/pgsql/ulogd_output_PGSQL.c @@ -105,9 +105,16 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi) } if (PQresultStatus(pi->pgres) == PGRES_TUPLES_OK) { - ulogd_log(ULOGD_DEBUG, "using schema %s\n", - schema_ce(upi->config_kset).u.string); - pi->db_inst.schema = schema_ce(upi->config_kset).u.string; + if (PQntuples(pi->pgres)) { + ulogd_log(ULOGD_DEBUG, "using schema %s\n", + schema_ce(upi->config_kset).u.string); + pi->db_inst.schema = schema_ce(upi->config_kset).u.string; + } else { + ulogd_log(ULOGD_ERROR, "schema %s not found: %s\n", + schema_ce(upi->config_kset).u.string, PQerrorMessage(pi->dbh)); + PQclear(pi->pgres); + return -1; + } } else { pi->db_inst.schema = NULL; } @@ -223,6 +230,8 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi) char *user = user_ce(upi->config_kset).u.string; char *pass = pass_ce(upi->config_kset).u.string; char *db = db_ce(upi->config_kset).u.string; + char *schema = NULL; + char pgbuf[128]; /* 80 is more than what we need for the fixed parts below */ len = 80 + strlen(user) + strlen(db); @@ -270,11 +279,29 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi) } if (pgsql_namespace(upi)) { - ulogd_log(ULOGD_ERROR, "unable to test for pgsql schemas\n"); + ulogd_log(ULOGD_ERROR, "problem testing for pgsql schemas\n"); close_db_pgsql(upi); return -1; } + pi=(struct pgsql_instance *)upi->private; + schema = pi->db_inst.schema; + + if (!(schema == NULL) && (strcmp(schema,"public"))) { + snprintf(pgbuf, 128, "SET search_path='%.63s', \"$user\", 'public'", schema); + pi->pgres = PQexec(pi->dbh, pgbuf); + if ((PQresultStatus(pi->pgres) == PGRES_COMMAND_OK)) { + PQclear(pi->pgres); + } else { + ulogd_log(ULOGD_ERROR, "could not set search path to (%s): %s\n", + schema, PQerrorMessage(pi->dbh)); + PQclear(pi->pgres); + close_db_pgsql(upi); + return -1; + } + + } + return 0; } diff --git a/ulogd.conf.in b/ulogd.conf.in index 3bd464b..c630b88 100644 --- a/ulogd.conf.in +++ b/ulogd.conf.in @@ -213,6 +213,7 @@ db="nulog" host="localhost" user="nupik" table="ulog" +#schema="public" pass="changeme" procedure="INSERT_PACKET_FULL" @@ -221,6 +222,7 @@ db="nulog" host="localhost" user="nupik" table="ulog2_ct" +#schema="public" pass="changeme" procedure="INSERT_CT" @@ -229,6 +231,7 @@ db="nulog" host="localhost" user="nupik" table="ulog2_ct" +#schema="public" pass="changeme" procedure="INSERT_OR_REPLACE_CT" @@ -237,6 +240,7 @@ db="nulog" host="localhost" user="nupik" table="nfacct" +#schema="public" pass="changeme" procedure="INSERT_NFACCT"