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