From: sulfinu@gmail.com
To: pgsql-jdbc@postgresql.org
Subject: JDBC driver patch for non-ASCII users
Date: Fri, 7 Dec 2007 14:48:21 +0200
Message-ID: <200712071448.21411.sulfinu@gmail.com> (raw)
Hello.
A few weeks ago I ran into a problem with the JDBC authentication, namely I
wasn't able to connect to a PostgreSQL database whose name used non-ASCII
characters. I've discussed the issue on the pgsql-hackers list and come up
with the attached solution which has been used in an ISO-8859-2 environment
since then.
The purpose of this patch is to allow user names, user passwords and database
names to contain non-ASCII characters. The only restriction upon this was to
make sure that authentications that succeeded before should also succeed
after the patch was applied.
This patch maximizes the chances for the authentication to succeed and on top
of that, it makes possible to configure the PostgreSQL database so as the
authentication could always work. Without it, the behaviour of the current
JDBC driver is UNDETERMINED in a non-ASCII environment (for disbelievers:
check the javadoc for String.getBytes() and String.getBytes(String)).
During the authentication phase, the current JDBC driver uses the poor old
ASCII encoding regardless of the environment it is used in. On the other
side, postgres performs no conversion on the byte representation of the user,
password and database strings submitted by the driver. It is only afterwards
and the server takes into account the use of a UTF-8 encoding that the client
announces.
What I proposed below is for the driver to use the environment's encoding in
an attempt to match the encoding used at server side to store the user,
password and database strings, provided that this local encoding is an ASCII
extension. If it's not, the driver uses the UTF-8 encoding, thus allowing for
a setup where things can work regardless of the environment (specifically,
the database cluster should be (re)created with the UTF-8 encoding).
I suspect that there are CJK encodings that are ASCII extensions; if so,
please add them in the Encoding.isAsciiExtension(String) method. I know next
to nothing about the Asian encodings.
Regards.
Attachments:l10n_authentication.diff (text/x-diff, 7.4K) download
diff -rub postgresql-jdbc-8.2-506.src/build.xml src/build.xml
--- postgresql-jdbc-8.2-506.src/build.xml 2007-04-24 21:19:07.000000000 +0300+++ src/build.xml 2007-11-28 19:57:59.000000000 +0200@@ -138,7 +138,7 @@
<!-- jdbc3 subpackage -->
<include name="${package}/jdbc3/Abstract*.java" if="jdbc3plus" />
- <include name="${package}/jdbc3/PSQLSavepoint.java" if="jdbcplus" />+ <include name="${package}/jdbc3/PSQLSavepoint.java" if="jdbc3plus" />
<include name="${package}/jdbc3/Jdbc3Array.java" if="jdbc3"/>
<include name="${package}/jdbc3/Jdbc3Blob.java" if="jdbc3"/>
diff -rub postgresql-jdbc-8.2-506.src/org/postgresql/core/Encoding.java src/org/postgresql/core/Encoding.java
--- postgresql-jdbc-8.2-506.src/org/postgresql/core/Encoding.java 2006-10-30 20:30:16.000000000 +0200+++ src/org/postgresql/core/Encoding.java 2007-11-28 19:24:53.000000000 +0200@@ -23,7 +23,27 @@
*/
public class Encoding
{
- private static final Encoding DEFAULT_ENCODING = new Encoding(null);+ /**+ * The charset used for encoding user name, database name and password during the authentication phase+ * of a connection (before things like encoding can be "discussed" with the server). + */+ public static final String AUTHENTICATION_PHASE_ENCODING;+ static+ {+ // beginning with version 1.5 there's a method in Charset that gives the platform encoding + String platformEncoding = null;+ try+ {+ platformEncoding = System.getProperty("file.encoding");+ }+ catch (Exception e)+ {}+ if (isAsciiExtension(platformEncoding))+ AUTHENTICATION_PHASE_ENCODING = platformEncoding;+ else AUTHENTICATION_PHASE_ENCODING = "UTF-8"; // this is guaranteed to be supported and is also an ASCII extension+ }+ + private static final Encoding DEFAULT_ENCODING = new Encoding(AUTHENTICATION_PHASE_ENCODING);
/*
* Preferred JVM encodings for backend encodings.
@@ -245,6 +265,40 @@
}
/**
+ * Tells wheter the submitted JVM encoding is known to be an ASCII extension.+ * An encoding is an ASCII extension if any string of valid ASCII characters is a valid string with the same+ * binary representation in this encoding as well.+ * @param encodingName the JVM charset name+ * @return <code>true</code> when the supplied charset is known to be an ASCII extension; a <code>false</code>+ * does not necessarily imply that the supplied charset is not an ASCII extension, but merely that the fact is not known. + */+ public static boolean isAsciiExtension(String encodingName)+ {+ if (encodingName == null)+ return false;+ + // bring all letters to lower case and eliminate delimiters (see the legal charset names in java.nio.charset.Charset)+ StringBuffer enc = new StringBuffer(encodingName.toLowerCase());+ int i = 0;+ char c;+ while (i < enc.length())+ {+ c = enc.charAt(i);+ if (c == '-' || c == '.' || c == ':' || c == '_')+ enc.deleteCharAt(i);+ else i++;+ }+ encodingName = enc.toString(); + if (encodingName.startsWith("latin")+ || encodingName.startsWith("iso8859")+ || encodingName.startsWith("cp125")+ || encodingName.startsWith("windows125")+ || encodingName.startsWith("utf8")) // others may exist, please add if so...+ return true;+ return false;+ }++ /**
* Test if an encoding is available in the JVM.
*
* @param encodingName the JVM encoding name to test
diff -rub postgresql-jdbc-8.2-506.src/org/postgresql/core/PGStream.java src/org/postgresql/core/PGStream.java
--- postgresql-jdbc-8.2-506.src/org/postgresql/core/PGStream.java 2006-10-30 20:30:16.000000000 +0200+++ src/org/postgresql/core/PGStream.java 2007-11-28 19:24:53.000000000 +0200@@ -57,7 +57,7 @@
this.port = port;
changeSocket(new Socket(host, port));
- setEncoding(Encoding.getJVMEncoding("US-ASCII"));+ setEncoding(Encoding.defaultEncoding());
}
public String getHost() {
diff -rub postgresql-jdbc-8.2-506.src/org/postgresql/core/v3/ConnectionFactoryImpl.java src/org/postgresql/core/v3/ConnectionFactoryImpl.java
--- postgresql-jdbc-8.2-506.src/org/postgresql/core/v3/ConnectionFactoryImpl.java 2006-12-01 10:53:45.000000000 +0200+++ src/org/postgresql/core/v3/ConnectionFactoryImpl.java 2007-11-28 19:24:53.000000000 +0200@@ -220,8 +220,8 @@
byte[][] encodedParams = new byte[params.length * 2][];
for (int i = 0; i < params.length; ++i)
{
- encodedParams[i*2] = params[i][0].getBytes("US-ASCII");- encodedParams[i*2 + 1] = params[i][1].getBytes("US-ASCII");+ encodedParams[i*2] = params[i][0].getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING);+ encodedParams[i*2 + 1] = params[i][1].getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING);
length += encodedParams[i * 2].length + 1 + encodedParams[i * 2 + 1].length + 1;
}
@@ -290,7 +290,7 @@
byte[] rst = new byte[2];
rst[0] = (byte)pgStream.ReceiveChar();
rst[1] = (byte)pgStream.ReceiveChar();
- String salt = new String(rst, 0, 2, "US-ASCII");+ String salt = new String(rst, 0, 2, Encoding.AUTHENTICATION_PHASE_ENCODING);
if (logger.logDebug())
logger.debug(" <=BE AuthenticationReqCrypt(salt='" + salt + "')");
@@ -299,7 +299,7 @@
throw new PSQLException(GT.tr("The server requested password-based authentication, but no password was provided."), PSQLState.CONNECTION_REJECTED);
String result = UnixCrypt.crypt(salt, password);
- byte[] encodedResult = result.getBytes("US-ASCII");+ byte[] encodedResult = result.getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING);
if (logger.logDebug())
logger.debug(" FE=> Password(crypt='" + result + "')");
@@ -351,7 +351,7 @@
if (password == null)
throw new PSQLException(GT.tr("The server requested password-based authentication, but no password was provided."), PSQLState.CONNECTION_REJECTED);
- byte[] encodedPassword = password.getBytes("US-ASCII");+ byte[] encodedPassword = password.getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING);
pgStream.SendChar('p');
pgStream.SendInteger4(4 + encodedPassword.length + 1);
diff -rub postgresql-jdbc-8.2-506.src/org/postgresql/util/MD5Digest.java src/org/postgresql/util/MD5Digest.java
--- postgresql-jdbc-8.2-506.src/org/postgresql/util/MD5Digest.java 2005-01-11 10:25:49.000000000 +0200+++ src/org/postgresql/util/MD5Digest.java 2007-11-28 19:24:53.000000000 +0200@@ -18,6 +18,8 @@
import java.security.*;
+import org.postgresql.core.Encoding;+
public class MD5Digest
{
private MD5Digest()
@@ -45,8 +47,8 @@
{
md = MessageDigest.getInstance("MD5");
- md.update(password.getBytes("US-ASCII"));- md.update(user.getBytes("US-ASCII"));+ md.update(password.getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING));+ md.update(user.getBytes(Encoding.AUTHENTICATION_PHASE_ENCODING));
temp_digest = md.digest();
bytesToHex(temp_digest, hex_digest, 0);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: sulfinu@gmail.com
Subject: Re: JDBC driver patch for non-ASCII users
In-Reply-To: <200712071448.21411.sulfinu@gmail.com>
References: <200712071448.21411.sulfinu@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox