1 /** 2 * 3 * Copyright 2003-2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.geronimo.security.util; 19 20 import java.lang.reflect.Constructor; 21 import java.security.AccessController; 22 import java.security.PrivilegedActionException; 23 import java.security.PrivilegedExceptionAction; 24 import java.util.Set; 25 import javax.security.auth.Subject; 26 import javax.security.auth.x500.X500Principal; 27 import javax.security.jacc.PolicyContext; 28 import javax.security.jacc.PolicyContextException; 29 import javax.security.jacc.PolicyContextHandler; 30 31 import org.apache.geronimo.common.DeploymentException; 32 import org.apache.geronimo.common.GeronimoSecurityException; 33 import org.apache.geronimo.security.DomainPrincipal; 34 import org.apache.geronimo.security.PrimaryDomainPrincipal; 35 import org.apache.geronimo.security.PrimaryPrincipal; 36 import org.apache.geronimo.security.PrimaryRealmPrincipal; 37 import org.apache.geronimo.security.RealmPrincipal; 38 import org.apache.geronimo.security.deploy.DefaultDomainPrincipal; 39 import org.apache.geronimo.security.deploy.DefaultPrincipal; 40 import org.apache.geronimo.security.deploy.DefaultRealmPrincipal; 41 import org.apache.geronimo.security.deploy.PrincipalInfo; 42 43 44 /** 45 * A collection of utility functions that assist with the configuration of 46 * <code>PolicyConfiguration</code>s. 47 * 48 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $ 49 * @see javax.security.jacc.PolicyConfiguration 50 * @see "JSR 115" Java Authorization Contract for Containers 51 */ 52 public class ConfigurationUtil { 53 54 /** 55 * Create an X500Principal from a deployment description. 56 * 57 * @param name the distinguished name of the principal 58 * @return an X500Principal from a deployment description 59 */ 60 public static X500Principal generateX500Principal(String name) { 61 return new X500Principal(name); 62 } 63 64 /** 65 * Create a Principal from a deployment description. 66 * 67 * @param principalInfo the deployment description of the principal to be created. 68 * @param classLoader 69 * @return a RealmPrincipal from a deployment description 70 */ 71 public static java.security.Principal generatePrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) { 72 return generatePrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 73 } 74 75 public static java.security.Principal generatePrincipal(final String className, final String principalName, final ClassLoader classLoader) { 76 try { 77 return (java.security.Principal) AccessController.doPrivileged(new PrivilegedExceptionAction() { 78 public Object run() throws Exception { 79 Class clazz = classLoader.loadClass(className); 80 Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class}); 81 return (java.security.Principal) constructor.newInstance(new Object[]{principalName}); 82 } 83 }); 84 } catch (PrivilegedActionException e) { 85 e.printStackTrace(); 86 if (e.getException() != null) { 87 e.getException().printStackTrace(); 88 } 89 return null; 90 } 91 } 92 93 /** 94 * Create a RealmPrincipal from a deployment description. 95 * 96 * @param principalInfo the deployment description of the principal to be created. 97 * @param classLoader 98 * @return a RealmPrincipal from a deployment description 99 */ 100 public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) { 101 return generateRealmPrincipal(realm, loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 102 } 103 104 public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final String className, final String principalName, 105 ClassLoader classLoader) 106 { 107 return new RealmPrincipal(realm, loginDomain, generatePrincipal(className, principalName, classLoader)); 108 } 109 110 /** 111 * Create a DomainPrincipal from a deployment description. 112 * 113 * @param principalInfo the deployment description of the principal to be created. 114 * @param classLoader 115 * @return a RealmPrincipal from a deployment description 116 */ 117 public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) { 118 return generateDomainPrincipal(loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 119 } 120 121 public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final String className, final String principalName, ClassLoader classLoader) { 122 return new DomainPrincipal(loginDomain, generatePrincipal(className, principalName, classLoader)); 123 } 124 125 /** 126 * Create a RealmPrincipal from a deployment description. 127 * 128 * @param principalInfo the deployment description of the principal to be created. 129 * @param classLoader 130 * @return a PrimaryRealmPrincipal from a deployment description 131 */ 132 public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 133 return generatePrimaryRealmPrincipal(realm, domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 134 } 135 136 public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final String className, final String principalName, 137 final ClassLoader classLoader) throws DeploymentException 138 { 139 try { 140 return (PrimaryRealmPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() { 141 public Object run() throws Exception { 142 java.security.Principal p = null; 143 Class clazz = classLoader.loadClass(className); 144 Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class}); 145 p = (java.security.Principal) constructor.newInstance(new Object[]{principalName}); 146 147 return new PrimaryRealmPrincipal(realm, domain, p); 148 } 149 }); 150 } catch (PrivilegedActionException pae) { 151 throw new DeploymentException("Unable to create realm principal", pae.getException()); 152 } 153 } 154 155 /** 156 * Create a DomainPrincipal from a deployment description. 157 * 158 * @param principalInfo the deployment description of the principal to be created. 159 * @param classLoader 160 * @return a PrimaryDomainPrincipal from a deployment description 161 */ 162 public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 163 return generatePrimaryDomainPrincipal(domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 164 } 165 166 public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final String className, final String principalName, 167 final ClassLoader classLoader) throws DeploymentException 168 { 169 try { 170 return (PrimaryDomainPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() { 171 public Object run() throws Exception { 172 java.security.Principal p = null; 173 Class clazz = classLoader.loadClass(className); 174 Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class}); 175 p = (java.security.Principal) constructor.newInstance(new Object[]{principalName}); 176 177 return new PrimaryDomainPrincipal(domain, p); 178 } 179 }); 180 } catch (PrivilegedActionException pae) { 181 throw new DeploymentException("Unable to create domain principal", pae.getException()); 182 } 183 } 184 185 /** 186 * Create a Principal from a deployment description. 187 * 188 * @param principalInfo the deployment description of the principal to be created. 189 * @param classLoader 190 * @return a Principal from a deployment description 191 */ 192 public static PrimaryPrincipal generatePrimaryPrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 193 return generatePrimaryPrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 194 } 195 196 public static PrimaryPrincipal generatePrimaryPrincipal(final String className, final String principalName, final ClassLoader classLoader) throws DeploymentException { 197 try { 198 return (PrimaryPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction() { 199 public Object run() throws Exception { 200 java.security.Principal p = null; 201 Class clazz = classLoader.loadClass(className); 202 Constructor constructor = clazz.getDeclaredConstructor(new Class[]{String.class}); 203 p = (java.security.Principal) constructor.newInstance(new Object[]{principalName}); 204 205 return new PrimaryPrincipal(p); 206 } 207 }); 208 } catch (PrivilegedActionException pae) { 209 throw new DeploymentException("Unable to create principal", pae.getException()); 210 } 211 } 212 213 /** 214 * Generate the default principal from the security config. 215 * 216 * @param defaultPrincipal 217 * @param classLoader 218 * @return the default principal 219 */ 220 public static Subject generateDefaultSubject(DefaultPrincipal defaultPrincipal, ClassLoader classLoader) throws DeploymentException { 221 if (defaultPrincipal == null) { 222 throw new GeronimoSecurityException("No DefaultPrincipal configuration supplied"); 223 } 224 Subject defaultSubject = new Subject(); 225 java.security.Principal principal; 226 java.security.Principal primaryPrincipal; 227 228 if (defaultPrincipal instanceof DefaultRealmPrincipal) { 229 DefaultRealmPrincipal defaultRealmPrincipal = (DefaultRealmPrincipal) defaultPrincipal; 230 principal = generateRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader); 231 primaryPrincipal = generatePrimaryRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader); 232 } else if (defaultPrincipal instanceof DefaultDomainPrincipal) { 233 DefaultDomainPrincipal defaultDomainPrincipal = (DefaultDomainPrincipal) defaultPrincipal; 234 principal = generateDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader); 235 primaryPrincipal = generatePrimaryDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader); 236 } else { 237 principal = generatePrincipal(defaultPrincipal.getPrincipal(), classLoader); 238 primaryPrincipal = generatePrimaryPrincipal(defaultPrincipal.getPrincipal(), classLoader); 239 240 } 241 defaultSubject.getPrincipals().add(principal); 242 defaultSubject.getPrincipals().add(primaryPrincipal); 243 244 Set namedUserPasswordCredentials = defaultPrincipal.getNamedUserPasswordCredentials(); 245 if (namedUserPasswordCredentials != null) { 246 defaultSubject.getPrivateCredentials().addAll(namedUserPasswordCredentials); 247 } 248 249 return defaultSubject; 250 } 251 252 253 /** 254 * A simple helper method to register PolicyContextHandlers 255 * 256 * @param handler an object that implements the <code>PolicyContextHandler</code> 257 * interface. The value of this parameter must not be null. 258 * @param replace this boolean value defines the behavior of this method 259 * if, when it is called, a <code>PolicyContextHandler</code> has already 260 * been registered to handle the same key. In that case, and if the value 261 * of this argument is true, the existing handler is replaced with the 262 * argument handler. If the value of this parameter is false the existing 263 * registration is preserved and an exception is thrown. 264 */ 265 public static void registerPolicyContextHandler(PolicyContextHandler handler, boolean replace) throws PolicyContextException { 266 String[] keys = handler.getKeys(); 267 268 for (int i = 0; i < keys.length; i++) { 269 PolicyContext.registerHandler(keys[i], handler, replace); 270 } 271 } 272 273 274 }