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