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  * @modules java.management.rmi
30  *          java.management/com.sun.jmx.remote.security
31  * @run clean RMIPasswdAuthTest
32  * @run build RMIPasswdAuthTest SimpleStandard SimpleStandardMBean
33  * @run main RMIPasswdAuthTest
34  */
35 
36 import java.io.File;
37 import java.rmi.RemoteException;
38 import java.rmi.registry.Registry;
39 import java.rmi.registry.LocateRegistry;
40 import java.util.HashMap;
41 import java.util.Properties;
42 
43 import javax.management.Attribute;
44 import javax.management.MBeanServer;
45 import javax.management.MBeanServerConnection;
46 import javax.management.MBeanServerFactory;
47 import javax.management.Notification;
48 import javax.management.NotificationListener;
49 import javax.management.ObjectName;
50 import javax.management.remote.JMXConnector;
51 import javax.management.remote.JMXConnectorFactory;
52 import javax.management.remote.JMXConnectorServer;
53 import javax.management.remote.JMXConnectorServerFactory;
54 import javax.management.remote.JMXServiceURL;
55 import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
56 
57 public class RMIPasswdAuthTest {
58 
main(String[] args)59     public static void main(String[] args) {
60         try {
61 
62             // Set the default password file
63             //
64             final String passwordFile = System.getProperty("test.src") +
65                 File.separator + "jmxremote.password";
66             System.out.println("Password file = " + passwordFile);
67 
68             // Create an RMI registry
69             //
70             System.out.println("Start RMI registry...");
71             Registry reg = null;
72             int port = 5800;
73             while (port++ < 6000) {
74                 try {
75                     reg = LocateRegistry.createRegistry(port);
76                     System.out.println("RMI registry running on port " + port);
77                     break;
78                 } catch (RemoteException e) {
79                     // Failed to create RMI registry...
80                     System.out.println("Failed to create RMI registry " +
81                                        "on port " + port);
82                 }
83             }
84             if (reg == null) {
85                 System.exit(1);
86             }
87 
88             // Instantiate the MBean server
89             //
90             System.out.println("Create the MBean server");
91             MBeanServer mbs = MBeanServerFactory.createMBeanServer();
92             // Register the ClassPathClassLoaderMBean
93             //
94             System.out.println("Create ClassPathClassLoader MBean");
95             ObjectName cpcl =
96                 new ObjectName("ClassLoader:name=ClassPathClassLoader");
97             mbs.createMBean("javax.management.loading.MLet", cpcl);
98             // Register the SimpleStandardMBean
99             //
100             System.out.println("Create SimpleStandard MBean");
101             mbs.createMBean("SimpleStandard",
102                             new ObjectName("MBeans:name=SimpleStandard"));
103             // Create Properties containing the location of the password file
104             //
105             Properties props = new Properties();
106             props.setProperty("jmx.remote.x.password.file", passwordFile);
107             // Initialize environment map to be passed to the connector server
108             //
109             System.out.println("Initialize environment map");
110             HashMap env = new HashMap();
111             env.put("jmx.remote.authenticator",
112                     new JMXPluggableAuthenticator(props));
113             // Create an RMI connector server
114             //
115             System.out.println("Create an RMI connector server");
116             JMXServiceURL url =
117                 new JMXServiceURL("rmi", null, 0,
118                                   "/jndi/rmi://:" + port + "/server" + port);
119             JMXConnectorServer rcs =
120                 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
121             rcs.start();
122 
123             // Create an RMI connector client
124             //
125             System.out.println("Create an RMI connector client");
126             HashMap cli_env = new HashMap();
127             // These credentials must match those in the supplied password file
128             //
129             String[] credentials = new String[] { "monitorRole" , "QED" };
130             cli_env.put("jmx.remote.credentials", credentials);
131             JMXConnector jmxc = JMXConnectorFactory.connect(url, cli_env);
132             MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
133             // Get domains from MBeanServer
134             //
135             System.out.println("Domains:");
136             String domains[] = mbsc.getDomains();
137             for (int i = 0; i < domains.length; i++) {
138                 System.out.println("\tDomain[" + i + "] = " + domains[i]);
139             }
140             // Get MBean count
141             //
142             System.out.println("MBean count = " + mbsc.getMBeanCount());
143             // Get State attribute
144             //
145             String oldState =
146                 (String) mbsc.getAttribute(
147                               new ObjectName("MBeans:name=SimpleStandard"),
148                               "State");
149             System.out.println("Old State = \"" + oldState + "\"");
150             // Set State attribute
151             //
152             System.out.println("Set State to \"changed state\"");
153             mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"),
154                               new Attribute("State", "changed state"));
155             // Get State attribute
156             //
157             String newState =
158                 (String) mbsc.getAttribute(
159                               new ObjectName("MBeans:name=SimpleStandard"),
160                               "State");
161             System.out.println("New State = \"" + newState + "\"");
162             if (!newState.equals("changed state")) {
163                 System.out.println("Invalid State = \"" + newState + "\"");
164                 System.exit(1);
165             }
166             // Add notification listener on SimpleStandard MBean
167             //
168             System.out.println("Add notification listener...");
169             mbsc.addNotificationListener(
170                  new ObjectName("MBeans:name=SimpleStandard"),
171                  new NotificationListener() {
172                      public void handleNotification(Notification notification,
173                                                     Object handback) {
174                          System.out.println("Received notification: " +
175                                             notification);
176                      }
177                  },
178                  null,
179                  null);
180             // Unregister SimpleStandard MBean
181             //
182             System.out.println("Unregister SimpleStandard MBean...");
183             mbsc.unregisterMBean(new ObjectName("MBeans:name=SimpleStandard"));
184             // Close MBeanServer connection
185             //
186             jmxc.close();
187             System.out.println("Bye! Bye!");
188         } catch (Exception e) {
189             System.out.println("Unexpected exception caught = " + e);
190             e.printStackTrace();
191             System.exit(1);
192         }
193     }
194 }
195