Search in sources :

Example 6 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project jdk8u_jdk by JetBrains.

the class TCPEndpoint method read.

/**
     * Get the endpoint from the input stream.
     * @param in the input stream
     * @exception IOException If id could not be read (due to stream failure)
     */
public static TCPEndpoint read(ObjectInput in) throws IOException, ClassNotFoundException {
    String host;
    int port;
    RMIClientSocketFactory csf = null;
    byte format = in.readByte();
    switch(format) {
        case FORMAT_HOST_PORT:
            host = in.readUTF();
            port = in.readInt();
            break;
        case FORMAT_HOST_PORT_FACTORY:
            host = in.readUTF();
            port = in.readInt();
            csf = (RMIClientSocketFactory) in.readObject();
            break;
        default:
            throw new IOException("invalid endpoint format");
    }
    return new TCPEndpoint(host, port, csf, null);
}
Also used : ConnectIOException(java.rmi.ConnectIOException) IOException(java.io.IOException) Endpoint(sun.rmi.transport.Endpoint) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory)

Example 7 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project hbase by apache.

the class JMXListener method startConnectorServer.

public void startConnectorServer(int rmiRegistryPort, int rmiConnectorPort) throws IOException {
    boolean rmiSSL = false;
    boolean authenticate = true;
    String passwordFile = null;
    String accessFile = null;
    System.setProperty("java.rmi.server.randomIDs", "true");
    String rmiSSLValue = System.getProperty("com.sun.management.jmxremote.ssl", "false");
    rmiSSL = Boolean.parseBoolean(rmiSSLValue);
    String authenticateValue = System.getProperty("com.sun.management.jmxremote.authenticate", "false");
    authenticate = Boolean.parseBoolean(authenticateValue);
    passwordFile = System.getProperty("com.sun.management.jmxremote.password.file");
    accessFile = System.getProperty("com.sun.management.jmxremote.access.file");
    LOG.info("rmiSSL:" + rmiSSLValue + ",authenticate:" + authenticateValue + ",passwordFile:" + passwordFile + ",accessFile:" + accessFile);
    // Environment map
    HashMap<String, Object> jmxEnv = new HashMap<>();
    RMIClientSocketFactory csf = null;
    RMIServerSocketFactory ssf = null;
    if (rmiSSL) {
        if (rmiRegistryPort == rmiConnectorPort) {
            throw new IOException("SSL is enabled. " + "rmiConnectorPort cannot share with the rmiRegistryPort!");
        }
        csf = new SslRMIClientSocketFactorySecure();
        ssf = new SslRMIServerSocketFactorySecure();
    }
    if (csf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
    }
    if (ssf != null) {
        jmxEnv.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    // Configure authentication
    if (authenticate) {
        jmxEnv.put("jmx.remote.x.password.file", passwordFile);
        jmxEnv.put("jmx.remote.x.access.file", accessFile);
    }
    // Create the RMI registry
    rmiRegistry = LocateRegistry.createRegistry(rmiRegistryPort);
    // Retrieve the PlatformMBeanServer.
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    // Build jmxURL
    JMXServiceURL serviceUrl = buildJMXServiceURL(rmiRegistryPort, rmiConnectorPort);
    try {
        // Start the JMXListener with the connection string
        synchronized (JMXListener.class) {
            if (JMX_CS != null) {
                throw new RuntimeException("Started by another thread?");
            }
            JMX_CS = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, jmxEnv, mbs);
            JMX_CS.start();
        }
        LOG.info("ConnectorServer started!");
    } catch (IOException e) {
        LOG.error("fail to start connector server!", e);
        // deregister the RMI registry
        if (rmiRegistry != null) {
            UnicastRemoteObject.unexportObject(rmiRegistry, true);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) IOException(java.io.IOException) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) MBeanServer(javax.management.MBeanServer)

Example 8 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project jdk8u_jdk by JetBrains.

the class ConnectorBootstrap method exportMBeanServer.

private static JMXConnectorServerData exportMBeanServer(MBeanServer mbs, int port, int rmiPort, boolean useSsl, boolean useRegistrySsl, String sslConfigFileName, String[] enabledCipherSuites, String[] enabledProtocols, boolean sslNeedClientAuth, boolean useAuthentication, String loginConfigName, String passwordFileName, String accessFileName, String bindAddress) throws IOException, MalformedURLException {
    /* Make sure we use non-guessable RMI object IDs.  Otherwise
         * attackers could hijack open connections by guessing their
         * IDs.  */
    System.setProperty("java.rmi.server.randomIDs", "true");
    JMXServiceURL url = new JMXServiceURL("rmi", bindAddress, rmiPort);
    Map<String, Object> env = new HashMap<>();
    PermanentExporter exporter = new PermanentExporter();
    env.put(RMIExporter.EXPORTER_ATTRIBUTE, exporter);
    env.put(EnvHelp.CREDENTIAL_TYPES, new String[] { String[].class.getName(), String.class.getName() });
    boolean useSocketFactory = bindAddress != null && !useSsl;
    if (useAuthentication) {
        if (loginConfigName != null) {
            env.put("jmx.remote.x.login.config", loginConfigName);
        }
        if (passwordFileName != null) {
            env.put("jmx.remote.x.password.file", passwordFileName);
        }
        env.put("jmx.remote.x.access.file", accessFileName);
        if (env.get("jmx.remote.x.password.file") != null || env.get("jmx.remote.x.login.config") != null) {
            env.put(JMXConnectorServer.AUTHENTICATOR, new AccessFileCheckerAuthenticator(env));
        }
    }
    RMIClientSocketFactory csf = null;
    RMIServerSocketFactory ssf = null;
    if (useSsl || useRegistrySsl) {
        csf = new SslRMIClientSocketFactory();
        ssf = createSslRMIServerSocketFactory(sslConfigFileName, enabledCipherSuites, enabledProtocols, sslNeedClientAuth, bindAddress);
    }
    if (useSsl) {
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    if (useSocketFactory) {
        ssf = new HostAwareSocketFactory(bindAddress);
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
    }
    JMXConnectorServer connServer = null;
    try {
        connServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        connServer.start();
    } catch (IOException e) {
        if (connServer == null || connServer.getAddress() == null) {
            throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR, e, url.toString());
        } else {
            throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR, e, connServer.getAddress().toString());
        }
    }
    if (useRegistrySsl) {
        registry = new SingleEntryRegistry(port, csf, ssf, "jmxrmi", exporter.firstExported);
    } else if (useSocketFactory) {
        registry = new SingleEntryRegistry(port, csf, ssf, "jmxrmi", exporter.firstExported);
    } else {
        registry = new SingleEntryRegistry(port, "jmxrmi", exporter.firstExported);
    }
    int registryPort = ((UnicastRef) ((RemoteObject) registry).getRef()).getLiveRef().getPort();
    String jmxUrlStr = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", url.getHost(), registryPort);
    JMXServiceURL remoteURL = new JMXServiceURL(jmxUrlStr);
    return new JMXConnectorServerData(connServer, remoteURL);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) IOException(java.io.IOException) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) JMXConnectorServer(javax.management.remote.JMXConnectorServer) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject) SslRMIServerSocketFactory(javax.rmi.ssl.SslRMIServerSocketFactory) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) AgentConfigurationError(sun.management.AgentConfigurationError) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) RemoteObject(java.rmi.server.RemoteObject)

