]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
52c8f52aca74a667c0f0d9260fc89eace07b6950
[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 %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 )) == NULL ) {
407                     perror( "strdup" );
408                     exit( EXIT_FAILURE );
409                 }
410                 expect_ct = 1;
411             }
412             continue;   /* 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\t(LDAP host/port does not match replica: lines)\n",
419                         prog, dn );
420                 free( dn );
421                 return( 0 );
422             }
423
424             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
425                 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
426                         new_entry = 0;
427                         expect_modop = 1;
428                 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
429                         new_entry = 1;
430                 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
431                         || strcasecmp( value, T_MODDNCTSTR ) == 0
432                         || strcasecmp( value, T_RENAMECTSTR ) == 0)
433                 {
434                     expect_newrdn = 1;
435                 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
436                     got_all = delete_entry = 1;
437                 } else {
438                     fprintf( stderr,
439                             "%s:  unknown %s \"%s\" (line %d of entry: %s)\n",
440                             prog, T_CHANGETYPESTR, value, linenum, dn );
441                     rc = LDAP_PARAM_ERROR;
442                 }
443                 continue;
444             } else if ( new ) {         /*  missing changetype => add */
445                 new_entry = 1;
446                 modop = LDAP_MOD_ADD;
447             } else {
448                 expect_modop = 1;       /* missing changetype => modify */
449             }
450         }
451
452         if ( expect_modop ) {
453             expect_modop = 0;
454             expect_sep = 1;
455             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
456                 modop = LDAP_MOD_ADD;
457                 continue;
458             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
459                 modop = LDAP_MOD_REPLACE;
460                 continue;
461             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
462                 modop = LDAP_MOD_DELETE;
463                 addmodifyop( &pmods, modop, value, NULL, 0 );
464                 continue;
465             } else {    /* no modify op:  use default */
466                 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
467             }
468         }
469
470         if ( expect_newrdn ) {
471             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
472                 if (( newrdn = strdup( value )) == NULL ) {
473                     perror( "strdup" );
474                     exit( EXIT_FAILURE );
475                 }
476                 expect_deleteoldrdn = 1;
477                 expect_newrdn = 0;
478             } else {
479                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
480                         prog, T_NEWRDNSTR, type, linenum, dn );
481                 rc = LDAP_PARAM_ERROR;
482             }
483         } else if ( expect_deleteoldrdn ) {
484             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
485                 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
486                 expect_deleteoldrdn = 0;
487                 expect_newsup = 1;
488                 got_all = 1;
489             } else {
490                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
491                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
492                 rc = LDAP_PARAM_ERROR;
493             }
494         } else if ( expect_newsup ) {
495             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
496                 if (( newsup = strdup( value )) == NULL ) {
497                     perror( "strdup" );
498                     exit( EXIT_FAILURE );
499                 }
500                 expect_newsup = 0;
501             } else {
502                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
503                         prog, T_NEWSUPSTR, type, linenum, dn );
504                 rc = LDAP_PARAM_ERROR;
505             }
506         } else if ( got_all ) {
507             fprintf( stderr,
508                     "%s: extra lines at end (line %d of entry %s)\n",
509                     prog, linenum, dn );
510             rc = LDAP_PARAM_ERROR;
511         } else {
512             addmodifyop( &pmods, modop, type, value, vlen );
513         }
514     }
515
516         if( linenum == 0 ) {
517                 return 0;
518         }
519
520         if( version && linenum == 1 ) {
521                 return 0;
522         }
523
524     if ( rc == 0 ) {
525         if ( delete_entry ) {
526             rc = dodelete( dn );
527         } else if ( newrdn != NULL ) {
528             rc = domodrdn( dn, newrdn, deleteoldrdn );
529         } else {
530             rc = domodify( dn, pmods, new_entry );
531         }
532
533         if ( rc == LDAP_SUCCESS ) {
534             rc = 0;
535         }
536     }
537
538     if ( dn != NULL ) {
539         free( dn );
540     }
541     if ( newrdn != NULL ) {
542         free( newrdn );
543     }
544     if ( pmods != NULL ) {
545         ldap_mods_free( pmods, 1 );
546     }
547
548     return( rc );
549 }
550
551
552 static int
553 process_ldapmod_rec( char *rbuf )
554 {
555     char        *line, *dn, *p, *q, *attr, *value;
556     int         rc, linenum, modop;
557     LDAPMod     **pmods;
558
559     pmods = NULL;
560     dn = NULL;
561     linenum = 0;
562     line = rbuf;
563     rc = 0;
564
565     while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
566         ++linenum;
567         if (( p = strchr( rbuf, '\n' )) == NULL ) {
568             rbuf = NULL;
569         } else {
570             if ( *(p-1) == '\\' ) {     /* lines ending in '\' are continued */
571                 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
572                 rbuf = p;
573                 continue;
574             }
575             *p++ = '\0';
576             rbuf = p;
577         }
578
579         if ( dn == NULL ) {     /* first line contains DN */
580             if (( dn = strdup( line )) == NULL ) {
581                 perror( "strdup" );
582                 exit( EXIT_FAILURE );
583             }
584         } else {
585             if (( p = strchr( line, '=' )) == NULL ) {
586                 value = NULL;
587                 p = line + strlen( line );
588             } else {
589                 *p++ = '\0';
590                 value = p;
591             }
592
593             for ( attr = line;
594                   *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
595                 ;       /* skip attribute leading white space */
596             }
597
598             for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
599                 *q = '\0';      /* remove attribute trailing white space */
600             }
601
602             if ( value != NULL ) {
603                 while ( isspace( (unsigned char) *value )) {
604                     ++value;            /* skip value leading white space */
605                 }
606                 for ( q = value + strlen( value ) - 1; q > value &&
607                         isspace( (unsigned char) *q ); --q ) {
608                     *q = '\0';  /* remove value trailing white space */
609                 }
610                 if ( *value == '\0' ) {
611                     value = NULL;
612                 }
613
614             }
615
616             if ( value == NULL && new ) {
617                 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
618                         prog, linenum, attr );
619                 rc = LDAP_PARAM_ERROR;
620             } else {
621                  switch ( *attr ) {
622                 case '-':
623                     modop = LDAP_MOD_DELETE;
624                     ++attr;
625                     break;
626                 case '+':
627                     modop = LDAP_MOD_ADD;
628                     ++attr;
629                     break;
630                 default:
631                     modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
632                 }
633
634                 addmodifyop( &pmods, modop, attr, value,
635                         ( value == NULL ) ? 0 : strlen( value ));
636             }
637         }
638
639         line = rbuf;
640     }
641
642     if ( rc == 0 ) {
643         if ( dn == NULL ) {
644             rc = LDAP_PARAM_ERROR;
645         } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
646             rc = 0;
647         }
648     }
649
650     if ( pmods != NULL ) {
651         ldap_mods_free( pmods, 1 );
652     }
653     if ( dn != NULL ) {
654         free( dn );
655     }
656
657     return( rc );
658 }
659
660
661 static void
662 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
663 {
664     LDAPMod             **pmods;
665     int                 i, j;
666     struct berval       *bvp;
667
668     pmods = *pmodsp;
669     modop |= LDAP_MOD_BVALUES;
670
671     i = 0;
672     if ( pmods != NULL ) {
673         for ( ; pmods[ i ] != NULL; ++i ) {
674             if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
675                     pmods[ i ]->mod_op == modop ) {
676                 break;
677             }
678         }
679     }
680
681     if ( pmods == NULL || pmods[ i ] == NULL ) {
682         if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
683                 sizeof( LDAPMod * ))) == NULL ) {
684             perror( "realloc" );
685             exit( EXIT_FAILURE );
686         }
687         *pmodsp = pmods;
688         pmods[ i + 1 ] = NULL;
689         if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
690                 == NULL ) {
691             perror( "calloc" );
692             exit( EXIT_FAILURE );
693         }
694         pmods[ i ]->mod_op = modop;
695         if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
696             perror( "strdup" );
697             exit( EXIT_FAILURE );
698         }
699     }
700
701     if ( value != NULL ) {
702         j = 0;
703         if ( pmods[ i ]->mod_bvalues != NULL ) {
704             for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
705                 ;
706             }
707         }
708         if (( pmods[ i ]->mod_bvalues =
709                 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
710                 (j + 2) * sizeof( struct berval * ))) == NULL ) {
711             perror( "ber_realloc" );
712             exit( EXIT_FAILURE );
713         }
714         pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
715         if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
716                 == NULL ) {
717             perror( "ber_memalloc" );
718             exit( EXIT_FAILURE );
719         }
720         pmods[ i ]->mod_bvalues[ j ] = bvp;
721
722         if ( valsfromfiles && *value == '/' ) { /* get value from file */
723             if ( fromfile( value, bvp ) < 0 ) {
724                 exit( EXIT_FAILURE );
725             }
726         } else {
727             bvp->bv_len = vlen;
728             if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
729                 perror( "malloc" );
730                 exit( EXIT_FAILURE );
731             }
732             SAFEMEMCPY( bvp->bv_val, value, vlen );
733             bvp->bv_val[ vlen ] = '\0';
734         }
735     }
736 }
737
738
739 static int
740 domodify( char *dn, LDAPMod **pmods, int newentry )
741 {
742     int                 i, j, k, notascii, op;
743     struct berval       *bvp;
744
745     if ( pmods == NULL ) {
746         fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
747                 prog, dn );
748         return( LDAP_PARAM_ERROR );
749     }
750
751     if ( verbose ) {
752         for ( i = 0; pmods[ i ] != NULL; ++i ) {
753             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
754             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
755                     "replace" : op == LDAP_MOD_ADD ?
756                     "add" : "delete", pmods[ i ]->mod_type );
757             if ( pmods[ i ]->mod_bvalues != NULL ) {
758                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
759                     bvp = pmods[ i ]->mod_bvalues[ j ];
760                     notascii = 0;
761                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
762                         if ( !isascii( bvp->bv_val[ k ] )) {
763                             notascii = 1;
764                             break;
765                         }
766                     }
767                     if ( notascii ) {
768                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
769                     } else {
770                         printf( "\t%s\n", bvp->bv_val );
771                     }
772                 }
773             }
774         }
775     }
776
777     if ( newentry ) {
778         printf( "%sadding new entry %s\n", not ? "!" : "", dn );
779     } else {
780         printf( "%smodifying entry %s\n", not ? "!" : "", dn );
781     }
782
783     if ( !not ) {
784         if ( newentry ) {
785             i = ldap_add_s( ld, dn, pmods );
786         } else {
787             i = ldap_modify_s( ld, dn, pmods );
788         }
789         if ( i != LDAP_SUCCESS ) {
790             ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
791         } else if ( verbose ) {
792             printf( "modify complete\n" );
793         }
794     } else {
795         i = LDAP_SUCCESS;
796     }
797
798     putchar( '\n' );
799
800     return( i );
801 }
802
803
804 static int
805 dodelete( char *dn )
806 {
807     int rc;
808
809     printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
810     if ( !not ) {
811         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
812             ldap_perror( ld, "ldap_delete" );
813         } else if ( verbose ) {
814             printf( "delete complete" );
815         }
816     } else {
817         rc = LDAP_SUCCESS;
818     }
819
820     putchar( '\n' );
821
822     return( rc );
823 }
824
825
826 static int
827 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
828 {
829     int rc;
830
831     if ( verbose ) {
832         printf( "new RDN: %s (%skeep existing values)\n",
833                 newrdn, deleteoldrdn ? "do not " : "" );
834     }
835
836     printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
837     if ( !not ) {
838         if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
839                 != LDAP_SUCCESS ) {
840             ldap_perror( ld, "ldap_modrdn" );
841         } else {
842             printf( "modrdn completed\n" );
843         }
844     } else {
845         rc = LDAP_SUCCESS;
846     }
847
848     putchar( '\n' );
849
850     return( rc );
851 }
852
853
854 static int
855 fromfile( char *path, struct berval *bv )
856 {
857         FILE            *fp;
858         long            rlen;
859         int             eof;
860
861         if (( fp = fopen( path, "r" )) == NULL ) {
862                 perror( path );
863                 return( -1 );
864         }
865
866         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
867                 perror( path );
868                 fclose( fp );
869                 return( -1 );
870         }
871
872         bv->bv_len = ftell( fp );
873
874         if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
875                 perror( "malloc" );
876                 fclose( fp );
877                 return( -1 );
878         }
879
880         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
881                 perror( path );
882                 fclose( fp );
883                 ber_memfree( bv->bv_val );
884                 bv->bv_val = NULL;
885                 return( -1 );
886         }
887
888         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
889         eof = feof( fp );
890         fclose( fp );
891
892         if ( (unsigned long) rlen != bv->bv_len ) {
893                 perror( path );
894                 ber_memfree( bv->bv_val );
895                 bv->bv_val = NULL;
896                 return( -1 );
897         }
898
899         return( bv->bv_len );
900 }
901
902
903 static char *
904 read_one_record( FILE *fp )
905 {
906     char        *buf, line[ LDAPMOD_MAXLINE ];
907     int         lcur, lmax;
908
909     lcur = lmax = 0;
910     buf = NULL;
911
912     while ( fgets( line, sizeof(line), fp ) != NULL ) {
913         int len = strlen( line );
914
915                 if( len < 2 ) {
916                         if( buf == NULL ) {
917                                 continue;
918                         } else {
919                                 break;
920                         }
921                 }
922
923                 if ( lcur + len + 1 > lmax ) {
924                         lmax = LDAPMOD_MAXLINE
925                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
926
927                         if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
928                                 perror( "realloc" );
929                                 exit( EXIT_FAILURE );
930                         }
931                 }
932
933                 strcpy( buf + lcur, line );
934                 lcur += len;
935     }
936
937     return( buf );
938 }