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