Example 9 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project jdk8u_jdk by JetBrains.

the class LiveRef method remoteEquals.

public boolean remoteEquals(Object obj) {
    if (obj != null && obj instanceof LiveRef) {
        LiveRef ref = (LiveRef) obj;
        TCPEndpoint thisEp = ((TCPEndpoint) ep);
        TCPEndpoint refEp = ((TCPEndpoint) ref.ep);
        RMIClientSocketFactory thisClientFactory = thisEp.getClientSocketFactory();
        RMIClientSocketFactory refClientFactory = refEp.getClientSocketFactory();
        /**
             * Fix for 4254103: LiveRef.remoteEquals should not fail
             * if one of the objects in the comparison has a null
             * server socket.  Comparison should only consider the
             * following criteria:
             *
             * hosts, ports, client socket factories and object IDs.
             */
        if (thisEp.getPort() != refEp.getPort() || !thisEp.getHost().equals(refEp.getHost())) {
            return false;
        }
        if ((thisClientFactory == null) ^ (refClientFactory == null)) {
            return false;
        }
        if ((thisClientFactory != null) && !((thisClientFactory.getClass() == refClientFactory.getClass()) && (thisClientFactory.equals(refClientFactory)))) {
            return false;
        }
        return (id.equals(ref.id));
    } else {
        return false;
    }
}
Also used : TCPEndpoint(sun.rmi.transport.tcp.TCPEndpoint) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory)

Example 10 with RMIClientSocketFactory

use of java.rmi.server.RMIClientSocketFactory in project geode by apache.

the class ConnectionNotificationFilterImpl method startRMIConnectorServer.

/**
   * Defines and starts the JMX RMIConnector and service.
   * <p>
   * If {@link AgentConfig#isRmiEnabled} returns false, then this adaptor will not be started.
   */
