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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.tools;
028    
029    
030    import java.util.ArrayList;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    
036    
037    
038    /**
039     * This class defines options used while creating an LDAP connection
040     * to the server.
041     */
042    public class LDAPConnectionOptions
043    {
044    
045      private boolean reportAuthzID = false;
046      private boolean useSSL =  false;
047      private boolean startTLS = false;
048      private boolean saslExternal = false;
049      private boolean usePasswordPolicyControl = false;
050      private SSLConnectionFactory sslConnectionFactory = null;
051      private String saslMechanism = null;
052      private int versionNumber = 3;
053      private Map<String, List<String>> saslProperties =
054                                             new HashMap<String, List<String>> ();
055      private boolean verbose = false;
056    
057      /**
058       * Creates a the connection options instance.
059       *
060       */
061      public LDAPConnectionOptions()
062      {
063      }
064    
065      /**
066       * Set whether to use SSL for the connection or not.
067       *
068       * @param useSSL    True if SSL should be used, false otherwise.
069       *
070       */
071    
072      public void setUseSSL(boolean useSSL)
073      {
074        this.useSSL = useSSL;
075      }
076    
077      /**
078       * Return the useSSL flag value.
079       *
080       * @return  <CODE>true</CODE> if SSL should be used, or <CODE>false</CODE> if
081       *          not.
082       */
083      public boolean useSSL()
084      {
085        return useSSL;
086      }
087    
088      /**
089       * Set whether to use startTLS for the connection or not.
090       *
091       * @param startTLS    True if startTLS should be used, false otherwise.
092       *
093       */
094    
095      public void setStartTLS(boolean startTLS)
096      {
097        this.startTLS = startTLS;
098      }
099    
100      /**
101       * Return the startTLS flag value.
102       *
103       * @return  <CODE>true</CODE> if StartTLS should be used, or
104       *          <CODE>false</CODE> if not.
105       */
106      public boolean useStartTLS()
107      {
108        return startTLS;
109      }
110    
111      /**
112       * Set whether to use SASL EXTERNAL for the connection or not.
113       *
114       * @param saslExternal    True if SASL EXTERNAL should be used,
115       *                        false otherwise.
116       *
117       */
118    
119      public void setSASLExternal(boolean saslExternal)
120      {
121        this.saslExternal = saslExternal;
122      }
123    
124      /**
125       * Return the saslExternal flag value.
126       *
127       * @return  <CODE>true</CODE> if SASL EXTERNAL should be used, or
128       *          <CODE>false</CODE> if not.
129       */
130      public boolean useSASLExternal()
131      {
132        return saslExternal;
133      }
134    
135      /**
136       * Set the SSL connection factory to use to create SSL connections.
137       *
138       * @param sslConnectionFactory    The SSL connection factory.
139       *
140       */
141    
142      public void setSSLConnectionFactory(SSLConnectionFactory sslConnectionFactory)
143      {
144        this.sslConnectionFactory = sslConnectionFactory;
145      }
146    
147      /**
148       * Return the SSLConnectionFactory instance.
149       *
150       * @return  The SSL connection factory to use when establishing secure
151       *          connections.
152       */
153      public SSLConnectionFactory getSSLConnectionFactory()
154      {
155        return sslConnectionFactory;
156      }
157    
158      /**
159       * Set the SASL mechanism used for authentication.
160       *
161       * @param  mechanism  The SASL mechanism string, in "name=value" form.
162       *
163       * @return  <CODE>true</CODE> if the SASL mechanism was set, or
164       *          <CODE>false</CODE> if not.
165       */
166      public boolean setSASLMechanism(String mechanism)
167      {
168        int idx = mechanism.indexOf("=");
169        if(idx == -1)
170        {
171          System.err.println("Invalid SASL mechanism property:" + mechanism);
172          return false;
173        }
174        this.saslMechanism = mechanism.substring(idx+1, mechanism.length());
175        if(saslMechanism.equalsIgnoreCase("EXTERNAL"))
176        {
177          setSASLExternal(true);
178        }
179        return true;
180      }
181    
182      /**
183       * Get the SASL mechanism used for authentication.
184       *
185       * @return  The SASL mechanism used for authentication.
186       */
187      public String getSASLMechanism()
188      {
189        return saslMechanism;
190      }
191    
192      /**
193       * Get the SASL options used for authentication.
194       *
195       * @return  The SASL options used for authentication.
196       */
197      public Map<String, List<String>> getSASLProperties()
198      {
199        return saslProperties;
200      }
201    
202      /**
203       * Add a property to the list of SASL properties.
204       *
205       * @param  property  The property (in name=value form) to add to the set of
206       *                   SASL properties.
207       *
208       * @return  <CODE>true</CODE> if the property was set properly, or
209       *          <CODE>false</CODE> if not.
210       */
211    
212      public boolean addSASLProperty(String property)
213      {
214        int idx = property.indexOf("=");
215        if(idx == -1)
216        {
217          System.err.println("Invalid SASL property format:" + property);
218          return false;
219        }
220        String key = property.substring(0, idx);
221        String value = property.substring(idx+1, property.length());
222        List<String> valList = saslProperties.get(key);
223        if(valList == null)
224        {
225          valList = new ArrayList<String> ();
226        }
227        valList.add(value);
228    
229        saslProperties.put(key, valList);
230        return true;
231      }
232    
233      /**
234       * Set the LDAP version number.
235       *
236       * @param  version  The LDAP version number.
237       */
238      public void setVersionNumber(int version)
239      {
240        this.versionNumber = version;
241      }
242    
243      /**
244       * Get the LDAP version number.
245       *
246       * @return  The LDAP version number.
247       */
248      public int getVersionNumber()
249      {
250        return this.versionNumber;
251      }
252    
253    
254    
255      /**
256       * Indicates whether to request that the server return the authorization ID in
257       * the bind response.
258       *
259       * @return  <CODE>true</CODE> if the server should include the authorization
260       *          ID in the bind response, or <CODE>false</CODE> if not.
261       */
262      public boolean getReportAuthzID()
263      {
264        return reportAuthzID;
265      }
266    
267    
268    
269      /**
270       * Specifies whether to request that the server return the authorization ID in
271       * the bind response.
272       *
273       * @param  reportAuthzID  Specifies whether to request that the server return
274       *                        the authorization ID in the bind response.
275       */
276      public void setReportAuthzID(boolean reportAuthzID)
277      {
278        this.reportAuthzID = reportAuthzID;
279      }
280    
281    
282    
283      /**
284       * Indicates whether to use the password policy control in the bind request.
285       *
286       * @return  <CODE>true</CODE> if the password policy control should be
287       *          included in the bind request, or <CODE>false</CODE> if not.
288       */
289      public boolean usePasswordPolicyControl()
290      {
291        return usePasswordPolicyControl;
292      }
293    
294    
295    
296      /**
297       * Specifies whether to use the password policy control in the bind request.
298       *
299       * @param  usePasswordPolicyControl  Specifies whether to use the password
300       *                                   policy control in the bind request.
301       */
302      public void setUsePasswordPolicyControl(boolean usePasswordPolicyControl)
303      {
304        this.usePasswordPolicyControl = usePasswordPolicyControl;
305      }
306    
307      /**
308       * Indicates whether verbose tracing is enabled.
309       *
310       * @return <CODE>true</CODE> if verbose tracing is enabled.
311       */
312      public boolean isVerbose()
313      {
314        return verbose;
315      }
316    
317      /**
318       * Specifies whether verbose tracing should be enabled.
319       * @param verbose Specifies whether verbose tracing should be enabled.
320       */
321      public void setVerbose(boolean verbose)
322      {
323        this.verbose = verbose;
324      }
325    }
326