1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
4 import java
.net
.Inet6Address
;
5 import java
.net
.InetAddress
;
7 import java
.net
.URISyntaxException
;
8 import java
.nio
.file
.Files
;
9 import java
.nio
.file
.Path
;
10 import java
.security
.PrivilegedExceptionAction
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Dictionary
;
13 import java
.util
.HashMap
;
14 import java
.util
.Hashtable
;
15 import java
.util
.Iterator
;
18 import javax
.naming
.ldap
.LdapName
;
19 import javax
.security
.auth
.Subject
;
20 import javax
.security
.auth
.callback
.Callback
;
21 import javax
.security
.auth
.callback
.CallbackHandler
;
22 import javax
.security
.auth
.callback
.NameCallback
;
23 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
24 import javax
.security
.auth
.kerberos
.KerberosPrincipal
;
25 import javax
.security
.auth
.login
.LoginContext
;
26 import javax
.security
.auth
.login
.LoginException
;
27 import javax
.transaction
.TransactionManager
;
29 import org
.apache
.commons
.httpclient
.auth
.AuthPolicy
;
30 import org
.apache
.commons
.httpclient
.auth
.CredentialsProvider
;
31 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
32 import org
.apache
.commons
.httpclient
.params
.DefaultHttpParams
;
33 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
34 import org
.apache
.commons
.httpclient
.params
.HttpParams
;
35 import org
.apache
.commons
.logging
.Log
;
36 import org
.apache
.commons
.logging
.LogFactory
;
37 import org
.argeo
.cms
.CmsException
;
38 import org
.argeo
.cms
.internal
.http
.NodeHttp
;
39 import org
.argeo
.cms
.internal
.http
.client
.HttpCredentialProvider
;
40 import org
.argeo
.cms
.internal
.http
.client
.SpnegoAuthScheme
;
41 import org
.argeo
.naming
.DnsBrowser
;
42 import org
.argeo
.node
.NodeConstants
;
43 import org
.argeo
.osgi
.useradmin
.AbstractUserDirectory
;
44 import org
.argeo
.osgi
.useradmin
.AggregatingUserAdmin
;
45 import org
.argeo
.osgi
.useradmin
.LdapUserAdmin
;
46 import org
.argeo
.osgi
.useradmin
.LdifUserAdmin
;
47 import org
.argeo
.osgi
.useradmin
.OsUserDirectory
;
48 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
49 import org
.argeo
.osgi
.useradmin
.UserDirectory
;
50 import org
.ietf
.jgss
.GSSCredential
;
51 import org
.ietf
.jgss
.GSSException
;
52 import org
.ietf
.jgss
.GSSManager
;
53 import org
.ietf
.jgss
.GSSName
;
54 import org
.ietf
.jgss
.Oid
;
55 import org
.osgi
.framework
.BundleContext
;
56 import org
.osgi
.framework
.Constants
;
57 import org
.osgi
.framework
.FrameworkUtil
;
58 import org
.osgi
.framework
.ServiceRegistration
;
59 import org
.osgi
.service
.cm
.ConfigurationException
;
60 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
61 import org
.osgi
.service
.useradmin
.UserAdmin
;
62 import org
.osgi
.util
.tracker
.ServiceTracker
;
64 import bitronix
.tm
.BitronixTransactionManager
;
65 import bitronix
.tm
.resource
.ehcache
.EhCacheXAResourceProducer
;
68 * Aggregates multiple {@link UserDirectory} and integrates them with system
71 class NodeUserAdmin
extends AggregatingUserAdmin
implements ManagedServiceFactory
, KernelConstants
{
72 private final static Log log
= LogFactory
.getLog(NodeUserAdmin
.class);
73 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
76 private Map
<String
, LdapName
> pidToBaseDn
= new HashMap
<>();
77 private Map
<String
, ServiceRegistration
<UserDirectory
>> pidToServiceRegs
= new HashMap
<>();
78 private ServiceRegistration
<UserAdmin
> userAdminReg
;
81 private final ServiceTracker
<TransactionManager
, TransactionManager
> tmTracker
;
82 private final String cacheName
= UserDirectory
.class.getName();
85 private Path nodeKeyTab
= KernelUtils
.getOsgiInstancePath(KernelConstants
.NODE_KEY_TAB_PATH
);
86 private GSSCredential acceptorCredentials
;
88 private boolean singleUser
= false;
90 public NodeUserAdmin(String systemRolesBaseDn
) {
91 super(systemRolesBaseDn
);
92 tmTracker
= new ServiceTracker
<>(bc
, TransactionManager
.class, null);
97 public void updated(String pid
, Dictionary
<String
, ?
> properties
) throws ConfigurationException
{
98 String uri
= (String
) properties
.get(UserAdminConf
.uri
.name());
102 String baseDn
= (String
) properties
.get(UserAdminConf
.baseDn
.name());
103 u
= KernelUtils
.getOsgiInstanceUri(KernelConstants
.DIR_NODE
+ '/' + baseDn
+ ".ldif");
106 } catch (URISyntaxException e
) {
107 throw new CmsException("Badly formatted URI " + uri
, e
);
111 AbstractUserDirectory userDirectory
;
112 if (UserAdminConf
.SCHEME_LDAP
.equals(u
.getScheme())) {
113 userDirectory
= new LdapUserAdmin(properties
);
114 } else if (UserAdminConf
.SCHEME_FILE
.equals(u
.getScheme())) {
115 userDirectory
= new LdifUserAdmin(u
, properties
);
116 } else if (UserAdminConf
.SCHEME_OS
.equals(u
.getScheme())) {
117 userDirectory
= new OsUserDirectory(u
, properties
);
120 throw new CmsException("Unsupported scheme " + u
.getScheme());
122 Object realm
= userDirectory
.getProperties().get(UserAdminConf
.realm
.name());
123 addUserDirectory(userDirectory
);
126 LdapName baseDn
= userDirectory
.getBaseDn();
127 Dictionary
<String
, Object
> regProps
= new Hashtable
<>();
128 regProps
.put(Constants
.SERVICE_PID
, pid
);
129 if (isSystemRolesBaseDn(baseDn
))
130 regProps
.put(Constants
.SERVICE_RANKING
, Integer
.MAX_VALUE
);
131 regProps
.put(UserAdminConf
.baseDn
.name(), baseDn
);
132 ServiceRegistration
<UserDirectory
> reg
= bc
.registerService(UserDirectory
.class, userDirectory
, regProps
);
133 pidToBaseDn
.put(pid
, baseDn
);
134 pidToServiceRegs
.put(pid
, reg
);
136 if (log
.isDebugEnabled())
137 log
.debug("User directory " + userDirectory
.getBaseDn() + " [" + u
.getScheme() + "] enabled."
138 + (realm
!= null ?
" " + realm
+ " realm." : ""));
140 if (!isSystemRolesBaseDn(baseDn
)) {
141 if (userAdminReg
!= null)
142 userAdminReg
.unregister();
143 // register self as main user admin
144 Dictionary
<String
, Object
> userAdminregProps
= currentState();
145 userAdminregProps
.put(NodeConstants
.CN
, NodeConstants
.DEFAULT
);
146 userAdminregProps
.put(Constants
.SERVICE_RANKING
, Integer
.MAX_VALUE
);
147 userAdminReg
= bc
.registerService(UserAdmin
.class, this, userAdminregProps
);
152 public void deleted(String pid
) {
153 assert pidToServiceRegs
.get(pid
) != null;
154 assert pidToBaseDn
.get(pid
) != null;
155 pidToServiceRegs
.remove(pid
).unregister();
156 LdapName baseDn
= pidToBaseDn
.remove(pid
);
157 removeUserDirectory(baseDn
);
161 public String
getName() {
162 return "Node User Admin";
165 protected void postAdd(AbstractUserDirectory userDirectory
) {
167 TransactionManager tm
= tmTracker
.getService();
169 throw new CmsException("A JTA transaction manager must be available.");
170 userDirectory
.setTransactionManager(tm
);
171 if (tmTracker
.getService() instanceof BitronixTransactionManager
)
172 EhCacheXAResourceProducer
.registerXAResource(cacheName
, userDirectory
.getXaResource());
174 Object realm
= userDirectory
.getProperties().get(UserAdminConf
.realm
.name());
176 if (Files
.exists(nodeKeyTab
)) {
177 String servicePrincipal
= getKerberosServicePrincipal(realm
.toString());
178 if (servicePrincipal
!= null) {
179 CallbackHandler callbackHandler
= new CallbackHandler() {
181 public void handle(Callback
[] callbacks
) throws IOException
, UnsupportedCallbackException
{
182 for (Callback callback
: callbacks
)
183 if (callback
instanceof NameCallback
)
184 ((NameCallback
) callback
).setName(servicePrincipal
);
189 LoginContext nodeLc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_NODE
, callbackHandler
);
191 acceptorCredentials
= logInAsAcceptor(nodeLc
.getSubject(), servicePrincipal
);
192 } catch (LoginException e
) {
193 throw new CmsException("Cannot log in kernel", e
);
198 // Register client-side SPNEGO auth scheme
199 AuthPolicy
.registerAuthScheme(SpnegoAuthScheme
.NAME
, SpnegoAuthScheme
.class);
200 HttpParams params
= DefaultHttpParams
.getDefaultParams();
201 ArrayList
<String
> schemes
= new ArrayList
<>();
202 schemes
.add(SpnegoAuthScheme
.NAME
);// SPNEGO preferred
203 // schemes.add(AuthPolicy.BASIC);// incompatible with Basic
204 params
.setParameter(AuthPolicy
.AUTH_SCHEME_PRIORITY
, schemes
);
205 params
.setParameter(CredentialsProvider
.PROVIDER
, new HttpCredentialProvider());
206 params
.setParameter(HttpMethodParams
.COOKIE_POLICY
, CookiePolicy
.BROWSER_COMPATIBILITY
);
207 // params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
211 protected void preDestroy(AbstractUserDirectory userDirectory
) {
212 if (tmTracker
.getService() instanceof BitronixTransactionManager
)
213 EhCacheXAResourceProducer
.unregisterXAResource(cacheName
, userDirectory
.getXaResource());
215 Object realm
= userDirectory
.getProperties().get(UserAdminConf
.realm
.name());
217 if (acceptorCredentials
!= null) {
219 acceptorCredentials
.dispose();
220 } catch (GSSException e
) {
223 acceptorCredentials
= null;
228 private String
getKerberosServicePrincipal(String realm
) {
230 try (DnsBrowser dnsBrowser
= new DnsBrowser()) {
231 InetAddress localhost
= InetAddress
.getLocalHost();
232 hostname
= localhost
.getHostName();
233 String dnsZone
= hostname
.substring(hostname
.indexOf('.') + 1);
234 String ipfromDns
= dnsBrowser
.getRecord(hostname
, localhost
instanceof Inet6Address ?
"AAAA" : "A");
235 boolean consistentIp
= localhost
.getHostAddress().equals(ipfromDns
);
236 String kerberosDomain
= dnsBrowser
.getRecord("_kerberos." + dnsZone
, "TXT");
237 if (consistentIp
&& kerberosDomain
!= null && kerberosDomain
.equals(realm
) && Files
.exists(nodeKeyTab
)) {
238 return NodeHttp
.DEFAULT_SERVICE
+ "/" + hostname
+ "@" + kerberosDomain
;
241 } catch (Exception e
) {
242 log
.warn("Exception when determining kerberos principal", e
);
247 private GSSCredential
logInAsAcceptor(Subject subject
, String servicePrincipal
) {
249 Iterator
<KerberosPrincipal
> krb5It
= subject
.getPrincipals(KerberosPrincipal
.class).iterator();
250 if (!krb5It
.hasNext())
252 KerberosPrincipal krb5Principal
= null;
253 while (krb5It
.hasNext()) {
254 KerberosPrincipal principal
= krb5It
.next();
255 if (principal
.getName().equals(servicePrincipal
))
256 krb5Principal
= principal
;
259 if (krb5Principal
== null)
262 GSSManager manager
= GSSManager
.getInstance();
264 GSSName gssName
= manager
.createName(krb5Principal
.getName(), null);
265 GSSCredential serverCredentials
= Subject
.doAs(subject
, new PrivilegedExceptionAction
<GSSCredential
>() {
268 public GSSCredential
run() throws GSSException
{
269 return manager
.createCredential(gssName
, GSSCredential
.INDEFINITE_LIFETIME
, KERBEROS_OID
,
270 GSSCredential
.ACCEPT_ONLY
);
275 if (log
.isDebugEnabled())
276 log
.debug("GSS acceptor configured for " + krb5Principal
);
277 return serverCredentials
;
278 } catch (Exception gsse
) {
279 throw new CmsException("Cannot create acceptor credentials for " + krb5Principal
, gsse
);
283 public GSSCredential
getAcceptorCredentials() {
284 return acceptorCredentials
;
287 public boolean isSingleUser() {
291 public final static Oid KERBEROS_OID
;
294 KERBEROS_OID
= new Oid("1.3.6.1.5.5.2");
295 } catch (GSSException e
) {
296 throw new IllegalStateException("Cannot create Kerberos OID", e
);