]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserAdminConf.java
3631de40f45a9c26bed050b1f1f7445e73c7fc24
[lgpl/argeo-commons.git] / 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.util.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 /** Override all passwords with this value (typically for testing purposes) */
47 forcedPassword(null);
48
49 public final static String FACTORY_PID = "org.argeo.osgi.useradmin.config";
50
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";
56
57 /** The default value. */
58 private Object def;
59
60 UserAdminConf(Object def) {
61 this.def = def;
62 }
63
64 public Object getDefault() {
65 return def;
66 }
67
68 /**
69 * For use as Java property.
70 *
71 * @deprecated use {@link #name()} instead
72 */
73 @Deprecated
74 public String property() {
75 return name();
76 }
77
78 public String getValue(Dictionary<String, ?> properties) {
79 Object res = getRawValue(properties);
80 if (res == null)
81 return null;
82 return res.toString();
83 }
84
85 @SuppressWarnings("unchecked")
86 public <T> T getRawValue(Dictionary<String, ?> properties) {
87 Object res = properties.get(name());
88 if (res == null)
89 res = getDefault();
90 return (T) res;
91 }
92
93 /** @deprecated use {@link #valueOf(String)} instead */
94 @Deprecated
95 public static UserAdminConf local(String property) {
96 return UserAdminConf.valueOf(property);
97 }
98
99 /** Hides host and credentials. */
100 public static URI propertiesAsUri(Dictionary<String, ?> properties) {
101 StringBuilder query = new StringBuilder();
102
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")) {
111 // if (first)
112 // first = false;
113 // else
114 // query.append('&');
115 // query.append(valueOf(key).name());
116 // query.append('=').append(properties.get(key).toString());
117 // }
118 // }
119
120 keys: for (UserAdminConf key : UserAdminConf.values()) {
121 if (key.equals(baseDn) || key.equals(uri))
122 continue keys;
123 Object value = properties.get(key.name());
124 if (value == null)
125 continue keys;
126 if (first)
127 first = false;
128 else
129 query.append('&');
130 query.append(key.name());
131 query.append('=').append(value.toString());
132
133 }
134
135 Object bDnObj = properties.get(baseDn.name());
136 String bDn = bDnObj != null ? bDnObj.toString() : null;
137 try {
138 return new URI(null, null, bDn != null ? '/' + bDn : null, query.length() != 0 ? query.toString() : null,
139 null);
140 } catch (URISyntaxException e) {
141 throw new UserDirectoryException("Cannot create URI from properties", e);
142 }
143 }
144
145 public static Dictionary<String, Object> uriAsProperties(String uriStr) {
146 try {
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();
153 }
154 String path = u.getPath();
155 // base DN
156 String bDn = path.substring(path.lastIndexOf('/') + 1, path.length());
157 if (bDn.equals("") && SCHEME_OS.equals(scheme)) {
158 bDn = getBaseDnFromHostname();
159 }
160
161 if (bDn.endsWith(".ldif"))
162 bDn = bDn.substring(0, bDn.length() - ".ldif".length());
163
164 // Normalize base DN as LDAP name
165 bDn = new LdapName(bDn).toString();
166
167 String principal = null;
168 String credentials = null;
169 if (scheme != 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;
176 }
177 } else if (scheme.equals(SCHEME_FILE)) {
178 } else if (scheme.equals(SCHEME_IPA)) {
179 } else if (scheme.equals(SCHEME_OS)) {
180 } else
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));
188 } else {
189 throw new UserDirectoryException("Only single values are supported");
190 }
191 }
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 + ":///");
202 } else {
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());
206 }
207 }
208 return res;
209 } catch (Exception e) {
210 throw new UserDirectoryException("Cannot convert " + uri + " to properties", e);
211 }
212 }
213
214 private static String getBaseDnFromHostname() {
215 String hostname;
216 try {
217 hostname = InetAddress.getLocalHost().getHostName();
218 } catch (UnknownHostException e) {
219 hostname = "localhost.localdomain";
220 }
221 int dotIdx = hostname.indexOf('.');
222 if (dotIdx >= 0) {
223 String domain = hostname.substring(dotIdx + 1, hostname.length());
224 String bDn = ("." + domain).replaceAll("\\.", ",dc=");
225 bDn = bDn.substring(1, bDn.length());
226 return bDn;
227 } else {
228 return "dc=" + hostname;
229 }
230 }
231
232 /**
233 * Hash the base DN in order to have a deterministic string to be used as a cn
234 * for the underlying user directory.
235 */
236 public static String baseDnHash(Dictionary<String, Object> properties) {
237 String bDn = (String) properties.get(baseDn.name());
238 if (bDn == null)
239 throw new UserDirectoryException("No baseDn in " + properties);
240 return DigestUtils.sha1str(bDn);
241 }
242 }