]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Add LDBM comment
[openldap] / clients / tools / ldapmodify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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 #include <ac/ctype.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #ifdef HAVE_SYS_STAT_H
18 #include <sys/stat.h>
19 #endif
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 <ldap.h>
29
30 #include "lutil.h"
31 #include "lutil_ldap.h"
32 #include "ldif.h"
33 #include "ldap_defaults.h"
34 #include "ldap_log.h"
35 #include "ldap_pvt.h"
36
37 #include "common.h"
38
39
40 static int      ldapadd, force = 0;
41 static char *rejfile = NULL;
42 static LDAP     *ld = NULL;
43
44 #define LDAPMOD_MAXLINE         4096
45
46 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
47 #define T_VERSION_STR           "version"
48 #define T_REPLICA_STR           "replica"
49 #define T_DN_STR                "dn"
50 #define T_CONTROL_STR           "control"
51 #define T_CHANGETYPESTR         "changetype"
52 #define T_ADDCTSTR              "add"
53 #define T_MODIFYCTSTR           "modify"
54 #define T_DELETECTSTR           "delete"
55 #define T_MODRDNCTSTR           "modrdn"
56 #define T_MODDNCTSTR            "moddn"
57 #define T_RENAMECTSTR           "rename"
58 #define T_MODOPADDSTR           "add"
59 #define T_MODOPREPLACESTR       "replace"
60 #define T_MODOPDELETESTR        "delete"
61 #define T_MODSEPSTR             "-"
62 #define T_NEWRDNSTR             "newrdn"
63 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
64 #define T_NEWSUPSTR             "newsuperior"
65
66
67 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
68 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
69 static void addmodifyop LDAP_P((
70         LDAPMod ***pmodsp, int modop,
71         const char *attr,
72         struct berval *value ));
73 static int domodify LDAP_P((
74         const char *dn,
75         LDAPMod **pmods,
76     LDAPControl **pctrls,
77         int newentry ));
78 static int dodelete LDAP_P((
79         const char *dn,
80     LDAPControl **pctrls ));
81 static int dorename LDAP_P((
82         const char *dn,
83         const char *newrdn,
84         const char *newsup,
85         int deleteoldrdn,
86     LDAPControl **pctrls ));
87 static char *read_one_record LDAP_P(( FILE *fp ));
88
89 void
90 usage( void )
91 {
92     fprintf( stderr,
93 "Add or modify entries from an LDAP server\n\n"
94 "usage: %s [options]\n"
95 "       The list of desired operations are read from stdin or from the file\n"
96 "       specified by \"-f file\".\n"
97 "Add or modify options:\n"
98 "  -a         add values (default%s)\n"
99 "  -F         force all changes records to be used\n"
100 "  -S file    write skipped modifications to `file'\n"
101                  , prog, (ldapadd ? "" : " is to replace") );
102         tool_common_usage();
103     exit( EXIT_FAILURE );
104 }
105
106
107 const char options[] = "aFrS:"
108         "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
109
110 int
111 handle_private_option( int i )
112 {
113         switch ( i ) {
114 #if 0
115                 char    *control, *cvalue;
116                 int             crit;
117         case 'E': /* modify controls */
118                 if( protocol == LDAP_VERSION2 ) {
119                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
120                                 prog, protocol );
121                         exit( EXIT_FAILURE );
122                 }
123
124                 /* should be extended to support comma separated list of
125                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
126                  */
127
128                 crit = 0;
129                 cvalue = NULL;
130                 if( optarg[0] == '!' ) {
131                         crit = 1;
132                         optarg++;
133                 }
134
135                 control = ber_strdup( optarg );
136                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
137                         *cvalue++ = '\0';
138                 }
139                 fprintf( stderr, "Invalid modify control name: %s\n", control );
140                 usage();
141 #endif
142
143         case 'a':       /* add */
144             ldapadd = 1;
145             break;
146
147         case 'F':       /* force all changes records to be used */
148             force = 1;
149             break;
150
151         case 'r':       /* replace (obsolete) */
152                 break;
153
154         case 'S':       /* skipped modifications to file */
155                 if( rejfile != NULL ) {
156                         fprintf( stderr, "%s: -S previously specified\n", prog );
157                         exit( EXIT_FAILURE );
158                 }
159                 rejfile = ber_strdup( optarg );
160                 break;
161
162         default:
163                 return 0;
164         }
165         return 1;
166 }
167
168
169 int
170 main( int argc, char **argv )
171 {
172     char                *rbuf, *start, *rejbuf = NULL;
173     FILE                *fp, *rejfp;
174         char            *matched_msg, *error_msg;
175         int             rc, retval;
176         int count, len;
177
178     prog = lutil_progname( "ldapmodify", argc, argv );
179
180         /* strncmp instead of strcmp since NT binaries carry .exe extension */
181     ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
182
183     /* Print usage when no parameters */
184     if( argc < 2 ) usage();
185
186         tool_args( argc, argv );
187
188         if ( argc != optind )
189         usage();
190
191     if ( rejfile != NULL ) {
192         if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
193             perror( rejfile );
194             return( EXIT_FAILURE );
195         }
196     } else {
197         rejfp = NULL;
198     }
199
200     if ( infile != NULL ) {
201         if (( fp = fopen( infile, "r" )) == NULL ) {
202             perror( infile );
203             return( EXIT_FAILURE );
204         }
205     } else {
206         fp = stdin;
207     }
208
209         if ( debug )
210                 ldif_debug = debug;
211
212         ld = tool_conn_setup( not, 0 );
213
214     if ( !not ) {
215         if ( pw_file || want_bindpw ) {
216                 if ( pw_file ) {
217                         rc = lutil_get_filed_password( pw_file, &passwd );
218                         if( rc ) return EXIT_FAILURE;
219                 } else {
220                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
221                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
222                 }
223         }
224
225         tool_bind( ld );
226     }
227
228     rc = 0;
229
230         if ( authzid || manageDSAit || noop )
231                 tool_server_controls( ld, NULL, 0 );
232
233         count = 0;
234         retval = 0;
235     while (( rc == 0 || contoper ) &&
236                 ( rbuf = read_one_record( fp )) != NULL ) {
237         count++;
238
239         start = rbuf;
240
241         if ( rejfp ) {
242                 len = strlen( rbuf );
243                 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
244                         perror( "malloc" );
245                         exit( EXIT_FAILURE );
246                 }
247                 memcpy( rejbuf, rbuf, len+1 );
248         }
249
250     rc = process_ldif_rec( start, count );
251
252         if ( rc )
253                 retval = rc;
254         if ( rc && rejfp ) {
255                 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
256
257                 matched_msg = NULL;
258                 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
259                 if ( matched_msg != NULL ) {
260                         if ( *matched_msg != '\0' )
261                                 fprintf( rejfp, ", matched DN: %s", matched_msg );
262                         ldap_memfree( matched_msg );
263                 }
264
265                 error_msg = NULL;
266                 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
267                 if ( error_msg != NULL ) {
268                         if ( *error_msg != '\0' )
269                                 fprintf( rejfp, ", additional info: %s", error_msg );
270                         ldap_memfree( error_msg );
271                 }
272                 fprintf( rejfp, "\n%s\n", rejbuf );
273         }
274                 if (rejfp) 
275                         free( rejbuf );
276                 free( rbuf );
277     }
278
279     if ( !not ) {
280                 ldap_unbind( ld );
281     }
282
283     if ( rejfp != NULL ) {
284             fclose( rejfp );
285     }
286
287     return( retval );
288 }
289
290
291 static int
292 process_ldif_rec( char *rbuf, int count )
293 {
294     char        *line, *dn, *type, *newrdn, *newsup, *p;
295     int         rc, linenum, modop, replicaport;
296     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
297     int         expect_deleteoldrdn, deleteoldrdn;
298     int         saw_replica, use_record, new_entry, delete_entry, got_all;
299     LDAPMod     **pmods;
300         int version;
301         struct berval val;
302     LDAPControl **pctrls;
303
304     new_entry = ldapadd;
305
306     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
307     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
308         expect_sep = expect_ct = 0;
309     linenum = 0;
310         version = 0;
311     deleteoldrdn = 1;
312     use_record = force;
313     pmods = NULL;
314     pctrls = NULL;
315     dn = newrdn = newsup = NULL;
316
317     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
318         ++linenum;
319
320         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
321             expect_sep = 0;
322             expect_ct = 1;
323             continue;
324         }
325         
326         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
327             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
328                     prog, linenum, dn == NULL ? "" : dn );
329             rc = LDAP_PARAM_ERROR;
330             break;
331         }
332
333         if ( dn == NULL ) {
334             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
335                 ++saw_replica;
336                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
337                     replicaport = 0;
338                 } else {
339                     *p++ = '\0';
340                     replicaport = atoi( p );
341                 }
342                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
343                         replicaport == ldapport ) {
344                     use_record = 1;
345                 }
346             } else if ( count == 1 && linenum == 1 && 
347                         strcasecmp( type, T_VERSION_STR ) == 0 )
348                 {
349                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
350                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
351                                 prog, val.bv_val, linenum );
352                         }
353                         version++;
354
355             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
356                 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
357                     perror( "strdup" );
358                     exit( EXIT_FAILURE );
359                 }
360                 expect_ct = 1;
361             }
362             goto end_line;      /* skip all lines until we see "dn:" */
363         }
364
365         if ( expect_ct ) {
366         
367         /* Check for "control" tag after dn and before changetype. */
368         if (strcasecmp(type, T_CONTROL_STR) == 0) {
369             /* Parse and add it to the list of controls */
370             rc = parse_ldif_control( line, &pctrls );
371             if (rc != 0) {
372                         fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
373                                 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
374             }
375             goto end_line;
376         }
377         
378             expect_ct = 0;
379             if ( !use_record && saw_replica ) {
380                 printf( "%s: skipping change record for entry: %s\n"
381                         "\t(LDAP host/port does not match replica: lines)\n",
382                         prog, dn );
383                 free( dn );
384                 ber_memfree( type );
385                 ber_memfree( val.bv_val );
386                 return( 0 );
387             }
388
389             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
390 #ifdef LIBERAL_CHANGETYPE_MODOP
391                 /* trim trailing spaces (and log warning ...) */
392
393                 int icnt;
394                 for ( icnt = val.bv_len; --icnt > 0; ) {
395                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
396                         break;
397                     }
398                 }
399
400                 if ( ++icnt != val.bv_len ) {
401                     fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
402                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
403                     val.bv_val[icnt] = '\0';
404                 }
405 #endif /* LIBERAL_CHANGETYPE_MODOP */
406
407                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
408                         new_entry = 0;
409                         expect_modop = 1;
410                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
411                         new_entry = 1;
412                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
413                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
414                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
415                 {
416                     expect_newrdn = 1;
417                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
418                     got_all = delete_entry = 1;
419                 } else {
420                     fprintf( stderr,
421                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
422                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
423                     rc = LDAP_PARAM_ERROR;
424                 }
425                 goto end_line;
426             } else if ( ldapadd ) {             /*  missing changetype => add */
427                 new_entry = 1;
428                 modop = LDAP_MOD_ADD;
429             } else {
430                 expect_modop = 1;       /* missing changetype => modify */
431             }
432         }
433
434         if ( expect_modop ) {
435 #ifdef LIBERAL_CHANGETYPE_MODOP
436             /* trim trailing spaces (and log warning ...) */
437             
438             int icnt;
439             for ( icnt = val.bv_len; --icnt > 0; ) {
440                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
441                     break;
442                 }
443             }
444             
445             if ( ++icnt != val.bv_len ) {
446                 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
447                         prog, type, val.bv_val, linenum, dn );
448                 val.bv_val[icnt] = '\0';
449             }
450 #endif /* LIBERAL_CHANGETYPE_MODOP */
451
452             expect_modop = 0;
453             expect_sep = 1;
454             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
455                 modop = LDAP_MOD_ADD;
456                 goto end_line;
457             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
458                 modop = LDAP_MOD_REPLACE;
459                 addmodifyop( &pmods, modop, val.bv_val, NULL );
460                 goto end_line;
461             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
462                 modop = LDAP_MOD_DELETE;
463                 addmodifyop( &pmods, modop, val.bv_val, NULL );
464                 goto end_line;
465             } else {    /* no modify op:  use default */
466                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
467             }
468         }
469
470         if ( expect_newrdn ) {
471             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
472                         if (( newrdn = ber_strdup( val.bv_val )) == 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 = ( *val.bv_val == '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 = ber_strdup( val.bv_val )) == 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, &val );
513         }
514
515 end_line:
516         ber_memfree( type );
517         ber_memfree( val.bv_val );
518     }
519
520         if( linenum == 0 ) {
521                 return 0;
522         }
523
524         if( version && linenum == 1 ) {
525                 return 0;
526         }
527
528     /* If default controls are set (as with -M option) and controls are
529        specified in the LDIF file, we must add the default controls to
530        the list of controls sent with the ldap operation.
531     */
532     if ( rc == 0 ) {
533         if (pctrls) {
534             LDAPControl **defctrls = NULL;   /* Default server controls */
535             LDAPControl **newctrls = NULL;
536             ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
537             if (defctrls) {
538                 int npc=0;                  /* Number of LDIF controls */
539                 int ndefc=0;                /* Number of default controls */
540                 while (pctrls[npc])         /* Count LDIF controls */
541                     npc++; 
542                 while (defctrls[ndefc])     /* Count default controls */
543                     ndefc++;
544                 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
545                 if (newctrls == NULL)
546                     rc = LDAP_NO_MEMORY;
547                 else {
548                     int i;
549                     pctrls = newctrls;
550                     for (i=npc; i<npc+ndefc; i++) {
551                         pctrls[i] = ldap_control_dup(defctrls[i-npc]);
552                         if (pctrls[i] == NULL) {
553                             rc = LDAP_NO_MEMORY;
554                             break;
555                         }
556                     }
557                     pctrls[npc+ndefc] = NULL;
558                 }
559                 ldap_controls_free(defctrls);  /* Must be freed by library */
560             }
561         }
562     }
563
564
565     if ( rc == 0 ) {
566         if ( delete_entry ) {
567             rc = dodelete( dn, pctrls );
568         } else if ( newrdn != NULL ) {
569             rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
570         } else {
571             rc = domodify( dn, pmods, pctrls, new_entry );
572         }
573
574         if ( rc == LDAP_SUCCESS ) {
575             rc = 0;
576         }
577     }
578
579     if ( dn != NULL ) {
580         free( dn );
581     }
582     if ( newrdn != NULL ) {
583         free( newrdn );
584     }
585     if ( pmods != NULL ) {
586         ldap_mods_free( pmods, 1 );
587     }
588
589     if (pctrls != NULL) {
590         ldap_controls_free( pctrls );
591     }
592
593     return( rc );
594 }
595
596 /* Parse an LDIF control line of the form
597       control:  oid  [true/false]  [: value]              or
598       control:  oid  [true/false]  [:: base64-value]      or
599       control:  oid  [true/false]  [:< url]
600    The control is added to the list of controls in *ppctrls.
601 */      
602 static int
603 parse_ldif_control( char *line, 
604                     LDAPControl ***ppctrls )
605 {
606     char *oid = NULL;
607     int criticality = 0;   /* Default is false if not present */
608     char *type=NULL;
609     char *val = NULL;
610     ber_len_t value_len = 0;
611     int i, rc=0;
612     char *s, *oidStart, *pcolon;
613     LDAPControl *newctrl = NULL;
614     LDAPControl **pctrls = NULL;
615
616     if (ppctrls) {
617         pctrls = *ppctrls;
618     }
619     s = line + strlen(T_CONTROL_STR);  /* Skip over "control" */
620     pcolon = s;                        /* Save this position for later */
621     if (*s++ != ':')                   /* Make sure colon follows */
622         return ( LDAP_PARAM_ERROR );
623     while (*s && isspace((unsigned char)*s))
624                 s++;                           /* Skip white space before OID */
625
626     /* OID should come next. Validate and extract it. */
627     if (*s == 0)
628         return ( LDAP_PARAM_ERROR );
629     oidStart = s;
630     while (isdigit((unsigned char)*s) || *s == '.')
631                 s++;                           /* OID should be digits or . */
632     if (s == oidStart) 
633         return ( LDAP_PARAM_ERROR );   /* OID was not present */
634     if (*s) {                          /* End of OID should be space or NULL */
635         if (!isspace((unsigned char)*s))
636             return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
637         *s++ = 0;                    /* Replace space with null to terminate */
638     }
639
640     
641     oid = ber_strdup(oidStart);
642     if (oid == NULL)
643         return ( LDAP_NO_MEMORY );
644
645     /* Optional Criticality field is next. */
646     while (*s && isspace((unsigned char)*s))
647                 s++;                         /* Skip white space before criticality */
648     if (strncasecmp(s, "true", 4) == 0) {
649         criticality = 1;
650         s += 4;
651     } 
652     else if (strncasecmp(s, "false", 5) == 0) {
653         criticality = 0;
654         s += 5;
655     }
656
657     /* Optional value field is next */
658     while (*s && isspace((unsigned char)*s))
659                 s++;                         /* Skip white space before value */
660     if (*s) {
661         if (*s != ':') {           /* If value is present, must start with : */
662             rc = LDAP_PARAM_ERROR;
663             goto cleanup;
664         }
665
666         /* Shift value down over OID and criticality so it's in the form
667              control: value
668              control:: base64-value
669              control:< url
670            Then we can use ldif_parse_line to extract and decode the value
671         */
672         while ( (*pcolon++ = *s++) != 0)     /* Shift value */
673             ;
674         rc = ldif_parse_line(line, &type, &val, &value_len);
675         if (type)  ber_memfree(type);   /* Don't need this field*/
676         if (rc < 0) {
677             rc = LDAP_PARAM_ERROR;
678             goto cleanup;
679         }
680     }
681
682     /* Create a new LDAPControl structure. */
683     newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
684     if ( newctrl == NULL ) {
685         rc = LDAP_NO_MEMORY;
686         goto cleanup;
687     }
688     newctrl->ldctl_oid = oid;
689     oid = NULL;
690     newctrl->ldctl_iscritical = criticality;
691     newctrl->ldctl_value.bv_len = value_len;
692     newctrl->ldctl_value.bv_val = val;
693     val = NULL;
694
695     /* Add the new control to the passed-in list of controls. */
696     i = 0;
697     if (pctrls) {
698         while ( pctrls[i] )      /* Count the # of controls passed in */
699             i++;
700     }
701     /* Allocate 1 more slot for the new control and 1 for the NULL. */
702     pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
703     if (pctrls == NULL) {
704         rc = LDAP_NO_MEMORY;
705         goto cleanup;
706     }
707     pctrls[i] = newctrl;
708     newctrl = NULL;
709     pctrls[i+1] = NULL;
710     *ppctrls = pctrls;
711
712 cleanup:
713     if (newctrl) {
714         if (newctrl->ldctl_oid)
715             ber_memfree(newctrl->ldctl_oid);
716         if (newctrl->ldctl_value.bv_val)
717             ber_memfree(newctrl->ldctl_value.bv_val);
718         ber_memfree(newctrl);
719     }
720     if (val)
721         ber_memfree(val);
722     if (oid)
723         ber_memfree(oid);
724
725     return( rc );
726 }
727
728
729 static void
730 addmodifyop(
731         LDAPMod ***pmodsp,
732         int modop,
733         const char *attr,
734         struct berval *val )
735 {
736         LDAPMod         **pmods;
737         int                     i, j;
738
739         pmods = *pmodsp;
740         modop |= LDAP_MOD_BVALUES;
741
742         i = 0;
743         if ( pmods != NULL ) {
744                 for ( ; pmods[ i ] != NULL; ++i ) {
745                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
746                                 pmods[ i ]->mod_op == modop )
747                         {
748                                 break;
749                         }
750                 }
751         }
752
753         if ( pmods == NULL || pmods[ i ] == NULL ) {
754                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
755                         sizeof( LDAPMod * ))) == NULL )
756                 {
757                         perror( "realloc" );
758                         exit( EXIT_FAILURE );
759                 }
760
761                 *pmodsp = pmods;
762                 pmods[ i + 1 ] = NULL;
763
764                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
765                 if ( pmods[ i ] == NULL ) {
766                         perror( "calloc" );
767                         exit( EXIT_FAILURE );
768                 }
769
770                 pmods[ i ]->mod_op = modop;
771                 pmods[ i ]->mod_type = ber_strdup( attr );
772                 if ( pmods[ i ]->mod_type == NULL ) {
773                         perror( "strdup" );
774                         exit( EXIT_FAILURE );
775                 }
776         }
777
778         if ( val != NULL ) {
779                 j = 0;
780                 if ( pmods[ i ]->mod_bvalues != NULL ) {
781                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
782                                 /* Empty */;
783                         }
784                 }
785
786                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
787                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
788                 if ( pmods[ i ]->mod_bvalues == NULL ) {
789                         perror( "ber_realloc" );
790                         exit( EXIT_FAILURE );
791                 }
792
793                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
794                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
795                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
796                         perror( "ber_bvdup" );
797                         exit( EXIT_FAILURE );
798                 }
799         }
800 }
801
802
803 static int
804 domodify(
805         const char *dn,
806         LDAPMod **pmods,
807     LDAPControl **pctrls,
808         int newentry )
809 {
810     int                 i, j, k, notascii, op;
811     struct berval       *bvp;
812
813     if ( pmods == NULL ) {
814         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
815                 prog, dn );
816         return( LDAP_PARAM_ERROR );
817     } 
818
819     for ( i = 0; pmods[ i ] != NULL; ++i ) {
820         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
821         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
822                 fprintf( stderr,
823                         "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
824                         prog, pmods[i]->mod_type, dn );
825                 return LDAP_PARAM_ERROR;
826         }
827     }
828
829     if ( verbose ) {
830         for ( i = 0; pmods[ i ] != NULL; ++i ) {
831             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
832             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
833                     "replace" : op == LDAP_MOD_ADD ?
834                     "add" : "delete", pmods[ i ]->mod_type );
835             if ( pmods[ i ]->mod_bvalues != NULL ) {
836                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
837                     bvp = pmods[ i ]->mod_bvalues[ j ];
838                     notascii = 0;
839                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
840                         if ( !isascii( bvp->bv_val[ k ] )) {
841                             notascii = 1;
842                             break;
843                         }
844                     }
845                     if ( notascii ) {
846                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
847                     } else {
848                         printf( "\t%s\n", bvp->bv_val );
849                     }
850                 }
851             }
852         }
853     }
854
855     if ( newentry ) {
856         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
857     } else {
858         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
859     }
860
861     if ( !not ) {
862         if ( newentry ) {
863             i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
864         } else {
865             i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
866         }
867         if ( i != LDAP_SUCCESS ) {
868                 /* print error message about failed update including DN */
869                 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
870                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
871         } else if ( verbose ) {
872             printf( "modify complete\n" );
873         }
874     } else {
875         i = LDAP_SUCCESS;
876     }
877
878     putchar( '\n' );
879
880     return( i );
881 }
882
883
884 static int
885 dodelete(
886         const char *dn,
887     LDAPControl **pctrls )
888 {
889     int rc;
890
891     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
892     if ( !not ) {
893         if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
894                 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
895                 ldap_perror( ld, "ldap_delete" );
896         } else if ( verbose ) {
897             printf( "delete complete" );
898         }
899     } else {
900         rc = LDAP_SUCCESS;
901     }
902
903     putchar( '\n' );
904
905     return( rc );
906 }
907
908
909 static int
910 dorename(
911         const char *dn,
912         const char *newrdn,
913         const char* newsup,
914         int deleteoldrdn,
915     LDAPControl **pctrls )
916 {
917     int rc;
918
919
920     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
921     if ( verbose ) {
922         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
923                 newrdn, deleteoldrdn ? "do not " : "" );
924     }
925     if ( !not ) {
926         if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
927                 != LDAP_SUCCESS ) {
928                 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
929                 ldap_perror( ld, "ldap_modrdn" );
930         } else {
931             printf( "modrdn completed\n" );
932         }
933     } else {
934         rc = LDAP_SUCCESS;
935     }
936
937     putchar( '\n' );
938
939     return( rc );
940 }
941
942
943 static char *
944 read_one_record( FILE *fp )
945 {
946     char        *buf, line[ LDAPMOD_MAXLINE ];
947     int         lcur, lmax;
948
949     lcur = lmax = 0;
950     buf = NULL;
951
952     while ( fgets( line, sizeof(line), fp ) != NULL ) {
953         int len = strlen( line );
954
955                 if( len < 2 || ( len == 2 && *line == '\r' )) {
956                         if( buf == NULL ) {
957                                 continue;
958                         } else {
959                                 break;
960                         }
961                 }
962
963                 if ( lcur + len + 1 > lmax ) {
964                         lmax = LDAPMOD_MAXLINE
965                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
966
967                         if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
968                                 perror( "realloc" );
969                                 exit( EXIT_FAILURE );
970                         }
971                 }
972
973                 strcpy( buf + lcur, line );
974                 lcur += len;
975     }
976
977     return( buf );
978 }
979
980