]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Improve usage message and check, by JR Heisey
[openldap] / clients / tools / ldapmodify.c
1 /* ldapmodify.c - generic program to modify or add entries using LDAP */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/ctype.h>
9 #include <ac/signal.h>
10 #include <ac/string.h>
11 #include <ac/unistd.h>
12
13 #include <sys/stat.h>
14
15 #ifdef HAVE_SYS_FILE_H
16 #include <sys/file.h>
17 #endif
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
21
22 #include <lber.h>
23 #include <ldap.h>
24 #include <ldif.h>
25
26 static char     *prog;
27 static char     *binddn = NULL;
28 static char     *passwd = NULL;
29 static char     *ldaphost = NULL;
30 static int      ldapport = 0;
31 static int      new, replace, not, verbose, contoper, force, valsfromfiles;
32 static LDAP     *ld;
33
34 #define safe_realloc( ptr, size )       ( ptr == NULL ? malloc( size ) : \
35                                          realloc( ptr, size ))
36
37 #define LDAPMOD_MAXLINE         4096
38
39 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
40 #define T_REPLICA_STR           "replica"
41 #define T_DN_STR                "dn"
42 #define T_CHANGETYPESTR         "changetype"
43 #define T_ADDCTSTR              "add"
44 #define T_MODIFYCTSTR           "modify"
45 #define T_DELETECTSTR           "delete"
46 #define T_MODRDNCTSTR           "modrdn"
47 #define T_MODOPADDSTR           "add"
48 #define T_MODOPREPLACESTR       "replace"
49 #define T_MODOPDELETESTR        "delete"
50 #define T_MODSEPSTR             "-"
51 #define T_NEWRDNSTR             "newrdn"
52 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
53
54
55 static void usage LDAP_P(( const char *prog ));
56 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
57 static int process_ldif_rec LDAP_P(( char *rbuf ));
58 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
59         char *value, int vlen ));
60 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
61 static int dodelete LDAP_P(( char *dn ));
62 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
63 static void freepmods LDAP_P(( LDAPMod **pmods ));
64 static int fromfile LDAP_P(( char *path, struct berval *bv ));
65 static char *read_one_record LDAP_P(( FILE *fp ));
66
67 static void
68 usage( const char *prog )
69 {
70     fprintf( stderr, "Add or modify entries from an LDAP server\n\n"
71              "usage: %s [-abcknrvF] [-d debug-level] [-P version] [-h ldaphost]\n"
72              "            [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
73              "       a    - add values (default%s)\n"
74              "       b    - read values from files (for binary attributes)\n"
75              "       c    - continuous operation\n"
76              "       D    - bind DN\n"
77              "       d    - debug level\n"
78              "       f    - read from file\n"
79              "       F    - force all changes records to be used\n"
80              "       h    - ldap host\n"
81              "       n    - print adds, don't actually do them\n"
82              "       p    - LDAP port\n"
83              "       r    - replace values\n"
84              "       v    - verbose mode\n"
85              "       w    - password\n"
86              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
87     exit( 1 );
88 }
89
90
91 int
92 main( int argc, char **argv )
93 {
94     char                *infile, *rbuf, *start, *p, *q;
95     FILE                *fp;
96         int             rc, i, use_ldif, authmethod, version, want_bindpw, debug;
97
98     if (( prog = strrchr( argv[ 0 ], '/' )) == NULL ) {
99         prog = argv[ 0 ];
100     } else {
101         ++prog;
102     }
103
104     // Print usage when no parameters
105     if( argc < 2 )
106         usage( prog );
107
108     new = ( strcmp( prog, "ldapadd" ) == 0 );
109
110     infile = NULL;
111     not = verbose = valsfromfiles = want_bindpw = debug = 0;
112     authmethod = LDAP_AUTH_SIMPLE;
113         version = -1;
114
115     while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
116         switch( i ) {
117         case 'a':       /* add */
118             new = 1;
119             break;
120         case 'b':       /* read values from files (for binary attributes) */
121             valsfromfiles = 1;
122             break;
123         case 'c':       /* continuous operation */
124             contoper = 1;
125             break;
126         case 'r':       /* default is to replace rather than add values */
127             replace = 1;
128             break;
129         case 'k':       /* kerberos bind */
130 #ifdef HAVE_KERBEROS
131                 authmethod = LDAP_AUTH_KRBV4;
132 #else
133                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
134 #endif
135             break;
136         case 'K':       /* kerberos bind, part 1 only */
137 #ifdef HAVE_KERBEROS
138                 authmethod = LDAP_AUTH_KRBV41;
139 #else
140                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
141 #endif
142             break;
143         case 'F':       /* force all changes records to be used */
144             force = 1;
145             break;
146         case 'h':       /* ldap host */
147             ldaphost = strdup( optarg );
148             break;
149         case 'D':       /* bind DN */
150             binddn = strdup( optarg );
151             break;
152         case 'w':       /* password */
153             passwd = strdup( optarg );
154             break;
155         case 'd':
156             debug |= atoi( optarg );
157             break;
158         case 'f':       /* read from file */
159             infile = strdup( optarg );
160             break;
161         case 'p':
162             ldapport = atoi( optarg );
163             break;
164         case 'n':       /* print adds, don't actually do them */
165             ++not;
166             break;
167         case 'v':       /* verbose mode */
168             verbose++;
169             break;
170         case 'W':
171                 want_bindpw++;
172                 break;
173         case 'P':
174                 switch(optarg[0])
175                 {
176                 case '2':
177                         version = LDAP_VERSION2;
178                         break;
179                 case '3':
180                         version = LDAP_VERSION3;
181                         break;
182                 }
183                 break;
184         default:
185             usage( prog );
186         }
187     }
188
189     if ( argc != optind )
190         usage( prog );
191
192     if ( infile != NULL ) {
193         if (( fp = fopen( infile, "r" )) == NULL ) {
194             perror( infile );
195             exit( 1 );
196         }
197     } else {
198         fp = stdin;
199     }
200
201         if ( debug ) {
202                 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
203                 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
204                 ldif_debug = debug;
205         }
206
207 #ifdef SIGPIPE
208         (void) SIGNAL( SIGPIPE, SIG_IGN );
209 #endif
210
211     if ( !not ) {
212         if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
213             perror( "ldap_init" );
214             exit( 1 );
215         }
216
217         /* this seems prudent */
218         {
219                 int deref = LDAP_DEREF_NEVER;
220                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
221         }
222
223         if (want_bindpw)
224                 passwd = getpass("Enter LDAP Password: ");
225
226         if( version != -1 ) {
227                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
228         }
229
230         if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
231             ldap_perror( ld, "ldap_bind" );
232             exit( 1 );
233         }
234     }
235
236     rc = 0;
237
238     while (( rc == 0 || contoper ) &&
239                 ( rbuf = read_one_record( fp )) != NULL ) {
240         /*
241          * we assume record is ldif/slapd.replog if the first line
242          * has a colon that appears to the left of any equal signs, OR
243          * if the first line consists entirely of digits (an entry id)
244          */
245         use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
246                 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
247                 (( q = strchr( rbuf, '=' )) == NULL || p < q );
248
249         start = rbuf;
250
251         if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
252             for ( p = rbuf; p < q; ++p ) {
253                 if ( !isdigit( (unsigned char) *p )) {
254                     break;
255                 }
256             }
257             if ( p >= q ) {
258                 use_ldif = 1;
259                 start = q + 1;
260             }
261         }
262
263         if ( use_ldif ) {
264             rc = process_ldif_rec( start );
265         } else {
266             rc = process_ldapmod_rec( start );
267         }
268
269         if( rc )
270             fprintf( stderr, "%s() = %d\n",
271                      use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
272
273         free( rbuf );
274     }
275
276     if ( !not ) {
277         ldap_unbind( ld );
278     }
279
280     exit( rc );
281
282         /* UNREACHABLE */
283         return(0);
284 }
285
286
287 static int
288 process_ldif_rec( char *rbuf )
289 {
290     char        *line, *dn, *type, *value, *newrdn, *p;
291     int         rc, linenum, vlen, modop, replicaport;
292     int         expect_modop, expect_sep, expect_ct, expect_newrdn;
293     int         expect_deleteoldrdn, deleteoldrdn;
294     int         saw_replica, use_record, new_entry, delete_entry, got_all;
295     LDAPMod     **pmods;
296
297     new_entry = new;
298
299     rc = got_all = saw_replica = delete_entry = expect_modop = 0;
300     expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
301     linenum = 0;
302     deleteoldrdn = 1;
303     use_record = force;
304     pmods = NULL;
305     dn = newrdn = NULL;
306
307     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
308         ++linenum;
309         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
310             expect_sep = 0;
311             expect_ct = 1;
312             continue;
313         }
314         
315         if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
316             fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
317                     prog, linenum, dn == NULL ? "" : dn );
318             rc = LDAP_PARAM_ERROR;
319             break;
320         }
321
322         if ( dn == NULL ) {
323             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
324                 ++saw_replica;
325                 if (( p = strchr( value, ':' )) == NULL ) {
326                     replicaport = 0;
327                 } else {
328                     *p++ = '\0';
329                     replicaport = atoi( p );
330                 }
331                 if ( strcasecmp( value, ldaphost ) == 0 &&
332                         replicaport == ldapport ) {
333                     use_record = 1;
334                 }
335             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
336                 if (( dn = strdup( value )) == NULL ) {
337                     perror( "strdup" );
338                     exit( 1 );
339                 }
340                 expect_ct = 1;
341             }
342             continue;   /* skip all lines until we see "dn:" */
343         }
344
345         if ( expect_ct ) {
346             expect_ct = 0;
347             if ( !use_record && saw_replica ) {
348                 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
349                         prog, dn );
350                 free( dn );
351                 return( 0 );
352             }
353
354             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
355                 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
356                         new_entry = 0;
357                         expect_modop = 1;
358                 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
359                         new_entry = 1;
360                 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
361                     expect_newrdn = 1;
362                 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
363                     got_all = delete_entry = 1;
364                 } else {
365                     fprintf( stderr,
366                             "%s:  unknown %s \"%s\" (line %d of entry: %s)\n",
367                             prog, T_CHANGETYPESTR, value, linenum, dn );
368                     rc = LDAP_PARAM_ERROR;
369                 }
370                 continue;
371             } else if ( new ) {         /*  missing changetype => add */
372                 new_entry = 1;
373                 modop = LDAP_MOD_ADD;
374             } else {
375                 expect_modop = 1;       /* missing changetype => modify */
376             }
377         }
378
379         if ( expect_modop ) {
380             expect_modop = 0;
381             expect_sep = 1;
382             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
383                 modop = LDAP_MOD_ADD;
384                 continue;
385             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
386                 modop = LDAP_MOD_REPLACE;
387                 continue;
388             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
389                 modop = LDAP_MOD_DELETE;
390                 addmodifyop( &pmods, modop, value, NULL, 0 );
391                 continue;
392             } else {    /* no modify op:  use default */
393                 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
394             }
395         }
396
397         if ( expect_newrdn ) {
398             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
399                 if (( newrdn = strdup( value )) == NULL ) {
400                     perror( "strdup" );
401                     exit( 1 );
402                 }
403                 expect_deleteoldrdn = 1;
404                 expect_newrdn = 0;
405             } else {
406                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
407                         prog, T_NEWRDNSTR, type, linenum, dn );
408                 rc = LDAP_PARAM_ERROR;
409             }
410         } else if ( expect_deleteoldrdn ) {
411             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
412                 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
413                 got_all = 1;
414             } else {
415                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
416                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
417                 rc = LDAP_PARAM_ERROR;
418             }
419         } else if ( got_all ) {
420             fprintf( stderr,
421                     "%s: extra lines at end (line %d of entry %s)\n",
422                     prog, linenum, dn );
423             rc = LDAP_PARAM_ERROR;
424         } else {
425             addmodifyop( &pmods, modop, type, value, vlen );
426         }
427     }
428
429     if ( rc == 0 ) {
430         if ( delete_entry ) {
431             rc = dodelete( dn );
432         } else if ( newrdn != NULL ) {
433             rc = domodrdn( dn, newrdn, deleteoldrdn );
434         } else {
435             rc = domodify( dn, pmods, new_entry );
436         }
437
438         if ( rc == LDAP_SUCCESS ) {
439             rc = 0;
440         }
441     }
442
443     if ( dn != NULL ) {
444         free( dn );
445     }
446     if ( newrdn != NULL ) {
447         free( newrdn );
448     }
449     if ( pmods != NULL ) {
450         freepmods( pmods );
451     }
452
453     return( rc );
454 }
455
456
457 static int
458 process_ldapmod_rec( char *rbuf )
459 {
460     char        *line, *dn, *p, *q, *attr, *value;
461     int         rc, linenum, modop;
462     LDAPMod     **pmods;
463
464     pmods = NULL;
465     dn = NULL;
466     linenum = 0;
467     line = rbuf;
468     rc = 0;
469
470     while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
471         ++linenum;
472         if (( p = strchr( rbuf, '\n' )) == NULL ) {
473             rbuf = NULL;
474         } else {
475             if ( *(p-1) == '\\' ) {     /* lines ending in '\' are continued */
476                 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
477                 rbuf = p;
478                 continue;
479             }
480             *p++ = '\0';
481             rbuf = p;
482         }
483
484         if ( dn == NULL ) {     /* first line contains DN */
485             if (( dn = strdup( line )) == NULL ) {
486                 perror( "strdup" );
487                 exit( 1 );
488             }
489         } else {
490             if (( p = strchr( line, '=' )) == NULL ) {
491                 value = NULL;
492                 p = line + strlen( line );
493             } else {
494                 *p++ = '\0';
495                 value = p;
496             }
497
498             for ( attr = line;
499                   *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
500                 ;       /* skip attribute leading white space */
501             }
502
503             for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
504                 *q = '\0';      /* remove attribute trailing white space */
505             }
506
507             if ( value != NULL ) {
508                 while ( isspace( (unsigned char) *value )) {
509                     ++value;            /* skip value leading white space */
510                 }
511                 for ( q = value + strlen( value ) - 1; q > value &&
512                         isspace( (unsigned char) *q ); --q ) {
513                     *q = '\0';  /* remove value trailing white space */
514                 }
515                 if ( *value == '\0' ) {
516                     value = NULL;
517                 }
518
519             }
520
521             if ( value == NULL && new ) {
522                 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
523                         prog, linenum, attr );
524                 rc = LDAP_PARAM_ERROR;
525             } else {
526                  switch ( *attr ) {
527                 case '-':
528                     modop = LDAP_MOD_DELETE;
529                     ++attr;
530                     break;
531                 case '+':
532                     modop = LDAP_MOD_ADD;
533                     ++attr;
534                     break;
535                 default:
536                     modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
537                 }
538
539                 addmodifyop( &pmods, modop, attr, value,
540                         ( value == NULL ) ? 0 : strlen( value ));
541             }
542         }
543
544         line = rbuf;
545     }
546
547     if ( rc == 0 ) {
548         if ( dn == NULL ) {
549             rc = LDAP_PARAM_ERROR;
550         } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
551             rc = 0;
552         }
553     }
554
555     if ( pmods != NULL ) {
556         freepmods( pmods );
557     }
558     if ( dn != NULL ) {
559         free( dn );
560     }
561
562     return( rc );
563 }
564
565
566 static void
567 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
568 {
569     LDAPMod             **pmods;
570     int                 i, j;
571     struct berval       *bvp;
572
573     pmods = *pmodsp;
574     modop |= LDAP_MOD_BVALUES;
575
576     i = 0;
577     if ( pmods != NULL ) {
578         for ( ; pmods[ i ] != NULL; ++i ) {
579             if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
580                     pmods[ i ]->mod_op == modop ) {
581                 break;
582             }
583         }
584     }
585
586     if ( pmods == NULL || pmods[ i ] == NULL ) {
587         if (( pmods = (LDAPMod **)safe_realloc( pmods, (i + 2) *
588                 sizeof( LDAPMod * ))) == NULL ) {
589             perror( "safe_realloc" );
590             exit( 1 );
591         }
592         *pmodsp = pmods;
593         pmods[ i + 1 ] = NULL;
594         if (( pmods[ i ] = (LDAPMod *)calloc( 1, sizeof( LDAPMod )))
595                 == NULL ) {
596             perror( "calloc" );
597             exit( 1 );
598         }
599         pmods[ i ]->mod_op = modop;
600         if (( pmods[ i ]->mod_type = strdup( attr )) == NULL ) {
601             perror( "strdup" );
602             exit( 1 );
603         }
604     }
605
606     if ( value != NULL ) {
607         j = 0;
608         if ( pmods[ i ]->mod_bvalues != NULL ) {
609             for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
610                 ;
611             }
612         }
613         if (( pmods[ i ]->mod_bvalues =
614                 (struct berval **)safe_realloc( pmods[ i ]->mod_bvalues,
615                 (j + 2) * sizeof( struct berval * ))) == NULL ) {
616             perror( "safe_realloc" );
617             exit( 1 );
618         }
619         pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
620         if (( bvp = (struct berval *)malloc( sizeof( struct berval )))
621                 == NULL ) {
622             perror( "malloc" );
623             exit( 1 );
624         }
625         pmods[ i ]->mod_bvalues[ j ] = bvp;
626
627         if ( valsfromfiles && *value == '/' ) { /* get value from file */
628             if ( fromfile( value, bvp ) < 0 ) {
629                 exit( 1 );
630             }
631         } else {
632             bvp->bv_len = vlen;
633             if (( bvp->bv_val = (char *)malloc( vlen + 1 )) == NULL ) {
634                 perror( "malloc" );
635                 exit( 1 );
636             }
637             SAFEMEMCPY( bvp->bv_val, value, vlen );
638             bvp->bv_val[ vlen ] = '\0';
639         }
640     }
641 }
642
643
644 static int
645 domodify( char *dn, LDAPMod **pmods, int newentry )
646 {
647     int                 i, j, k, notascii, op;
648     struct berval       *bvp;
649
650     if ( pmods == NULL ) {
651         fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
652                 prog, dn );
653         return( LDAP_PARAM_ERROR );
654     }
655
656     if ( verbose ) {
657         for ( i = 0; pmods[ i ] != NULL; ++i ) {
658             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
659             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
660                     "replace" : op == LDAP_MOD_ADD ?
661                     "add" : "delete", pmods[ i ]->mod_type );
662             if ( pmods[ i ]->mod_bvalues != NULL ) {
663                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
664                     bvp = pmods[ i ]->mod_bvalues[ j ];
665                     notascii = 0;
666                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
667                         if ( !isascii( bvp->bv_val[ k ] )) {
668                             notascii = 1;
669                             break;
670                         }
671                     }
672                     if ( notascii ) {
673                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
674                     } else {
675                         printf( "\t%s\n", bvp->bv_val );
676                     }
677                 }
678             }
679         }
680     }
681
682     if ( newentry ) {
683         printf( "%sadding new entry %s\n", not ? "!" : "", dn );
684     } else {
685         printf( "%smodifying entry %s\n", not ? "!" : "", dn );
686     }
687
688     if ( !not ) {
689         if ( newentry ) {
690             i = ldap_add_s( ld, dn, pmods );
691         } else {
692             i = ldap_modify_s( ld, dn, pmods );
693         }
694         if ( i != LDAP_SUCCESS ) {
695             ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
696         } else if ( verbose ) {
697             printf( "modify complete\n" );
698         }
699     } else {
700         i = LDAP_SUCCESS;
701     }
702
703     putchar( '\n' );
704
705     return( i );
706 }
707
708
709 static int
710 dodelete( char *dn )
711 {
712     int rc;
713
714     printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
715     if ( !not ) {
716         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
717             ldap_perror( ld, "ldap_delete" );
718         } else if ( verbose ) {
719             printf( "delete complete" );
720         }
721     } else {
722         rc = LDAP_SUCCESS;
723     }
724
725     putchar( '\n' );
726
727     return( rc );
728 }
729
730
731 static int
732 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
733 {
734     int rc;
735
736     if ( verbose ) {
737         printf( "new RDN: %s (%skeep existing values)\n",
738                 newrdn, deleteoldrdn ? "do not " : "" );
739     }
740
741     printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
742     if ( !not ) {
743         if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
744                 != LDAP_SUCCESS ) {
745             ldap_perror( ld, "ldap_modrdn" );
746         } else {
747             printf( "modrdn completed\n" );
748         }
749     } else {
750         rc = LDAP_SUCCESS;
751     }
752
753     putchar( '\n' );
754
755     return( rc );
756 }
757
758
759
760 static void
761 freepmods( LDAPMod **pmods )
762 {
763     int i;
764
765     for ( i = 0; pmods[ i ] != NULL; ++i ) {
766         if ( pmods[ i ]->mod_bvalues != NULL ) {
767             ber_bvecfree( pmods[ i ]->mod_bvalues );
768         }
769         if ( pmods[ i ]->mod_type != NULL ) {
770             free( pmods[ i ]->mod_type );
771         }
772         free( pmods[ i ] );
773     }
774     free( pmods );
775 }
776
777
778 static int
779 fromfile( char *path, struct berval *bv )
780 {
781         FILE            *fp;
782         long            rlen;
783         int             eof;
784
785         if (( fp = fopen( path, "r" )) == NULL ) {
786                 perror( path );
787                 return( -1 );
788         }
789
790         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
791                 perror( path );
792                 fclose( fp );
793                 return( -1 );
794         }
795
796         bv->bv_len = ftell( fp );
797
798         if (( bv->bv_val = (char *)malloc( bv->bv_len )) == NULL ) {
799                 perror( "malloc" );
800                 fclose( fp );
801                 return( -1 );
802         }
803
804         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
805                 perror( path );
806                 fclose( fp );
807                 return( -1 );
808         }
809
810         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
811         eof = feof( fp );
812         fclose( fp );
813
814         if ( (unsigned long) rlen != bv->bv_len ) {
815                 perror( path );
816                 free( bv->bv_val );
817                 return( -1 );
818         }
819
820         return( bv->bv_len );
821 }
822
823
824 static char *
825 read_one_record( FILE *fp )
826 {
827     int         len;
828     char        *buf, line[ LDAPMOD_MAXLINE ];
829     int         lcur, lmax;
830
831     lcur = lmax = 0;
832     buf = NULL;
833
834     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
835             (( len = strlen( line )) > 1 )) {
836         if ( lcur + len + 1 > lmax ) {
837             lmax = LDAPMOD_MAXLINE
838                     * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
839             if (( buf = (char *)safe_realloc( buf, lmax )) == NULL ) {
840                 perror( "safe_realloc" );
841                 exit( 1 );
842             }
843         }
844         strcpy( buf + lcur, line );
845         lcur += len;
846     }
847
848     return( buf );
849 }