Skip to content

Commit a3ba3c1

Browse files
committed
Follow-up to "Track StartTlsResponse per connection in JNDIRealm"
Improve clean-up in error handling
1 parent 5f7085b commit a3ba3c1

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

‎java/org/apache/catalina/realm/JNDIRealm.java‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import javax.net.ssl.SSLSocketFactory;
6666

6767
import org.apache.catalina.LifecycleException;
68+
import org.apache.tomcat.util.ExceptionUtils;
6869
import org.apache.tomcat.util.buf.StringUtils;
6970
import org.apache.tomcat.util.collections.SynchronizedStack;
7071
import org.ietf.jgss.GSSContext;
@@ -2794,30 +2795,49 @@ private DirContext createTlsDirContext(JNDIConnection connection, Hashtable<Stri
27942795
}
27952796
}
27962797
LdapContext result = null;
2798+
StartTlsResponse tls = null;
27972799
try {
27982800
result = new InitialLdapContext(env, null);
2799-
StartTlsResponse tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest());
2801+
tls = (StartTlsResponse) result.extendedOperation(new StartTlsRequest());
2802+
if (getHostnameVerifier() != null) {
2803+
tls.setHostnameVerifier(getHostnameVerifier());
2804+
}
2805+
if (getCipherSuitesArray() != null) {
2806+
tls.setEnabledCipherSuites(getCipherSuitesArray());
2807+
}
2808+
SSLSession negotiate = tls.negotiate(getSSLSocketFactory());
2809+
containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol()));
2810+
for (Map.Entry<String,Object> savedEntry : savedEnv.entrySet()) {
2811+
result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue());
2812+
}
2813+
connection.tls = tls;
2814+
} catch (Throwable t) {
2815+
ExceptionUtils.handleThrowable(t);
2816+
NamingException ne;
2817+
if (t instanceof NamingException) {
2818+
ne = (NamingException) t;
2819+
} else {
2820+
ne = new NamingException(t.getMessage());
2821+
ne.initCause(t);
2822+
}
28002823
try {
2801-
if (getHostnameVerifier() != null) {
2802-
tls.setHostnameVerifier(getHostnameVerifier());
2803-
}
2804-
if (getCipherSuitesArray() != null) {
2805-
tls.setEnabledCipherSuites(getCipherSuitesArray());
2824+
if (tls != null) {
2825+
tls.close();
28062826
}
2807-
SSLSession negotiate = tls.negotiate(getSSLSocketFactory());
2808-
containerLog.debug(sm.getString("jndiRealm.negotiatedTls", negotiate.getProtocol()));
2809-
connection.tls = tls;
2810-
} catch (IOException ioe) {
2811-
NamingException ne = new NamingException(ioe.getMessage());
2812-
ne.initCause(ioe);
2813-
throw ne;
2827+
} catch (Throwable t1) {
2828+
ExceptionUtils.handleThrowable(t1);
2829+
ne.addSuppressed(t1);
28142830
}
2815-
} finally {
2816-
if (result != null) {
2817-
for (Map.Entry<String,Object> savedEntry : savedEnv.entrySet()) {
2818-
result.addToEnvironment(savedEntry.getKey(), savedEntry.getValue());
2831+
try {
2832+
if (result != null) {
2833+
result.close();
28192834
}
2835+
} catch (Throwable t2) {
2836+
ExceptionUtils.handleThrowable(t2);
2837+
ne.addSuppressed(t2);
28202838
}
2839+
result = null;
2840+
throw ne;
28212841
}
28222842
return result;
28232843
}

0 commit comments

Comments
 (0)