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