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