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