]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/osgi/useradmin/UserAdminConf.java
1 package org
.argeo
.osgi
.useradmin
;
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 javax
.naming
.Context
;
13 import javax
.naming
.ldap
.LdapName
;
15 import org
.argeo
.util
.naming
.NamingUtils
;
17 /** Properties used to configure user admins. */
18 public enum UserAdminConf
{
19 /** Base DN (cannot be configured externally) */
20 baseDn("dc=example,dc=com"),
22 /** URI of the underlying resource (cannot be configured externally) */
23 uri("ldap://localhost:10389"),
25 /** User objectClass */
26 userObjectClass("inetOrgPerson"),
28 /** Relative base DN for users */
29 userBase("ou=People"),
31 /** Groups objectClass */
32 groupObjectClass("groupOfNames"),
34 /** Relative base DN for users */
35 groupBase("ou=Groups"),
37 /** Read-only source */
40 /** Disabled source */
43 /** Authentication realm */
46 /** Override all passwords with this value (typically for testing purposes) */
49 public final static String FACTORY_PID
= "org.argeo.osgi.useradmin.config";
51 public final static String SCHEME_LDAP
= "ldap";
52 public final static String SCHEME_LDAPS
= "ldaps";
53 public final static String SCHEME_FILE
= "file";
54 public final static String SCHEME_OS
= "os";
55 public final static String SCHEME_IPA
= "ipa";
57 /** The default value. */
60 UserAdminConf(Object def
) {
64 public Object
getDefault() {
69 * For use as Java property.
71 * @deprecated use {@link #name()} instead
74 public String
property() {
78 public String
getValue(Dictionary
<String
, ?
> properties
) {
79 Object res
= getRawValue(properties
);
82 return res
.toString();
85 @SuppressWarnings("unchecked")
86 public <T
> T
getRawValue(Dictionary
<String
, ?
> properties
) {
87 Object res
= properties
.get(name());
93 /** @deprecated use {@link #valueOf(String)} instead */
95 public static UserAdminConf
local(String property
) {
96 return UserAdminConf
.valueOf(property
);
99 /** Hides host and credentials. */
100 public static URI
propertiesAsUri(Dictionary
<String
, ?
> properties
) {
101 StringBuilder query
= new StringBuilder();
103 boolean first
= true;
104 // for (Enumeration<String> keys = properties.keys(); keys.hasMoreElements();) {
105 // String key = keys.nextElement();
106 // // TODO clarify which keys are relevant (list only the enum?)
107 // if (!key.equals("service.factoryPid") && !key.equals("cn") && !key.equals("dn")
108 // && !key.equals(Constants.SERVICE_PID) && !key.startsWith("java") && !key.equals(baseDn.name())
109 // && !key.equals(uri.name()) && !key.equals(Constants.OBJECTCLASS)
110 // && !key.equals(Constants.SERVICE_ID) && !key.equals("bundle.id")) {
114 // query.append('&');
115 // query.append(valueOf(key).name());
116 // query.append('=').append(properties.get(key).toString());
120 keys
: for (UserAdminConf key
: UserAdminConf
.values()) {
121 if (key
.equals(baseDn
) || key
.equals(uri
))
123 Object value
= properties
.get(key
.name());
130 query
.append(key
.name());
131 query
.append('=').append(value
.toString());
135 Object bDnObj
= properties
.get(baseDn
.name());
136 String bDn
= bDnObj
!= null ? bDnObj
.toString() : null;
138 return new URI(null, null, bDn
!= null ?
'/' + bDn
: null, query
.length() != 0 ? query
.toString() : null,
140 } catch (URISyntaxException e
) {
141 throw new UserDirectoryException("Cannot create URI from properties", e
);
145 public static Dictionary
<String
, Object
> uriAsProperties(String uriStr
) {
147 Hashtable
<String
, Object
> res
= new Hashtable
<String
, Object
>();
148 URI u
= new URI(uriStr
);
149 String scheme
= u
.getScheme();
150 if (scheme
!= null && scheme
.equals(SCHEME_IPA
)) {
151 return IpaUtils
.convertIpaUri(u
);
152 // scheme = u.getScheme();
154 String path
= u
.getPath();
156 String bDn
= path
.substring(path
.lastIndexOf('/') + 1, path
.length());
157 if (bDn
.equals("") && SCHEME_OS
.equals(scheme
)) {
158 bDn
= getBaseDnFromHostname();
161 if (bDn
.endsWith(".ldif"))
162 bDn
= bDn
.substring(0, bDn
.length() - ".ldif".length());
164 // Normalize base DN as LDAP name
165 bDn
= new LdapName(bDn
).toString();
167 String principal
= null;
168 String credentials
= null;
170 if (scheme
.equals(SCHEME_LDAP
) || scheme
.equals(SCHEME_LDAPS
)) {
171 // TODO additional checks
172 if (u
.getUserInfo() != null) {
173 String
[] userInfo
= u
.getUserInfo().split(":");
174 principal
= userInfo
.length
> 0 ? userInfo
[0] : null;
175 credentials
= userInfo
.length
> 1 ? userInfo
[1] : null;
177 } else if (scheme
.equals(SCHEME_FILE
)) {
178 } else if (scheme
.equals(SCHEME_IPA
)) {
179 } else if (scheme
.equals(SCHEME_OS
)) {
181 throw new UserDirectoryException("Unsupported scheme " + scheme
);
182 Map
<String
, List
<String
>> query
= NamingUtils
.queryToMap(u
);
183 for (String key
: query
.keySet()) {
184 UserAdminConf ldapProp
= UserAdminConf
.valueOf(key
);
185 List
<String
> values
= query
.get(key
);
186 if (values
.size() == 1) {
187 res
.put(ldapProp
.name(), values
.get(0));
189 throw new UserDirectoryException("Only single values are supported");
192 res
.put(baseDn
.name(), bDn
);
193 if (SCHEME_OS
.equals(scheme
))
194 res
.put(readOnly
.name(), "true");
195 if (principal
!= null)
196 res
.put(Context
.SECURITY_PRINCIPAL
, principal
);
197 if (credentials
!= null)
198 res
.put(Context
.SECURITY_CREDENTIALS
, credentials
);
199 if (scheme
!= null) {// relative URIs are dealt with externally
200 if (SCHEME_OS
.equals(scheme
)) {
201 res
.put(uri
.name(), SCHEME_OS
+ ":///");
203 URI bareUri
= new URI(scheme
, null, u
.getHost(), u
.getPort(),
204 scheme
.equals(SCHEME_FILE
) ? u
.getPath() : null, null, null);
205 res
.put(uri
.name(), bareUri
.toString());
209 } catch (Exception e
) {
210 throw new UserDirectoryException("Cannot convert " + uri
+ " to properties", e
);
214 private static String
getBaseDnFromHostname() {
217 hostname
= InetAddress
.getLocalHost().getHostName();
218 } catch (UnknownHostException e
) {
219 hostname
= "localhost.localdomain";
221 int dotIdx
= hostname
.indexOf('.');
223 String domain
= hostname
.substring(dotIdx
+ 1, hostname
.length());
224 String bDn
= ("." + domain
).replaceAll("\\.", ",dc=");
225 bDn
= bDn
.substring(1, bDn
.length());
228 return "dc=" + hostname
;
233 * Hash the base DN in order to have a deterministic string to be used as a cn
234 * for the underlying user directory.
236 public static String
baseDnHash(Dictionary
<String
, Object
> properties
) {
237 String bDn
= (String
) properties
.get(baseDn
.name());
239 throw new UserDirectoryException("No baseDn in " + properties
);
240 return DigestUtils
.sha1str(bDn
);