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
.argeo
.naming
.LdapAttrs
;
26 import org
.osgi
.framework
.Filter
;
27 import org
.osgi
.service
.useradmin
.Role
;
28 import org
.osgi
.service
.useradmin
.User
;
31 * A user admin based on a LDAP server. Requires a {@link TransactionManager}
32 * and an open transaction for write access.
34 public class LdapUserAdmin
extends AbstractUserDirectory
{
35 private InitialLdapContext initialLdapContext
= null;
37 public LdapUserAdmin(Dictionary
<String
, ?
> properties
) {
38 super(null, properties
);
40 Hashtable
<String
, Object
> connEnv
= new Hashtable
<String
, Object
>();
41 connEnv
.put(Context
.INITIAL_CONTEXT_FACTORY
, "com.sun.jndi.ldap.LdapCtxFactory");
42 connEnv
.put(Context
.PROVIDER_URL
, getUri().toString());
43 connEnv
.put("java.naming.ldap.attributes.binary", LdapAttrs
.userPassword
.name());
45 initialLdapContext
= new InitialLdapContext(connEnv
, null);
46 // StartTlsResponse tls = (StartTlsResponse) ctx
47 // .extendedOperation(new StartTlsRequest());
49 Object securityAuthentication
= properties
.get(Context
.SECURITY_AUTHENTICATION
);
50 if (securityAuthentication
!= null)
51 initialLdapContext
.addToEnvironment(Context
.SECURITY_AUTHENTICATION
, securityAuthentication
);
53 initialLdapContext
.addToEnvironment(Context
.SECURITY_AUTHENTICATION
, "simple");
54 Object principal
= properties
.get(Context
.SECURITY_PRINCIPAL
);
55 if (principal
!= null) {
56 initialLdapContext
.addToEnvironment(Context
.SECURITY_PRINCIPAL
, principal
.toString());
57 Object creds
= properties
.get(Context
.SECURITY_CREDENTIALS
);
59 initialLdapContext
.addToEnvironment(Context
.SECURITY_CREDENTIALS
, creds
.toString());
63 } catch (Exception e
) {
64 throw new UserDirectoryException("Cannot connect to LDAP", e
);
68 public void destroy() {
71 initialLdapContext
.close();
72 } catch (NamingException e
) {
78 protected AbstractUserDirectory
scope(User user
) {
79 Dictionary
<String
, Object
> credentials
= user
.getCredentials();
80 String username
= (String
) credentials
.get(SHARED_STATE_USERNAME
);
82 username
= user
.getName();
83 Dictionary
<String
, Object
> properties
= cloneProperties();
84 properties
.put(Context
.SECURITY_PRINCIPAL
, username
.toString());
85 Object pwdCred
= credentials
.get(SHARED_STATE_PASSWORD
);
86 byte[] pwd
= (byte[]) pwdCred
;
88 char[] password
= DigestUtils
.bytesToChars(pwd
);
89 properties
.put(Context
.SECURITY_CREDENTIALS
, new String(password
));
91 properties
.put(Context
.SECURITY_AUTHENTICATION
, "GSSAPI");
93 return new LdapUserAdmin(properties
);
96 protected InitialLdapContext
getLdapContext() {
97 return initialLdapContext
;
101 protected Boolean
daoHasRole(LdapName dn
) {
103 return daoGetRole(dn
) != null;
104 } catch (NameNotFoundException e
) {
110 protected DirectoryUser
daoGetRole(LdapName name
) throws NameNotFoundException
{
112 Attributes attrs
= getLdapContext().getAttributes(name
);
113 if (attrs
.size() == 0)
115 int roleType
= roleType(name
);
117 if (roleType
== Role
.GROUP
)
118 res
= new LdifGroup(this, name
, attrs
);
119 else if (roleType
== Role
.USER
)
120 res
= new LdifUser(this, name
, attrs
);
122 throw new UserDirectoryException("Unsupported LDAP type for " + name
);
124 } catch (NameNotFoundException e
) {
126 } catch (NamingException e
) {
132 protected List
<DirectoryUser
> doGetRoles(Filter f
) {
134 String searchFilter
= f
!= null ? f
.toString()
135 : "(|(" + objectClass
+ "=" + getUserObjectClass() + ")(" + objectClass
+ "="
136 + getGroupObjectClass() + "))";
137 SearchControls searchControls
= new SearchControls();
138 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
140 LdapName searchBase
= getBaseDn();
141 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
143 ArrayList
<DirectoryUser
> res
= new ArrayList
<DirectoryUser
>();
144 results
: while (results
.hasMoreElements()) {
145 SearchResult searchResult
= results
.next();
146 Attributes attrs
= searchResult
.getAttributes();
147 Attribute objectClassAttr
= attrs
.get(objectClass
.name());
148 LdapName dn
= toDn(searchBase
, searchResult
);
150 if (objectClassAttr
.contains(getGroupObjectClass())
151 || objectClassAttr
.contains(getGroupObjectClass().toLowerCase()))
152 role
= new LdifGroup(this, dn
, attrs
);
153 else if (objectClassAttr
.contains(getUserObjectClass())
154 || objectClassAttr
.contains(getUserObjectClass().toLowerCase()))
155 role
= new LdifUser(this, dn
, attrs
);
157 // log.warn("Unsupported LDAP type for " + searchResult.getName());
163 } catch (Exception e
) {
164 throw new UserDirectoryException("Cannot get roles for filter " + f
, e
);
168 private LdapName
toDn(LdapName baseDn
, Binding binding
) throws InvalidNameException
{
169 return new LdapName(binding
.isRelative() ? binding
.getName() + "," + baseDn
: binding
.getName());
173 protected List
<LdapName
> getDirectGroups(LdapName dn
) {
174 List
<LdapName
> directGroups
= new ArrayList
<LdapName
>();
176 String searchFilter
= "(&(" + objectClass
+ "=" + getGroupObjectClass() + ")(" + getMemberAttributeId()
179 SearchControls searchControls
= new SearchControls();
180 searchControls
.setSearchScope(SearchControls
.SUBTREE_SCOPE
);
182 LdapName searchBase
= getBaseDn();
183 NamingEnumeration
<SearchResult
> results
= getLdapContext().search(searchBase
, searchFilter
, searchControls
);
185 while (results
.hasMoreElements()) {
186 SearchResult searchResult
= (SearchResult
) results
.nextElement();
187 directGroups
.add(toDn(searchBase
, searchResult
));
190 } catch (Exception e
) {
191 throw new UserDirectoryException("Cannot populate direct members of " + dn
, e
);
196 protected void prepare(UserDirectoryWorkingCopy wc
) {
198 getLdapContext().reconnect(getLdapContext().getConnectControls());
200 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
201 if (!entryExists(dn
))
202 throw new UserDirectoryException("User to delete no found " + dn
);
205 for (LdapName dn
: wc
.getNewUsers().keySet()) {
207 throw new UserDirectoryException("User to create found " + dn
);
210 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
211 if (!wc
.getNewUsers().containsKey(dn
) && !entryExists(dn
))
212 throw new UserDirectoryException("User to modify not found " + dn
);
214 } catch (NamingException e
) {
215 throw new UserDirectoryException("Cannot prepare LDAP", e
);
219 private boolean entryExists(LdapName dn
) throws NamingException
{
221 return getLdapContext().getAttributes(dn
).size() != 0;
222 } catch (NameNotFoundException e
) {
228 protected void commit(UserDirectoryWorkingCopy wc
) {
231 for (LdapName dn
: wc
.getDeletedUsers().keySet()) {
232 getLdapContext().destroySubcontext(dn
);
235 for (LdapName dn
: wc
.getNewUsers().keySet()) {
236 DirectoryUser user
= wc
.getNewUsers().get(dn
);
237 getLdapContext().createSubcontext(dn
, user
.getAttributes());
240 for (LdapName dn
: wc
.getModifiedUsers().keySet()) {
241 Attributes modifiedAttrs
= wc
.getModifiedUsers().get(dn
);
242 getLdapContext().modifyAttributes(dn
, DirContext
.REPLACE_ATTRIBUTE
, modifiedAttrs
);
244 } catch (NamingException e
) {
245 throw new UserDirectoryException("Cannot commit LDAP", e
);
250 protected void rollback(UserDirectoryWorkingCopy wc
) {
251 // prepare not impacting