private void startRMIConnectorServer() {
    if (!this.agentConfig.isRmiEnabled())
        return;
    String rmiBindAddress = this.agentConfig.getRmiBindAddress();
    // Set RMI Stubs to use the given RMI Bind Address
    // Default bindAddress is "", if none is set - ignore if not set
    // If java.rmi.server.hostname property is specified then
    // that override is not changed
    String rmiStubServerNameKey = "java.rmi.server.hostname";
    String overrideHostName = System.getProperty(rmiStubServerNameKey);
    if ((overrideHostName == null || overrideHostName.trim().length() == 0) && (rmiBindAddress != null && rmiBindAddress.trim().length() != 0)) {
        System.setProperty(rmiStubServerNameKey, rmiBindAddress);
        logger.info(LocalizedMessage.create(LocalizedStrings.AgentImpl_SETTING_0, new StringBuilder(rmiStubServerNameKey).append(" = ").append(rmiBindAddress)));
    }
    try {
        createRMIRegistry();
        ObjectName objName = getRMIConnectorServerName();
        // make sure this adaptor is not already registered...
        if (getMBeanServer().isRegistered(objName)) {
            // dunno how we got here...
            logger.info(LocalizedMessage.create(LocalizedStrings.AgentImpl_RMICONNECTORSERVER_ALREADY_REGISTERED_AS__0, objName));
            return;
        }
        /*
       * url defined as: service:jmx:protocol:sap where 1. protocol: rmi 2. sap is:
       * [host[:port]][url-path] where host: rmi-binding-address port: rmi-server-port url-path:
       * /jndi/rmi://<rmi-binding-address>:<rmi-port><JNDI_NAME>
       */
        String urlString = null;
        String connectorServerHost = "";
        int connectorServerPort = this.agentConfig.getRmiServerPort();
        String rmiRegistryHost = "";
        int rmiRegistryPort = this.agentConfig.getRmiPort();
        // RMI stubs would use a default IP if namingHost is left empty
        if (rmiBindAddress == null || rmiBindAddress.trim().length() == 0) {
            connectorServerHost = "localhost";
            rmiRegistryHost = "";
        } else {
            connectorServerHost = applyRFC2732(rmiBindAddress);
            rmiRegistryHost = connectorServerHost;
        }
        urlString = MessageFormat.format(AgentImpl.JMX_SERVICE_URL, connectorServerHost, String.valueOf(connectorServerPort), rmiRegistryHost, String.valueOf(rmiRegistryPort), JNDI_NAME);
        logger.debug("JMX Service URL string is : \"{}\"", urlString);
        // The address of the connector
        JMXServiceURL url = new JMXServiceURL(urlString);
        Map<String, Object> env = new HashMap<String, Object>();
        // env.put(Context.INITIAL_CONTEXT_FACTORY,
        // "com.sun.jndi.rmi.registry.RegistryContextFactory");
        // env.put(Context.PROVIDER_URL, "rmi://localhost:1099");
        RMIServerSocketFactory ssf = new // true,
        MX4JServerSocketFactory(// true,
        this.agentConfig.isAgentSSLEnabled(), // true,
        this.agentConfig.isAgentSSLRequireAuth(), // "any",
        this.agentConfig.getAgentSSLProtocols(), // "any",
        this.agentConfig.getAgentSSLCiphers(), // backlog
        this.agentConfig.getRmiBindAddress(), // backlog
        10, this.agentConfig.getGfSecurityProperties());
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, ssf);
        if (this.agentConfig.isAgentSSLEnabled()) {
            RMIClientSocketFactory csf = new SslRMIClientSocketFactory();
            env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, csf);
        }
        // will be set by registering w/ mbeanServer
        MBeanServer mbs = null;
        this.rmiConnector = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        // for cleanup
        this.rmiConnector.addNotificationListener(new ConnectionNotificationAdapter(), new ConnectionNotificationFilterImpl(), this);
        // Register the JMXConnectorServer in the MBeanServer
        getMBeanServer().registerMBean(this.rmiConnector, objName);
        // Start the JMXConnectorServer
        this.rmiConnector.start();
    } catch (VirtualMachineError err) {
        SystemFailure.initiateFailure(err);
        // now, so don't let this thread continue.
        throw err;
    } catch (Throwable t) {
        // Whenever you catch Error or Throwable, you must also
        // catch VirtualMachineError (see above). However, there is
        // _still_ a possibility that you are dealing with a cascading
        // error condition, so you also need to check to see if the JVM
        // is still usable:
        SystemFailure.checkFailure();
        logger.error(LocalizedStrings.AgentImpl_FAILED_TO_START_RMICONNECTORSERVER, t);
        throw new StartupException(LocalizedStrings.AgentImpl_FAILED_TO_START_RMI_SERVICE.toLocalizedString(), t);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) ObjectName(javax.management.ObjectName) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIClientSocketFactory(java.rmi.server.RMIClientSocketFactory) SslRMIClientSocketFactory(javax.rmi.ssl.SslRMIClientSocketFactory) RMIServerSocketFactory(java.rmi.server.RMIServerSocketFactory) MBeanServer(javax.management.MBeanServer)

Aggregations

RMIClientSocketFactory (java.rmi.server.RMIClientSocketFactory)12 RMIServerSocketFactory (java.rmi.server.RMIServerSocketFactory)8 SslRMIClientSocketFactory (javax.rmi.ssl.SslRMIClientSocketFactory)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)4 JMXServiceURL (javax.management.remote.JMXServiceURL)4 MBeanServer (javax.management.MBeanServer)3 InetAddress (java.net.InetAddress)2 ConnectIOException (java.rmi.ConnectIOException)2 RemoteException (java.rmi.RemoteException)2 RemoteObject (java.rmi.server.RemoteObject)2 SslRMIServerSocketFactory (javax.rmi.ssl.SslRMIServerSocketFactory)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 AlreadyBoundException (java.rmi.AlreadyBoundException)1 Remote (java.rmi.Remote)1 LocateRegistry (java.rmi.registry.LocateRegistry)1 Registry (java.rmi.registry.Registry)1