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