001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 */
026package org.forgerock.opendj.server.config.meta;
027
028
029
030import org.forgerock.opendj.config.AdministratorAction;
031import org.forgerock.opendj.config.BooleanPropertyDefinition;
032import org.forgerock.opendj.config.ClassPropertyDefinition;
033import org.forgerock.opendj.config.client.ConcurrentModificationException;
034import org.forgerock.opendj.config.client.ManagedObject;
035import org.forgerock.opendj.config.client.MissingMandatoryPropertiesException;
036import org.forgerock.opendj.config.client.OperationRejectedException;
037import org.forgerock.opendj.config.DefaultBehaviorProvider;
038import org.forgerock.opendj.config.DefinedDefaultBehaviorProvider;
039import org.forgerock.opendj.config.EnumPropertyDefinition;
040import org.forgerock.opendj.config.ManagedObjectAlreadyExistsException;
041import org.forgerock.opendj.config.ManagedObjectDefinition;
042import org.forgerock.opendj.config.PropertyOption;
043import org.forgerock.opendj.config.PropertyProvider;
044import org.forgerock.opendj.config.server.ConfigurationChangeListener;
045import org.forgerock.opendj.config.server.ServerManagedObject;
046import org.forgerock.opendj.config.Tag;
047import org.forgerock.opendj.ldap.DN;
048import org.forgerock.opendj.ldap.LdapException;
049import org.forgerock.opendj.server.config.client.CryptPasswordStorageSchemeCfgClient;
050import org.forgerock.opendj.server.config.server.CryptPasswordStorageSchemeCfg;
051import org.forgerock.opendj.server.config.server.PasswordStorageSchemeCfg;
052
053
054
055/**
056 * An interface for querying the Crypt Password Storage Scheme managed
057 * object definition meta information.
058 * <p>
059 * The Crypt Password Storage Scheme provides a mechanism for encoding
060 * user passwords like Unix crypt does. Like on most Unix systems, the
061 * password may be encrypted using different algorithms, either Unix
062 * crypt, md5, sha256 or sha512.
063 */
064public final class CryptPasswordStorageSchemeCfgDefn extends ManagedObjectDefinition<CryptPasswordStorageSchemeCfgClient, CryptPasswordStorageSchemeCfg> {
065
066  /** The singleton configuration definition instance. */
067  private static final CryptPasswordStorageSchemeCfgDefn INSTANCE = new CryptPasswordStorageSchemeCfgDefn();
068
069
070
071  /**
072   * Defines the set of permissable values for the "crypt-password-storage-encryption-algorithm" property.
073   * <p>
074   * Specifies the algorithm to use to encrypt new passwords.
075   * <p>
076   * Select the crypt algorithm to use to encrypt new passwords. The
077   * value can either be "unix", which means the password is encrypted
078   * with the weak Unix crypt algorithm, or "md5" which means the
079   * password is encrypted with the BSD MD5 algorithm and has a $1$
080   * prefix, or "sha256" which means the password is encrypted with the
081   * SHA256 algorithm and has a $5$ prefix, or "sha512" which means the
082   * password is encrypted with the SHA512 algorithm and has a $6$
083   * prefix.
084   */
085  public static enum CryptPasswordStorageEncryptionAlgorithm {
086
087    /**
088     * New passwords are encrypted with the BSD MD5 algorithm.
089     */
090    MD5("md5"),
091
092
093
094    /**
095     * New passwords are encrypted with the Unix crypt SHA256
096     * algorithm.
097     */
098    SHA256("sha256"),
099
100
101
102    /**
103     * New passwords are encrypted with the Unix crypt SHA512
104     * algorithm.
105     */
106    SHA512("sha512"),
107
108
109
110    /**
111     * New passwords are encrypted with the Unix crypt algorithm.
112     * Passwords are truncated at 8 characters and the top bit of each
113     * character is ignored.
114     */
115    UNIX("unix");
116
117
118
119    /** String representation of the value. */
120    private final String name;
121
122
123
124    /** Private constructor. */
125    private CryptPasswordStorageEncryptionAlgorithm(String name) { this.name = name; }
126
127
128
129    /** {@inheritDoc} */
130    public String toString() { return name; }
131
132  }
133
134
135
136  /** The "crypt-password-storage-encryption-algorithm" property definition. */
137  private static final EnumPropertyDefinition<CryptPasswordStorageEncryptionAlgorithm> PD_CRYPT_PASSWORD_STORAGE_ENCRYPTION_ALGORITHM;
138
139
140
141  /** The "java-class" property definition. */
142  private static final ClassPropertyDefinition PD_JAVA_CLASS;
143
144
145
146  /** Build the "crypt-password-storage-encryption-algorithm" property definition. */
147  static {
148      EnumPropertyDefinition.Builder<CryptPasswordStorageEncryptionAlgorithm> builder = EnumPropertyDefinition.createBuilder(INSTANCE, "crypt-password-storage-encryption-algorithm");
149      builder.setOption(PropertyOption.MANDATORY);
150      builder.setAdministratorAction(new AdministratorAction(AdministratorAction.Type.NONE, INSTANCE, "crypt-password-storage-encryption-algorithm"));
151      DefaultBehaviorProvider<CryptPasswordStorageEncryptionAlgorithm> provider = new DefinedDefaultBehaviorProvider<CryptPasswordStorageEncryptionAlgorithm>("unix");
152      builder.setDefaultBehaviorProvider(provider);
153      builder.setEnumClass(CryptPasswordStorageEncryptionAlgorithm.class);
154      PD_CRYPT_PASSWORD_STORAGE_ENCRYPTION_ALGORITHM = builder.getInstance();
155      INSTANCE.registerPropertyDefinition(PD_CRYPT_PASSWORD_STORAGE_ENCRYPTION_ALGORITHM);
156  }
157
158
159
160  /** Build the "java-class" property definition. */
161  static {
162      ClassPropertyDefinition.Builder builder = ClassPropertyDefinition.createBuilder(INSTANCE, "java-class");
163      builder.setOption(PropertyOption.MANDATORY);
164      builder.setOption(PropertyOption.ADVANCED);
165      builder.setAdministratorAction(new AdministratorAction(AdministratorAction.Type.NONE, INSTANCE, "java-class"));
166      DefaultBehaviorProvider<String> provider = new DefinedDefaultBehaviorProvider<String>("org.opends.server.extensions.CryptPasswordStorageScheme");
167      builder.setDefaultBehaviorProvider(provider);
168      builder.addInstanceOf("org.opends.server.api.PasswordStorageScheme");
169      PD_JAVA_CLASS = builder.getInstance();
170      INSTANCE.registerPropertyDefinition(PD_JAVA_CLASS);
171  }
172
173
174
175  // Register the tags associated with this managed object definition.
176  static {
177    INSTANCE.registerTag(Tag.valueOf("user-management"));
178  }
179
180
181
182  /**
183   * Get the Crypt Password Storage Scheme configuration definition
184   * singleton.
185   *
186   * @return Returns the Crypt Password Storage Scheme configuration
187   *         definition singleton.
188   */
189  public static CryptPasswordStorageSchemeCfgDefn getInstance() {
190    return INSTANCE;
191  }
192
193
194
195  /**
196   * Private constructor.
197   */
198  private CryptPasswordStorageSchemeCfgDefn() {
199    super("crypt-password-storage-scheme", PasswordStorageSchemeCfgDefn.getInstance());
200  }
201
202
203
204  /** {@inheritDoc} */
205  public CryptPasswordStorageSchemeCfgClient createClientConfiguration(
206      ManagedObject<? extends CryptPasswordStorageSchemeCfgClient> impl) {
207    return new CryptPasswordStorageSchemeCfgClientImpl(impl);
208  }
209
210
211
212  /** {@inheritDoc} */
213  public CryptPasswordStorageSchemeCfg createServerConfiguration(
214      ServerManagedObject<? extends CryptPasswordStorageSchemeCfg> impl) {
215    return new CryptPasswordStorageSchemeCfgServerImpl(impl);
216  }
217
218
219
220  /** {@inheritDoc} */
221  public Class<CryptPasswordStorageSchemeCfg> getServerConfigurationClass() {
222    return CryptPasswordStorageSchemeCfg.class;
223  }
224
225
226
227  /**
228   * Get the "crypt-password-storage-encryption-algorithm" property definition.
229   * <p>
230   * Specifies the algorithm to use to encrypt new passwords.
231   * <p>
232   * Select the crypt algorithm to use to encrypt new passwords. The
233   * value can either be "unix", which means the password is encrypted
234   * with the weak Unix crypt algorithm, or "md5" which means the
235   * password is encrypted with the BSD MD5 algorithm and has a $1$
236   * prefix, or "sha256" which means the password is encrypted with the
237   * SHA256 algorithm and has a $5$ prefix, or "sha512" which means the
238   * password is encrypted with the SHA512 algorithm and has a $6$
239   * prefix.
240   *
241   * @return Returns the "crypt-password-storage-encryption-algorithm" property definition.
242   */
243  public EnumPropertyDefinition<CryptPasswordStorageEncryptionAlgorithm> getCryptPasswordStorageEncryptionAlgorithmPropertyDefinition() {
244    return PD_CRYPT_PASSWORD_STORAGE_ENCRYPTION_ALGORITHM;
245  }
246
247
248
249  /**
250   * Get the "enabled" property definition.
251   * <p>
252   * Indicates whether the Crypt Password Storage Scheme is enabled
253   * for use.
254   *
255   * @return Returns the "enabled" property definition.
256   */
257  public BooleanPropertyDefinition getEnabledPropertyDefinition() {
258    return PasswordStorageSchemeCfgDefn.getInstance().getEnabledPropertyDefinition();
259  }
260
261
262
263  /**
264   * Get the "java-class" property definition.
265   * <p>
266   * Specifies the fully-qualified name of the Java class that
267   * provides the Crypt Password Storage Scheme implementation.
268   *
269   * @return Returns the "java-class" property definition.
270   */
271  public ClassPropertyDefinition getJavaClassPropertyDefinition() {
272    return PD_JAVA_CLASS;
273  }
274
275
276
277  /**
278   * Managed object client implementation.
279   */
280  private static class CryptPasswordStorageSchemeCfgClientImpl implements
281    CryptPasswordStorageSchemeCfgClient {
282
283    /** Private implementation. */
284    private ManagedObject<? extends CryptPasswordStorageSchemeCfgClient> impl;
285
286
287
288    /** Private constructor. */
289    private CryptPasswordStorageSchemeCfgClientImpl(
290        ManagedObject<? extends CryptPasswordStorageSchemeCfgClient> impl) {
291      this.impl = impl;
292    }
293
294
295
296    /** {@inheritDoc} */
297    public CryptPasswordStorageEncryptionAlgorithm getCryptPasswordStorageEncryptionAlgorithm() {
298      return impl.getPropertyValue(INSTANCE.getCryptPasswordStorageEncryptionAlgorithmPropertyDefinition());
299    }
300
301
302
303    /** {@inheritDoc} */
304    public void setCryptPasswordStorageEncryptionAlgorithm(CryptPasswordStorageEncryptionAlgorithm value) {
305      impl.setPropertyValue(INSTANCE.getCryptPasswordStorageEncryptionAlgorithmPropertyDefinition(), value);
306    }
307
308
309
310    /** {@inheritDoc} */
311    public Boolean isEnabled() {
312      return impl.getPropertyValue(INSTANCE.getEnabledPropertyDefinition());
313    }
314
315
316
317    /** {@inheritDoc} */
318    public void setEnabled(boolean value) {
319      impl.setPropertyValue(INSTANCE.getEnabledPropertyDefinition(), value);
320    }
321
322
323
324    /** {@inheritDoc} */
325    public String getJavaClass() {
326      return impl.getPropertyValue(INSTANCE.getJavaClassPropertyDefinition());
327    }
328
329
330
331    /** {@inheritDoc} */
332    public void setJavaClass(String value) {
333      impl.setPropertyValue(INSTANCE.getJavaClassPropertyDefinition(), value);
334    }
335
336
337
338    /** {@inheritDoc} */
339    public ManagedObjectDefinition<? extends CryptPasswordStorageSchemeCfgClient, ? extends CryptPasswordStorageSchemeCfg> definition() {
340      return INSTANCE;
341    }
342
343
344
345    /** {@inheritDoc} */
346    public PropertyProvider properties() {
347      return impl;
348    }
349
350
351
352    /** {@inheritDoc} */
353    public void commit() throws ManagedObjectAlreadyExistsException,
354        MissingMandatoryPropertiesException, ConcurrentModificationException,
355        OperationRejectedException, LdapException {
356      impl.commit();
357    }
358
359
360
361    /** {@inheritDoc} */
362    public String toString() {
363      return impl.toString();
364    }
365  }
366
367
368
369  /**
370   * Managed object server implementation.
371   */
372  private static class CryptPasswordStorageSchemeCfgServerImpl implements
373    CryptPasswordStorageSchemeCfg {
374
375    /** Private implementation. */
376    private ServerManagedObject<? extends CryptPasswordStorageSchemeCfg> impl;
377
378    /** The value of the "crypt-password-storage-encryption-algorithm" property. */
379    private final CryptPasswordStorageEncryptionAlgorithm pCryptPasswordStorageEncryptionAlgorithm;
380
381    /** The value of the "enabled" property. */
382    private final boolean pEnabled;
383
384    /** The value of the "java-class" property. */
385    private final String pJavaClass;
386
387
388
389    /** Private constructor. */
390    private CryptPasswordStorageSchemeCfgServerImpl(ServerManagedObject<? extends CryptPasswordStorageSchemeCfg> impl) {
391      this.impl = impl;
392      this.pCryptPasswordStorageEncryptionAlgorithm = impl.getPropertyValue(INSTANCE.getCryptPasswordStorageEncryptionAlgorithmPropertyDefinition());
393      this.pEnabled = impl.getPropertyValue(INSTANCE.getEnabledPropertyDefinition());
394      this.pJavaClass = impl.getPropertyValue(INSTANCE.getJavaClassPropertyDefinition());
395    }
396
397
398
399    /** {@inheritDoc} */
400    public void addCryptChangeListener(
401        ConfigurationChangeListener<CryptPasswordStorageSchemeCfg> listener) {
402      impl.registerChangeListener(listener);
403    }
404
405
406
407    /** {@inheritDoc} */
408    public void removeCryptChangeListener(
409        ConfigurationChangeListener<CryptPasswordStorageSchemeCfg> listener) {
410      impl.deregisterChangeListener(listener);
411    }
412    /** {@inheritDoc} */
413    public void addChangeListener(
414        ConfigurationChangeListener<PasswordStorageSchemeCfg> listener) {
415      impl.registerChangeListener(listener);
416    }
417
418
419
420    /** {@inheritDoc} */
421    public void removeChangeListener(
422        ConfigurationChangeListener<PasswordStorageSchemeCfg> listener) {
423      impl.deregisterChangeListener(listener);
424    }
425
426
427
428    /** {@inheritDoc} */
429    public CryptPasswordStorageEncryptionAlgorithm getCryptPasswordStorageEncryptionAlgorithm() {
430      return pCryptPasswordStorageEncryptionAlgorithm;
431    }
432
433
434
435    /** {@inheritDoc} */
436    public boolean isEnabled() {
437      return pEnabled;
438    }
439
440
441
442    /** {@inheritDoc} */
443    public String getJavaClass() {
444      return pJavaClass;
445    }
446
447
448
449    /** {@inheritDoc} */
450    public Class<? extends CryptPasswordStorageSchemeCfg> configurationClass() {
451      return CryptPasswordStorageSchemeCfg.class;
452    }
453
454
455
456    /** {@inheritDoc} */
457    public DN dn() {
458      return impl.getDN();
459    }
460
461
462
463    /** {@inheritDoc} */
464    public String toString() {
465      return impl.toString();
466    }
467  }
468}