]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
fc737a4862c62c21910cdf17c3378bff998f996e
[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_MODOPINCREMENTSTR     "increment"
62 #define T_MODSEPSTR             "-"
63 #define T_NEWRDNSTR             "newrdn"
64 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
65 #define T_NEWSUPSTR             "newsuperior"
66
67
68 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
69 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
70 static void addmodifyop LDAP_P((
71         LDAPMod ***pmodsp, int modop,
72         const char *attr,
73         struct berval *value ));
74 static int domodify LDAP_P((
75         const char *dn,
76         LDAPMod **pmods,
77     LDAPControl **pctrls,
78         int newentry ));
79 static int dodelete LDAP_P((
80         const char *dn,
81     LDAPControl **pctrls ));
82 static int dorename LDAP_P((
83         const char *dn,
84         const char *newrdn,
85         const char *newsup,
86         int deleteoldrdn,
87     LDAPControl **pctrls ));
88 static char *read_one_record LDAP_P(( FILE *fp ));
89
90 void
91 usage( void )
92 {
93     fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
94     fprintf( stderr, _("usage: %s [options]\n"), prog);
95     fprintf( stderr, _("        The list of desired operations are read from stdin or from the file\n"));
96     fprintf( stderr, _("        specified by \"-f file\".\n"));
97     fprintf( stderr, _("Add or modify options:\n"));
98     fprintf( stderr, _("  -a         add values (%s)\n"),
99                 (ldapadd ? _("default") : _("default is to replace")));
100     fprintf( stderr, _("  -F         force all changes records to be used\n"));
101     fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
102     tool_common_usage();
103     exit( EXIT_FAILURE );
104 }
105
106
107 const char options[] = "aFrS:"
108         "cCd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
109
110 int
111 handle_private_option( int i )
112 {
113         switch ( i ) {
114 #if 0
115                 char    *control, *cvalue;
116                 int             crit;
117         case 'E': /* modify controls */
118                 if( protocol == LDAP_VERSION2 ) {
119                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
120                                 prog, protocol );
121                         exit( EXIT_FAILURE );
122                 }
123
124                 /* should be extended to support comma separated list of
125                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
126                  */
127
128                 crit = 0;
129                 cvalue = NULL;
130                 if( optarg[0] == '!' ) {
131                         crit = 1;
132                         optarg++;
133                 }
134
135                 control = ber_strdup( optarg );
136                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
137                         *cvalue++ = '\0';
138                 }
139                 fprintf( stderr, _("Invalid modify control name: %s\n"), control );
140                 usage();
141 #endif
142
143         case 'a':       /* add */
144             ldapadd = 1;
145             break;
146
147         case 'F':       /* force all changes records to be used */
148             force = 1;
149             break;
150
151         case 'r':       /* replace (obsolete) */
152                 break;
153
154         case 'S':       /* skipped modifications to file */
155                 if( rejfile != NULL ) {
156                         fprintf( stderr, _("%s: -S previously specified\n"), prog );
157                         exit( EXIT_FAILURE );
158                 }
159                 rejfile = ber_strdup( optarg );
160                 break;
161
162         default:
163                 return 0;
164         }
165         return 1;
166 }
167
168
169 int
170 main( int argc, char **argv )
171 {
172         char            *rbuf, *start, *rejbuf = NULL;
173         FILE            *fp, *rejfp;
174         char            *matched_msg, *error_msg;
175         int             rc, retval;
176         int count, len;
177
178         tool_init();
179         prog = lutil_progname( "ldapmodify", argc, argv );
180
181         /* strncmp instead of strcmp since NT binaries carry .exe extension */
182         ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
183
184         /* Print usage when no parameters */
185         if( argc < 2 ) usage();
186
187         tool_args( argc, argv );
188
189         if ( argc != optind ) 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 ) 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                 tool_bind( ld );
224         }
225
226     rc = 0;
227
228         if ( assertion || authzid || manageDSAit || noop ) {
229                 tool_server_controls( ld, NULL, 0 );
230         }
231
232         count = 0;
233         retval = 0;
234         while (( rc == 0 || contoper ) &&
235                 ( rbuf = read_one_record( fp )) != NULL )
236         {
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 ) 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                                 }
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                                 }
271                                 ldap_memfree( error_msg );
272                         }
273                         fprintf( rejfp, "\n%s\n", rejbuf );
274                 }
275
276                 if (rejfp) free( rejbuf );
277                 free( rbuf );
278         }
279
280         if ( !not ) {
281                 ldap_unbind( ld );
282         }
283
284         if ( rejfp != NULL ) {
285                 fclose( rejfp );
286         }
287
288         return( retval );
289 }
290
291
292 static int
293 process_ldif_rec( char *rbuf, int count )
294 {
295     char        *line, *dn, *type, *newrdn, *newsup, *p;
296     int         rc, linenum, modop, replicaport;
297     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
298     int         expect_deleteoldrdn, deleteoldrdn;
299     int         saw_replica, use_record, new_entry, delete_entry, got_all;
300     LDAPMod     **pmods;
301         int version;
302         struct berval val;
303     LDAPControl **pctrls;
304
305     new_entry = ldapadd;
306
307     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
308     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
309         expect_sep = expect_ct = 0;
310     linenum = 0;
311         version = 0;
312     deleteoldrdn = 1;
313     use_record = force;
314     pmods = NULL;
315     pctrls = NULL;
316     dn = newrdn = newsup = NULL;
317
318     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
319         ++linenum;
320
321         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
322             expect_sep = 0;
323             expect_ct = 1;
324             continue;
325         }
326         
327         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
328             fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
329                     prog, linenum, dn == NULL ? "" : dn );
330             rc = LDAP_PARAM_ERROR;
331             break;
332         }
333
334         if ( dn == NULL ) {
335             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
336                 ++saw_replica;
337                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
338                     replicaport = 0;
339                 } else {
340                     *p++ = '\0';
341                     replicaport = atoi( p );
342                 }
343                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
344                         replicaport == ldapport ) {
345                     use_record = 1;
346                 }
347             } else if ( count == 1 && linenum == 1 && 
348                         strcasecmp( type, T_VERSION_STR ) == 0 )
349                 {
350                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
351                         fprintf( stderr, _("%s: invalid version %s, line %d (ignored)\n"),
352                                 prog, val.bv_val, linenum );
353                         }
354                         version++;
355
356             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
357                 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
358                     perror( "strdup" );
359                     exit( EXIT_FAILURE );
360                 }
361                 expect_ct = 1;
362             }
363             goto end_line;      /* skip all lines until we see "dn:" */
364         }
365
366         if ( expect_ct ) {
367         
368         /* Check for "control" tag after dn and before changetype. */
369         if (strcasecmp(type, T_CONTROL_STR) == 0) {
370             /* Parse and add it to the list of controls */
371             rc = parse_ldif_control( line, &pctrls );
372             if (rc != 0) {
373                         fprintf( stderr, _("%s: Error processing %s line, line %d: %s\n"),
374                                 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
375             }
376             goto end_line;
377         }
378         
379             expect_ct = 0;
380             if ( !use_record && saw_replica ) {
381                 printf(_("%s: skipping change record for entry: %s\n"), prog, dn);
382                 printf(_("\t(LDAP host/port does not match replica: lines)\n"));
383                 free( dn );
384                 ber_memfree( type );
385                 ber_memfree( val.bv_val );
386                 return( 0 );
387             }
388
389             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
390 #ifdef LIBERAL_CHANGETYPE_MODOP
391                 /* trim trailing spaces (and log warning ...) */
392
393                 int icnt;
394                 for ( icnt = val.bv_len; --icnt > 0; ) {
395                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
396                         break;
397                     }
398                 }
399
400                 if ( ++icnt != val.bv_len ) {
401                     fprintf( stderr, _("%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
402                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
403                     val.bv_val[icnt] = '\0';
404                 }
405 #endif /* LIBERAL_CHANGETYPE_MODOP */
406
407                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
408                         new_entry = 0;
409                         expect_modop = 1;
410                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
411                         new_entry = 1;
412                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
413                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
414                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
415                 {
416                     expect_newrdn = 1;
417                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
418                     got_all = delete_entry = 1;
419                 } else {
420                     fprintf( stderr,
421                             _("%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n"),
422                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
423                     rc = LDAP_PARAM_ERROR;
424                 }
425                 goto end_line;
426             } else if ( ldapadd ) {             /*  missing changetype => add */
427                 new_entry = 1;
428                 modop = LDAP_MOD_ADD;
429             } else {
430                 expect_modop = 1;       /* missing changetype => modify */
431             }
432         }
433
434         if ( expect_modop ) {
435 #ifdef LIBERAL_CHANGETYPE_MODOP
436             /* trim trailing spaces (and log warning ...) */
437             
438             int icnt;
439             for ( icnt = val.bv_len; --icnt > 0; ) {
440                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
441                     break;
442                 }
443             }
444             
445             if ( ++icnt != val.bv_len ) {
446                 fprintf( stderr, _("%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
447                         prog, type, val.bv_val, linenum, dn );
448                 val.bv_val[icnt] = '\0';
449             }
450 #endif /* LIBERAL_CHANGETYPE_MODOP */
451
452             expect_modop = 0;
453             expect_sep = 1;
454             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
455                 modop = LDAP_MOD_ADD;
456                 goto end_line;
457             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
458                 modop = LDAP_MOD_REPLACE;
459                 addmodifyop( &pmods, modop, val.bv_val, NULL );
460                 goto end_line;
461             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
462                 modop = LDAP_MOD_DELETE;
463                 addmodifyop( &pmods, modop, val.bv_val, NULL );
464                 goto end_line;
465             } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
466                 modop = LDAP_MOD_INCREMENT;
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",
837                         op == LDAP_MOD_REPLACE ? _("replace") :
838                                 op == LDAP_MOD_ADD ?  _("add") :
839                                         op == LDAP_MOD_INCREMENT ?  _("increment") :
840                                                 op == LDAP_MOD_DELETE ?  _("delete") :
841                                                         _("unknown"),
842                         pmods[ i ]->mod_type );
843             if ( pmods[ i ]->mod_bvalues != NULL ) {
844                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
845                     bvp = pmods[ i ]->mod_bvalues[ j ];
846                     notascii = 0;
847                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
848                         if ( !isascii( bvp->bv_val[ k ] )) {
849                             notascii = 1;
850                             break;
851                         }
852                     }
853                     if ( notascii ) {
854                         printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
855                     } else {
856                         printf( "\t%s\n", bvp->bv_val );
857                     }
858                 }
859             }
860         }
861     }
862
863     if ( newentry ) {
864         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
865     } else {
866         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
867     }
868
869     if ( !not ) {
870         if ( newentry ) {
871             i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
872         } else {
873             i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
874         }
875         if ( i != LDAP_SUCCESS ) {
876                 /* print error message about failed update including DN */
877                 fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
878                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
879         } else if ( verbose ) {
880             printf( _("modify complete\n") );
881         }
882     } else {
883         i = LDAP_SUCCESS;
884     }
885
886     putchar( '\n' );
887
888     return( i );
889 }
890
891
892 static int
893 dodelete(
894         const char *dn,
895     LDAPControl **pctrls )
896 {
897     int rc;
898
899     printf( _("%sdeleting entry \"%s\"\n"), not ? "!" : "", dn );
900     if ( !not ) {
901         if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
902                 fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
903                 ldap_perror( ld, "ldap_delete" );
904         } else if ( verbose ) {
905             printf( _("delete complete") );
906         }
907     } else {
908         rc = LDAP_SUCCESS;
909     }
910
911     putchar( '\n' );
912
913     return( rc );
914 }
915
916
917 static int
918 dorename(
919         const char *dn,
920         const char *newrdn,
921         const char* newsup,
922         int deleteoldrdn,
923     LDAPControl **pctrls )
924 {
925     int rc;
926
927
928     printf( _("%smodifying rdn of entry \"%s\"\n"), not ? "!" : "", dn );
929     if ( verbose ) {
930         printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
931                 newrdn, deleteoldrdn ? _("do not ") : "" );
932     }
933     if ( !not ) {
934         if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
935                 != LDAP_SUCCESS ) {
936                 fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
937                 ldap_perror( ld, "ldap_modrdn" );
938         } else {
939             printf( _("modrdn completed\n") );
940         }
941     } else {
942         rc = LDAP_SUCCESS;
943     }
944
945     putchar( '\n' );
946
947     return( rc );
948 }
949
950
951 static char *
952 read_one_record( FILE *fp )
953 {
954     char        *buf, line[ LDAPMOD_MAXLINE ];
955     int         lcur, lmax;
956
957     lcur = lmax = 0;
958     buf = NULL;
959
960     while ( fgets( line, sizeof(line), fp ) != NULL ) {
961         int len = strlen( line );
962
963                 if( len < 2 || ( len == 2 && *line == '\r' )) {
964                         if( buf == NULL ) {
965                                 continue;
966                         } else {
967                                 break;
968                         }
969                 }
970
971                 if ( lcur + len + 1 > lmax ) {
972                         lmax = LDAPMOD_MAXLINE
973                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
974
975                         if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
976                                 perror( "realloc" );
977                                 exit( EXIT_FAILURE );
978                         }
979                 }
980
981                 strcpy( buf + lcur, line );
982                 lcur += len;
983     }
984
985     return( buf );
986 }
987
988