Hi,

I am using postfix-pgsql with ssl authentication. Therefore I need to
provide a connection string for postgres connections. A patch is
attached to do so.

See https://bugs.launchpad.net/ubuntu/+source/postfix/+bug/1553928 for
more details.


Description: connection string support for pgsql
 You can use a postgres connection string at the hosts line
 .
 postfix (3.1.0-3) unstable; urgency=medium
Author: Arpad Magosanyi <m...@magwas.rulez.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: other
Bug-Ubuntu: https://launchpad.net/bugs/1553928
Last-Update: <2016-12-11>

--- postfix-3.1.0.orig/src/global/dict_pgsql.c
+++ postfix-3.1.0/src/global/dict_pgsql.c
@@ -180,6 +180,7 @@
 
 #define TYPEUNIX			(1<<0)
 #define TYPEINET			(1<<1)
+#define TYPECONNSTRING		(1<<2)
 
 #define RETRY_CONN_MAX			100
 #define RETRY_CONN_INTV			60	/* 1 minute */
@@ -190,7 +191,7 @@ typedef struct {
     char   *hostname;
     char   *name;
     char   *port;
-    unsigned type;			/* TYPEUNIX | TYPEINET */
+    unsigned type;			/* TYPEUNIX | TYPEINET | TYPECONNSTRING*/
     unsigned stat;			/* STATUNTRIED | STATFAIL | STATCUR */
     time_t  ts;				/* used for attempting reconnection */
 } HOST;
@@ -467,7 +468,8 @@ static HOST *dict_pgsql_get_active(PLPGS
 
     /* try the active connections first; prefer the ones to UNIX sockets */
     if ((host = dict_pgsql_find_host(PLDB, STATACTIVE, TYPEUNIX)) != NULL ||
-	(host = dict_pgsql_find_host(PLDB, STATACTIVE, TYPEINET)) != NULL) {
+	(host = dict_pgsql_find_host(PLDB, STATACTIVE, TYPEINET)) != NULL ||
+	(host = dict_pgsql_find_host(PLDB, STATACTIVE, TYPECONNSTRING)) != NULL) {
 	if (msg_verbose)
 	    msg_info("%s: found active connection to host %s", myname,
 		     host->hostname);
@@ -483,7 +485,9 @@ static HOST *dict_pgsql_get_active(PLPGS
 	   ((host = dict_pgsql_find_host(PLDB, STATUNTRIED | STATFAIL,
 					 TYPEUNIX)) != NULL ||
 	    (host = dict_pgsql_find_host(PLDB, STATUNTRIED | STATFAIL,
-					 TYPEINET)) != NULL)) {
+					 TYPEINET)) != NULL ||
+	    (host = dict_pgsql_find_host(PLDB, STATUNTRIED | STATFAIL,
+					 TYPECONNSTRING)) != NULL)) {
 	if (msg_verbose)
 	    msg_info("%s: attempting to connect to host %s", myname,
 		     host->hostname);
@@ -622,11 +626,21 @@ static PGSQL_RES *plpgsql_query(DICT_PGS
  */
 static void plpgsql_connect_single(HOST *host, char *dbname, char *username, char *password)
 {
-    if ((host->db = PQsetdbLogin(host->name, host->port, NULL, NULL,
-				 dbname, username, password)) == NULL
-	|| PQstatus(host->db) != CONNECTION_OK) {
-	msg_warn("connect to pgsql server %s: %s",
-		 host->hostname, PQerrorMessage(host->db));
+    PGconn *conn;
+    if (msg_verbose)
+	msg_info("dict_pgsql: connecting to host %s (%u)", host->hostname,
+		 host->type);
+    if (host->type = TYPECONNSTRING) {
+	conn = PQconnectdb(host->name);
+    } else {
+	conn =
+	    PQsetdbLogin(host->name, host->port, NULL, NULL, dbname, username,
+			 password);
+    }
+    host->db = conn;
+    if (conn == NULL || PQstatus(host->db) != CONNECTION_OK) {
+	msg_warn("connect to pgsql server %s: %s", host->hostname,
+		 PQerrorMessage(host->db));
 	plpgsql_down_host(host);
 	return;
     }
@@ -815,6 +829,12 @@ static HOST *host_init(const char *hostn
      * Ad-hoc parsing code. Expect "unix:pathname" or "inet:host:port", where
      * both "inet:" and ":port" are optional.
      */
+   if (strncmp(d, "postgresql:", 11) == 0) {
+       host->type = TYPECONNSTRING;
+       host->name = mystrdup(d);
+       host->port = 0;
+   } else {
+
     if (strncmp(d, "unix:", 5) == 0 || strncmp(d, "inet:", 5) == 0)
 	d += 5;
     host->name = mystrdup(d);
@@ -825,11 +845,11 @@ static HOST *host_init(const char *hostn
 	host->type = TYPEINET;
     else
 	host->type = TYPEUNIX;
-
+   }
     if (msg_verbose > 1)
 	msg_info("%s: host=%s, port=%s, type=%s", myname, host->name,
 		 host->port ? host->port : "",
-		 host->type == TYPEUNIX ? "unix" : "inet");
+		 host->type == TYPEUNIX ? "unix" : (host->type == TYPEINET ? "inet" : "connstring"), host->type);
     return host;
 }
 

Reply via email to