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
.User
;
32 * A user admin based on a LDAP server. Requires a {@link TransactionManager}
33 * and an open transaction for write access.
35 public class LdapUserAdmin
extends AbstractUserDirectory
{
36 private final static Log log
= LogFactory
.getLog(LdapUserAdmin
.class);
38 private InitialLdapContext initialLdapContext
= null;
40 public LdapUserAdmin(Dictionary
<String
, ?
> properties
) {
43 Hashtable
<String
, Object
> connEnv
= new Hashtable
<String
, Object
>();
44 connEnv
.put(Context
.INITIAL_CONTEXT_FACTORY
, "com.sun.jndi.ldap.LdapCtxFactory");
45 connEnv
.put(Context
.PROVIDER_URL
, getUri().toString());
46 connEnv
.put("java.naming.ldap.attributes.binary", LdapAttrs
.userPassword
.name());
48 initialLdapContext
= new InitialLdapContext(connEnv
, null);
49 // StartTlsResponse tls = (StartTlsResponse) ctx
50 // .extendedOperation(new StartTlsRequest());
52 initialLdapContext
.addToEnvironment(Context
.SECURITY_AUTHENTICATION
, "simple");
53 Object principal
= properties
.get(Context
.SECURITY_PRINCIPAL
);
54 if (principal
!= null) {
55 initialLdapContext
.addToEnvironment(Context
.SECURITY_PRINCIPAL
, principal
.toString());
56 Object creds
= properties
.get(Context
.SECURITY_CREDENTIALS
);
58 initialLdapContext
.addToEnvironment(Context
.SECURITY_CREDENTIALS
, creds
.toString());
62 // initialLdapContext.addToEnvironment(Context.SECURITY_PRINCIPAL,
63 // "uid=admin,ou=system");
64 // initialLdapContext.addToEnvironment(Context.SECURITY_CREDENTIALS,
66 } catch (Exception e
) {
67 throw new UserDirectoryException("Cannot connect to LDAP", e
);
71 public void destroy() {
74 initialLdapContext
.close();
75 } catch (NamingException e
) {
76 log
.error("Cannot destroy LDAP user admin", e
);
82 @SuppressWarnings("unchecked")
84 protected AbstractUserDirectory
scope(User user
) {
85 Dictionary
<String
, Object
> credentials
= user
.getCredentials();
87 Object usernameObj
= credentials
.get(SHARED_STATE_USERNAME
);
88 Object passwordObj
= credentials
.get(SHARED_STATE_PASSWORD
);
89 Dictionary
<String
, Object
> properties
= cloneProperties();
90 properties
.put(Context
.SECURITY_PRINCIPAL
, usernameObj
.toString());
91 properties
.put(Context
.SECURITY_CREDENTIALS
, passwordObj
.toString());
92 return new LdapUserAdmin(properties
);
95 protected InitialLdapContext
getLdapContext() {
96 return initialLdapContext
;
100 protected Boolean
daoHasRole(LdapName dn
) {
101 return daoGetRole(dn
) != null;
105 protected DirectoryUser
daoGetRole(LdapName name
) {
107 Attributes attrs
= getLdapContext().getAttributes(name
);
108 if (attrs
.size() == 0)
111 if (attrs
.get(objectClass
.name()).contains(getGroupObjectClass()))
112 res
= new LdifGroup(this, name
, attrs
);
113 else if (attrs
.get(objectClass
.name()).contains(getUserObjectClass()))
114 res
= new LdifUser(this, name
, attrs
);
116 throw new UserDirectoryException("Unsupported LDAP type for " + name
);
118 } catch (NamingException e
) {
124 protected List
<DirectoryUser
> doGetRoles(Filter f
) {
126 String searchFilter
= f
!= null ? f
.toString()
127 : "(|(" + objectClass
+ "=" + getUserObjectClass() + ")(" + objectClass
+ "="
128 + getGroupObjectClass() + "))";
129 SearchControls searchControls
= new SearchControls();
130 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
132 LdapName searchBase
= getBaseDn();
133 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
135 ArrayList
<DirectoryUser
> res
= new ArrayList
<DirectoryUser
>();
136 results
: while (results
.hasMoreElements()) {
137 SearchResult searchResult
= results
.next();
138 Attributes attrs
= searchResult
.getAttributes();
139 Attribute objectClassAttr
= attrs
.get(objectClass
.name());
140 LdapName dn
= toDn(searchBase
, searchResult
);
142 if (objectClassAttr
.contains(getGroupObjectClass()))
143 role
= new LdifGroup(this, dn
, attrs
);
144 else if (objectClassAttr
.contains(getUserObjectClass()))
145 role
= new LdifUser(this, dn
, attrs
);
147 log
.warn("Unsupported LDAP type for " + searchResult
.getName());
153 } catch (Exception e
) {
154 throw new UserDirectoryException("Cannot get roles for filter " + f
, e
);
158 private LdapName
toDn(LdapName baseDn
, Binding binding
) throws InvalidNameException
{
159 return new LdapName(binding
.isRelative() ? binding
.getName() + "," + baseDn
: binding
.getName());
163 protected List
<LdapName
> getDirectGroups(LdapName dn
) {
164 List
<LdapName
> directGroups
= new ArrayList
<LdapName
>();
166 String searchFilter
= "(&(" + objectClass
+ "=" + getGroupObjectClass() + ")(" + getMemberAttributeId()
169 SearchControls searchControls
= new SearchControls();
170 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
172 LdapName searchBase
= getBaseDn();
173 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
175 while (results
.hasMoreElements()) {
176 SearchResult searchResult
= (SearchResult
) results
.nextElement();
177 directGroups
.add(toDn(searchBase
, searchResult
));
180 } catch (Exception e
) {
181 throw new UserDirectoryException("Cannot populate direct members of " + dn
, e
);
186 protected void prepare(UserDirectoryWorkingCopy wc
) {
188 getLdapContext().reconnect(getLdapContext().getConnectControls());
190 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
191 if (!entryExists(dn
))
192 throw new UserDirectoryException("User to delete no found " + dn
);
195 for (LdapName dn
: wc
.getNewUsers().keySet()) {
197 throw new UserDirectoryException("User to create found " + dn
);
200 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
201 if (!wc
.getNewUsers().containsKey(dn
) && !entryExists(dn
))
202 throw new UserDirectoryException("User to modify not found " + dn
);
204 } catch (NamingException e
) {
205 throw new UserDirectoryException("Cannot prepare LDAP", e
);
209 private boolean entryExists(LdapName dn
) throws NamingException
{
211 return getLdapContext().getAttributes(dn
).size() != 0;
212 } catch (NameNotFoundException e
) {
218 protected void commit(UserDirectoryWorkingCopy wc
) {
221 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
222 getLdapContext().destroySubcontext(dn
);
225 for (LdapName dn
: wc
.getNewUsers().keySet()) {
226 DirectoryUser user
= wc
.getNewUsers().get(dn
);
227 getLdapContext().createSubcontext(dn
, user
.getAttributes());
230 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
231 Attributes modifiedAttrs
= wc
.getModifiedUsers().get(dn
);
232 getLdapContext().modifyAttributes(dn
, DirContext
.REPLACE_ATTRIBUTE
, modifiedAttrs
);
234 } catch (NamingException e
) {
235 throw new UserDirectoryException("Cannot commit LDAP", e
);
240 protected void rollback(UserDirectoryWorkingCopy wc
) {
241 // prepare not impacting