View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2012  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.http;
20  
21  import davmail.Settings;
22  import org.apache.log4j.Logger;
23  
24  import javax.security.auth.login.AppConfigurationEntry;
25  import javax.security.auth.login.Configuration;
26  import java.util.HashMap;
27  
28  /**
29   * Custom JAAS login configuration.
30   * Equivalent to the following configuration:
31   * spnego-client {
32   * com.sun.security.auth.module.Krb5LoginModule required useTicketCache=true renewTGT=true;
33   * };
34   * spnego-server {
35   * com.sun.security.auth.module.Krb5LoginModule required isInitiator=false useKeyTab=false storeKey=true;
36   * };
37   * <p/>
38   */
39  public class KerberosLoginConfiguration extends Configuration {
40      protected static final Logger LOGGER = Logger.getLogger(KerberosLoginConfiguration.class);
41      protected static final AppConfigurationEntry[] CLIENT_LOGIN_MODULE;
42      protected static final AppConfigurationEntry[] SERVER_LOGIN_MODULE;
43  
44      static {
45          HashMap<String, String> clientLoginModuleOptions = new HashMap<>();
46          if (LOGGER.isDebugEnabled()) {
47              clientLoginModuleOptions.put("debug", "true");
48          }
49  
50          clientLoginModuleOptions.put("useTicketCache", "true");
51          clientLoginModuleOptions.put("renewTGT", "true");
52  
53          String krb5ccName = System.getenv().get("KRB5CCNAME");
54          if (krb5ccName != null && !krb5ccName.isEmpty()) {
55              clientLoginModuleOptions.put("ticketCache", krb5ccName);
56          } else if (Settings.isLinux()) {
57              LOGGER.warn("KRB5CCNAME environment variable not set, this may cause Kerberos authentication to fail if default_ccache_name is set to KEYRING");
58          }
59  
60          CLIENT_LOGIN_MODULE = new AppConfigurationEntry[]{new AppConfigurationEntry(
61                  "com.sun.security.auth.module.Krb5LoginModule",
62                  AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
63                  clientLoginModuleOptions)};
64  
65          HashMap<String, String> serverLoginModuleOptions = new HashMap<>();
66          if (LOGGER.isDebugEnabled()) {
67              serverLoginModuleOptions.put("debug", "true");
68          }
69  
70          serverLoginModuleOptions.put("isInitiator", "false"); // acceptor (server) mode
71          serverLoginModuleOptions.put("useKeyTab", "false"); // do not use credentials stored in keytab file
72          serverLoginModuleOptions.put("storeKey", "true"); // store credentials in subject
73          SERVER_LOGIN_MODULE = new AppConfigurationEntry[]{new AppConfigurationEntry(
74                  "com.sun.security.auth.module.Krb5LoginModule",
75                  AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
76                  serverLoginModuleOptions)};
77      }
78  
79      @Override
80      public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
81          if ("spnego-client".equals(name)) {
82              return CLIENT_LOGIN_MODULE;
83          } else if ("spnego-server".equals(name)) {
84              return SERVER_LOGIN_MODULE;
85          } else {
86              return null;
87          }
88      }
89  
90      @Override
91      public void refresh() {
92          // nothing to do
93      }
94  }