1 package org
.argeo
.osgi
.useradmin
;
3 import static org
.argeo
.naming
.LdapAttrs
.objectClass
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Dictionary
;
7 import java
.util
.Hashtable
;
10 import javax
.naming
.Binding
;
11 import javax
.naming
.Context
;
12 import javax
.naming
.InvalidNameException
;
13 import javax
.naming
.NameNotFoundException
;
14 import javax
.naming
.NamingEnumeration
;
15 import javax
.naming
.NamingException
;
16 import javax
.naming
.directory
.Attribute
;
17 import javax
.naming
.directory
.Attributes
;
18 import javax
.naming
.directory
.DirContext
;
19 import javax
.naming
.directory
.SearchControls
;
20 import javax
.naming
.directory
.SearchResult
;
21 import javax
.naming
.ldap
.InitialLdapContext
;
22 import javax
.naming
.ldap
.LdapName
;
23 import javax
.transaction
.TransactionManager
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.naming
.LdapAttrs
;
28 import org
.osgi
.framework
.Filter
;
29 import org
.osgi
.service
.useradmin
.Role
;
30 import org
.osgi
.service
.useradmin
.User
;
33 * A user admin based on a LDAP server. Requires a {@link TransactionManager}
34 * and an open transaction for write access.
36 public class LdapUserAdmin
extends AbstractUserDirectory
{
37 private final static Log log
= LogFactory
.getLog(LdapUserAdmin
.class);
39 private InitialLdapContext initialLdapContext
= null;
41 public LdapUserAdmin(Dictionary
<String
, ?
> properties
) {
42 super(null, properties
);
44 Hashtable
<String
, Object
> connEnv
= new Hashtable
<String
, Object
>();
45 connEnv
.put(Context
.INITIAL_CONTEXT_FACTORY
, "com.sun.jndi.ldap.LdapCtxFactory");
46 connEnv
.put(Context
.PROVIDER_URL
, getUri().toString());
47 connEnv
.put("java.naming.ldap.attributes.binary", LdapAttrs
.userPassword
.name());
49 initialLdapContext
= new InitialLdapContext(connEnv
, null);
50 // StartTlsResponse tls = (StartTlsResponse) ctx
51 // .extendedOperation(new StartTlsRequest());
53 Object securityAuthentication
= properties
.get(Context
.SECURITY_AUTHENTICATION
);
54 if (securityAuthentication
!= null)
55 initialLdapContext
.addToEnvironment(Context
.SECURITY_AUTHENTICATION
, securityAuthentication
);
57 initialLdapContext
.addToEnvironment(Context
.SECURITY_AUTHENTICATION
, "simple");
58 Object principal
= properties
.get(Context
.SECURITY_PRINCIPAL
);
59 if (principal
!= null) {
60 initialLdapContext
.addToEnvironment(Context
.SECURITY_PRINCIPAL
, principal
.toString());
61 Object creds
= properties
.get(Context
.SECURITY_CREDENTIALS
);
63 initialLdapContext
.addToEnvironment(Context
.SECURITY_CREDENTIALS
, creds
.toString());
67 } catch (Exception e
) {
68 throw new UserDirectoryException("Cannot connect to LDAP", e
);
72 public void destroy() {
75 initialLdapContext
.close();
76 } catch (NamingException e
) {
77 log
.error("Cannot destroy LDAP user admin", e
);
81 @SuppressWarnings("unchecked")
83 protected AbstractUserDirectory
scope(User user
) {
84 Dictionary
<String
, Object
> credentials
= user
.getCredentials();
85 String username
= (String
) credentials
.get(SHARED_STATE_USERNAME
);
87 username
= user
.getName();
88 Dictionary
<String
, Object
> properties
= cloneProperties();
89 properties
.put(Context
.SECURITY_PRINCIPAL
, username
.toString());
90 Object pwdCred
= credentials
.get(SHARED_STATE_PASSWORD
);
91 byte[] pwd
= (byte[]) pwdCred
;
93 char[] password
= DigestUtils
.bytesToChars(pwd
);
94 properties
.put(Context
.SECURITY_CREDENTIALS
, new String(password
));
96 properties
.put(Context
.SECURITY_AUTHENTICATION
, "GSSAPI");
98 return new LdapUserAdmin(properties
);
101 protected InitialLdapContext
getLdapContext() {
102 return initialLdapContext
;
106 protected Boolean
daoHasRole(LdapName dn
) {
108 return daoGetRole(dn
) != null;
109 } catch (NameNotFoundException e
) {
115 protected DirectoryUser
daoGetRole(LdapName name
) throws NameNotFoundException
{
117 Attributes attrs
= getLdapContext().getAttributes(name
);
118 if (attrs
.size() == 0)
120 int roleType
= roleType(name
);
122 if (roleType
== Role
.GROUP
)
123 res
= new LdifGroup(this, name
, attrs
);
124 else if (roleType
== Role
.USER
)
125 res
= new LdifUser(this, name
, attrs
);
127 throw new UserDirectoryException("Unsupported LDAP type for " + name
);
129 } catch (NameNotFoundException e
) {
131 } catch (NamingException e
) {
132 if (log
.isTraceEnabled())
133 log
.error("Cannot get role: " + name
, e
);
139 protected List
<DirectoryUser
> doGetRoles(Filter f
) {
141 String searchFilter
= f
!= null ? f
.toString()
142 : "(|(" + objectClass
+ "=" + getUserObjectClass() + ")(" + objectClass
+ "="
143 + getGroupObjectClass() + "))";
144 SearchControls searchControls
= new SearchControls();
145 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
147 LdapName searchBase
= getBaseDn();
148 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
150 ArrayList
<DirectoryUser
> res
= new ArrayList
<DirectoryUser
>();
151 results
: while (results
.hasMoreElements()) {
152 SearchResult searchResult
= results
.next();
153 Attributes attrs
= searchResult
.getAttributes();
154 Attribute objectClassAttr
= attrs
.get(objectClass
.name());
155 LdapName dn
= toDn(searchBase
, searchResult
);
157 if (objectClassAttr
.contains(getGroupObjectClass())
158 || objectClassAttr
.contains(getGroupObjectClass().toLowerCase()))
159 role
= new LdifGroup(this, dn
, attrs
);
160 else if (objectClassAttr
.contains(getUserObjectClass())
161 || objectClassAttr
.contains(getUserObjectClass().toLowerCase()))
162 role
= new LdifUser(this, dn
, attrs
);
164 log
.warn("Unsupported LDAP type for " + searchResult
.getName());
170 } catch (Exception e
) {
171 throw new UserDirectoryException("Cannot get roles for filter " + f
, e
);
175 private LdapName
toDn(LdapName baseDn
, Binding binding
) throws InvalidNameException
{
176 return new LdapName(binding
.isRelative() ? binding
.getName() + "," + baseDn
: binding
.getName());
180 protected List
<LdapName
> getDirectGroups(LdapName dn
) {
181 List
<LdapName
> directGroups
= new ArrayList
<LdapName
>();
183 String searchFilter
= "(&(" + objectClass
+ "=" + getGroupObjectClass() + ")(" + getMemberAttributeId()
186 SearchControls searchControls
= new SearchControls();
187 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
189 LdapName searchBase
= getBaseDn();
190 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
192 while (results
.hasMoreElements()) {
193 SearchResult searchResult
= (SearchResult
) results
.nextElement();
194 directGroups
.add(toDn(searchBase
, searchResult
));
197 } catch (Exception e
) {
198 throw new UserDirectoryException("Cannot populate direct members of " + dn
, e
);
203 protected void prepare(UserDirectoryWorkingCopy wc
) {
205 getLdapContext().reconnect(getLdapContext().getConnectControls());
207 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
208 if (!entryExists(dn
))
209 throw new UserDirectoryException("User to delete no found " + dn
);
212 for (LdapName dn
: wc
.getNewUsers().keySet()) {
214 throw new UserDirectoryException("User to create found " + dn
);
217 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
218 if (!wc
.getNewUsers().containsKey(dn
) && !entryExists(dn
))
219 throw new UserDirectoryException("User to modify not found " + dn
);
221 } catch (NamingException e
) {
222 throw new UserDirectoryException("Cannot prepare LDAP", e
);
226 private boolean entryExists(LdapName dn
) throws NamingException
{
228 return getLdapContext().getAttributes(dn
).size() != 0;
229 } catch (NameNotFoundException e
) {
235 protected void commit(UserDirectoryWorkingCopy wc
) {
238 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
239 getLdapContext().destroySubcontext(dn
);
242 for (LdapName dn
: wc
.getNewUsers().keySet()) {
243 DirectoryUser user
= wc
.getNewUsers().get(dn
);
244 getLdapContext().createSubcontext(dn
, user
.getAttributes());
247 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
248 Attributes modifiedAttrs
= wc
.getModifiedUsers().get(dn
);
249 getLdapContext().modifyAttributes(dn
, DirContext
.REPLACE_ATTRIBUTE
, modifiedAttrs
);
251 } catch (NamingException e
) {
252 throw new UserDirectoryException("Cannot commit LDAP", e
);
257 protected void rollback(UserDirectoryWorkingCopy wc
) {
258 // prepare not impacting