]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.enterprise/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
.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 public final static String FACTORY_PID
= "org.argeo.osgi.useradmin.config";
48 public final static String SCHEME_LDAP
= "ldap";
49 public final static String SCHEME_LDAPS
= "ldaps";
50 public final static String SCHEME_FILE
= "file";
51 public final static String SCHEME_OS
= "os";
52 public final static String SCHEME_IPA
= "ipa";
54 /** The default value. */
57 UserAdminConf(Object def
) {
61 public Object
getDefault() {
66 * For use as Java property.
68 * @deprecated use {@link #name()} instead
71 public String
property() {
75 public String
getValue(Dictionary
<String
, ?
> properties
) {
76 Object res
= getRawValue(properties
);
79 return res
.toString();
82 @SuppressWarnings("unchecked")
83 public <T
> T
getRawValue(Dictionary
<String
, ?
> properties
) {
84 Object res
= properties
.get(name());
90 /** @deprecated use {@link #valueOf(String)} instead */
92 public static UserAdminConf
local(String property
) {
93 return UserAdminConf
.valueOf(property
);
96 /** Hides host and credentials. */
97 public static URI
propertiesAsUri(Dictionary
<String
, ?
> properties
) {
98 StringBuilder query
= new StringBuilder();
100 boolean first
= true;
101 // for (Enumeration<String> keys = properties.keys(); keys.hasMoreElements();) {
102 // String key = keys.nextElement();
103 // // TODO clarify which keys are relevant (list only the enum?)
104 // if (!key.equals("service.factoryPid") && !key.equals("cn") && !key.equals("dn")
105 // && !key.equals(Constants.SERVICE_PID) && !key.startsWith("java") && !key.equals(baseDn.name())
106 // && !key.equals(uri.name()) && !key.equals(Constants.OBJECTCLASS)
107 // && !key.equals(Constants.SERVICE_ID) && !key.equals("bundle.id")) {
111 // query.append('&');
112 // query.append(valueOf(key).name());
113 // query.append('=').append(properties.get(key).toString());
117 keys
: for (UserAdminConf key
: UserAdminConf
.values()) {
118 if (key
.equals(baseDn
) || key
.equals(uri
))
120 Object value
= properties
.get(key
.name());
127 query
.append(key
.name());
128 query
.append('=').append(value
.toString());
132 Object bDnObj
= properties
.get(baseDn
.name());
133 String bDn
= bDnObj
!= null ? bDnObj
.toString() : null;
135 return new URI(null, null, bDn
!= null ?
'/' + bDn
: null, query
.length() != 0 ? query
.toString() : null,
137 } catch (URISyntaxException e
) {
138 throw new UserDirectoryException("Cannot create URI from properties", e
);
142 public static Dictionary
<String
, Object
> uriAsProperties(String uriStr
) {
144 Hashtable
<String
, Object
> res
= new Hashtable
<String
, Object
>();
145 URI u
= new URI(uriStr
);
146 String scheme
= u
.getScheme();
147 if (scheme
!= null && scheme
.equals(SCHEME_IPA
)) {
148 return IpaUtils
.convertIpaUri(u
);
149 // scheme = u.getScheme();
151 String path
= u
.getPath();
153 String bDn
= path
.substring(path
.lastIndexOf('/') + 1, path
.length());
154 if (bDn
.equals("") && SCHEME_OS
.equals(scheme
)) {
155 bDn
= getBaseDnFromHostname();
158 if (bDn
.endsWith(".ldif"))
159 bDn
= bDn
.substring(0, bDn
.length() - ".ldif".length());
161 // Normalize base DN as LDAP name
162 bDn
= new LdapName(bDn
).toString();
164 String principal
= null;
165 String credentials
= null;
167 if (scheme
.equals(SCHEME_LDAP
) || scheme
.equals(SCHEME_LDAPS
)) {
168 // TODO additional checks
169 if (u
.getUserInfo() != null) {
170 String
[] userInfo
= u
.getUserInfo().split(":");
171 principal
= userInfo
.length
> 0 ? userInfo
[0] : null;
172 credentials
= userInfo
.length
> 1 ? userInfo
[1] : null;
174 } else if (scheme
.equals(SCHEME_FILE
)) {
175 } else if (scheme
.equals(SCHEME_IPA
)) {
176 } else if (scheme
.equals(SCHEME_OS
)) {
178 throw new UserDirectoryException("Unsupported scheme " + scheme
);
179 Map
<String
, List
<String
>> query
= NamingUtils
.queryToMap(u
);
180 for (String key
: query
.keySet()) {
181 UserAdminConf ldapProp
= UserAdminConf
.valueOf(key
);
182 List
<String
> values
= query
.get(key
);
183 if (values
.size() == 1) {
184 res
.put(ldapProp
.name(), values
.get(0));
186 throw new UserDirectoryException("Only single values are supported");
189 res
.put(baseDn
.name(), bDn
);
190 if (SCHEME_OS
.equals(scheme
))
191 res
.put(readOnly
.name(), "true");
192 if (principal
!= null)
193 res
.put(Context
.SECURITY_PRINCIPAL
, principal
);
194 if (credentials
!= null)
195 res
.put(Context
.SECURITY_CREDENTIALS
, credentials
);
196 if (scheme
!= null) {// relative URIs are dealt with externally
197 if (SCHEME_OS
.equals(scheme
)) {
198 res
.put(uri
.name(), SCHEME_OS
+ ":///");
200 URI bareUri
= new URI(scheme
, null, u
.getHost(), u
.getPort(),
201 scheme
.equals(SCHEME_FILE
) ? u
.getPath() : null, null, null);
202 res
.put(uri
.name(), bareUri
.toString());
206 } catch (Exception e
) {
207 throw new UserDirectoryException("Cannot convert " + uri
+ " to properties", e
);
211 private static String
getBaseDnFromHostname() {
214 hostname
= InetAddress
.getLocalHost().getHostName();
215 } catch (UnknownHostException e
) {
216 hostname
= "localhost.localdomain";
218 int dotIdx
= hostname
.indexOf('.');
220 String domain
= hostname
.substring(dotIdx
+ 1, hostname
.length());
221 String bDn
= ("." + domain
).replaceAll("\\.", ",dc=");
222 bDn
= bDn
.substring(1, bDn
.length());
225 return "dc=" + hostname
;
230 * Hash the base DN in order to have a deterministic string to be used as a cn
231 * for the underlying user directory.
233 public static String
baseDnHash(Dictionary
<String
, Object
> properties
) {
234 String bDn
= (String
) properties
.get(baseDn
.name());
236 throw new UserDirectoryException("No baseDn in " + properties
);
237 return DigestUtils
.sha1str(bDn
);