mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
30.27.2009 902747f3618c2ba285058670ee6d0cf57e51c34e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 */
package org.opends.sdk.tools;
 
 
 
import static org.opends.messages.ToolMessages.*;
import static org.opends.messages.UtilityMessages.INFO_TIME_IN_DAYS_HOURS_MINUTES_SECONDS;
import static org.opends.messages.UtilityMessages.INFO_TIME_IN_HOURS_MINUTES_SECONDS;
import static org.opends.messages.UtilityMessages.INFO_TIME_IN_MINUTES_SECONDS;
import static org.opends.messages.UtilityMessages.INFO_TIME_IN_SECONDS;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
 
import org.opends.messages.Message;
import org.opends.sdk.Connection;
import org.opends.sdk.DecodeException;
import org.opends.sdk.ErrorResultException;
import org.opends.sdk.AuthenticatedConnectionFactory.AuthenticatedConnection;
import org.opends.sdk.controls.*;
import org.opends.sdk.responses.BindResult;
import org.opends.sdk.util.ByteString;
import org.opends.sdk.util.StaticUtils;
import org.opends.server.util.cli.ConsoleApplication;
 
 
 
/**
 * This class provides utility functions for all the client side tools.
 */
final class Utils
{
  // Prevent instantiation.
  private Utils()
  {
    // Do nothing.
  }
 
 
 
  /**
   * Parse the specified command line argument to create the appropriate
   * LDAPControl. The argument string should be in the format
   * controloid[:criticality[:value|::b64value|:<fileurl]]
   *
   * @param argString
   *          The argument string containing the encoded control
   *          information.
   * @return The control decoded from the provided string, or
   *         <CODE>null</CODE> if an error occurs while parsing the
   *         argument value.
   * @throws org.opends.sdk.DecodeException
   *           If an error occurs.
   */
  public static GenericControl getControl(String argString)
      throws DecodeException
  {
    String controlOID = null;
    boolean controlCriticality = false;
    ByteString controlValue = null;
 
    int idx = argString.indexOf(":");
 
    if (idx < 0)
    {
      controlOID = argString;
    }
    else
    {
      controlOID = argString.substring(0, idx);
    }
 
    String lowerOID = StaticUtils.toLowerCase(controlOID);
    if (lowerOID.equals("accountusable")
        || lowerOID.equals("accountusability"))
    {
      controlOID = AccountUsabilityControl.OID_ACCOUNT_USABLE_CONTROL;
    }
    else if (lowerOID.equals("authzid")
        || lowerOID.equals("authorizationidentity"))
    {
      controlOID = AuthorizationIdentityControl.OID_AUTHZID_REQUEST;
    }
    else if (lowerOID.equals("noop") || lowerOID.equals("no-op"))
    {
      // controlOID = OID_LDAP_NOOP_OPENLDAP_ASSIGNED;
    }
    else if (lowerOID.equals("subentries"))
    {
      // controlOID = OID_LDAP_SUBENTRIES;
    }
    else if (lowerOID.equals("managedsait"))
    {
      // controlOID = OID_MANAGE_DSAIT_CONTROL;
    }
    else if (lowerOID.equals("pwpolicy")
        || lowerOID.equals("passwordpolicy"))
    {
      controlOID = PasswordPolicyControl.OID_PASSWORD_POLICY_CONTROL;
    }
    else if (lowerOID.equals("subtreedelete")
        || lowerOID.equals("treedelete"))
    {
      controlOID = SubtreeDeleteControl.OID_SUBTREE_DELETE_CONTROL;
    }
    else if (lowerOID.equals("realattrsonly")
        || lowerOID.equals("realattributesonly"))
    {
      // controlOID = OID_REAL_ATTRS_ONLY;
    }
    else if (lowerOID.equals("virtualattrsonly")
        || lowerOID.equals("virtualattributesonly"))
    {
      // controlOID = OID_VIRTUAL_ATTRS_ONLY;
    }
    else if (lowerOID.equals("effectiverights")
        || lowerOID.equals("geteffectiverights"))
    {
      controlOID = GetEffectiveRightsRequestControl.OID_GET_EFFECTIVE_RIGHTS;
    }
 
    if (idx < 0)
    {
      return new GenericControl(controlOID);
    }
 
    String remainder = argString.substring(idx + 1, argString.length());
 
    idx = remainder.indexOf(":");
    if (idx == -1)
    {
      if (remainder.equalsIgnoreCase("true"))
      {
        controlCriticality = true;
      }
      else if (remainder.equalsIgnoreCase("false"))
      {
        controlCriticality = false;
      }
      else
      {
        // TODO: I18N
        throw DecodeException.error(Message
            .raw("Invalid format for criticality value:" + remainder));
      }
      return new GenericControl(controlOID, controlCriticality);
 
    }
 
    String critical = remainder.substring(0, idx);
    if (critical.equalsIgnoreCase("true"))
    {
      controlCriticality = true;
    }
    else if (critical.equalsIgnoreCase("false"))
    {
      controlCriticality = false;
    }
    else
    {
      // TODO: I18N
      throw DecodeException.error(Message
          .raw("Invalid format for criticality value:" + critical));
    }
 
    String valString = remainder.substring(idx + 1, remainder.length());
    if (valString.charAt(0) == ':')
    {
      controlValue = ByteString.valueOf(valString.substring(1,
          valString.length()));
    }
    else if (valString.charAt(0) == '<')
    {
      // Read data from the file.
      String filePath = valString.substring(1, valString.length());
      try
      {
        byte[] val = readBytesFromFile(filePath);
        controlValue = ByteString.wrap(val);
      }
      catch (Exception e)
      {
        return null;
      }
    }
    else
    {
      controlValue = ByteString.valueOf(valString);
    }
 
    return new GenericControl(controlOID, controlCriticality,
        controlValue);
  }
 
 
 
