RMIJRMPServerImpl.java 10.1 KB
Newer Older
D
duke 已提交
1
/*
S
sjiang 已提交
2
 * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28
 */

package javax.management.remote.rmi;

import java.io.IOException;
29
import sun.misc.ObjectInputFilter;
D
duke 已提交
30 31 32 33 34 35 36 37 38 39 40 41
import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.server.RemoteObject;
import java.util.Map;
import java.util.Collections;
import javax.security.auth.Subject;

import com.sun.jmx.remote.internal.RMIExporter;
42
import com.sun.jmx.remote.util.EnvHelp;
S
sjiang 已提交
43
import java.util.ArrayList;
44 45 46
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
S
sjiang 已提交
47 48
import sun.reflect.misc.ReflectUtil;
import sun.rmi.server.DeserializationChecker;
49 50
import sun.rmi.server.UnicastServerRef;
import sun.rmi.server.UnicastServerRef2;
51
import sun.rmi.transport.LiveRef;
D
duke 已提交
52 53 54 55 56 57 58 59 60 61 62

/**
 * <p>An {@link RMIServer} object that is exported through JRMP and that
 * creates client connections as RMI objects exported through JRMP.
 * User code does not usually reference this class directly.</p>
 *
 * @see RMIServerImpl
 *
 * @since 1.5
 */
public class RMIJRMPServerImpl extends RMIServerImpl {
S
sjiang 已提交
63

D
duke 已提交
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
    /**
     * <p>Creates a new {@link RMIServer} object that will be exported
     * on the given port using the given socket factories.</p>
     *
     * @param port the port on which this object and the {@link
     * RMIConnectionImpl} objects it creates will be exported.  Can be
     * zero, to indicate any available port.
     *
     * @param csf the client socket factory for the created RMI
     * objects.  Can be null.
     *
     * @param ssf the server socket factory for the created RMI
     * objects.  Can be null.
     *
     * @param env the environment map.  Can be null.
     *
     * @exception IOException if the {@link RMIServer} object
     * cannot be created.
     *
     * @exception IllegalArgumentException if <code>port</code> is
     * negative.
     */
    public RMIJRMPServerImpl(int port,
                             RMIClientSocketFactory csf,
                             RMIServerSocketFactory ssf,
                             Map<String,?> env)
            throws IOException {

        super(env);

        if (port < 0)
            throw new IllegalArgumentException("Negative port: " + port);

        this.port = port;
        this.csf = csf;
        this.ssf = ssf;
        this.env = (env == null) ? Collections.<String, Object>emptyMap() : env;
S
sjiang 已提交
101

102 103 104 105
        // This attribute was represented by RMIConnectorServer.CREDENTIALS_TYPES.
        // This attribute is superceded by
        // RMIConnectorServer.CREDENTIALS_FILTER_PATTERN.
        // Retaining this for backward compatibility.
S
sjiang 已提交
106
        String[] credentialsTypes
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                = (String[]) this.env.get("jmx.remote.rmi.server.credential.types");

        String credentialsFilter
                = (String) this.env.get(RMIConnectorServer.CREDENTIALS_FILTER_PATTERN);

        // It is impossible for both attributes to be specified
        if(credentialsTypes != null && credentialsFilter != null)
            throw new IllegalArgumentException("Cannot specify both \""
                    + "jmx.remote.rmi.server.credential.types" + "\" and \""
           + RMIConnectorServer.CREDENTIALS_FILTER_PATTERN + "\"");
        else if(credentialsFilter != null){
            cFilter = ObjectInputFilter.Config.createFilter(credentialsFilter);
            allowedTypes = null;
         }
        else if (credentialsTypes != null) {
            allowedTypes = Arrays.stream(credentialsTypes).filter(
                    s -> s!= null).collect(Collectors.toSet());
            allowedTypes.stream().forEach(ReflectUtil::checkPackageAccess);
            cFilter = this::newClientCheckInput;
        } else {
            allowedTypes = null;
            cFilter = null;
S
sjiang 已提交
129
        }
130 131 132 133 134 135 136

        String userJmxFilter =
                (String) this.env.get(RMIConnectorServer.SERIAL_FILTER_PATTERN);
        if(userJmxFilter != null && !userJmxFilter.isEmpty())
            jmxRmiFilter = ObjectInputFilter.Config.createFilter(userJmxFilter);
        else
            jmxRmiFilter = null;
D
duke 已提交
137 138 139
    }

