Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9281300a3af0b4aa8b370b13f5903c5ef13e79f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/****************************************************************************
 * Copyright (c) 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Thomas Joiner - HttpClient 4 implementation
 *     Yatta Solutions - HttpClient 4.5 implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.filetransfer.httpclient45;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.eclipse.ecf.core.util.Proxy;
import org.eclipse.ecf.core.util.ProxyAddress;
import org.eclipse.ecf.core.util.Trace;
import org.eclipse.ecf.internal.provider.filetransfer.DebugOptions;
import org.eclipse.ecf.provider.filetransfer.httpclient45.HttpClientRetrieveFileTransfer;
import org.osgi.framework.FrameworkUtil;

public class HttpClientProxyCredentialProvider extends BasicCredentialsProvider {
	private static final String COMPUTERNAME_ENV = "COMPUTERNAME"; //$NON-NLS-1$
	private static final String HOSTNAME_ENV = "HOSTNAME"; //$NON-NLS-1$
	private static final String LOGONSERVER_ENV = "LOGONSERVER"; //$NON-NLS-1$
	private static final String USER_DOMAIN_ENV = "USERDOMAIN"; //$NON-NLS-1$
	private static final String USER_DNS_DOMAIN_ENV = "USERDNSDOMAIN"; //$NON-NLS-1$

	private static final char BACKSLASH = '\\';
	private static final char SLASH = '/';

	private static final String OSGI_OS = "osgi.os"; //$NON-NLS-1$
	private static final String OSGI_OS_WIN32 = "win32"; //$NON-NLS-1$

	private static final String NTLM_DOMAIN_PROPERTY = "http.auth.ntlm.domain"; //$NON-NLS-1$

	protected Proxy getECFProxy() {
		return null;
	}

	protected boolean allowNTLMAuthentication() {
		DefaultNTLMProxyHandler.setSeenNTLM();
		return Activator.getDefault().getNTLMProxyHandler().allowNTLMAuthentication(null);
	}

	protected Credentials getNTLMCredentials(Credentials credentials) {
		DefaultNTLMProxyHandler.setSeenNTLM();
		Credentials fixed = fixNTCredentials(credentials);
		if (fixed == credentials || allowNTLMAuthentication()) {
			return fixed;
		}
		return null;
	}

	protected NTCredentials getNTLMCredentials(Proxy proxy) {
		if (allowNTLMAuthentication()) {
			return createNTLMCredentials(proxy);
		}
		return null;
	}

	@Override
	public Credentials getCredentials(AuthScope authscope) {
		Trace.entering(Activator.PLUGIN_ID, DebugOptions.METHODS_ENTERING, HttpClientProxyCredentialProvider.class, "getCredentials " + authscope); //$NON-NLS-1$

		// First check to see whether given authscope matches any authscope
		// already cached.
		Credentials result = super.getCredentials(authscope);
		// If we have a match, return credentials
		if (result != null) {
			if ("ntlm".equalsIgnoreCase(authscope.getScheme())) { //$NON-NLS-1$
				// We might have gotten these from a password prompt, making them
				// UsernamePasswordCredentials...
				Credentials fixed = fixNTCredentials(result);
				if (fixed != result) {
					result = fixed;
					setCredentials(authscope, fixed);
				}
			}
			return result;
		}

		// If we don't have a match, first get ECF proxy, if any
		Proxy proxy = getECFProxy();
		if (proxy == null)
			return null;

		// Make sure that authscope and proxy host and port match
		if (!matchAuthScopeAndProxy(authscope, proxy))
			return null;

		// Then match scheme, and get credentials from proxy (if it's scheme we know about)
		Credentials credentials = null;
		if ("ntlm".equalsIgnoreCase(authscope.getScheme())) { //$NON-NLS-1$
			credentials = getNTLMCredentials(proxy);
		} else if ("basic".equalsIgnoreCase(authscope.getScheme()) || //$NON-NLS-1$
				"digest".equalsIgnoreCase(authscope.getScheme())) { //$NON-NLS-1$
			final String proxyUsername = proxy.getUsername();
			final String proxyPassword = proxy.getPassword();
			// If credentials present for proxy then we're done
			if (proxyUsername != null) {
				credentials = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
			}
		} else if ("negotiate".equalsIgnoreCase(authscope.getScheme())) { //$NON-NLS-1$
			Trace.trace(Activator.PLUGIN_ID, "SPNEGO is not supported, if you can contribute support, please do so."); //$NON-NLS-1$
		} else {
			Trace.trace(Activator.PLUGIN_ID, "Unrecognized authentication scheme."); //$NON-NLS-1$
		}

		// Put found credentials in cache for next time
		if (credentials != null)
			setCredentials(authscope, credentials);

		return credentials;
	}

	private static boolean matchAuthScopeAndProxy(AuthScope authscope, Proxy proxy) {
		ProxyAddress proxyAddress = proxy.getAddress();
		return (authscope.getHost().equals(proxyAddress.getHostName()) && (authscope.getPort() == proxyAddress.getPort()));
	}

	/**
	 * @param p
	 *              proxy to create NTCredentials for
	 * @return NTCredentials new ntlm credentials given proxy
	 * @since 5.0
	 */
	public static NTCredentials createNTLMCredentials(Proxy p) {
		if (p == null) {
			return null;
		}
		return createNTLMCredentials(p.getUsername(), p.getPassword());
	}

	/**
	 * @param p
	 *              proxy to create NTCredentials for
	 * @return NTCredentials new ntlm credentials given proxy
	 * @since 5.0
	 */
	protected static NTCredentials createNTLMCredentials(String username, String password) {
		String un = getNTLMUserName(username);
		String domain = getNTLMDomainName(username);
		if (un == null || domain == null || un.isEmpty() || domain.isEmpty()) {
			return null;
		}

		String workstation = getNTLMWorkstation();

		return new NTCredentials(un, password, workstation, domain);
	}

	public static Credentials fixNTCredentials(Credentials credentials) {
		if (credentials == null) {
			return null;
		}
		if (credentials instanceof NTCredentials) {
			NTCredentials ntCreds = (NTCredentials) credentials;
			String userName = ntCreds.getUserName();
			String domainUser = getNTLMUserName(userName);
			if (ntCreds.getDomain() == null || domainUser != userName) {
				String domain = getNTLMDomainName(userName);
				String workstation = getNTLMWorkstation();
				return new NTCredentials(domainUser, ntCreds.getPassword(), workstation, domain);
			}
		} else if (credentials instanceof UsernamePasswordCredentials) {
			UsernamePasswordCredentials basicCredentials = (UsernamePasswordCredentials) credentials;
			return createNTLMCredentials(basicCredentials.getUserName(), basicCredentials.getPassword());
		}
		return credentials;
	}

	public static String getNTLMWorkstation() {
		String dnsName = getDnsHostName();
		if (dnsName != null) {
			return dnsName;
		}
		String hostName = getEnvHostName();
		if (hostName != null) {
			return hostName;
		}
		return null;
	}

	private static boolean isWindows() {
		String os = FrameworkUtil.getBundle(HttpClientProxyCredentialProvider.class).getBundleContext().getProperty(OSGI_OS);
		return OSGI_OS_WIN32.equalsIgnoreCase(os);
	}

	private static String getEnvHostName() {
		boolean isWindows = isWindows();
		String envKey = isWindows ? COMPUTERNAME_ENV : HOSTNAME_ENV;
		String hostName = System.getenv(envKey);
		if (hostName == null && isWindows) {
			hostName = System.getenv(HOSTNAME_ENV);
		}
		return hostName;
	}

	private static String getDnsHostName() {
		try {
			InetAddress localHost = InetAddress.getLocalHost();
			if (!localHost.isLoopbackAddress()) {
				String hostName = localHost.getHostName();
				if (hostName != null && !"".equals(hostName) && !"localhost".equals(hostName) //$NON-NLS-1$ //$NON-NLS-2$
						&& !hostName.equals(localHost.getHostAddress())) {
					return hostName;
				}
			}
		} catch (UnknownHostException e) {
			Trace.catching(Activator.PLUGIN_ID, DebugOptions.EXCEPTIONS_CATCHING, HttpClientRetrieveFileTransfer.class, "getDnsHostName", e); //$NON-NLS-1$
		}
		return null;
	}

	public static String getNTLMDomainName(String userName) {
		String domain = getDomainFromUserName(userName);
		if (domain != null) {
			return domain;
		}
		domain = getDomainFromSystemProperties();
		if (domain != null) {
			return domain;
		}
		return getDomainFromEnv();
	}

	private static String getDomainFromEnv() {
		// FIXME some systems seem to need the DNS domain name instead of the NetBIOS
		// name (from USERDNSDOMAIN env variable)
		String domain = System.getenv(USER_DOMAIN_ENV);
		if (domain != null) {
			if (isRealDomain(domain)) {
				return domain;
			}
			return null;
		}
		domain = System.getenv(USER_DNS_DOMAIN_ENV);
		if (domain != null) {
			return domain;
		}
		return null;
	}

	private static String getDomainFromSystemProperties() {
		String domain;
		domain = System.getProperty(NTLM_DOMAIN_PROPERTY);
		return domain;
	}

	private static boolean isRealDomain(String domain) {
		String hostName = getEnvHostName();
		if (!domain.equalsIgnoreCase(hostName)) {
			return true;
		}
		if (isWindows()) {
			String logonHost = System.getenv(LOGONSERVER_ENV);
			if (logonHost != null && !logonHost.equalsIgnoreCase(Character.toString(BACKSLASH) + BACKSLASH + domain)) {
				return true;
			}
		}
		return false;
	}

	private static String getDomainFromUserName(String userName) {
		if (userName == null) {
			return null;
		}
		int pos = getNTLMDomainUserSeparatorPos(userName);
		if (pos != -1) {
			return userName.substring(0, pos);
		}
		return null;
	}

	private static int getNTLMDomainUserSeparatorPos(String userName) {
		int pos = userName.indexOf(BACKSLASH);
		if (pos == -1) {
			pos = userName.indexOf(SLASH);
		}
		return pos;
	}

	public static String getNTLMUserName(String userName) {
		if (userName == null) {
			return null;
		}
		int pos = getNTLMDomainUserSeparatorPos(userName);
		if (pos == -1) {
			return userName;
		}
		if (userName.length() > pos + 1 && (userName.charAt(pos + 1) == SLASH || userName.charAt(pos + 1) == BACKSLASH)) {
			pos++;
		}
		if (userName.length() >= pos + 1) {
			return userName.substring(pos + 1);
		}
		return userName;
	}

}

Back to the top