  /**
   * Read the data from the specified file and return it in a byte
   * array.
   *
   * @param filePath
   *          The path to the file that should be read.
   * @return A byte array containing the contents of the requested file.
   * @throws IOException
   *           If a problem occurs while trying to read the specified
   *           file.
   */
  public static byte[] readBytesFromFile(String filePath)
      throws IOException
  {
    byte[] val = null;
    FileInputStream fis = null;
    try
    {
      File file = new File(filePath);
      fis = new FileInputStream(file);
      long length = file.length();
      val = new byte[(int) length];
      // Read in the bytes
      int offset = 0;
      int numRead = 0;
      while (offset < val.length
          && (numRead = fis.read(val, offset, val.length - offset)) >= 0)
      {
        offset += numRead;
      }
 
      // Ensure all the bytes have been read in
      if (offset < val.length)
      {
        throw new IOException("Could not completely read file "
            + filePath);
      }
 
      return val;
    }
    finally
    {
      if (fis != null)
      {
        fis.close();
      }
    }
  }
 
 
 
  /**
   * Prints a multi-line error message with the provided information to
   * the given print stream.
   *
   * @param app
   *          The console app to use to write the error message.
   * @param ere
   *          The error result.
   * @return The error code.
   */
  public static int printErrorMessage(ConsoleApplication app,
      ErrorResultException ere)
  {
    // if ((ere.getMessage() != null) && (ere.getMessage().length() >
    // 0))
    // {
    // app.println(Message.raw(ere.getMessage()));
    // }
 
    if (ere.getResult().getResultCode().intValue() >= 0)
    {
      app.println(ERR_TOOL_RESULT_CODE.get(ere.getResult()
          .getResultCode().intValue(), ere.getResult().getResultCode()
          .toString()));
    }
 
    if ((ere.getResult().getDiagnosticMessage() != null)
        && (ere.getResult().getDiagnosticMessage().length() > 0))
    {
      app.println(ERR_TOOL_ERROR_MESSAGE.get(ere.getResult()
          .getDiagnosticMessage()));
    }
 
    if (ere.getResult().getMatchedDN() != null
        && ere.getResult().getMatchedDN().length() > 0)
    {
      app.println(ERR_TOOL_MATCHED_DN.get(ere.getResult()
          .getMatchedDN()));
    }
 
    if (app.isVerbose() && ere.getResult().getCause() != null)
    {
      ere.getResult().getCause().printStackTrace(app.getErrorStream());
    }
 
    return ere.getResult().getResultCode().intValue();
  }
 
 
 
