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