]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
2003d23dc937b0a72b3cc3c1f65a47d801fbc400
[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 (%s)\n"),
98                 (ldapadd ? _("default") : _("default is to replace")));
99     fprintf( stderr, _("  -F         force all changes records to be used\n"));
100     fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
101     tool_common_usage();
102     exit( EXIT_FAILURE );
103 }
104
105
106 const char options[] = "aFrS:"
107         "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
108
109 int
110 handle_private_option( int i )
111 {
112         switch ( i ) {
113 #if 0
114                 char    *control, *cvalue;
115                 int             crit;
116         case 'E': /* modify controls */
117                 if( protocol == LDAP_VERSION2 ) {
118                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
119                                 prog, protocol );
120                         exit( EXIT_FAILURE );
121                 }
122
123                 /* should be extended to support comma separated list of
124                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
125                  */
126
127                 crit = 0;
128                 cvalue = NULL;
129                 if( optarg[0] == '!' ) {
130                         crit = 1;
131                         optarg++;
132                 }
133
134                 control = ber_strdup( optarg );
135                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
136                         *cvalue++ = '\0';
137                 }
138                 fprintf( stderr, _("Invalid modify control name: %s\n"), control );
139                 usage();
140 #endif
141
142         case 'a':       /* add */
143             ldapadd = 1;
144             break;
145
146         case 'F':       /* force all changes records to be used */
147             force = 1;
148             break;
149
150         case 'r':       /* replace (obsolete) */
151                 break;
152
153         case 'S':       /* skipped modifications to file */
154                 if( rejfile != NULL ) {
155                         fprintf( stderr, _("%s: -S previously specified\n"), prog );
156                         exit( EXIT_FAILURE );
157                 }
158                 rejfile = ber_strdup( optarg );
159                 break;
160
161         default:
162                 return 0;
163         }
164         return 1;
165 }
166
167
168 int
169 main( int argc, char **argv )
170 {
171     char                *rbuf, *start, *rejbuf = NULL;
172     FILE                *fp, *rejfp;
173         char            *matched_msg, *error_msg;
174         int             rc, retval;
175         int count, len;
176
177     tool_init();
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"), prog, dn);
381                 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
382                 free( dn );
383                 ber_memfree( type );
384                 ber_memfree( val.bv_val );
385                 return( 0 );
386             }
387
388             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
389 #ifdef LIBERAL_CHANGETYPE_MODOP
390                 /* trim trailing spaces (and log warning ...) */
391
392                 int icnt;
393                 for ( icnt = val.bv_len; --icnt > 0; ) {
394                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
395                         break;
396                     }
397                 }
398
399                 if ( ++icnt != val.bv_len ) {
400                     fprintf( stderr, _("%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
401                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
402                     val.bv_val[icnt] = '\0';
403                 }
404 #endif /* LIBERAL_CHANGETYPE_MODOP */
405
406                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
407                         new_entry = 0;
408                         expect_modop = 1;
409                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
410                         new_entry = 1;
411                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
412                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
413                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
414                 {
415                     expect_newrdn = 1;
416                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
417                     got_all = delete_entry = 1;
418                 } else {
419                     fprintf( stderr,
420                             _("%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n"),
421                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
422                     rc = LDAP_PARAM_ERROR;
423                 }
424                 goto end_line;
425             } else if ( ldapadd ) {             /*  missing changetype => add */
426                 new_entry = 1;
427                 modop = LDAP_MOD_ADD;
428             } else {
429                 expect_modop = 1;       /* missing changetype => modify */
430             }
431         }
432
433         if ( expect_modop ) {
434 #ifdef LIBERAL_CHANGETYPE_MODOP
435             /* trim trailing spaces (and log warning ...) */
436             
437             int icnt;
438             for ( icnt = val.bv_len; --icnt > 0; ) {
439                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
440                     break;
441                 }
442             }
443             
444             if ( ++icnt != val.bv_len ) {
445                 fprintf( stderr, _("%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
446                         prog, type, val.bv_val, linenum, dn );
447                 val.bv_val[icnt] = '\0';
448             }
449 #endif /* LIBERAL_CHANGETYPE_MODOP */
450
451             expect_modop = 0;
452             expect_sep = 1;
453             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
454                 modop = LDAP_MOD_ADD;
455                 goto end_line;
456             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
457                 modop = LDAP_MOD_REPLACE;
458                 addmodifyop( &pmods, modop, val.bv_val, NULL );
459                 goto end_line;
460             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
461                 modop = LDAP_MOD_DELETE;
462                 addmodifyop( &pmods, modop, val.bv_val, NULL );
463                 goto end_line;
464             } else {    /* no modify op:  use default */
465                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
466             }
467         }
468
469         if ( expect_newrdn ) {
470             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
471                         if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
472                     perror( "strdup" );
473                     exit( EXIT_FAILURE );
474                 }
475                 expect_deleteoldrdn = 1;
476                 expect_newrdn = 0;
477             } else {
478                 fprintf( stderr, _("%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n"),
479                         prog, T_NEWRDNSTR, type, linenum, dn );
480                 rc = LDAP_PARAM_ERROR;
481             }
482         } else if ( expect_deleteoldrdn ) {
483             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
484                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
485                 expect_deleteoldrdn = 0;
486                 expect_newsup = 1;
487                 got_all = 1;
488             } else {
489                 fprintf( stderr, _("%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n"),
490                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
491                 rc = LDAP_PARAM_ERROR;
492             }
493         } else if ( expect_newsup ) {
494             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
495                 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
496                     perror( "strdup" );
497                     exit( EXIT_FAILURE );
498                 }
499                 expect_newsup = 0;
500             } else {
501                 fprintf( stderr, _("%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n"),
502                         prog, T_NEWSUPSTR, type, linenum, dn );
503                 rc = LDAP_PARAM_ERROR;
504             }
505         } else if ( got_all ) {
506             fprintf( stderr,
507                     _("%s: extra lines at end (line %d of entry \"%s\")\n"),
508                     prog, linenum, dn );
509             rc = LDAP_PARAM_ERROR;
510         } else {
511                 addmodifyop( &pmods, modop, type, &val );
512         }
513
514 end_line:
515         ber_memfree( type );
516         ber_memfree( val.bv_val );
517     }
518
519         if( linenum == 0 ) {
520                 return 0;
521         }
522
523         if( version && linenum == 1 ) {
524                 return 0;
525         }
526
527     /* If default controls are set (as with -M option) and controls are
528        specified in the LDIF file, we must add the default controls to
529        the list of controls sent with the ldap operation.
530     */
531     if ( rc == 0 ) {
532         if (pctrls) {
533             LDAPControl **defctrls = NULL;   /* Default server controls */
534             LDAPControl **newctrls = NULL;
535             ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
536             if (defctrls) {
537                 int npc=0;                  /* Number of LDIF controls */
538                 int ndefc=0;                /* Number of default controls */
539                 while (pctrls[npc])         /* Count LDIF controls */
540                     npc++; 
541                 while (defctrls[ndefc])     /* Count default controls */
542                     ndefc++;
543                 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
544                 if (newctrls == NULL)
545                     rc = LDAP_NO_MEMORY;
546                 else {
547                     int i;
548                     pctrls = newctrls;
549                     for (i=npc; i<npc+ndefc; i++) {
550                         pctrls[i] = ldap_control_dup(defctrls[i-npc]);
551                         if (pctrls[i] == NULL) {
552                             rc = LDAP_NO_MEMORY;
553                             break;
554                         }
555                     }
556                     pctrls[npc+ndefc] = NULL;
557                 }
558                 ldap_controls_free(defctrls);  /* Must be freed by library */
559             }
560         }
561     }
562
563
564     if ( rc == 0 ) {
565         if ( delete_entry ) {
566             rc = dodelete( dn, pctrls );
567         } else if ( newrdn != NULL ) {
568             rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
569         } else {
570             rc = domodify( dn, pmods, pctrls, new_entry );
571         }
572
573         if ( rc == LDAP_SUCCESS ) {
574             rc = 0;
575         }
576     }
577
578     if ( dn != NULL ) {
579         free( dn );
580     }
581     if ( newrdn != NULL ) {
582         free( newrdn );
583     }
584     if ( pmods != NULL ) {
585         ldap_mods_free( pmods, 1 );
586     }
587
588     if (pctrls != NULL) {
589         ldap_controls_free( pctrls );
590     }
591
592     return( rc );
593 }
594
595 /* Parse an LDIF control line of the form
596       control:  oid  [true/false]  [: value]              or
597       control:  oid  [true/false]  [:: base64-value]      or
598       control:  oid  [true/false]  [:< url]
599    The control is added to the list of controls in *ppctrls.
600 */      
601 static int
602 parse_ldif_control( char *line, 
603                     LDAPControl ***ppctrls )
604 {
605     char *oid = NULL;
606     int criticality = 0;   /* Default is false if not present */
607     char *type=NULL;
608     char *val = NULL;
609     ber_len_t value_len = 0;
610     int i, rc=0;
611     char *s, *oidStart, *pcolon;
612     LDAPControl *newctrl = NULL;
613     LDAPControl **pctrls = NULL;
614
615     if (ppctrls) {
616         pctrls = *ppctrls;
617     }
618     s = line + strlen(T_CONTROL_STR);  /* Skip over "control" */
619     pcolon = s;                        /* Save this position for later */
620     if (*s++ != ':')                   /* Make sure colon follows */
621         return ( LDAP_PARAM_ERROR );
622     while (*s && isspace((unsigned char)*s))
623                 s++;                           /* Skip white space before OID */
624
625     /* OID should come next. Validate and extract it. */
626     if (*s == 0)
627         return ( LDAP_PARAM_ERROR );
628     oidStart = s;
629     while (isdigit((unsigned char)*s) || *s == '.')
630                 s++;                           /* OID should be digits or . */
631     if (s == oidStart) 
632         return ( LDAP_PARAM_ERROR );   /* OID was not present */
633     if (*s) {                          /* End of OID should be space or NULL */
634         if (!isspace((unsigned char)*s))
635             return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
636         *s++ = 0;                    /* Replace space with null to terminate */
637     }
638
639     
640     oid = ber_strdup(oidStart);
641     if (oid == NULL)
642         return ( LDAP_NO_MEMORY );
643
644     /* Optional Criticality field is next. */
645     while (*s && isspace((unsigned char)*s))
646                 s++;                         /* Skip white space before criticality */
647     if (strncasecmp(s, "true", 4) == 0) {
648         criticality = 1;
649         s += 4;
650     } 
651     else if (strncasecmp(s, "false", 5) == 0) {
652         criticality = 0;
653         s += 5;
654     }
655
656     /* Optional value field is next */
657     while (*s && isspace((unsigned char)*s))
658                 s++;                         /* Skip white space before value */
659     if (*s) {
660         if (*s != ':') {           /* If value is present, must start with : */
661             rc = LDAP_PARAM_ERROR;
662             goto cleanup;
663         }
664
665         /* Shift value down over OID and criticality so it's in the form
666              control: value
667              control:: base64-value
668              control:< url
669            Then we can use ldif_parse_line to extract and decode the value
670         */
671         while ( (*pcolon++ = *s++) != 0)     /* Shift value */
672             ;
673         rc = ldif_parse_line(line, &type, &val, &value_len);
674         if (type)  ber_memfree(type);   /* Don't need this field*/
675         if (rc < 0) {
676             rc = LDAP_PARAM_ERROR;
677             goto cleanup;
678         }
679     }
680
681     /* Create a new LDAPControl structure. */
682     newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
683     if ( newctrl == NULL ) {
684         rc = LDAP_NO_MEMORY;
685         goto cleanup;
686     }
687     newctrl->ldctl_oid = oid;
688     oid = NULL;
689     newctrl->ldctl_iscritical = criticality;
690     newctrl->ldctl_value.bv_len = value_len;
691     newctrl->ldctl_value.bv_val = val;
692     val = NULL;
693
694     /* Add the new control to the passed-in list of controls. */
695     i = 0;
696     if (pctrls) {
697         while ( pctrls[i] )      /* Count the # of controls passed in */
698             i++;
699     }
700     /* Allocate 1 more slot for the new control and 1 for the NULL. */
701     pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
702     if (pctrls == NULL) {
703         rc = LDAP_NO_MEMORY;
704         goto cleanup;
705     }
706     pctrls[i] = newctrl;
707     newctrl = NULL;
708     pctrls[i+1] = NULL;
709     *ppctrls = pctrls;
710
711 cleanup:
712     if (newctrl) {
713         if (newctrl->ldctl_oid)
714             ber_memfree(newctrl->ldctl_oid);
715         if (newctrl->ldctl_value.bv_val)
716             ber_memfree(newctrl->ldctl_value.bv_val);
717         ber_memfree(newctrl);
718     }
719     if (val)
720         ber_memfree(val);
721     if (oid)
722         ber_memfree(oid);
723
724     return( rc );
725 }
726
727
728 static void
729 addmodifyop(
730         LDAPMod ***pmodsp,
731         int modop,
732         const char *attr,
733         struct berval *val )
734 {
735         LDAPMod         **pmods;
736         int                     i, j;
737
738         pmods = *pmodsp;
739         modop |= LDAP_MOD_BVALUES;
740
741         i = 0;
742         if ( pmods != NULL ) {
743                 for ( ; pmods[ i ] != NULL; ++i ) {
744                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
745                                 pmods[ i ]->mod_op == modop )
746                         {
747                                 break;
748                         }
749                 }
750         }
751
752         if ( pmods == NULL || pmods[ i ] == NULL ) {
753                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
754                         sizeof( LDAPMod * ))) == NULL )
755                 {
756                         perror( "realloc" );
757                         exit( EXIT_FAILURE );
758                 }
759
760                 *pmodsp = pmods;
761                 pmods[ i + 1 ] = NULL;
762
763                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
764                 if ( pmods[ i ] == NULL ) {
765                         perror( "calloc" );
766                         exit( EXIT_FAILURE );
767                 }
768
769                 pmods[ i ]->mod_op = modop;
770                 pmods[ i ]->mod_type = ber_strdup( attr );
771                 if ( pmods[ i ]->mod_type == NULL ) {
772                         perror( "strdup" );
773                         exit( EXIT_FAILURE );
774                 }
775         }
776
777         if ( val != NULL ) {
778                 j = 0;
779                 if ( pmods[ i ]->mod_bvalues != NULL ) {
780                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
781                                 /* Empty */;
782                         }
783                 }
784
785                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
786                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
787                 if ( pmods[ i ]->mod_bvalues == NULL ) {
788                         perror( "ber_realloc" );
789                         exit( EXIT_FAILURE );
790                 }
791
792                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
793                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
794                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
795                         perror( "ber_bvdup" );
796                         exit( EXIT_FAILURE );
797                 }
798         }
799 }
800
801
802 static int
803 domodify(
804         const char *dn,
805         LDAPMod **pmods,
806     LDAPControl **pctrls,
807         int newentry )
808 {
809     int                 i, j, k, notascii, op;
810     struct berval       *bvp;
811
812     if ( pmods == NULL ) {
813         fprintf( stderr, _("%s: no attributes to change or add (entry=\"%s\")\n"),
814                 prog, dn );
815         return( LDAP_PARAM_ERROR );
816     } 
817
818     for ( i = 0; pmods[ i ] != NULL; ++i ) {
819         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
820         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
821                 fprintf( stderr,
822                         _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
823                         prog, pmods[i]->mod_type, dn );
824                 return LDAP_PARAM_ERROR;
825         }
826     }
827
828     if ( verbose ) {
829         for ( i = 0; pmods[ i ] != NULL; ++i ) {
830             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
831             printf( "%s %s:\n",
832                         op == LDAP_MOD_REPLACE ? _("replace") : op == LDAP_MOD_ADD
833                                 ?  _("add") : _("delete"),
834                         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