1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5016508
  27  * @summary Tests the default JAAS configuration for authenticating RMI clients
  28  * @author Luis-Miguel Alventosa
  29  *
  30  * @modules java.management.rmi
  31  *          java.management/com.sun.jmx.remote.security
  32  *
  33  * @run clean RMIPasswdAuthTest
  34  * @run build RMIPasswdAuthTest SimpleStandard SimpleStandardMBean
  35  * @run main RMIPasswdAuthTest
  36  */
  37 
  38 import java.io.File;
  39 import java.rmi.RemoteException;
  40 import java.rmi.registry.Registry;
  41 import java.rmi.registry.LocateRegistry;
  42 import java.util.HashMap;
  43 import java.util.Properties;
  44 
  45 import javax.management.Attribute;
  46 import javax.management.MBeanServer;
  47 import javax.management.MBeanServerConnection;
  48 import javax.management.MBeanServerFactory;
  49 import javax.management.Notification;
  50 import javax.management.NotificationListener;
  51 import javax.management.ObjectName;
  52 import javax.management.remote.JMXConnector;
  53 import javax.management.remote.JMXConnectorFactory;
  54 import javax.management.remote.JMXConnectorServer;
  55 import javax.management.remote.JMXConnectorServerFactory;
  56 import javax.management.remote.JMXServiceURL;
  57 import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
  58 
  59 public class RMIPasswdAuthTest {
  60 
  61     public static void main(String[] args) {
  62         try {
  63 
  64             // Set the default password file
  65             //
  66             final String passwordFile = System.getProperty("test.src") +
  67                 File.separator + "jmxremote.password";
  68             System.out.println("Password file = " + passwordFile);
  69 
  70             // Create an RMI registry
  71             //
  72             System.out.println("Start RMI registry...");
  73             Registry reg = null;
  74             int port = 5800;
  75             while (port++ < 6000) {
  76                 try {
  77                     reg = LocateRegistry.createRegistry(port);
  78                     System.out.println("RMI registry running on port " + port);
  79                     break;
  80                 } catch (RemoteException e) {
  81                     // Failed to create RMI registry...
  82                     System.out.println("Failed to create RMI registry " +
  83                                        "on port " + port);
  84                 }
  85             }
  86             if (reg == null) {
  87                 System.exit(1);
  88             }
  89 
  90             // Instantiate the MBean server
  91             //
  92             System.out.println("Create the MBean server");
  93             MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  94             // Register the ClassPathClassLoaderMBean
  95             //
  96             System.out.println("Create ClassPathClassLoader MBean");
  97             ObjectName cpcl =
  98                 new ObjectName("ClassLoader:name=ClassPathClassLoader");
  99             mbs.createMBean("javax.management.loading.MLet", cpcl);
 100             // Register the SimpleStandardMBean
 101             //
 102             System.out.println("Create SimpleStandard MBean");
 103             mbs.createMBean("SimpleStandard",
 104                             new ObjectName("MBeans:name=SimpleStandard"));
 105             // Create Properties containing the location of the password file
 106             //
 107             Properties props = new Properties();
 108             props.setProperty("jmx.remote.x.password.file", passwordFile);
 109             // Initialize environment map to be passed to the connector server
 110             //
 111             System.out.println("Initialize environment map");
 112             HashMap env = new HashMap();
 113             env.put("jmx.remote.authenticator",
 114                     new JMXPluggableAuthenticator(props));
 115             // Create an RMI connector server
 116             //
 117             System.out.println("Create an RMI connector server");
 118             JMXServiceURL url =
 119                 new JMXServiceURL("rmi", null, 0,
 120                                   "/jndi/rmi://:" + port + "/server" + port);
 121             JMXConnectorServer rcs =
 122                 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
 123             rcs.start();
 124 
 125             // Create an RMI connector client
 126             //
 127             System.out.println("Create an RMI connector client");
 128             HashMap cli_env = new HashMap();
 129             // These credentials must match those in the supplied password file
 130             //
 131             String[] credentials = new String[] { "monitorRole" , "QED" };
 132             cli_env.put("jmx.remote.credentials", credentials);
 133             JMXConnector jmxc = JMXConnectorFactory.connect(url, cli_env);
 134             MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
 135             // Get domains from MBeanServer
 136             //
 137             System.out.println("Domains:");
 138             String domains[] = mbsc.getDomains();
 139             for (int i = 0; i < domains.length; i++) {
 140                 System.out.println("\tDomain[" + i + "] = " + domains[i]);
 141             }
 142             // Get MBean count
 143             //
 144             System.out.println("MBean count = " + mbsc.getMBeanCount());
 145             // Get State attribute
 146             //
 147             String oldState =
 148                 (String) mbsc.getAttribute(
 149                               new ObjectName("MBeans:name=SimpleStandard"),
 150                               "State");
 151             System.out.println("Old State = \"" + oldState + "\"");
 152             // Set State attribute
 153             //
 154             System.out.println("Set State to \"changed state\"");
 155             mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"),
 156                               new Attribute("State", "changed state"));
 157             // Get State attribute
 158             //
 159             String newState =
 160                 (String) mbsc.getAttribute(
 161                               new ObjectName("MBeans:name=SimpleStandard"),
 162                               "State");
 163             System.out.println("New State = \"" + newState + "\"");
 164             if (!newState.equals("changed state")) {
 165                 System.out.println("Invalid State = \"" + newState + "\"");
 166                 System.exit(1);
 167             }
 168             // Add notification listener on SimpleStandard MBean
 169             //
 170             System.out.println("Add notification listener...");
 171             mbsc.addNotificationListener(
 172                  new ObjectName("MBeans:name=SimpleStandard"),
 173                  new NotificationListener() {
 174                      public void handleNotification(Notification notification,
 175                                                     Object handback) {
 176                          System.out.println("Received notification: " +
 177                                             notification);
 178                      }
 179                  },
 180                  null,
 181                  null);
 182             // Unregister SimpleStandard MBean
 183             //
 184             System.out.println("Unregister SimpleStandard MBean...");
 185             mbsc.unregisterMBean(new ObjectName("MBeans:name=SimpleStandard"));
 186             // Close MBeanServer connection
 187             //
 188             jmxc.close();
 189             System.out.println("Bye! Bye!");
 190         } catch (Exception e) {
 191             System.out.println("Unexpected exception caught = " + e);
 192             e.printStackTrace();
 193             System.exit(1);
 194         }
 195     }
 196 }