Path: blob/master/src/java.base/windows/native/libnet/NTLMAuthentication.c
41119 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <jni.h>26#include <windows.h>27#include "jni_util.h"28#include "jdk_util.h"29#include <urlmon.h>3031typedef HRESULT (WINAPI *CoInternetCreateSecurityManagerType)32(IServiceProvider*,IInternetSecurityManager**,DWORD);3334static CoInternetCreateSecurityManagerType fn_CoInternetCreateSecurityManager;3536JNIEXPORT jboolean JNICALL37Java_sun_net_www_protocol_http_ntlm_NTLMAuthentication_isTrustedSiteAvailable38(JNIEnv *env, jclass clazz)39{40HMODULE libUrlmon = JDK_LoadSystemLibrary("urlmon.dll");41if (libUrlmon != NULL) {42fn_CoInternetCreateSecurityManager = (CoInternetCreateSecurityManagerType)43GetProcAddress(libUrlmon, "CoInternetCreateSecurityManager");44if (fn_CoInternetCreateSecurityManager != NULL) {45return JNI_TRUE;46}47}48return JNI_FALSE;49}5051JNIEXPORT jboolean JNICALL52Java_sun_net_www_protocol_http_ntlm_NTLMAuthentication_isTrustedSite053(JNIEnv *env, jclass clazz, jstring url)54{55HRESULT hr;56DWORD dwZone;57DWORD pPolicy = 0;58IInternetSecurityManager *spSecurityManager;59jboolean ret;6061if (fn_CoInternetCreateSecurityManager == NULL)62return JNI_FALSE;6364// Create IInternetSecurityManager65hr = fn_CoInternetCreateSecurityManager(NULL, &spSecurityManager, (DWORD)0);66if (FAILED(hr)) {67return JNI_FALSE;68}6970const LPCWSTR bstrURL = (LPCWSTR)((*env)->GetStringChars(env, url, NULL));71if (bstrURL == NULL) {72if (!(*env)->ExceptionCheck(env))73JNU_ThrowOutOfMemoryError(env, NULL);74spSecurityManager->lpVtbl->Release(spSecurityManager);75return JNI_FALSE;76}7778// Determines the policy for the URLACTION_CREDENTIALS_USE action and display79// a user interface, if the policy indicates that the user should be queried80hr = spSecurityManager->lpVtbl->ProcessUrlAction(81spSecurityManager,82bstrURL,83URLACTION_CREDENTIALS_USE,84(LPBYTE)&pPolicy,85sizeof(DWORD), 0, 0, 0, 0);8687if (FAILED(hr)) {88ret = JNI_FALSE;89goto cleanupAndReturn;90}9192// If these two User Authentication Logon options is selected93// Anonymous logon94// Prompt for user name and password95if (pPolicy == URLPOLICY_CREDENTIALS_ANONYMOUS_ONLY ||96pPolicy == URLPOLICY_CREDENTIALS_MUST_PROMPT_USER) {97ret = JNI_FALSE;98goto cleanupAndReturn;99}100101// Option "Automatic logon with current user name and password" is selected102if (pPolicy == URLPOLICY_CREDENTIALS_SILENT_LOGON_OK) {103ret = JNI_TRUE;104goto cleanupAndReturn;105}106107// Option "Automatic logon only in intranet zone" is selected108if (pPolicy == URLPOLICY_CREDENTIALS_CONDITIONAL_PROMPT) {109110// Gets the zone index from the specified URL111hr = spSecurityManager->lpVtbl->MapUrlToZone(112spSecurityManager, bstrURL, &dwZone, 0);113if (FAILED(hr)) {114ret = JNI_FALSE;115goto cleanupAndReturn;116}117118// Check if the URL is in Local or Intranet zone119if (dwZone == URLZONE_INTRANET || dwZone == URLZONE_LOCAL_MACHINE) {120ret = JNI_TRUE;121goto cleanupAndReturn;122}123}124ret = JNI_FALSE;125126cleanupAndReturn:127(*env)->ReleaseStringChars(env, url, bstrURL);128spSecurityManager->lpVtbl->Release(spSecurityManager);129return ret;130}131132133