1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22 package org.jboss.security.plugins;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Method;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28
29 import javax.management.Attribute;
30 import javax.management.AttributeList;
31 import javax.management.AttributeNotFoundException;
32 import javax.management.DynamicMBean;
33 import javax.management.InvalidAttributeValueException;
34 import javax.management.MBeanAttributeInfo;
35 import javax.management.MBeanConstructorInfo;
36 import javax.management.MBeanException;
37 import javax.management.MBeanInfo;
38 import javax.management.MBeanOperationInfo;
39 import javax.management.ReflectionException;
40 import javax.security.auth.login.Configuration;
41
42 import org.jboss.logging.Logger;
43
44 /** An mbean that uses the default JAAS login configuration file based
45 implementation.
46
47 @author Scott.Stark@jboss.org
48 @version $Revision: 40069 $
49 */
50 public class DefaultLoginConfig implements DynamicMBean
51 {
52 private static Logger log = Logger.getLogger(DefaultLoginConfig.class);
53 private String authConfig = "auth.conf";
54 private Configuration theConfig;
55
56 /** Creates a new instance of DefaultLoginConfig */
57 public DefaultLoginConfig()
58 {
59 }
60
61 /** Get the resource path to the JAAS login configuration file to use.
62 */
63 public String getAuthConfig()
64 {
65 return authConfig;
66 }
67
68 /** Set the resource path or URL to the JAAS login configuration file to use.
69 The default is "auth.conf".
70 */
71 public void setAuthConfig(String authConfURL) throws MalformedURLException
72 {
73 this.authConfig = authConfURL;
74 // Set the JAAS login config file if not already set
75 ClassLoader loader = Thread.currentThread().getContextClassLoader();
76 URL loginConfig = loader.getResource(authConfig);
77 if( loginConfig != null )
78 {
79 System.setProperty("java.security.auth.login.config", loginConfig.toExternalForm());
80 log.info("Using JAAS LoginConfig: " + loginConfig.toExternalForm());
81 }
82 else
83 {
84 log.warn("Resource: " + authConfig + " not found");
85 }
86 }
87
88 /** Return the Configuration instance managed by this mbean. This simply
89 obtains the default Configuration by calling Configuration.getConfiguration.
90 Note that this means this mbean must be the first pushed onto the config
91 stack if it is used.
92 @see javax.security.auth.login.Configuration
93 */
94 public Configuration getConfiguration(Configuration currentConfig)
95 {
96 if( theConfig == null )
97 {
98 theConfig = Configuration.getConfiguration();
99 log.debug("theConfig set to: "+theConfig);
100 }
101 return theConfig;
102 }
103
104 // Begin DynamicMBean interfaces
105 public Object getAttribute(String name)
106 throws AttributeNotFoundException, MBeanException, ReflectionException
107 {
108 if( name.equals("AuthConfig") )
109 return getAuthConfig();
110 throw new AttributeNotFoundException(name+": is not an attribute");
111 }
112
113 public AttributeList getAttributes(String[] names)
114 {
115 AttributeList list = new AttributeList();
116 for(int n = 0; n < names.length; n ++)
117 {
118 String name = names[n];
119 try
120 {
121 Object value = getAttribute(name);
122 Attribute attr = new Attribute(name, value);
123 list.add(attr);
124 }
125 catch(Exception e)
126 {
127 }
128 }
129 return list;
130 }
131
132 public MBeanInfo getMBeanInfo()
133 {
134 Class c = getClass();
135 MBeanAttributeInfo[] attrInfo = {
136 new MBeanAttributeInfo("AuthConfig", "java.lang.String",
137 "", true, true, false)
138 };
139 Constructor ctor = null;
140 try
141 {
142 Class[] sig = {};
143 ctor = c.getDeclaredConstructor(sig);
144 }
145 catch(Exception e)
146 {
147 }
148 MBeanConstructorInfo[] ctorInfo = {
149 new MBeanConstructorInfo("Default ctor", ctor)
150 };
151 Method getConfiguration = null;
152 try
153 {
154 Class[] sig = {Configuration.class};
155 getConfiguration = c.getDeclaredMethod("getConfiguration", sig);
156 }
157 catch(Exception e)
158 {
159 }
160 MBeanOperationInfo[] opInfo = {
161 new MBeanOperationInfo("Access the LoginConfiguration", getConfiguration)
162 };
163 MBeanInfo info = new MBeanInfo(c.getName(), "Default JAAS LoginConfig",
164 attrInfo, ctorInfo, opInfo, null);
165 return info;
166 }
167
168 public Object invoke(String method, Object[] args, String[] signature)
169 throws MBeanException, ReflectionException
170 {
171 Object value = null;
172 if( method.equals("getConfiguration") )
173 {
174 Configuration currentConfig = (Configuration) args[0];
175 value = this.getConfiguration(currentConfig);
176 }
177 return value;
178 }
179
180 public void setAttribute(Attribute attribute)
181 throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
182 {
183 String name = attribute.getName();
184 String value = (String) attribute.getValue();
185 if( name.equals("AuthConfig") )
186 {
187 try
188 {
189 setAuthConfig(value);
190 }
191 catch(Exception e)
192 {
193 throw new MBeanException(e);
194 }
195 }
196 else
197 throw new AttributeNotFoundException(name+": is not an attribute");
198 }
199
200 public AttributeList setAttributes(AttributeList attributeList)
201 {
202 AttributeList list = new AttributeList();
203 for(int n = 0; n < attributeList.size(); n ++)
204 {
205 Attribute attr = (Attribute) attributeList.get(n);
206 try
207 {
208 setAttribute(attr);
209 list.add(attr);
210 }
211 catch(Exception e)
212 {
213 }
214 }
215 return list;
216 }
217 // End DynamicMBean interfaces
218
219 }