  /**
   * Retrieves a user-friendly string that indicates the length of time
   * (in days, hours, minutes, and seconds) in the specified number of
   * seconds.
   *
   * @param numSeconds
   *          The number of seconds to be converted to a more
   *          user-friendly value.
   * @return The user-friendly representation of the specified number of
   *         seconds.
   */
  public static Message secondsToTimeString(int numSeconds)
  {
    if (numSeconds < 60)
    {
      // We can express it in seconds.
      return INFO_TIME_IN_SECONDS.get(numSeconds);
    }
    else if (numSeconds < 3600)
    {
      // We can express it in minutes and seconds.
      int m = numSeconds / 60;
      int s = numSeconds % 60;
      return INFO_TIME_IN_MINUTES_SECONDS.get(m, s);
    }
    else if (numSeconds < 86400)
    {
      // We can express it in hours, minutes, and seconds.
      int h = numSeconds / 3600;
      int m = (numSeconds % 3600) / 60;
      int s = numSeconds % 3600 % 60;
      return INFO_TIME_IN_HOURS_MINUTES_SECONDS.get(h, m, s);
    }
    else
    {
      // We can express it in days, hours, minutes, and seconds.
      int d = numSeconds / 86400;
      int h = (numSeconds % 86400) / 3600;
      int m = (numSeconds % 86400 % 3600) / 60;
      int s = numSeconds % 86400 % 3600 % 60;
      return INFO_TIME_IN_DAYS_HOURS_MINUTES_SECONDS.get(d, h, m, s);
    }
  }
 
 
 
  public static void printPasswordPolicyResults(ConsoleApplication app,
      Connection connection)
  {
    if (connection instanceof AuthenticatedConnection)
    {
      AuthenticatedConnection conn = (AuthenticatedConnection) connection;
      BindResult result = conn.getAuthenticatedBindResult();
 
      Control control = result
          .getControl(AuthorizationIdentityControl.OID_AUTHZID_RESPONSE);
      if (control != null)
      {
        AuthorizationIdentityControl.Response dc = (AuthorizationIdentityControl.Response) control;
        Message message = INFO_BIND_AUTHZID_RETURNED.get(dc
            .getAuthorizationID());
        app.println(message);
      }
      control = result
          .getControl(PasswordExpiredControl.OID_NS_PASSWORD_EXPIRED);
      if (control != null)
      {
        Message message = INFO_BIND_PASSWORD_EXPIRED.get();
        app.println(message);
      }
      control = result
          .getControl(PasswordExpiringControl.OID_NS_PASSWORD_EXPIRING);
      if (control != null)
      {
        PasswordExpiringControl dc = (PasswordExpiringControl) control;
        Message timeString = Utils.secondsToTimeString(dc
            .getSecondsUntilExpiration());
        Message message = INFO_BIND_PASSWORD_EXPIRING.get(timeString);
        app.println(message);
      }
      control = result
          .getControl(PasswordPolicyControl.OID_PASSWORD_POLICY_CONTROL);
      if (control != null)
      {
        PasswordPolicyControl.Response dc = (PasswordPolicyControl.Response) control;
        PasswordPolicyErrorType errorType = dc.getErrorType();
        if (errorType == PasswordPolicyErrorType.PASSWORD_EXPIRED)
        {
          Message message = INFO_BIND_PASSWORD_EXPIRED.get();
          app.println(message);
        }
        else if (errorType == PasswordPolicyErrorType.ACCOUNT_LOCKED)
        {
          Message message = INFO_BIND_ACCOUNT_LOCKED.get();
          app.println(message);
        }
        else if (errorType == PasswordPolicyErrorType.CHANGE_AFTER_RESET)
        {
 
          Message message = INFO_BIND_MUST_CHANGE_PASSWORD.get();
          app.println(message);
        }
 
        PasswordPolicyWarningType warningType = dc.getWarningType();
        if (warningType == PasswordPolicyWarningType.TIME_BEFORE_EXPIRATION)
        {
          Message timeString = Utils.secondsToTimeString(dc
              .getWarningValue());
          Message message = INFO_BIND_PASSWORD_EXPIRING.get(timeString);
          app.println(message);
        }
        else if (warningType == PasswordPolicyWarningType.GRACE_LOGINS_REMAINING)
        {
          Message message = INFO_BIND_GRACE_LOGINS_REMAINING.get(dc
              .getWarningValue());
          app.println(message);
        }
      }
    }
  }
 
 
 
  /**
   * Filters the provided value to ensure that it is appropriate for use
   * as an exit code. Exit code values are generally only allowed to be
   * between 0 and 255, so any value outside of this range will be
   * converted to 255, which is the typical exit code used to indicate
   * an overflow value.
   *
   * @param exitCode
   *          The exit code value to be processed.
   * @return An integer value between 0 and 255, inclusive. If the
   *         provided exit code was already between 0 and 255, then the
   *         original value will be returned. If the provided value was
   *         out of this range, then 255 will be returned.
   */
  public static int filterExitCode(int exitCode)
  {
    if (exitCode < 0)
    {
      return 255;
    }
    else if (exitCode > 255)
    {
      return 255;
    }
    else
    {
      return exitCode;
    }
  }
}