    protected void export() throws IOException {
140
        export(this, cFilter);
D
duke 已提交
141 142
    }

143
    private void export(Remote obj, ObjectInputFilter typeFilter) throws RemoteException {
144
        final RMIExporter exporter =
D
duke 已提交
145
            (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
146 147 148 149 150 151 152 153
        final boolean daemon = EnvHelp.isServerDaemon(env);

        if (daemon && exporter != null) {
            throw new IllegalArgumentException("If "+EnvHelp.JMX_SERVER_DAEMON+
                    " is specified as true, "+RMIExporter.EXPORTER_ATTRIBUTE+
                    " cannot be used to specify an exporter!");
        }

154 155 156
        if (exporter != null) {
            exporter.exportObject(obj, port, csf, ssf, typeFilter);
        } else {
157
            if (csf == null && ssf == null) {
158
                new UnicastServerRef(new LiveRef(port), typeFilter).exportObject(obj, null, daemon);
159
            } else {
160
                new UnicastServerRef2(port, csf, ssf, typeFilter).exportObject(obj, null, daemon);
161 162
            }
        }
D
duke 已提交
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
    }

    private void unexport(Remote obj, boolean force)
            throws NoSuchObjectException {
        RMIExporter exporter =
            (RMIExporter) env.get(RMIExporter.EXPORTER_ATTRIBUTE);
        if (exporter == null)
            UnicastRemoteObject.unexportObject(obj, force);
        else
            exporter.unexportObject(obj, force);
    }

    protected String getProtocol() {
        return "rmi";
    }

    /**
     * <p>Returns a serializable stub for this {@link RMIServer} object.</p>
     *
     * @return a serializable stub.
     *
     * @exception IOException if the stub cannot be obtained - e.g the
     *            RMIJRMPServerImpl has not been exported yet.
     */
    public Remote toStub() throws IOException {
188
        return RemoteObject.toStub(this);
D
duke 已提交
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
    }

    /**
     * <p>Creates a new client connection as an RMI object exported
     * through JRMP. The port and socket factories for the new
     * {@link RMIConnection} object are the ones supplied
     * to the <code>RMIJRMPServerImpl</code> constructor.</p>
     *
     * @param connectionId the ID of the new connection. Every
     * connection opened by this connector server will have a
     * different id.  The behavior is unspecified if this parameter is
     * null.
     *
     * @param subject the authenticated subject.  Can be null.
     *
     * @return the newly-created <code>RMIConnection</code>.
     *
     * @exception IOException if the new {@link RMIConnection}
     * object cannot be created or exported.
     */
    protected RMIConnection makeClient(String connectionId, Subject subject)
            throws IOException {

        if (connectionId == null)
            throw new NullPointerException("Null connectionId");

        RMIConnection client =
            new RMIConnectionImpl(this, connectionId, getDefaultClassLoader(),
                                  subject, env);
218
        export(client, jmxRmiFilter);
D
duke 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        return client;
    }

    protected void closeClient(RMIConnection client) throws IOException {
        unexport(client, true);
    }

    /**
     * <p>Called by {@link #close()} to close the connector server by
     * unexporting this object.  After returning from this method, the
     * connector server must not accept any new connections.</p>
     *
     * @exception IOException if the attempt to close the connector
     * server failed.
     */
    protected void closeServer() throws IOException {
235
        unexport(this, true);
D
duke 已提交
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
    /**
     * Check that a type in the remote invocation of {@link RMIServerImpl#newClient}
     * is one of the {@code allowedTypes}.
     *
     * @param clazz       the class; may be null
     * @param size        the size for arrays, otherwise is 0
     * @param nObjectRefs the current number of object references
     * @param depth       the current depth
     * @param streamBytes the current number of bytes consumed
     * @return {@code ObjectInputFilter.Status.ALLOWED} if the class is allowed,
     *          otherwise {@code ObjectInputFilter.Status.REJECTED}
     */
    ObjectInputFilter.Status newClientCheckInput(ObjectInputFilter.FilterInfo filterInfo) {
        ObjectInputFilter.Status status = ObjectInputFilter.Status.UNDECIDED;
        if (allowedTypes != null && filterInfo.serialClass() != null) {
            // If enabled, check type
            String type = filterInfo.serialClass().getName();
            if (allowedTypes.contains(type))
                status = ObjectInputFilter.Status.ALLOWED;
            else
                status = ObjectInputFilter.Status.REJECTED;
         }
        return status;
    }
D
duke 已提交
262 263 264 265
    private final int port;
    private final RMIClientSocketFactory csf;
    private final RMIServerSocketFactory ssf;
    private final Map<String, ?> env;
266 267 268
    private final Set<String> allowedTypes;
    private final ObjectInputFilter jmxRmiFilter;
    private final ObjectInputFilter cFilter;
D
duke 已提交
269
}