1 package org
.argeo
.cms
.runtime
;
3 import java
.net
.InetAddress
;
5 import java
.net
.URISyntaxException
;
6 import java
.net
.UnknownHostException
;
7 import java
.util
.Dictionary
;
8 import java
.util
.Hashtable
;
12 import org
.argeo
.api
.acr
.ldap
.NamingUtils
;
13 import org
.argeo
.api
.cms
.directory
.DirectoryDigestUtils
;
14 import org
.argeo
.cms
.directory
.ldap
.IpaUtils
;
16 /** Properties used to configure user admins. */
17 public enum DirectoryConf
{
18 /** Base DN (cannot be configured externally) */
21 /** URI of the underlying resource (cannot be configured externally) */
24 /** User objectClass */
25 userObjectClass("inetOrgPerson"),
27 /** Relative base DN for users */
28 userBase("ou=People"),
30 /** Groups objectClass */
31 groupObjectClass("groupOfNames"),
33 /** Relative base DN for users */
34 groupBase("ou=Groups"),
36 /** Relative base DN for users */
37 systemRoleBase("ou=Roles"),
39 /** Read-only source */
42 /** Disabled source */
45 /** Authentication realm */
48 /** Override all passwords with this value (typically for testing purposes) */
51 public final static String FACTORY_PID
= "org.argeo.osgi.useradmin.config";
53 public final static String SCHEME_LDAP
= "ldap";
54 public final static String SCHEME_LDAPS
= "ldaps";
55 public final static String SCHEME_FILE
= "file";
56 public final static String SCHEME_OS
= "os";
57 public final static String SCHEME_IPA
= "ipa";
59 private final static String SECURITY_PRINCIPAL
= "java.naming.security.principal";
60 private final static String SECURITY_CREDENTIALS
= "java.naming.security.credentials";
62 /** The default value. */
65 DirectoryConf(Object def
) {
69 public Object
getDefault() {
74 * For use as Java property.
76 * @deprecated use {@link #name()} instead
79 public String
property() {
83 public String
getValue(Dictionary
<String
, ?
> properties
) {
84 Object res
= getRawValue(properties
);
87 return res
.toString();
90 @SuppressWarnings("unchecked")
91 public <T
> T
getRawValue(Dictionary
<String
, ?
> properties
) {
92 Object res
= properties
.get(name());
98 /** @deprecated use {@link #valueOf(String)} instead */
100 public static DirectoryConf
local(String property
) {
101 return DirectoryConf
.valueOf(property
);
104 /** Hides host and credentials. */
105 public static URI
propertiesAsUri(Dictionary
<String
, ?
> properties
) {
106 StringBuilder query
= new StringBuilder();
108 boolean first
= true;
109 // for (Enumeration<String> keys = properties.keys(); keys.hasMoreElements();) {
110 // String key = keys.nextElement();
111 // // TODO clarify which keys are relevant (list only the enum?)
112 // if (!key.equals("service.factoryPid") && !key.equals("cn") && !key.equals("dn")
113 // && !key.equals(Constants.SERVICE_PID) && !key.startsWith("java") && !key.equals(baseDn.name())
114 // && !key.equals(uri.name()) && !key.equals(Constants.OBJECTCLASS)
115 // && !key.equals(Constants.SERVICE_ID) && !key.equals("bundle.id")) {
119 // query.append('&');
120 // query.append(valueOf(key).name());
121 // query.append('=').append(properties.get(key).toString());
125 keys
: for (DirectoryConf key
: DirectoryConf
.values()) {
126 if (key
.equals(baseDn
) || key
.equals(uri
))
128 Object value
= properties
.get(key
.name());
135 query
.append(key
.name());
136 query
.append('=').append(value
.toString());
140 Object bDnObj
= properties
.get(baseDn
.name());
141 String bDn
= bDnObj
!= null ? bDnObj
.toString() : null;
143 return new URI(null, null, bDn
!= null ?
'/' + bDn
: null, query
.length() != 0 ? query
.toString() : null,
145 } catch (URISyntaxException e
) {
146 throw new IllegalArgumentException("Cannot create URI from properties", e
);
150 public static Dictionary
<String
, Object
> uriAsProperties(String uriStr
) {
152 Hashtable
<String
, Object
> res
= new Hashtable
<String
, Object
>();
153 URI u
= new URI(uriStr
);
154 String scheme
= u
.getScheme();
155 if (scheme
!= null && scheme
.equals(SCHEME_IPA
)) {
156 return IpaUtils
.convertIpaUri(u
);
157 // scheme = u.getScheme();
159 String path
= u
.getPath();
161 String bDn
= path
.substring(path
.lastIndexOf('/') + 1, path
.length());
162 if (bDn
.equals("") && SCHEME_OS
.equals(scheme
)) {
163 bDn
= getBaseDnFromHostname();
166 if (bDn
.endsWith(".ldif"))
167 bDn
= bDn
.substring(0, bDn
.length() - ".ldif".length());
169 // Normalize base DN as LDAP name
170 // bDn = new LdapName(bDn).toString();
172 String principal
= null;
173 String credentials
= null;
175 if (scheme
.equals(SCHEME_LDAP
) || scheme
.equals(SCHEME_LDAPS
)) {
176 // TODO additional checks
177 if (u
.getUserInfo() != null) {
178 String
[] userInfo
= u
.getUserInfo().split(":");
179 principal
= userInfo
.length
> 0 ? userInfo
[0] : null;
180 credentials
= userInfo
.length
> 1 ? userInfo
[1] : null;
182 } else if (scheme
.equals(SCHEME_FILE
)) {
183 } else if (scheme
.equals(SCHEME_IPA
)) {
184 } else if (scheme
.equals(SCHEME_OS
)) {
186 throw new IllegalArgumentException("Unsupported scheme " + scheme
);
187 Map
<String
, List
<String
>> query
= NamingUtils
.queryToMap(u
);
188 for (String key
: query
.keySet()) {
189 DirectoryConf ldapProp
= DirectoryConf
.valueOf(key
);
190 List
<String
> values
= query
.get(key
);
191 if (values
.size() == 1) {
192 res
.put(ldapProp
.name(), values
.get(0));
194 throw new IllegalArgumentException("Only single values are supported");
197 res
.put(baseDn
.name(), bDn
);
198 if (SCHEME_OS
.equals(scheme
))
199 res
.put(readOnly
.name(), "true");
200 if (principal
!= null)
201 res
.put(SECURITY_PRINCIPAL
, principal
);
202 if (credentials
!= null)
203 res
.put(SECURITY_CREDENTIALS
, credentials
);
204 if (scheme
!= null) {// relative URIs are dealt with externally
205 if (SCHEME_OS
.equals(scheme
)) {
206 res
.put(uri
.name(), SCHEME_OS
+ ":///");
208 URI bareUri
= new URI(scheme
, null, u
.getHost(), u
.getPort(),
209 scheme
.equals(SCHEME_FILE
) ? u
.getPath() : null, null, null);
210 res
.put(uri
.name(), bareUri
.toString());
214 } catch (URISyntaxException e
) {
215 throw new IllegalArgumentException("Cannot convert " + uri
+ " to properties", e
);
219 private static String
getBaseDnFromHostname() {
222 hostname
= InetAddress
.getLocalHost().getHostName();
223 } catch (UnknownHostException e
) {
224 hostname
= "localhost.localdomain";
226 int dotIdx
= hostname
.indexOf('.');
228 String domain
= hostname
.substring(dotIdx
+ 1, hostname
.length());
229 String bDn
= ("." + domain
).replaceAll("\\.", ",dc=");
230 bDn
= bDn
.substring(1, bDn
.length());
233 return "dc=" + hostname
;
238 * Hash the base DN in order to have a deterministic string to be used as a cn
239 * for the underlying user directory.
241 public static String
baseDnHash(Dictionary
<String
, Object
> properties
) {
242 String bDn
= (String
) properties
.get(baseDn
.name());
244 throw new IllegalStateException("No baseDn in " + properties
);
245 return DirectoryDigestUtils
.sha1str(bDn
);