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