File: LDAPModify.java

package info (click to toggle)
ldapjdk 4.20.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,708 kB
  • sloc: ansic: 44,727; java: 39,132; xml: 7,419; sh: 4,185; perl: 3,774; makefile: 1,681; cpp: 979
file content (507 lines) | stat: -rw-r--r-- 20,679 bytes parent folder | download | duplicates (11)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * @(#) LDAPModify.java
 *
 */

import java.io.*;
import java.net.*;
import java.util.*;
import org.ietf.ldap.*;
import org.ietf.ldap.util.*;

/**
 * Executes modify, delete, add, and modRDN.
 * This class is implemented based on the java LDAP classes.
 *
 * <pre>
 * usage       : java LDAPModify [options]
 * example     : java LDAPModify -D "uid=johnj,ou=People,o=Airius.com"
 *                 -w "password" -h ldap.netscape.com -p 389
 *                 -f modify.cfg
 *
 * options: {np = no parameters, p = requires parameters}
 *  'D' bind DN --------------------------------------------- p
 *  'w' bind password --------------------------------------- p
 *  'f' input file ------------------------------------------ p
 *  'h' LDAP host ------------------------------------------- p
 *  'p' LDAP port ------------------------------------------- p
 *  'e' record rejected records in a text file -------------- p
 *  'c' continuous, do not stop on error ------------------- np
 *  'a' add, if no operation is specified ------------------ np
 *  'r' replace, if no operation is specified -------------- np
 *  'b' binary, read values starting with / from a file ---- np
 *  'd' debugging level ------------------------------------- p
 *  'V' version, specify LDAP protocol version (2 or 3) ----- p
 *  'R' do not follow referrals ---------------------------- np
 *  'O' hop limit ------------------------------------------- p
 *  'H' help, display usage--------------------------------- np
 *  'M' manage referrals, do not follow them --------------- np
 *  'n' show what would be done but do not do it ----------- np
 *  'v' verbose mode --------------------------------------- np
 *  'e' reject file, where to list rejected records --------- p
 *  'y' proxy DN, DN to use for access control -------------- p
 *
 * note: '-' or '/' is used to mark an option field.
 *       e.g. -a -b /c /d parameter -e parameter
 *
 * </pre>
 *
 * @version 1.0
 */

public class LDAPModify extends LDAPTool { /* LDAPModify */

    public static void main(String args[]) { /* main */

        /* extract parameters from the arguments list */
        extractParameters(args);

        /* perform an LDAP client connection operation */
        try {
            if (!m_justShow) {
                m_client = new LDAPConnection();
                m_client.connect( m_ldaphost, m_ldapport );
            }
        } catch(Exception e) {
            System.err.println("Error: client connection failed!");
            System.exit(0);
        }

        /* perform an LDAP bind operation */
        if (!m_justShow) {
			if ( (m_binddn != null) && (m_passwd != null) ) {
			    try {
                    m_client.bind( m_version, m_binddn, m_passwd.getBytes() );
			    } catch (Exception e) {
                    System.err.println( e.toString() );
                    System.exit(0);
			    }
			}
        }

        try {
            if ( m_file != null )
                m_ldif = new LDIF(m_file);
            else {
                m_ldif = new LDIF();
}
        } catch (Exception e) {
            if ( m_file == null )
                m_file = "stdin";
            System.err.println("Failed to read LDIF file " + m_file +
                               ", " + e.toString());
            System.exit(0);
        }

        /* performs a JDAP Modify operation */
        try {
            doModify();
        } catch (Exception e) {
            System.err.println( e.toString() );
        }

        /* disconnect */
        try {
            if (!m_justShow)
                m_client.disconnect();
        } catch (Exception e) {
            System.err.println( e.toString() );
        }
        System.exit(0);
    } /* main */

    /**
     * Prints usage.
     */
    private static void doUsage() {
        System.err.println( "usage: LDAPModify [options]" );
        System.err.println("options");
        System.err.println("  -h host       LDAP server name or IP address");
        System.err.println("  -p port       LDAP server TCP port number");
        System.err.println("  -V version    LDAP protocol version " +
                           "number (default is 3)");
        System.err.println("  -D binddn     bind dn");
        System.err.println("  -w password   bind passwd (for simple " +
                           "authentication)");
        System.err.println("  -d level      set LDAP debugging level " +
                           "to \'level\'");
        System.err.println("  -R            do not automatically follow " +
                           "referrals");
        System.err.println("  -O hop limit  maximum number of referral " +
                           "hops to traverse");
        System.err.println("  -H            display usage information");
        System.err.println("  -c            continuous mode (do not " +
                           "stop on errors)");
        System.err.println("  -M            manage references (treat them " +
                           "as regular entries)");
        System.err.println("  -f file       read modifications from " +
                           "file instead of standard input");
        System.err.println("  -a            add entries");
        System.err.println("  -b            read values that start with " +
                           "/ from files (for bin attrs)");
        System.err.println("  -n            show what would be done but " +
                           "don\'t actually do it");
        System.err.println("  -v            run in verbose mode");
        System.err.println("  -r            replace existing values by " +
                           "default");
        System.err.println("  -e rejectfile save rejected entries in " +
                           "\'rejfile\'");
        System.err.println("  -y proxy-DN   DN to use for access control");
    }

    /**
     * This class-method is used to extract specified parameters from the
     * arguments list.
     */
    /* extract parameters */
    protected static void extractParameters(String args[]) {

        String privateOpts = "abcHFre:f:";

        GetOpt options = LDAPTool.extractParameters( privateOpts, args );

        /* -H Help */
        if (options.hasOption('H')) {
            doUsage();
            System.exit(0);
        } /* Help */

        if (options.hasOption('F'))
            m_force = true;

        if (options.hasOption('a'))
            m_add = true;

        if (options.hasOption('c'))
            m_continuous = true;

        if (options.hasOption('r'))
            m_add = false;

        if (options.hasOption('b'))
            m_binaryFiles = true;

        /* -f input file */
        if(options.hasOption('f')) { /* Is input file */
            m_file = (String)options.getOptionParam('f');
        } /* End Is input file */

        /* -e rejects file */
        if(options.hasOption('e')) { /* rejects file */
            m_rejectsFile = (String)options.getOptionParam('e');
        } /* End rejects file */

    } /* extract parameters */

    /**
     * Call the LDAPConnection modify operation with the
     * specified options, and/or parameters.
     */
    private static void doModify() throws IOException { /* doModify */
        PrintWriter reject = null;
        LDAPConstraints baseCons = null;
        if (!m_justShow) {
            baseCons = m_client.getConstraints();
            Vector controlVector = new Vector();
            if (m_proxyControl != null)
                controlVector.addElement(m_proxyControl);
            if (m_ordinary) {
                controlVector.addElement( new LDAPControl(
                    LDAPControl.MANAGEDSAIT, true, null) );
            }
            if (controlVector.size() > 0) {
                LDAPControl[] controls = new LDAPControl[controlVector.size()];
                controlVector.copyInto(controls);
                baseCons.setControls(controls);
            }
            baseCons.setReferralFollowing( m_referrals );
            if ( m_referrals ) {
                setDefaultReferralCredentials( baseCons );
            }
            baseCons.setHopLimit( m_hopLimit );
        }

        LDIFRecord rec = m_ldif.nextRecord();

        for (; rec != null; rec = m_ldif.nextRecord() ) {
            LDAPConstraints cons = baseCons;
            if (!m_justShow) {
                // Were there any controls specified in the LDIF
                // record?
                LDAPControl[] newControls = rec.getControls();
                if ( newControls != null ) {
                    LDAPControl[] controls = newControls;
                    LDAPControl[] oldControls = baseCons.getControls();
                    if ( oldControls != null ) {
                        // If there were already controls defined, merge in
                        // the new ones
                        controls =
                            new LDAPControl[oldControls.length +
                                           newControls.length];
                        for( int i = 0; i < oldControls.length; i++ ) {
                            controls[i] = oldControls[i];
                        }
                        for( int i = 0; i < newControls.length; i++ ) {
                            controls[i+oldControls.length] = newControls[i];
                        }
                    }
                    // Assign the merged controls to a copy of the constraints
                    cons = (LDAPSearchConstraints)baseCons.clone();
                    cons.setControls( controls );
                }
            }

            LDIFContent content = rec.getContent();
            LDAPModification mods[] = null;
            LDAPAttribute addAttrs[] = null;
            boolean doDelete = false;
            boolean doModDN = false;
            LDAPEntry newEntry = null;

            /* What type of record is this? */
            if ( content instanceof LDIFModifyContent ) {
                mods = ((LDIFModifyContent)content).getModifications();
            } else if ( content instanceof LDIFAddContent ) {
                addAttrs = ((LDIFAddContent)content).getAttributes();
            } else if ( content instanceof LDIFAttributeContent ) {
                /* No change type; decide what to do based on options */
                if ( m_add ) {
                    addAttrs =
                        ((LDIFAttributeContent)content).getAttributes();
                } else {
                    LDAPAttribute[] tmpAttrs =
                        ((LDIFAttributeContent)content).getAttributes();
                    mods = new LDAPModification[tmpAttrs.length];
                    for( int ta = 0; ta < tmpAttrs.length; ta++ ) {
                        mods[ta] = new LDAPModification(
                            LDAPModification.REPLACE, tmpAttrs[ta] );
                    }
                }
            } else if ( content instanceof LDIFDeleteContent ) {
                doDelete = true;
            } else if (content instanceof LDIFModDNContent ) {
                doModDN = true;
            } else {
            }

            /* Prepare for adding */
            if ( addAttrs != null ) {
                LDAPAttributeSet newAttrSet = new LDAPAttributeSet();
                for( int a = 0; a < addAttrs.length; a++ )
                    newAttrSet.add( addAttrs[a] );
                newEntry = new LDAPEntry( rec.getDN(), newAttrSet );
            }

            /* Get values from files? */
            boolean skip = false;
            if ( m_binaryFiles ) {
                /* Check each value of each attribute, to see if it
                   needs replacing with the contents of a file */
                if ( mods != null ) {
                    for( int m = 0; m < mods.length; m++ ) {
                        LDAPModification mod = mods[m];
                        LDAPAttribute attr = mods[m].getAttribute();

                        LDAPAttribute newAttr = checkFiles( attr );
                        if ( newAttr == null )
                            skip = true;
                        else
                            mods[m] = new LDAPModification(
                                mod.getOp(), newAttr );
                    }
                } else if ( addAttrs != null ) {
                    LDAPAttributeSet newAttrSet = new LDAPAttributeSet();
                    for( int a = 0; a < addAttrs.length; a++ ) {
                        LDAPAttribute attr = addAttrs[a];

                        LDAPAttribute newAttr = checkFiles( attr );
                        if ( newAttr == null ) {
                            skip = true;
                            break;
                        } else {
                            newAttrSet.add( newAttr );
                        }
                    }
                    if ( !skip ) {
                        newEntry = new LDAPEntry( rec.getDN(), newAttrSet );
                    }
                }
            }

            /* Do the directory operation */
            int errCode = 0;
            if ( !skip ) {
                try {
                    if ( mods != null ) {
                        System.out.println("\nmodifying entry "+rec.getDN() );
                        for( int m = 0; m < mods.length; m++ ) {
                            if (m_verbose) {
                                System.out.println("\t"+mods[m] );
			    }
                        }

                        if (!m_justShow) {
                            m_client.modify( rec.getDN(), mods, cons );
                        }
                    } else if ( newEntry != null ) {
                        System.out.println( "\nadding new entry " + newEntry.getDN() );
                        if ( m_verbose ) {
                            LDAPAttributeSet set = newEntry.getAttributeSet();
			    Iterator it = set.iterator();
			    while( it.hasNext() ) {
                                System.out.println("\t"+it.next() );
                            }
                        } 
                        if (!m_justShow) {
                            m_client.add( newEntry, cons );
                        }
                    } else if ( doDelete ) {
                        System.out.println( "\ndeleting entry " + rec.getDN() );
                        if (!m_justShow)
                            m_client.delete( rec.getDN(), cons );
                    } else if ( doModDN) {
                        System.out.println( "\nmodifying RDN of entry " + 
                            rec.getDN()+" and/or moving it beneath a new parent");
                        if ( m_verbose ) {
                            System.out.println( "\t"+content.toString());
                        }
                        if (!m_justShow) {
                            LDIFModDNContent moddnContent = (LDIFModDNContent)content;
                            m_client.rename( rec.getDN(), moddnContent.getRDN(),
                                moddnContent.getNewParent(), 
                                moddnContent.getDeleteOldRDN(), cons );
                            System.out.println( "rename completed");
                        }
                    }
                } catch (LDAPException e) {
                    System.err.println( rec.getDN() + ": " +
                                        e.resultCodeToString() );
                    if (e.getLDAPErrorMessage() != null)
                        System.err.println( "additional info: " +
                                        e.getLDAPErrorMessage() );
                    if ( !m_continuous ) {
                        System.exit(1);
                    }
                    skip = true;
                    errCode = e.getResultCode();
                }
            }

            /* Write to rejects file? */
            if ( skip && (m_rejectsFile != null) ) {
                try {
                    if ( reject == null ) {
                        reject = new PrintWriter(
                            new FileOutputStream( m_rejectsFile ) );
                    }
                } catch ( Exception e ) {
                }
                if ( reject != null ) {
                    reject.println( "dn: "+rec.getDN()+ " # Error: " +
                                    errCode );
                    if ( mods != null ) {
                        for( int m = 0; m < mods.length; m++ ) {
                            reject.println( mods[m].toString() );
                        }
                    } else if ( newEntry != null ) {
                        reject.println( "Add " + newEntry.toString() );
                    } else if ( doDelete ) {
                        reject.println( "Delete " + rec.getDN() );
                    } else if (doModDN) {
                        reject.println( "ModDN "+ 
                                    ((LDIFModDNContent)content).toString() );
                    }
                    reject.flush();
                }
            }
        }
        System.exit(0);
    } /* doModify */


    /**
     * Read in binary data for values specified with a leading /
     * @param attr Source attribute.
     * @return Updated attribute.
     **/
    private static LDAPAttribute checkFiles ( LDAPAttribute attr ) {
        LDAPAttribute newAttr =
            new LDAPAttribute( attr.getName() );

        /* Check each value and see if it is a file name */
        Enumeration e = attr.getStringValues();
        if (e != null) {
          while ( e.hasMoreElements() ) {
            String val = (String)e.nextElement();
            if ( (val != null) && (val.length() > 1)) {
                try {
                    File file = new File( val );
                    FileInputStream fi =
                        new FileInputStream( file );
                    byte[] bval = new byte[(int)file.length()];
                    fi.read( bval, 0, (int)file.length() );
                    newAttr.addValue( bval );
                } catch (FileNotFoundException ex) {
                    newAttr.addValue(val) ;
                } catch ( IOException ex ) {
                    System.err.println( "Unable to read value " +
                        "from file " + val );
                    if ( !m_continuous )
                        System.exit(1);
                    newAttr = null;
                }
            } else {
                newAttr.addValue( val );
            }
          }
        }
        else
          System.err.println("Failed to do string conversion for "+attr.getName());
        return newAttr;
    }

  private static boolean m_continuous = false;
  private static boolean m_force = false;
  private static boolean m_add = false;
  private static boolean m_binaryFiles = false;
  private static String m_rejectsFile = null;
  private static LDIF m_ldif = null;
  private static String m_file = null;
} /* LDAPModify */