1 7 package org.jboss.security.auth.login; 8 9 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.security.AccessController ; 18 import java.security.PrivilegedAction ; 19 import java.util.ArrayList ; 20 import javax.security.auth.AuthPermission ; 21 import javax.security.auth.login.AppConfigurationEntry ; 22 import javax.security.auth.login.Configuration ; 23 24 import org.jboss.logging.Logger; 25 import org.jboss.security.auth.spi.UsersObjectModelFactory; 26 import org.jboss.xb.binding.JBossXBException; 27 import org.jboss.xb.binding.Unmarshaller; 28 import org.jboss.xb.binding.UnmarshallerFactory; 29 30 49 public class XMLLoginConfigImpl extends Configuration 50 { 51 private static final String DEFAULT_APP_CONFIG_NAME = "other"; 52 private static final AuthPermission REFRESH_PERM = new AuthPermission ("refreshLoginConfiguration"); 53 private static Logger log = Logger.getLogger(XMLLoginConfigImpl.class); 54 57 PolicyConfig appConfigs = new PolicyConfig(); 58 59 protected URL loginConfigURL; 60 61 protected Configuration parentConfig; 62 63 private boolean validateDTD = true; 64 65 public void refresh() 67 { 68 SecurityManager sm = System.getSecurityManager(); 69 if (sm != null) 70 sm.checkPermission(REFRESH_PERM); 71 if (log.isTraceEnabled()) 72 log.trace("Begin refresh"); 73 appConfigs.clear(); 74 loadConfig(); 75 if (log.isTraceEnabled()) 76 log.trace("End refresh"); 77 } 78 79 public AppConfigurationEntry [] getAppConfigurationEntry(String appName) 80 { 81 if (log.isTraceEnabled()) 82 log.trace("Begin getAppConfigurationEntry("+appName+"), size="+appConfigs.size()); 83 if (loginConfigURL == null) 85 { 86 loadConfig(); 87 } 88 89 AppConfigurationEntry [] entry = null; 90 AuthenticationInfo authInfo = (AuthenticationInfo) appConfigs.get(appName); 91 if (authInfo == null) 92 { 93 if (log.isTraceEnabled()) 94 log.trace("getAppConfigurationEntry("+appName+"), no entry in appConfigs, tyring parentCont: "+parentConfig); 95 if (parentConfig != null) 96 entry = parentConfig.getAppConfigurationEntry(appName); 97 if (entry == null) 98 { 99 if (log.isTraceEnabled()) 100 log.trace("getAppConfigurationEntry("+appName+"), no entry in parentConfig, trying: "+DEFAULT_APP_CONFIG_NAME); 101 } 102 authInfo = (AuthenticationInfo) appConfigs.get(DEFAULT_APP_CONFIG_NAME); 103 } 104 105 if (authInfo != null) 106 { 107 if (log.isTraceEnabled()) 108 log.trace("End getAppConfigurationEntry("+appName+"), authInfo=" + authInfo); 109 final AuthenticationInfo theAuthInfo = authInfo; 111 PrivilegedAction action = new PrivilegedAction () 112 { 113 public Object run() 114 { 115 return theAuthInfo.copyAppConfigurationEntry(); 116 } 117 }; 118 entry = (AppConfigurationEntry []) AccessController.doPrivileged(action); 119 } 120 else 121 { 122 if (log.isTraceEnabled()) 123 log.trace("End getAppConfigurationEntry("+appName+"), failed to find entry"); 124 } 125 126 return entry; 127 } 128 130 133 public URL getConfigURL() 134 { 135 return loginConfigURL; 136 } 137 138 141 public void setConfigURL(URL loginConfigURL) 142 { 143 this.loginConfigURL = loginConfigURL; 144 } 145 146 public void setConfigResource(String resourceName) 147 throws IOException 148 { 149 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 150 loginConfigURL = tcl.getResource(resourceName); 151 if (loginConfigURL == null) 152 throw new IOException ("Failed to find resource: " + resourceName); 153 } 154 155 public void setParentConfig(Configuration parentConfig) 156 { 157 this.parentConfig = parentConfig; 158 } 159 160 162 public boolean getValidateDTD() 163 { 164 return this.validateDTD; 165 } 166 167 169 public void setValidateDTD(boolean flag) 170 { 171 this.validateDTD = flag; 172 } 173 174 176 public void addAppConfig(String appName, AppConfigurationEntry [] entries) 177 { 178 SecurityManager sm = System.getSecurityManager(); 179 if (sm != null) 180 sm.checkPermission(REFRESH_PERM); 181 AuthenticationInfo authInfo = new AuthenticationInfo(appName); 182 authInfo.setAppConfigurationEntry(entries); 183 if (log.isTraceEnabled()) 184 log.trace("addAppConfig("+appName+"), authInfo=" + authInfo); 185 appConfigs.add(authInfo); 186 } 187 188 public void removeAppConfig(String appName) 189 { 190 SecurityManager sm = System.getSecurityManager(); 191 if (sm != null) 192 sm.checkPermission(REFRESH_PERM); 193 if (log.isTraceEnabled()) 194 log.trace("removeAppConfig, appName="+appName); 195 appConfigs.remove(appName); 196 } 197 198 public void clear() 199 { 200 201 } 202 203 206 public void loadConfig() 207 { 208 String loginConfig = System.getProperty("java.security.auth.login.config"); 210 if (loginConfig == null) 211 loginConfig = "login-config.xml"; 212 213 if (loginConfigURL == null) 215 { 216 try 217 { 218 loginConfigURL = new URL (loginConfig); 220 } 221 catch (MalformedURLException e) 222 { 223 try 225 { 226 setConfigResource(loginConfig); 227 } 228 catch (IOException ignore) 229 { 230 File configFile = new File (loginConfig); 232 try 233 { 234 setConfigURL(configFile.toURL()); 235 } 236 catch (MalformedURLException ignore2) 237 { 238 } 239 } 240 } 241 } 242 243 if (loginConfigURL == null) 244 { 245 log.warn("Failed to find config: " + loginConfig); 246 return; 247 } 248 249 if (log.isTraceEnabled()) 250 log.trace("Begin loadConfig, loginConfigURL="+loginConfigURL); 251 try 253 { 254 loadConfig(loginConfigURL); 255 if (log.isTraceEnabled()) 256 log.trace("End loadConfig, loginConfigURL="+loginConfigURL); 257 } 258 catch (Exception e) 259 { 260 log.warn("End loadConfig, failed to load config: " + loginConfigURL, e); 261 } 262 } 263 264 protected String [] loadConfig(URL config) throws Exception 265 { 266 SecurityManager sm = System.getSecurityManager(); 267 if (sm != null) 268 sm.checkPermission(REFRESH_PERM); 269 270 ArrayList configNames = new ArrayList (); 271 log.debug("Try loading config as XML, url=" + config); 272 try 273 { 274 loadXMLConfig(config, configNames); 275 } 276 catch(Throwable e) 277 { 278 log.debug("Failed to load config as XML", e); 279 log.debug("Try loading config as Sun format, url=" + config); 280 loadSunConfig(config, configNames); 281 } 282 String [] names = new String [configNames.size()]; 283 configNames.toArray(names); 284 return names; 285 } 286 287 private void loadSunConfig(URL sunConfig, ArrayList configNames) 288 throws Exception 289 { 290 InputStream is = sunConfig.openStream(); 291 if (is == null) 292 throw new IOException ("InputStream is null for: " + sunConfig); 293 294 InputStreamReader configFile = new InputStreamReader (is); 295 boolean trace = log.isTraceEnabled(); 296 SunConfigParser.doParse(configFile, this, trace); 297 } 298 299 private void loadXMLConfig(URL loginConfigURL, ArrayList configNames) 300 throws IOException , JBossXBException 301 { 302 LoginConfigObjectModelFactory lcomf = new LoginConfigObjectModelFactory(); 303 UsersObjectModelFactory uomf = new UsersObjectModelFactory(); 304 305 InputStreamReader xmlReader = loadURL(loginConfigURL); 306 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance() 307 .newUnmarshaller(); 308 unmarshaller.mapFactoryToNamespace(uomf, "http://www.jboss.org/j2ee/schemas/XMLLoginModule"); 309 PolicyConfig config = (PolicyConfig) unmarshaller.unmarshal(xmlReader, lcomf, null); 310 configNames.addAll(config.getConfigNames()); 311 appConfigs.copy(config); 312 } 313 314 private InputStreamReader loadURL(URL configURL) 315 throws IOException 316 { 317 InputStream is = configURL.openStream(); 318 if (is == null) 319 throw new IOException ("Failed to obtain InputStream from url: " + configURL); 320 InputStreamReader xmlReader = new InputStreamReader (is); 321 return xmlReader; 322 } 323 324 } 325 | Popular Tags |