]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.enterprise/src/org/argeo/osgi/useradmin/UserAdminConf.java
Change the approach for releases
[lgpl/argeo-commons.git] / org.argeo.enterprise / src / org / argeo / osgi / useradmin / UserAdminConf.java
1 package org.argeo.osgi.useradmin;
2
3 import java.net.InetAddress;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.net.UnknownHostException;
7 import java.util.Dictionary;
8 import java.util.Hashtable;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.naming.Context;
13 import javax.naming.ldap.LdapName;
14
15 import org.argeo.naming.NamingUtils;
16
17 /** Properties used to configure user admins. */
18 public enum UserAdminConf {
19 /** Base DN (cannot be configured externally) */
20 baseDn("dc=example,dc=com"),
21
22 /** URI of the underlying resource (cannot be configured externally) */
23 uri("ldap://localhost:10389"),
24
25 /** User objectClass */
26 userObjectClass("inetOrgPerson"),
27
28 /** Relative base DN for users */
29 userBase("ou=People"),
30
31 /** Groups objectClass */
32 groupObjectClass("groupOfNames"),
33
34 /** Relative base DN for users */
35 groupBase("ou=Groups"),
36
37 /** Read-only source */
38 readOnly(null),
39
40 /** Disabled source */
41 disabled(null),
42
43 /** Authentication realm */
44 realm(null);
45
46 public final static String FACTORY_PID = "org.argeo.osgi.useradmin.config";
47
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";
53
54 /** The default value. */
55 private Object def;
56
57 UserAdminConf(Object def) {
58 this.def = def;
59 }
60
61 public Object getDefault() {
62 return def;
63 }
64
65 /**
66 * For use as Java property.
67 *
68 * @deprecated use {@link #name()} instead
69 */
70 @Deprecated
71 public String property() {
72 return name();
73 }
74
75 public String getValue(Dictionary<String, ?> properties) {
76 Object res = getRawValue(properties);
77 if (res == null)
78 return null;
79 return res.toString();
80 }
81
82 @SuppressWarnings("unchecked")
83 public <T> T getRawValue(Dictionary<String, ?> properties) {
84 Object res = properties.get(name());
85 if (res == null)
86 res = getDefault();
87 return (T) res;
88 }
89
90 /** @deprecated use {@link #valueOf(String)} instead */
91 @Deprecated
92 public static UserAdminConf local(String property) {
93 return UserAdminConf.valueOf(property);
94 }
95
96 /** Hides host and credentials. */
97 public static URI propertiesAsUri(Dictionary<String, ?> properties) {
98 StringBuilder query = new StringBuilder();
99
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")) {
108 // if (first)
109 // first = false;
110 // else
111 // query.append('&');
112 // query.append(valueOf(key).name());
113 // query.append('=').append(properties.get(key).toString());
114 // }
115 // }
116
117 keys: for (UserAdminConf key : UserAdminConf.values()) {
118 if (key.equals(baseDn) || key.equals(uri))
119 continue keys;
120 Object value = properties.get(key.name());
121 if (value == null)
122 continue keys;
123 if (first)
124 first = false;
125 else
126 query.append('&');
127 query.append(key.name());
128 query.append('=').append(value.toString());
129
130 }
131
132 Object bDnObj = properties.get(baseDn.name());
133 String bDn = bDnObj != null ? bDnObj.toString() : null;
134 try {
135 return new URI(null, null, bDn != null ? '/' + bDn : null, query.length() != 0 ? query.toString() : null,
136 null);
137 } catch (URISyntaxException e) {
138 throw new UserDirectoryException("Cannot create URI from properties", e);
139 }
140 }
141
142 public static Dictionary<String, Object> uriAsProperties(String uriStr) {
143 try {
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();
150 }
151 String path = u.getPath();
152 // base DN
153 String bDn = path.substring(path.lastIndexOf('/') + 1, path.length());
154 if (bDn.equals("") && SCHEME_OS.equals(scheme)) {
155 bDn = getBaseDnFromHostname();
156 }
157
158 if (bDn.endsWith(".ldif"))
159 bDn = bDn.substring(0, bDn.length() - ".ldif".length());
160
161 // Normalize base DN as LDAP name
162 bDn = new LdapName(bDn).toString();
163
164 String principal = null;
165 String credentials = null;
166 if (scheme != 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;
173 }
174 } else if (scheme.equals(SCHEME_FILE)) {
175 } else if (scheme.equals(SCHEME_IPA)) {
176 } else if (scheme.equals(SCHEME_OS)) {
177 } else
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));
185 } else {
186 throw new UserDirectoryException("Only single values are supported");
187 }
188 }
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 + ":///");
199 } else {
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());
203 }
204 }
205 return res;
206 } catch (Exception e) {
207 throw new UserDirectoryException("Cannot convert " + uri + " to properties", e);
208 }
209 }
210
211 private static String getBaseDnFromHostname() {
212 String hostname;
213 try {
214 hostname = InetAddress.getLocalHost().getHostName();
215 } catch (UnknownHostException e) {
216 hostname = "localhost.localdomain";
217 }
218 int dotIdx = hostname.indexOf('.');
219 if (dotIdx >= 0) {
220 String domain = hostname.substring(dotIdx + 1, hostname.length());
221 String bDn = ("." + domain).replaceAll("\\.", ",dc=");
222 bDn = bDn.substring(1, bDn.length());
223 return bDn;
224 } else {
225 return "dc=" + hostname;
226 }
227 }
228
229 /**
230 * Hash the base DN in order to have a deterministic string to be used as a cn
231 * for the underlying user directory.
232 */
233 public static String baseDnHash(Dictionary<String, Object> properties) {
234 String bDn = (String) properties.get(baseDn.name());
235 if (bDn == null)
236 throw new UserDirectoryException("No baseDn in " + properties);
237 return DigestUtils.sha1str(bDn);
238 }
239 }