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