Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java
41161 views
/*1* Copyright (c) 1999, 2021, 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*/2425package com.sun.jndi.ldap;2627import jdk.internal.access.SharedSecrets;2829import java.net.MalformedURLException;30import java.net.URL;31import java.net.URLClassLoader;32import java.security.AccessControlContext;33import java.security.AccessController;34import java.security.PrivilegedAction;3536public final class VersionHelper {3738private static final VersionHelper helper = new VersionHelper();3940/**41* Determines whether classes may be loaded from an arbitrary URL code base.42*/43private static final boolean trustURLCodebase;4445/**46* Determines whether objects may be deserialized from the content of47* 'javaSerializedData' attribute.48*/49private static final boolean trustSerialData;5051static {52// System property to control whether classes may be loaded from an53// arbitrary URL code base54String trust = getPrivilegedProperty(55"com.sun.jndi.ldap.object.trustURLCodebase", "false");56trustURLCodebase = "true".equalsIgnoreCase(trust);5758// System property to control whether classes is allowed to be loaded from59// 'javaSerializedData' attribute60String trustSerialDataSp = getPrivilegedProperty(61"com.sun.jndi.ldap.object.trustSerialData", "true");62trustSerialData = "true".equalsIgnoreCase(trustSerialDataSp);63}6465@SuppressWarnings("removal")66private static String getPrivilegedProperty(String propertyName, String defaultVal) {67PrivilegedAction<String> action = () -> System.getProperty(propertyName, defaultVal);68if (System.getSecurityManager() == null) {69return action.run();70} else {71return AccessController.doPrivileged(action);72}73}7475private VersionHelper() {76}7778static VersionHelper getVersionHelper() {79return helper;80}8182/**83* Returns true if deserialization of objects from 'javaSerializedData'84* LDAP attribute is allowed.85*86* @return true if deserialization is allowed; false - otherwise87*/88public static boolean isSerialDataAllowed() {89return trustSerialData;90}9192ClassLoader getURLClassLoader(String[] url) throws MalformedURLException {93ClassLoader parent = getContextClassLoader();94/*95* Classes may only be loaded from an arbitrary URL code base when96* the system property com.sun.jndi.ldap.object.trustURLCodebase97* has been set to "true".98*/99if (url != null && trustURLCodebase) {100return URLClassLoader.newInstance(getUrlArray(url), parent);101} else {102return parent;103}104}105106Class<?> loadClass(String className) throws ClassNotFoundException {107return Class.forName(className, true, getContextClassLoader());108}109110@SuppressWarnings("removal")111Thread createThread(Runnable r) {112AccessControlContext acc = AccessController.getContext();113// 4290486: doPrivileged is needed to create a thread in114// an environment that restricts "modifyThreadGroup".115PrivilegedAction<Thread> act =116() -> SharedSecrets.getJavaLangAccess().newThreadWithAcc(r, acc);117return AccessController.doPrivileged(act);118}119120@SuppressWarnings("removal")121private ClassLoader getContextClassLoader() {122PrivilegedAction<ClassLoader> act =123Thread.currentThread()::getContextClassLoader;124return AccessController.doPrivileged(act);125}126127private static URL[] getUrlArray(String[] url) throws MalformedURLException {128URL[] urlArray = new URL[url.length];129for (int i = 0; i < urlArray.length; i++) {130urlArray[i] = new URL(url[i]);131}132return urlArray;133}134}135136137