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