1 /*
2 * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3 * Copyright (C) 2010 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
20 package davmail.exchange.auth;
21
22 import davmail.Settings;
23 import davmail.http.HttpClientAdapter;
24 import org.apache.http.HttpStatus;
25 import org.apache.http.client.methods.CloseableHttpResponse;
26 import org.apache.http.client.methods.HttpGet;
27
28 import java.io.IOException;
29
30 /**
31 * Implementation of -kerberos command line option, check Kerberos authentication.
32 */
33 public class KerberosCheck {
34
35 public boolean checkKerberosAuthentication() {
36 // enable debug output for Kerberos
37 System.setProperty("sun.security.krb5.debug", "true");
38 // force Kerberos authentication
39 Settings.setProperty("davmail.enableKerberos", "true");
40
41 String url = Settings.getProperty("davmail.url");
42 if (url == null || !url.toLowerCase().endsWith("/ews/exchange.asmx")) {
43 System.out.println("Unable to check Kerberos authentication, invalid Exchange URL, must end with /ews/exchange.asmx");
44 return false;
45 }
46 // try to authenticate on EWS endpoint
47 try (HttpClientAdapter httpClient = new HttpClientAdapter(url)) {
48 HttpGet httpGet = new HttpGet(url);
49 try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
50 return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
51 }
52 } catch (IOException e) {
53 System.out.println("Kerberos authentication failed");
54 }
55 return false;
56 }
57 }