1 23 24 package com.sun.enterprise.security.auth.realm.ldap; 25 26 import java.util.*; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 import javax.naming.Context ; 30 import com.sun.logging.LogDomains; 31 32 import com.sun.enterprise.security.acl.RoleMapper; 33 import com.sun.enterprise.security.auth.realm.User; 34 import com.sun.enterprise.security.auth.realm.Realm; 35 import com.sun.enterprise.security.auth.realm.BadRealmException; 36 import com.sun.enterprise.security.auth.realm.NoSuchUserException; 37 import com.sun.enterprise.security.auth.realm.NoSuchRealmException; 38 import com.sun.enterprise.security.auth.realm.AuthenticationHandler; 39 import com.sun.enterprise.security.auth.realm.InvalidOperationException; 40 41 import com.sun.enterprise.security.RealmConfig; 42 import com.sun.enterprise.security.auth.realm.IASRealm; 43 44 45 86 public final class LDAPRealm extends IASRealm 87 { 88 public static final String AUTH_TYPE = "ldap"; 90 91 public static final String PARAM_DIRURL="directory"; 93 public static final String PARAM_USERDN="base-dn"; 94 95 public static final String PARAM_SEARCH_FILTER="search-filter"; 99 public static final String PARAM_GRPDN="group-base-dn"; 100 public static final String PARAM_GRP_SEARCH_FILTER="group-search-filter"; 101 public static final String PARAM_GRP_TARGET="group-target"; 102 public static final String PARAM_MODE="mode"; 103 public static final String PARAM_JNDICF="jndiCtxFactory"; 104 public static final String PARAM_POOLSIZE="pool-size"; 105 106 public static final String PARAM_BINDDN="search-bind-dn"; 108 public static final String PARAM_BINDPWD="search-bind-password"; 109 110 public static final String MODE_FIND_BIND="find-bind"; 112 113 public static final String SUBST_SUBJECT_NAME="%s"; 115 public static final String SUBST_SUBJECT_DN="%d"; 116 117 private static final String SEARCH_FILTER_DEFAULT= 119 "uid="+SUBST_SUBJECT_NAME; 120 private static final String GRP_SEARCH_FILTER_DEFAULT= 121 "uniquemember="+SUBST_SUBJECT_DN; 122 private static final String GRP_TARGET_DEFAULT="cn"; 123 private static final String MODE_DEFAULT=MODE_FIND_BIND; 124 private static final String JNDICF_DEFAULT= 125 "com.sun.jndi.ldap.LdapCtxFactory"; 126 private static final int POOLSIZE_DEFAULT=5; 127 128 private static final String SUN_JNDI_POOL = "com.sun.jndi.ldap.connect.pool"; 129 private static final String SUN_JNDI_POOL_ = "com.sun.jndi.ldap.connect.pool."; 130 private static final String SUN_JNDI_POOL_MAXSIZE = "com.sun.jndi.ldap.connect.pool.maxsize"; 131 private static final String DYNAMIC_GROUP_OBJECT_FACTORY = 133 "com.sun.jndi.ldap.obj.LdapGroupFactory"; 134 public static final String DYNAMIC_GROUP_FACTORY_OBJECT_PROPERTY = 135 "java.naming.factory.object"; 136 private static final String DYNAMIC_GROUP_STATE_FACTORY = 137 "com.sun.jndi.ldap.obj.LdapGroupFactory"; 138 public static final String DYNAMIC_GROUP_STATE_FACTORY_PROPERTY = 139 "java.naming.factory.state"; 140 141 public static final String DYNAMIC_GROUP_FILTER = 142 "(&(objectclass=groupofuniquenames)(objectclass=*groupofurls*))"; 143 144 private HashMap groupCache; 145 private Vector emptyVector; 146 private Properties ldapBindProps = new Properties(); 147 148 160 public synchronized void init(Properties props) 161 throws BadRealmException, NoSuchRealmException 162 { 163 String url = props.getProperty(PARAM_DIRURL); 164 this.setProperty(PARAM_DIRURL, url); 165 ldapBindProps.setProperty(Context.PROVIDER_URL, url); 166 167 String dn = props.getProperty(PARAM_USERDN); 168 this.setProperty(PARAM_USERDN, dn); 169 170 String jaasCtx = props.getProperty(IASRealm.JAAS_CONTEXT_PARAM); 171 this.setProperty(IASRealm.JAAS_CONTEXT_PARAM, jaasCtx); 172 173 if (url==null || dn==null || jaasCtx==null) { 174 String msg = sm.getString("ldaprealm.badconfig", url, dn, jaasCtx); 175 throw new BadRealmException(msg); 176 } 177 178 String mode = props.getProperty(PARAM_MODE, MODE_DEFAULT); 179 if (!MODE_DEFAULT.equals(mode)) { 180 String msg = sm.getString("ldaprealm.badmode", mode); 181 throw new BadRealmException(msg); 182 } 183 this.setProperty(PARAM_MODE, mode); 184 185 String ctxF = props.getProperty(PARAM_JNDICF, JNDICF_DEFAULT); 186 this.setProperty(PARAM_JNDICF, ctxF); 187 ldapBindProps.setProperty(Context.INITIAL_CONTEXT_FACTORY, ctxF); 188 189 String searchFilter = props.getProperty( 190 PARAM_SEARCH_FILTER, SEARCH_FILTER_DEFAULT); 191 this.setProperty(PARAM_SEARCH_FILTER,searchFilter); 192 193 String grpDN = props.getProperty(PARAM_GRPDN, dn); 194 this.setProperty(PARAM_GRPDN, grpDN); 195 196 String grpSearchFilter = props.getProperty( 197 PARAM_GRP_SEARCH_FILTER, GRP_SEARCH_FILTER_DEFAULT); 198 this.setProperty(PARAM_GRP_SEARCH_FILTER, grpSearchFilter); 199 200 String grpTarget = props.getProperty( 201 PARAM_GRP_TARGET, GRP_TARGET_DEFAULT); 202 this.setProperty(PARAM_GRP_TARGET, grpTarget); 203 204 String objectFactory = props.getProperty( 205 DYNAMIC_GROUP_FACTORY_OBJECT_PROPERTY, DYNAMIC_GROUP_OBJECT_FACTORY); 206 this.setProperty(DYNAMIC_GROUP_FACTORY_OBJECT_PROPERTY, objectFactory); 207 ldapBindProps.setProperty(DYNAMIC_GROUP_FACTORY_OBJECT_PROPERTY, objectFactory); 208 209 String stateFactory = props.getProperty( 210 DYNAMIC_GROUP_STATE_FACTORY_PROPERTY, DYNAMIC_GROUP_STATE_FACTORY); 211 this.setProperty(DYNAMIC_GROUP_STATE_FACTORY_PROPERTY, stateFactory); 212 ldapBindProps.setProperty(DYNAMIC_GROUP_STATE_FACTORY_PROPERTY, stateFactory); 213 214 String bindDN = props.getProperty(PARAM_BINDDN); 215 if (bindDN != null) { 216 this.setProperty(PARAM_BINDDN, bindDN); 217 ldapBindProps.setProperty(Context.SECURITY_PRINCIPAL, bindDN); 218 } 219 String bindPWD = props.getProperty(PARAM_BINDPWD); 220 if (bindPWD != null) { 221 this.setProperty(PARAM_BINDPWD, bindPWD); 222 ldapBindProps.setProperty(Context.SECURITY_CREDENTIALS, bindPWD); 223 } 224 225 Enumeration penum = props.propertyNames(); 226 while (penum.hasMoreElements()) { 227 String propName = (String )penum.nextElement(); 228 if (propName.startsWith("java.naming.") || 229 propName.startsWith("javax.security.")) { 230 ldapBindProps.setProperty(propName, props.getProperty(propName)); 231 } else if (propName.startsWith(SUN_JNDI_POOL_) && 232 !SUN_JNDI_POOL_MAXSIZE.equals(propName)) { 233 if (System.getProperty(propName) == null) { 234 System.setProperty(propName, props.getProperty(propName)); 235 } 236 } 237 } 238 239 String poolSize = 240 Integer.getInteger(PARAM_POOLSIZE,POOLSIZE_DEFAULT).toString(); 241 String sunPoolSizeStr = props.getProperty(SUN_JNDI_POOL_MAXSIZE, 242 poolSize); 243 try { 245 sunPoolSizeStr = Integer.valueOf(sunPoolSizeStr).toString(); 246 } catch(Exception ex) { 247 sunPoolSizeStr = poolSize; 248 } 249 if (System.getProperty(SUN_JNDI_POOL_MAXSIZE) == null) { 250 System.setProperty(SUN_JNDI_POOL_MAXSIZE, sunPoolSizeStr); 251 } 252 this.setProperty(PARAM_POOLSIZE, sunPoolSizeStr); 253 254 String usePool = props.getProperty(SUN_JNDI_POOL, "true"); 255 ldapBindProps.setProperty(SUN_JNDI_POOL, usePool); 256 257 if (_logger.isLoggable(Level.FINE)) { 258 Properties tempProps = (Properties)ldapBindProps.clone(); 259 tempProps.remove(Context.SECURITY_CREDENTIALS); 260 _logger.log(Level.FINE, "LDAPRealm : " + tempProps); 261 } 262 263 groupCache = new HashMap(); 264 emptyVector = new Vector(); 265 } 266 267 268 275 public String getAuthType() 276 { 277 return AUTH_TYPE; 278 } 279 280 281 282 286 public Properties getLdapBindProps() 287 { 288 return (Properties)ldapBindProps.clone(); 289 } 290 291 292 305 public Enumeration getGroupNames (String username) 306 throws InvalidOperationException, NoSuchUserException 307 { 308 Vector v = (Vector)groupCache.get(username); 309 if (v == null) { 310 if (_logger.isLoggable(Level.FINE)) { 311 _logger.log(Level.FINE, "No groups available for: "+username); 312 } 313 return emptyVector.elements(); 314 } else { 315 return v.elements(); 316 } 317 } 318 319 320 326 public void setGroupNames(String username, String [] groups) 327 { 328 Vector v = new Vector(groups.length); 329 for (int i=0; i<groups.length; i++) { 330 v.add(groups[i]); 331 } 332 groupCache.put(username, v); 333 } 334 335 336 337 } 338 | Popular Tags |