1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package davmail.http;
21
22 import org.apache.http.auth.Credentials;
23 import org.apache.http.impl.auth.SPNegoScheme;
24 import org.apache.log4j.Logger;
25 import org.ietf.jgss.*;
26
27 import javax.security.auth.RefreshFailedException;
28 import javax.security.auth.Subject;
29 import javax.security.auth.kerberos.KerberosTicket;
30 import javax.security.auth.login.LoginContext;
31 import javax.security.auth.login.LoginException;
32 import java.security.PrivilegedAction;
33 import java.security.Security;
34
35
36
37
38
39 public class DavMailSPNegoScheme extends SPNegoScheme {
40 protected static final Logger LOGGER = Logger.getLogger(DavMailSPNegoScheme.class);
41 protected static final Object LOCK = new Object();
42 protected static final KerberosHelper.KerberosCallbackHandler KERBEROS_CALLBACK_HANDLER;
43 private static LoginContext clientLoginContext;
44
45 static {
46
47 Security.setProperty("login.configuration.provider", "davmail.http.KerberosLoginConfiguration");
48
49 KERBEROS_CALLBACK_HANDLER = new KerberosHelper.KerberosCallbackHandler();
50 }
51
52 public DavMailSPNegoScheme(final boolean stripPort, final boolean useCanonicalHostname) {
53 super(stripPort, useCanonicalHostname);
54 }
55
56 public DavMailSPNegoScheme(final boolean stripPort) {
57 super(stripPort);
58 }
59
60 public DavMailSPNegoScheme() {
61 super();
62 }
63
64 @Override
65 protected byte[] generateGSSToken(final byte[] input, final Oid oid, final String authServer, final Credentials credentials) throws GSSException {
66 String protocol = "HTTP";
67
68 LOGGER.debug("KerberosHelper.initSecurityContext " + protocol + '@' + authServer + ' ' + input.length + " bytes token");
69
70 synchronized (LOCK) {
71
72 if (clientLoginContext != null) {
73 for (Object ticket : clientLoginContext.getSubject().getPrivateCredentials(KerberosTicket.class)) {
74 KerberosTicket kerberosTicket = (KerberosTicket) ticket;
75 if (kerberosTicket.getServer().getName().startsWith("krbtgt") && !kerberosTicket.isCurrent()) {
76 LOGGER.debug("KerberosHelper.clientLogin cached TGT expired, try to relogin");
77 clientLoginContext = null;
78 }
79 }
80 }
81
82 if (clientLoginContext == null) {
83 final LoginContext localLoginContext;
84 try {
85 localLoginContext = new LoginContext("spnego-client", KERBEROS_CALLBACK_HANDLER);
86 localLoginContext.login();
87 clientLoginContext = localLoginContext;
88 } catch (LoginException e) {
89 LOGGER.error(e.getMessage(), e);
90 throw new GSSException(GSSException.FAILURE);
91 }
92 }
93
94 for (Object ticket : clientLoginContext.getSubject().getPrivateCredentials(KerberosTicket.class)) {
95 KerberosTicket kerberosTicket = (KerberosTicket) ticket;
96 LOGGER.debug("KerberosHelper.clientLogin ticket for " + kerberosTicket.getServer().getName() + " expires at " + kerberosTicket.getEndTime());
97 if (kerberosTicket.getEndTime().getTime() < System.currentTimeMillis() + 10000) {
98 if (kerberosTicket.isRenewable()) {
99 try {
100 kerberosTicket.refresh();
101 } catch (RefreshFailedException e) {
102 LOGGER.debug("KerberosHelper.clientLogin failed to renew ticket " + kerberosTicket);
103 }
104 } else {
105 LOGGER.debug("KerberosHelper.clientLogin ticket is not renewable");
106 }
107 }
108 }
109
110 Object result = internalGenerateGSSToken(input, oid, authServer, credentials);
111
112 if (result instanceof GSSException) {
113 LOGGER.info("KerberosHelper.initSecurityContext exception code " + ((GSSException) result).getMajor() + " minor code " + ((GSSException) result).getMinor() + " message " + ((Throwable) result).getMessage());
114 throw (GSSException) result;
115 }
116
117 LOGGER.debug("KerberosHelper.initSecurityContext return " + ((byte[]) result).length + " bytes token");
118 return (byte[]) result;
119 }
120 }
121
122 protected Object internalGenerateGSSToken(final byte[] input, final Oid oid, final String authServer, final Credentials credentials) {
123 return Subject.doAs(clientLoginContext.getSubject(), (PrivilegedAction<Object>) () -> {
124 Object result;
125 try {
126 result = super.generateGSSToken(input, oid, authServer, credentials);
127 } catch (GSSException e) {
128 result = e;
129 }
130 return result;
131 });
132 }
133
134 }