From 84ac39e9ffe8e4869b1c53db0eacd1571b90e0a5 Mon Sep 17 00:00:00 2001
From: Pavel Kajaba <pkajaba@redhat.com>
Date: Tue, 19 Jan 2016 12:22:04 +0100
Subject: [PATCH] Disable using SSPI under Linux

---
 .../postgresql/core/v3/ConnectionFactoryImpl.java  | 43 +++++++++++++++++++---
 .../main/java/org/postgresql/sspi/ISSPIClient.java | 37 +++++++++++++++++++
 .../main/java/org/postgresql/sspi/SSPIClient.java  |  2 +-
 3 files changed, 76 insertions(+), 6 deletions(-)
 create mode 100644 pgjdbc/src/main/java/org/postgresql/sspi/ISSPIClient.java

diff --git a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java
index 5f35858..e304fbb 100644
--- a/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java
+++ b/pgjdbc/src/main/java/org/postgresql/core/v3/ConnectionFactoryImpl.java
@@ -24,7 +24,7 @@ import org.postgresql.hostchooser.HostChooser;
 import org.postgresql.hostchooser.HostChooserFactory;
 import org.postgresql.hostchooser.HostRequirement;
 import org.postgresql.hostchooser.HostStatus;
-import org.postgresql.sspi.SSPIClient;
+import org.postgresql.sspi.ISSPIClient;
 import org.postgresql.util.GT;
 import org.postgresql.util.HostSpec;
 import org.postgresql.util.MD5Digest;
@@ -68,6 +68,37 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
   private static class UnsupportedProtocolException extends IOException {
   }
 
+  private ISSPIClient SSPIfactory(PGStream pgStream,
+      String spnServiceClass,
+      boolean enableNegotiate,
+      Logger logger) {
+      // arguments to initialize windows version of class
+      Class[] cArg = new Class[4];
+      cArg[0] = PGStream.class;
+      cArg[1] = String.class;
+      cArg[2] = boolean.class;
+      cArg[3] = Logger.class;
+
+      Class c = null;
+      String osName = System.getProperty("os.name");
+      if (osName.startsWith("Linux")) {
+          try {
+              c = Class.forName("org.postgresql.sspi.ISSPIClient");
+              return (ISSPIClient) c.getDeclaredConstructor().newInstance();
+          } catch (Throwable e) {
+              logger.info("Unable to find SSPI class for Linux", e);
+          }
+      } else {
+          try {
+              c = Class.forName("org.postgresql.sspi.SSPIClient");
+              return (ISSPIClient) c.getDeclaredConstructor(cArg).newInstance(pgStream, spnServiceClass, enableNegotiate, logger);
+          } catch (Throwable e) {
+               logger.info("Unable to find SSPI class for Linux", e);
+          }
+      }
+      return null;
+  }
+
   public ProtocolConnection openConnectionImpl(HostSpec[] hostSpecs, String user, String database,
       Properties info, Logger logger) throws SQLException {
     // Extract interesting values from the info properties:
@@ -404,7 +435,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
     String password = PGProperty.PASSWORD.get(info);
 
     /* SSPI negotiation state, if used */
-    SSPIClient sspiClient = null;
+    ISSPIClient sspiClient = null;
 
     try {
       authloop: while (true) {
@@ -562,10 +593,12 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
                   logger.debug(
                       "Using JSSE GSSAPI, gssapi requested by server and gsslib=sspi not forced");
                 } else {
+
                   /* Determine if SSPI is supported by the client */
-                  sspiClient = new SSPIClient(pgStream, PGProperty.SSPI_SERVICE_CLASS.get(info),
-                      /* Use negotiation for SSPI, or if explicitly requested for GSS */
-                      areq == AUTH_REQ_SSPI || (areq == AUTH_REQ_GSS && usespnego), logger);
+                  sspiClient = SSPIfactory(pgStream,
+                    PGProperty.SSPI_SERVICE_CLASS.get(info),
+                    /* Use negotiation for SSPI, or if explicitly requested for GSS */
+                    areq == AUTH_REQ_SSPI || (areq == AUTH_REQ_GSS && usespnego), logger);
 
                   useSSPI = sspiClient.isSSPISupported();
                   if (logger.logDebug()) {
diff --git a/pgjdbc/src/main/java/org/postgresql/sspi/ISSPIClient.java b/pgjdbc/src/main/java/org/postgresql/sspi/ISSPIClient.java
new file mode 100644
index 0000000..27add44
--- /dev/null
+++ b/pgjdbc/src/main/java/org/postgresql/sspi/ISSPIClient.java
@@ -0,0 +1,37 @@
+package org.postgresql.sspi;
+
+import java.io.IOException;
+import java.sql.SQLException;
+
+import org.postgresql.core.Logger;
+import org.postgresql.core.PGStream;
+import org.postgresql.util.HostSpec;
+import org.postgresql.util.PSQLException;
+import org.postgresql.util.PSQLState;
+
+/**
+ * Use Waffle-JNI to support SSPI authentication when PgJDBC is running on a Windows 
+ * client and talking to a Windows server.
+ * 
+ * SSPI is not supported on a non-Windows client.
+ * 
+ * 
+ * @author craig
+ *
+ */
+public class ISSPIClient {
+
+	public boolean isSSPISupported() {
+		return false;
+	}
+	
+	public void startSSPI() throws SQLException, IOException {
+	}
+	
+	public void continueSSPI(int msgLength) throws SQLException, IOException {
+	}
+	
+	public void dispose() {
+	}
+
+}
diff --git a/pgjdbc/src/main/java/org/postgresql/sspi/SSPIClient.java b/pgjdbc/src/main/java/org/postgresql/sspi/SSPIClient.java
index a2b32d0..789dcbd 100644
--- a/pgjdbc/src/main/java/org/postgresql/sspi/SSPIClient.java
+++ b/pgjdbc/src/main/java/org/postgresql/sspi/SSPIClient.java
@@ -26,7 +26,7 @@ import java.sql.SQLException;
  *
  * @author craig
  */
-public class SSPIClient {
+public class SSPIClient extends ISSPIClient {
 
   public static String SSPI_DEFAULT_SPN_SERVICE_CLASS = "POSTGRES";
 
-- 
2.5.0

