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