]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Parse server controls on each response, and print them.
[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-2007 The OpenLDAP Foundation.
6  * Portions Copyright 2006 Howard Chu.
7  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
8  * Portions Copyright 1998-2001 Net Boolean Incorporated.
9  * Portions Copyright 2001-2003 IBM Corporation.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted only as authorized by the OpenLDAP
14  * Public License.
15  *
16  * A copy of this license is available in the file LICENSE in the
17  * top-level directory of the distribution or, alternatively, at
18  * <http://www.OpenLDAP.org/license.html>.
19  */
20 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms are permitted
24  * provided that this notice is preserved and that due credit is given
25  * to the University of Michigan at Ann Arbor.  The name of the
26  * University may not be used to endorse or promote products derived
27  * from this software without specific prior written permission.  This
28  * software is provided ``as is'' without express or implied warranty.
29  */
30 /* ACKNOWLEDGEMENTS:
31  * This work was originally developed by the University of Michigan
32  * (as part of U-MICH LDAP).  Additional significant contributors
33  * include:
34  *   Kurt D. Zeilenga
35  *   Norbert Klasen
36  *   Howard Chu
37  */
38
39 #include "portable.h"
40
41 #include <stdio.h>
42
43 #include <ac/stdlib.h>
44 #include <ac/ctype.h>
45 #include <ac/string.h>
46 #include <ac/unistd.h>
47 #include <ac/socket.h>
48 #include <ac/time.h>
49
50 #ifdef HAVE_SYS_STAT_H
51 #include <sys/stat.h>
52 #endif
53
54 #ifdef HAVE_SYS_FILE_H
55 #include <sys/file.h>
56 #endif
57 #ifdef HAVE_FCNTL_H
58 #include <fcntl.h>
59 #endif
60
61 #include <ldap.h>
62
63 #include "lutil.h"
64 #include "lutil_ldap.h"
65 #include "ldif.h"
66 #include "ldap_defaults.h"
67 #include "ldap_log.h"
68 #include "ldap_pvt.h"
69 #include "lber_pvt.h"
70
71 #include "common.h"
72
73 static int      ldapadd, force = 0;
74 static char *rejfile = NULL;
75 static LDAP     *ld = NULL;
76
77 #define M_SEP   0x7f
78
79 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
80 static struct berval BV_VERSION = BER_BVC("version");
81 static struct berval BV_REPLICA = BER_BVC("replica");
82 static struct berval BV_DN = BER_BVC("dn");
83 static struct berval BV_CONTROL = BER_BVC("control");
84 static struct berval BV_CHANGETYPE = BER_BVC("changetype");
85 static struct berval BV_ADDCT = BER_BVC("add");
86 static struct berval BV_MODIFYCT = BER_BVC("modify");
87 static struct berval BV_DELETECT = BER_BVC("delete");
88 static struct berval BV_MODRDNCT = BER_BVC("modrdn");
89 static struct berval BV_MODDNCT = BER_BVC("moddn");
90 static struct berval BV_RENAMECT = BER_BVC("rename");
91 static struct berval BV_MODOPADD = BER_BVC("add");
92 static struct berval BV_MODOPREPLACE = BER_BVC("replace");
93 static struct berval BV_MODOPDELETE = BER_BVC("delete");
94 static struct berval BV_MODOPINCREMENT = BER_BVC("increment");
95 static struct berval BV_MODSEP = BER_BVC("-");
96 static struct berval BV_NEWRDN = BER_BVC("newrdn");
97 static struct berval BV_DELETEOLDRDN = BER_BVC("deleteoldrdn");
98 static struct berval BV_NEWSUP = BER_BVC("newsuperior");
99
100 #define BVICMP(a,b)     ((a)->bv_len != (b)->bv_len ? \
101         (a)->bv_len - (b)->bv_len : strcasecmp((a)->bv_val, (b)->bv_val))
102
103 static int process_ldif_rec LDAP_P(( char *rbuf, int lineno ));
104 static int parse_ldif_control LDAP_P(( struct berval *val, LDAPControl ***pctrls ));
105 static int domodify LDAP_P((
106         const char *dn,
107         LDAPMod **pmods,
108         LDAPControl **pctrls,
109         int newentry ));
110 static int dodelete LDAP_P((
111         const char *dn,
112         LDAPControl **pctrls ));
113 static int dorename LDAP_P((
114         const char *dn,
115         const char *newrdn,
116         const char *newsup,
117         int deleteoldrdn,
118         LDAPControl **pctrls ));
119 static int process_response(
120         LDAP *ld,
121         int msgid,
122         int res,
123         const char *dn );
124
125 #ifdef LDAP_X_TXN
126 static int txn = 0;
127 static int txnabort = 0;
128 struct berval *txn_id = NULL;
129 #endif
130
131 void
132 usage( void )
133 {
134         fprintf( stderr, _("Add or modify entries from an LDAP server\n\n"));
135         fprintf( stderr, _("usage: %s [options]\n"), prog);
136         fprintf( stderr, _("    The list of desired operations are read from stdin"
137                 " or from the file\n"));
138         fprintf( stderr, _("    specified by \"-f file\".\n"));
139         fprintf( stderr, _("Add or modify options:\n"));
140         fprintf( stderr, _("  -a         add values (%s)\n"),
141                 (ldapadd ? _("default") : _("default is to replace")));
142         fprintf( stderr, _("  -E [!]ext=extparam        modify extensions"
143                 " (! indicate s criticality)\n"));
144 #ifdef LDAP_X_TXN
145         fprintf( stderr,
146                 _("             [!]txn=<commit|abort>         (transaction)\n"));
147 #endif
148         fprintf( stderr, _("  -F         force all changes records to be used\n"));
149         fprintf( stderr, _("  -S file    write skipped modifications to `file'\n"));
150
151         tool_common_usage();
152         exit( EXIT_FAILURE );
153 }
154
155
156 const char options[] = "aE:FrS:"
157         "cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
158
159 int
160 handle_private_option( int i )
161 {
162         char    *control, *cvalue;
163         int             crit;
164
165         switch ( i ) {
166         case 'E': /* modify extensions */
167                 if( protocol == LDAP_VERSION2 ) {
168                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
169                                 prog, protocol );
170                         exit( EXIT_FAILURE );
171                 }
172
173                 /* should be extended to support comma separated list of
174                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
175                  */
176
177                 crit = 0;
178                 cvalue = NULL;
179                 if( optarg[0] == '!' ) {
180                         crit = 1;
181                         optarg++;
182                 }
183
184                 control = ber_strdup( optarg );
185                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
186                         *cvalue++ = '\0';
187                 }
188
189 #ifdef LDAP_X_TXN
190                 if( strcasecmp( control, "txn" ) == 0 ) {
191                         /* Transaction */
192                         if( txn ) {
193                                 fprintf( stderr,
194                                         _("txn control previously specified\n"));
195                                 exit( EXIT_FAILURE );
196                         }
197                         if( cvalue != NULL ) {
198                                 if( strcasecmp( cvalue, "abort" ) == 0 ) {
199                                         txnabort=1;
200                                 } else if( strcasecmp( cvalue, "commit" ) != 0 ) {
201                                         fprintf( stderr, _("Invalid value for txn control, %s\n"),
202                                                 cvalue );
203                                         exit( EXIT_FAILURE );
204                                 }
205                         }
206
207                         txn = 1 + crit;
208                 } else
209 #endif
210                 {
211                         fprintf( stderr, _("Invalid modify extension name: %s\n"),
212                                 control );
213                         usage();
214                 }
215                 break;
216
217         case 'a':       /* add */
218                 ldapadd = 1;
219                 break;
220
221         case 'F':       /* force all changes records to be used */
222                 force = 1;
223                 break;
224
225         case 'r':       /* replace (obsolete) */
226                 break;
227
228         case 'S':       /* skipped modifications to file */
229                 if( rejfile != NULL ) {
230                         fprintf( stderr, _("%s: -S previously specified\n"), prog );
231                         exit( EXIT_FAILURE );
232                 }
233                 rejfile = ber_strdup( optarg );
234                 break;
235
236         default:
237                 return 0;
238         }
239         return 1;
240 }
241
242
243 int
244 main( int argc, char **argv )
245 {
246         char            *rbuf = NULL, *rejbuf = NULL;
247         FILE            *rejfp;
248         struct LDIFFP *ldiffp, ldifdummy = {0};
249         char            *matched_msg, *error_msg;
250         int             rc, retval;
251         int             len;
252         int             i = 0;
253         int             lineno, nextline = 0, lmax = 0;
254         LDAPControl     c[1];
255
256         prog = lutil_progname( "ldapmodify", argc, argv );
257
258         /* strncmp instead of strcmp since NT binaries carry .exe extension */
259         ldapadd = ( strncasecmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
260
261         tool_init( ldapadd ? TOOL_ADD : TOOL_MODIFY );
262
263         tool_args( argc, argv );
264
265         if ( argc != optind ) usage();
266
267         if ( rejfile != NULL ) {
268                 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
269                         perror( rejfile );
270                         return( EXIT_FAILURE );
271                 }
272         } else {
273                 rejfp = NULL;
274         }
275
276         if ( infile != NULL ) {
277                 if (( ldiffp = ldif_open( infile, "r" )) == NULL ) {
278                         perror( infile );
279                         return( EXIT_FAILURE );
280                 }
281         } else {
282                 ldifdummy.fp = stdin;
283                 ldiffp = &ldifdummy;
284         }
285
286         if ( debug ) ldif_debug = debug;
287
288         ld = tool_conn_setup( dont, 0 );
289
290         if ( !dont ) {
291                 if ( pw_file || want_bindpw ) {
292                         if ( pw_file ) {
293                                 rc = lutil_get_filed_password( pw_file, &passwd );
294                                 if( rc ) return EXIT_FAILURE;
295                         } else {
296                                 passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
297                                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
298                         }
299                 }
300                 tool_bind( ld );
301         }
302
303 #ifdef LDAP_X_TXN
304         if( txn ) {
305                 /* start transaction */
306                 rc = ldap_txn_start_s( ld, NULL, NULL, &txn_id );
307                 if( rc != LDAP_SUCCESS ) {
308                         tool_perror( "ldap_txn_start_s", rc, NULL, NULL, NULL, NULL );
309                         if( txn > 1 ) return EXIT_FAILURE;
310                         txn = 0;
311                 }
312         }
313 #endif
314
315         if ( 0
316 #ifdef LDAP_X_TXN
317                 || txn
318 #endif
319                 )
320         {
321 #ifdef LDAP_X_TXN
322                 if( txn ) {
323                         c[i].ldctl_oid = LDAP_CONTROL_X_TXN_SPEC;
324                         c[i].ldctl_value = *txn_id;
325                         c[i].ldctl_iscritical = 1;
326                         i++;
327                 }
328 #endif
329         }
330
331         tool_server_controls( ld, c, i );
332
333         rc = 0;
334         retval = 0;
335         lineno = 1;
336         while (( rc == 0 || contoper ) && ldif_read_record( ldiffp, &nextline,
337                 &rbuf, &lmax ))
338         {
339                 if ( rejfp ) {
340                         len = strlen( rbuf );
341                         if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
342                                 perror( "malloc" );
343                                 exit( EXIT_FAILURE );
344                         }
345                         memcpy( rejbuf, rbuf, len+1 );
346                 }
347
348                 rc = process_ldif_rec( rbuf, lineno );
349                 lineno = nextline+1;
350
351                 if ( rc ) retval = rc;
352                 if ( rc && rejfp ) {
353                         fprintf(rejfp, _("# Error: %s (%d)"), ldap_err2string(rc), rc);
354
355                         matched_msg = NULL;
356                         ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
357                         if ( matched_msg != NULL ) {
358                                 if ( *matched_msg != '\0' ) {
359                                         fprintf( rejfp, _(", matched DN: %s"), matched_msg );
360                                 }
361                                 ldap_memfree( matched_msg );
362                         }
363
364                         error_msg = NULL;
365                         ldap_get_option(ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, &error_msg);
366                         if ( error_msg != NULL ) {
367                                 if ( *error_msg != '\0' ) {
368                                         fprintf( rejfp, _(", additional info: %s"), error_msg );
369                                 }
370                                 ldap_memfree( error_msg );
371                         }
372                         fprintf( rejfp, "\n%s\n", rejbuf );
373                 }
374
375                 if (rejfp) ber_memfree( rejbuf );
376         }
377         ber_memfree( rbuf );
378
379 #ifdef LDAP_X_TXN
380         if( retval == 0 && txn ) {
381                 rc = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, NULL );
382                 if ( rc != LDAP_OPT_SUCCESS ) {
383                         fprintf( stderr, "Could not unset controls for ldap_txn_end\n");
384                 }
385
386                 /* create transaction */
387                 rc = ldap_txn_end_s( ld, !txnabort, txn_id, NULL, NULL, NULL );
388                 if( rc != LDAP_SUCCESS ) {
389                         tool_perror( "ldap_txn_end_s", rc, NULL, NULL, NULL, NULL );
390                         retval = rc;
391                 }
392         }
393 #endif
394
395         if ( !dont ) {
396                 tool_unbind( ld );
397         }
398
399         if ( rejfp != NULL ) {
400                 fclose( rejfp );
401         }
402
403         tool_destroy();
404         return( retval );
405 }
406
407
408 static int
409 process_ldif_rec( char *rbuf, int linenum )
410 {
411         char    *line, *dn, *newrdn, *newsup;
412         int             rc, modop, replicaport;
413         int             expect_modop, expect_sep;
414         int             deleteoldrdn;
415         int             saw_replica, use_record, new_entry, delete_entry, got_all;
416         LDAPMod **pmods, *lm = NULL;
417         int version;
418         LDAPControl **pctrls;
419         int i, j, k, lines, idn, nmods;
420         struct berval *btype, *vals, **bvl, bv;
421         char *freeval;
422         unsigned char *mops = NULL;
423
424         new_entry = ldapadd;
425
426         rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
427         expect_sep = 0;
428         version = 0;
429         deleteoldrdn = 1;
430         use_record = force;
431         pmods = NULL;
432         pctrls = NULL;
433         dn = newrdn = newsup = NULL;
434
435         lines = ldif_countlines( rbuf );
436         btype = ber_memcalloc( 1, (lines+1)*2*sizeof(struct berval)+lines );
437         if ( !btype )
438                 return LDAP_NO_MEMORY;
439
440         vals = btype+lines+1;
441         freeval = (char *)(vals+lines+1);
442         i = -1;
443
444         while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
445                 int freev;
446
447                 if ( *line == '\n' || *line == '\0' ) {
448                         break;
449                 }
450
451                 ++i;
452
453                 if ( line[0] == '-' && !line[1] ) {
454                         BER_BVZERO( btype+i );
455                         freeval[i] = 0;
456                         continue;
457                 }
458         
459                 if ( ( rc = ldif_parse_line2( line, btype+i, vals+i, &freev ) ) < 0 ) {
460                         fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
461                                 prog, linenum+i, dn == NULL ? "" : dn );
462                         rc = LDAP_PARAM_ERROR;
463                         break;
464                 }
465                 freeval[i] = freev;
466
467                 if ( dn == NULL ) {
468                         if ( !use_record && !BVICMP( btype+i, &BV_REPLICA )) {
469                                 char *p;
470                                 ++saw_replica;
471                                 if (( p = strchr( vals[i].bv_val, ':' )) == NULL ) {
472                                         replicaport = 0;
473                                 } else {
474                                         *p++ = '\0';
475                                         if ( lutil_atoi( &replicaport, p ) != 0 ) {
476                                                 fprintf( stderr, _("%s: unable to parse replica port \"%s\" (line %d) entry: \"%s\"\n"),
477                                                         prog, p, linenum+i, dn == NULL ? "" : dn );
478                                                 rc = LDAP_PARAM_ERROR;
479                                                 break;
480                                         }
481                                 }
482                                 if ( ldaphost != NULL &&
483                                         strcasecmp( vals[i].bv_val, ldaphost ) == 0 &&
484                                         replicaport == ldapport )
485                                 {
486                                         use_record = 1;
487                                 }
488                         } else if ( linenum+i == 1 && !BVICMP( btype+i, &BV_VERSION )) {
489                                 int     v;
490                                 if( vals[i].bv_len == 0 || lutil_atoi( &v, vals[i].bv_val) != 0 || v != 1 ) {
491                                         fprintf( stderr,
492                                                 _("%s: invalid version %s, line %d (ignored)\n"),
493                                                 prog, vals[i].bv_val, linenum );
494                                 }
495                                 version++;
496
497                         } else if ( !BVICMP( btype+i, &BV_DN )) {
498                                 dn = vals[i].bv_val;
499                                 idn = i;
500                                 if ( !use_record && saw_replica ) {
501                                         printf(_("%s: skipping change record for entry: %s at line %d\n"),
502                                                 prog, dn, linenum+i);
503                                         printf(_("\t(LDAP host/port does not match replica: lines)\n"));
504                                         rc = 0;
505                                         goto leave;
506                                 }
507                         }
508                         /* skip all lines until we see "dn:" */
509                 }
510         }
511
512         /* check to make sure there was a dn: line */
513         if ( !dn ) {
514                 rc = 0;
515                 goto leave;
516         }
517
518         lines = i+1;
519
520         if( lines == 0 ) {
521                 rc = 0;
522                 goto leave;
523         }
524
525         if( version && lines == 1 ) {
526                 rc = 0;
527                 goto leave;
528         }
529
530         i = idn+1;
531         /* Check for "control" tag after dn and before changetype. */
532         if (!BVICMP( btype+i, &BV_CONTROL)) {
533                 /* Parse and add it to the list of controls */
534                 rc = parse_ldif_control( vals+i, &pctrls );
535                 if (rc != 0) {
536                         fprintf( stderr,
537                                 _("%s: Error processing %s line, line %d: %s\n"),
538                                 prog, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
539                 }
540                 i++;
541                 if ( i>= lines ) {
542 short_input:
543                         fprintf( stderr,
544                                 _("%s: Expecting more input after %s line, line %d\n"),
545                                 prog, btype[i-1].bv_val, linenum+i );
546                         
547                         rc = LDAP_PARAM_ERROR;
548                         goto leave;
549                 }
550         }
551
552         /* Check for changetype */
553         if ( !BVICMP( btype+i, &BV_CHANGETYPE )) {
554 #ifdef LIBERAL_CHANGETYPE_MODOP
555                 /* trim trailing spaces (and log warning ...) */
556                 int icnt;
557                 for ( icnt = vals[i].bv_len; --icnt > 0; ) {
558                         if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) {
559                                 break;
560                         }
561                 }
562
563                 if ( ++icnt != vals[i].bv_len ) {
564                         fprintf( stderr, _("%s: illegal trailing space after"
565                                 " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
566                                 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
567                         vals[i].bv_val[icnt] = '\0';
568                 }
569 #endif /* LIBERAL_CHANGETYPE_MODOP */
570
571                 if ( BVICMP( vals+i, &BV_MODIFYCT ) == 0 ) {
572                         new_entry = 0;
573                         expect_modop = 1;
574                 } else if ( BVICMP( vals+i, &BV_ADDCT ) == 0 ) {
575                         new_entry = 1;
576                         modop = LDAP_MOD_ADD;
577                 } else if ( BVICMP( vals+i, &BV_MODRDNCT ) == 0
578                         || BVICMP( vals+i, &BV_MODDNCT ) == 0
579                         || BVICMP( vals+i, &BV_RENAMECT ) == 0)
580                 {
581                         i++;
582                         if ( i >= lines )
583                                 goto short_input;
584                         if ( BVICMP( btype+i, &BV_NEWRDN )) {
585                                 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
586                                         " \"%s:\" (line %d, entry \"%s\")\n"),
587                                         prog, BV_NEWRDN.bv_val, btype[i].bv_val, linenum+i, dn );
588                                 rc = LDAP_PARAM_ERROR;
589                                 goto leave;
590                         }
591                         newrdn = vals[i].bv_val;
592                         i++;
593                         if ( i >= lines )
594                                 goto short_input;
595                         if ( BVICMP( btype+i, &BV_DELETEOLDRDN )) {
596                                 fprintf( stderr, _("%s: expecting \"%s:\" but saw"
597                                         " \"%s:\" (line %d, entry \"%s\")\n"),
598                                         prog, BV_DELETEOLDRDN.bv_val, btype[i].bv_val, linenum+i, dn );
599                                 rc = LDAP_PARAM_ERROR;
600                                 goto leave;
601                         }
602                         deleteoldrdn = ( vals[i].bv_val[0] == '0' ) ? 0 : 1;
603                         i++;
604                         if ( i < lines ) {
605                                 if ( BVICMP( btype+i, &BV_NEWSUP )) {
606                                         fprintf( stderr, _("%s: expecting \"%s:\" but saw"
607                                                 " \"%s:\" (line %d, entry \"%s\")\n"),
608                                                 prog, BV_NEWSUP.bv_val, btype[i].bv_val, linenum+i, dn );
609                                         rc = LDAP_PARAM_ERROR;
610                                         goto leave;
611                                 }
612                                 newsup = vals[i].bv_val;
613                                 i++;
614                         }
615                         got_all = 1;
616                 } else if ( BVICMP( vals+i, &BV_DELETECT ) == 0 ) {
617                         got_all = delete_entry = 1;
618                 } else {
619                         fprintf( stderr,
620                                 _("%s:  unknown %s \"%s\" (line %d, entry \"%s\")\n"),
621                                 prog, BV_CHANGETYPE.bv_val, vals[i].bv_val, linenum+i, dn );
622                         rc = LDAP_PARAM_ERROR;
623                         goto leave;
624                 }
625                 i++;
626         } else if ( ldapadd ) {         /*  missing changetype => add */
627                 new_entry = 1;
628                 modop = LDAP_MOD_ADD;
629         } else {
630                 expect_modop = 1;       /* missing changetype => modify */
631         }
632
633         if ( got_all ) {
634                 if ( i < lines ) {
635                         fprintf( stderr,
636                                 _("%s: extra lines at end (line %d, entry \"%s\")\n"),
637                                 prog, linenum+i, dn );
638                         rc = LDAP_PARAM_ERROR;
639                         goto leave;
640                 }
641                 goto doit;
642         }
643
644         nmods = lines - i;
645         idn = i;
646
647         if ( new_entry ) {
648                 int fv;
649
650                 /* Make sure all attributes with multiple values are contiguous */
651                 for (; i<lines; i++) {
652                         for (j=i+1; j<lines; j++) {
653                                 if ( !BVICMP( btype+i, btype+j )) {
654                                         nmods--;
655                                         /* out of order, move intervening attributes down */
656                                         if ( j != i+1 ) {
657                                                 bv = vals[j];
658                                                 fv = freeval[j];
659                                                 for (k=j; k>i; k--) {
660                                                         btype[k] = btype[k-1];
661                                                         vals[k] = vals[k-1];
662                                                         freeval[k] = freeval[k-1];
663                                                 }
664                                                 k++;
665                                                 btype[k] = btype[i];
666                                                 vals[k] = bv;
667                                                 freeval[k] = fv;
668                                         }
669                                         i++;
670                                 }
671                         }
672                 }
673                 /* Allocate space for array of mods, array of pointers to mods,
674                  * and array of pointers to values, allowing for NULL terminators
675                  * for the pointer arrays...
676                  */
677                 lm = ber_memalloc( nmods * sizeof(LDAPMod) +
678                         (nmods+1) * sizeof(LDAPMod*) +
679                         (lines + nmods - idn) * sizeof(struct berval *));
680                 pmods = (LDAPMod **)(lm+nmods);
681                 bvl = (struct berval **)(pmods+nmods+1);
682
683                 j = 0;
684                 k = -1;
685                 BER_BVZERO(&bv);
686                 for (i=idn; i<lines; i++) {
687                         if ( !BVICMP( btype+i, &BV_DN )) {
688                                 fprintf( stderr, _("%s: attributeDescription \"%s\":"
689                                         " (possible missing newline"
690                                                 " after line %d, entry \"%s\"?)\n"),
691                                         prog, btype[i].bv_val, linenum+i - 1, dn );
692                         }
693                         if ( BVICMP(btype+i,&bv)) {
694                                 bvl[k++] = NULL;
695                                 bv = btype[i];
696                                 lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
697                                 lm[j].mod_type = bv.bv_val;
698                                 lm[j].mod_bvalues = bvl+k;
699                                 pmods[j] = lm+j;
700                                 j++;
701                         }
702                         bvl[k++] = vals+i;
703                 }
704                 bvl[k] = NULL;
705                 pmods[j] = NULL;
706                 goto doit;
707         }
708
709         mops = ber_memalloc( lines+1 );
710         mops[lines] = M_SEP;
711         mops[i-1] = M_SEP;
712
713         for ( ; i<lines; i++ ) {
714                 if ( expect_modop ) {
715 #ifdef LIBERAL_CHANGETYPE_MODOP
716                         /* trim trailing spaces (and log warning ...) */
717                     int icnt;
718                     for ( icnt = vals[i].bv_len; --icnt > 0; ) {
719                                 if ( !isspace( (unsigned char) vals[i].bv_val[icnt] ) ) break;
720                         }
721     
722                         if ( ++icnt != vals[i].bv_len ) {
723                                 fprintf( stderr, _("%s: illegal trailing space after"
724                                         " \"%s: %s\" trimmed (line %d, entry \"%s\")\n"),
725                                         prog, type, vals[i].bv_val, linenum+i, dn );
726                                 vals[i].bv_val[icnt] = '\0';
727                         }
728 #endif /* LIBERAL_CHANGETYPE_MODOP */
729
730                         expect_modop = 0;
731                         expect_sep = 1;
732                         if ( BVICMP( btype+i, &BV_MODOPADD ) == 0 ) {
733                                 modop = LDAP_MOD_ADD;
734                                 mops[i] = M_SEP;
735                                 nmods--;
736                         } else if ( BVICMP( btype+i, &BV_MODOPREPLACE ) == 0 ) {
737                         /* defer handling these since they might have no values.
738                          * Use the BVALUES flag to signal that these were
739                          * deferred. If values are provided later, this
740                          * flag will be switched off.
741                          */
742                                 modop = LDAP_MOD_REPLACE;
743                                 mops[i] = modop | LDAP_MOD_BVALUES;
744                                 btype[i] = vals[i];
745                         } else if ( BVICMP( btype+i, &BV_MODOPDELETE ) == 0 ) {
746                                 modop = LDAP_MOD_DELETE;
747                                 mops[i] = modop | LDAP_MOD_BVALUES;
748                                 btype[i] = vals[i];
749                         } else if ( BVICMP( btype+i, &BV_MODOPINCREMENT ) == 0 ) {
750                                 modop = LDAP_MOD_INCREMENT;
751                                 mops[i] = M_SEP;
752                                 nmods--;
753                         } else {        /* no modify op: invalid LDIF */
754                                 fprintf( stderr, _("%s: modify operation type is missing at"
755                                         " line %d, entry \"%s\"\n"),
756                                         prog, linenum+i, dn );
757                                 rc = LDAP_PARAM_ERROR;
758                                 goto leave;
759                         }
760                         bv = vals[i];
761                 } else if ( expect_sep && BER_BVISEMPTY( btype+i )) {
762                         mops[i] = M_SEP;
763                         expect_sep = 0;
764                         expect_modop = 1;
765                         nmods--;
766                 } else {
767                         if ( BVICMP( btype+i, &bv )) {
768                                 fprintf( stderr, _("%s: wrong attributeType at"
769                                         " line %d, entry \"%s\"\n"),
770                                         prog, linenum+i, dn );
771                                 rc = LDAP_PARAM_ERROR;
772                                 goto leave;
773                         }
774                         mops[i] = modop;
775                         /* If prev op was deferred and matches this type,
776                          * clear the flag
777                          */
778                         if ( (mops[i-1]&LDAP_MOD_BVALUES) && !BVICMP(btype+i,
779                                 btype+i-1)) {
780                                 mops[i-1] = M_SEP;
781                                 nmods--;
782                         }
783                 }
784         }
785
786 #if 0   /* we should faithfully encode the LDIF, not combine */
787         /* Make sure all modops with multiple values are contiguous */
788         for (i=idn; i<lines; i++) {
789                 if ( mops[i] == M_SEP )
790                         continue;
791                 for (j=i+1; j<lines; j++) {
792                         if ( mops[j] == M_SEP || mops[i] != mops[j] )
793                                 continue;
794                         if ( !BVICMP( btype+i, btype+j )) {
795                                 nmods--;
796                                 /* out of order, move intervening attributes down */
797                                 if ( j != i+1 ) {
798                                         int c;
799                                         struct berval bv;
800                                         char fv;
801
802                                         c = mops[j];
803                                         bv = vals[j];
804                                         fv = freeval[j];
805                                         for (k=j; k>i; k--) {
806                                                 btype[k] = btype[k-1];
807                                                 vals[k] = vals[k-1];
808                                                 freeval[k] = freeval[k-1];
809                                                 mops[k] = mops[k-1];
810                                         }
811                                         k++;
812                                         btype[k] = btype[i];
813                                         vals[k] = bv;
814                                         freeval[k] = fv;
815                                         mops[k] = c;
816                                 }
817                                 i++;
818                         }
819                 }
820         }
821 #endif
822
823         /* Allocate space for array of mods, array of pointers to mods,
824          * and array of pointers to values, allowing for NULL terminators
825          * for the pointer arrays...
826          */
827         lm = ber_memalloc( nmods * sizeof(LDAPMod) +
828                 (nmods+1) * sizeof(LDAPMod*) +
829                 (lines + nmods - idn) * sizeof(struct berval *));
830         pmods = (LDAPMod **)(lm+nmods);
831         bvl = (struct berval **)(pmods+nmods+1);
832
833         j = 0;
834         k = -1;
835         BER_BVZERO(&bv);
836         mops[idn-1] = M_SEP;
837         for (i=idn; i<lines; i++) {
838                 if ( mops[i] == M_SEP )
839                         continue;
840                 if ( mops[i] != mops[i-1] || BVICMP(btype+i,&bv)) {
841                         bvl[k++] = NULL;
842                         bv = btype[i];
843                         lm[j].mod_op = mops[i] | LDAP_MOD_BVALUES;
844                         lm[j].mod_type = bv.bv_val;
845                         if ( mops[i] & LDAP_MOD_BVALUES ) {
846                                 lm[j].mod_bvalues = NULL;
847                         } else {
848                                 lm[j].mod_bvalues = bvl+k;
849                         }
850                         pmods[j] = lm+j;
851                         j++;
852                 }
853                 bvl[k++] = vals+i;
854         }
855         bvl[k] = NULL;
856         pmods[j] = NULL;
857
858 doit:
859         /* If default controls are set (as with -M option) and controls are
860            specified in the LDIF file, we must add the default controls to
861            the list of controls sent with the ldap operation.
862         */
863         if ( rc == 0 ) {
864                 if (pctrls) {
865                         LDAPControl **defctrls = NULL;   /* Default server controls */
866                         LDAPControl **newctrls = NULL;
867                         ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
868                         if (defctrls) {
869                                 int npc=0;                       /* Num of LDIF controls */
870                                 int ndefc=0;                     /* Num of default controls */
871                                 while (pctrls[npc]) npc++;       /* Count LDIF controls */
872                                 while (defctrls[ndefc]) ndefc++; /* Count default controls */
873                                 newctrls = ber_memrealloc(pctrls,
874                                         (npc+ndefc+1)*sizeof(LDAPControl*));
875
876                                 if (newctrls == NULL) {
877                                         rc = LDAP_NO_MEMORY;
878                                 } else {
879                                         int i;
880                                         pctrls = newctrls;
881                                         for (i=npc; i<npc+ndefc; i++) {
882                                                 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
883                                                 if (pctrls[i] == NULL) {
884                                                         rc = LDAP_NO_MEMORY;
885                                                         break;
886                                                 }
887                                         }
888                                         pctrls[npc+ndefc] = NULL;
889                                 }
890                                 ldap_controls_free(defctrls);  /* Must be freed by library */
891                         }
892                 }
893         }
894
895         if ( rc == 0 ) {
896                 if ( delete_entry ) {
897                         rc = dodelete( dn, pctrls );
898                 } else if ( newrdn != NULL ) {
899                         rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
900                 } else {
901                         rc = domodify( dn, pmods, pctrls, new_entry );
902                 }
903
904                 if ( rc == LDAP_SUCCESS ) {
905                         rc = 0;
906                 }
907         }
908
909 leave:
910     if (pctrls != NULL) {
911         ldap_controls_free( pctrls );
912         }
913         if ( lm != NULL ) {
914                 ber_memfree( lm );
915         }
916         if ( mops != NULL ) {
917                 ber_memfree( mops );
918         }
919         for (i=lines-1; i>=0; i--)
920                 if ( freeval[i] ) ber_memfree( vals[i].bv_val );
921         ber_memfree( btype );
922
923         return( rc );
924 }
925
926 /* Parse an LDIF control line of the form
927       control:  oid  [true/false]  [: value]              or
928       control:  oid  [true/false]  [:: base64-value]      or
929       control:  oid  [true/false]  [:< url]
930    The control is added to the list of controls in *ppctrls.
931 */      
932 static int
933 parse_ldif_control(
934         struct berval *bval,
935         LDAPControl ***ppctrls )
936 {
937         char *oid = NULL;
938         int criticality = 0;   /* Default is false if not present */
939         int i, rc=0;
940         char *s, *oidStart;
941         LDAPControl *newctrl = NULL;
942         LDAPControl **pctrls = NULL;
943         struct berval type, bv;
944         int freeval;
945
946         if (ppctrls) pctrls = *ppctrls;
947         /* OID should come first. Validate and extract it. */
948         s = bval->bv_val;
949         if (*s == 0) return ( LDAP_PARAM_ERROR );
950         oidStart = s;
951         while (isdigit((unsigned char)*s) || *s == '.') {
952                 s++;                           /* OID should be digits or . */
953         }
954         if (s == oidStart) { 
955                 return ( LDAP_PARAM_ERROR );   /* OID was not present */
956         }
957         if (*s) {                          /* End of OID should be space or NULL */
958                 if (!isspace((unsigned char)*s)) {
959                         return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
960                 }
961                 *s++ = 0;                    /* Replace space with null to terminate */
962         }
963
964         oid = ber_strdup(oidStart);
965         if (oid == NULL) return ( LDAP_NO_MEMORY );
966
967         /* Optional Criticality field is next. */
968         while (*s && isspace((unsigned char)*s)) {
969                 s++;                         /* Skip white space before criticality */
970         }
971         if (strncasecmp(s, "true", 4) == 0) {
972                 criticality = 1;
973                 s += 4;
974         } 
975         else if (strncasecmp(s, "false", 5) == 0) {
976                 criticality = 0;
977                 s += 5;
978         }
979
980         /* Optional value field is next */
981         while (*s && isspace((unsigned char)*s)) {
982                 s++;                         /* Skip white space before value */
983         }
984         if (*s) {
985                 if (*s != ':') {           /* If value is present, must start with : */
986                         rc = LDAP_PARAM_ERROR;
987                         goto cleanup;
988                 }
989
990                 /* Back up so value is in the form
991                      a: value
992                      a:: base64-value
993                      a:< url
994                    Then we can use ldif_parse_line2 to extract and decode the value
995                 */
996                 s--;
997                 *s = 'a';
998
999                 rc = ldif_parse_line2(s, &type, &bv, &freeval);
1000                 if (rc < 0) {
1001                         rc = LDAP_PARAM_ERROR;
1002                         goto cleanup;
1003                 }
1004     }
1005
1006         /* Create a new LDAPControl structure. */
1007         newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1008         if ( newctrl == NULL ) {
1009                 rc = LDAP_NO_MEMORY;
1010                 goto cleanup;
1011         }
1012         newctrl->ldctl_oid = oid;
1013         oid = NULL;
1014         newctrl->ldctl_iscritical = criticality;
1015         if ( freeval )
1016                 newctrl->ldctl_value = bv;
1017         else
1018                 ber_dupbv( &newctrl->ldctl_value, &bv );
1019
1020         /* Add the new control to the passed-in list of controls. */
1021         i = 0;
1022         if (pctrls) {
1023                 while ( pctrls[i] ) {    /* Count the # of controls passed in */
1024                         i++;
1025                 }
1026         }
1027         /* Allocate 1 more slot for the new control and 1 for the NULL. */
1028         pctrls = (LDAPControl **) ber_memrealloc(pctrls,
1029                 (i+2)*(sizeof(LDAPControl *)));
1030         if (pctrls == NULL) {
1031                 rc = LDAP_NO_MEMORY;
1032                 goto cleanup;
1033         }
1034         pctrls[i] = newctrl;
1035         newctrl = NULL;
1036         pctrls[i+1] = NULL;
1037         *ppctrls = pctrls;
1038
1039 cleanup:
1040         if (newctrl) {
1041                 if (newctrl->ldctl_oid) ber_memfree(newctrl->ldctl_oid);
1042                 if (newctrl->ldctl_value.bv_val) {
1043                         ber_memfree(newctrl->ldctl_value.bv_val);
1044                 }
1045                 ber_memfree(newctrl);
1046         }
1047         if (oid) ber_memfree(oid);
1048
1049         return( rc );
1050 }
1051
1052
1053 static int
1054 domodify(
1055         const char *dn,
1056         LDAPMod **pmods,
1057         LDAPControl **pctrls,
1058         int newentry )
1059 {
1060         int                     rc, i, j, k, notascii, op;
1061         struct berval   *bvp;
1062
1063         if ( dn == NULL ) {
1064                 fprintf( stderr, _("%s: no DN specified\n"), prog );
1065                 return( LDAP_PARAM_ERROR );
1066         }
1067
1068         if ( pmods == NULL ) {
1069                 /* implement "touch" (empty sequence)
1070                  * modify operation (note that there
1071                  * is no symmetry with the UNIX command,
1072                  * since \"touch\" on a non-existent entry
1073                  * will fail)*/
1074                 printf( "warning: no attributes to %sadd (entry=\"%s\")\n",
1075                         newentry ? "" : "change or ", dn );
1076
1077         } else {
1078                 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1079                         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1080                         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1081                                 fprintf( stderr,
1082                                         _("%s: attribute \"%s\" has no values (entry=\"%s\")\n"),
1083                                         prog, pmods[i]->mod_type, dn );
1084                                 return LDAP_PARAM_ERROR;
1085                         }
1086                 }
1087
1088                 if ( verbose ) {
1089                         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1090                                 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1091                                 printf( "%s %s:\n",
1092                                         op == LDAP_MOD_REPLACE ? _("replace") :
1093                                                 op == LDAP_MOD_ADD ?  _("add") :
1094                                                         op == LDAP_MOD_INCREMENT ?  _("increment") :
1095                                                                 op == LDAP_MOD_DELETE ?  _("delete") :
1096                                                                         _("unknown"),
1097                                         pmods[ i ]->mod_type );
1098         
1099                                 if ( pmods[ i ]->mod_bvalues != NULL ) {
1100                                         for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1101                                                 bvp = pmods[ i ]->mod_bvalues[ j ];
1102                                                 notascii = 0;
1103                                                 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1104                                                         if ( !isascii( bvp->bv_val[ k ] )) {
1105                                                                 notascii = 1;
1106                                                                 break;
1107                                                         }
1108                                                 }
1109                                                 if ( notascii ) {
1110                                                         printf( _("\tNOT ASCII (%ld bytes)\n"), bvp->bv_len );
1111                                                 } else {
1112                                                         printf( "\t%s\n", bvp->bv_val );
1113                                                 }
1114                                         }
1115                                 }
1116                         }
1117                 }
1118         }
1119
1120         if ( newentry ) {
1121                 printf( "%sadding new entry \"%s\"\n", dont ? "!" : "", dn );
1122         } else {
1123                 printf( "%smodifying entry \"%s\"\n", dont ? "!" : "", dn );
1124         }
1125
1126         if ( !dont ) {
1127                 int     msgid;
1128                 if ( newentry ) {
1129                         rc = ldap_add_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1130                 } else {
1131                         rc = ldap_modify_ext( ld, dn, pmods, pctrls, NULL, &msgid );
1132                 }
1133
1134                 if ( rc != LDAP_SUCCESS ) {
1135                         /* print error message about failed update including DN */
1136                         fprintf( stderr, _("%s: update failed: %s\n"), prog, dn );
1137                         tool_perror( newentry ? "ldap_add" : "ldap_modify",
1138                                 rc, NULL, NULL, NULL, NULL );
1139                         goto done;
1140                 } else if ( verbose ) {
1141                         printf( _("modify complete\n") );
1142                 }
1143
1144                 rc = process_response( ld, msgid,
1145                         newentry ? LDAP_RES_ADD : LDAP_RES_MODIFY, dn );
1146
1147         } else {
1148                 rc = LDAP_SUCCESS;
1149         }
1150
1151 done:
1152         putchar( '\n' );
1153         return rc;
1154 }
1155
1156
1157 static int
1158 dodelete(
1159         const char *dn,
1160         LDAPControl **pctrls )
1161 {
1162         int     rc;
1163         int msgid;
1164
1165         printf( _("%sdeleting entry \"%s\"\n"), dont ? "!" : "", dn );
1166         if ( !dont ) {
1167                 rc = ldap_delete_ext( ld, dn, pctrls, NULL, &msgid );
1168                 if ( rc != LDAP_SUCCESS ) {
1169                         fprintf( stderr, _("%s: delete failed: %s\n"), prog, dn );
1170                         tool_perror( "ldap_delete", rc, NULL, NULL, NULL, NULL );
1171                         goto done;
1172                 } else if ( verbose ) {
1173                         printf( _("delete complete") );
1174                 }
1175
1176                 rc = process_response( ld, msgid, LDAP_RES_DELETE, dn );
1177
1178         } else {
1179                 rc = LDAP_SUCCESS;
1180         }
1181
1182 done:
1183         putchar( '\n' );
1184         return( rc );
1185 }
1186
1187
1188 static int
1189 dorename(
1190         const char *dn,
1191         const char *newrdn,
1192         const char* newsup,
1193         int deleteoldrdn,
1194         LDAPControl **pctrls )
1195 {
1196         int     rc;
1197         int msgid;
1198
1199         printf( _("%smodifying rdn of entry \"%s\"\n"), dont ? "!" : "", dn );
1200         if ( verbose ) {
1201                 printf( _("\tnew RDN: \"%s\" (%skeep existing values)\n"),
1202                         newrdn, deleteoldrdn ? _("do not ") : "" );
1203         }
1204         if ( !dont ) {
1205                 rc = ldap_rename( ld, dn, newrdn, newsup, deleteoldrdn,
1206                         pctrls, NULL, &msgid );
1207                 if ( rc != LDAP_SUCCESS ) {
1208                         fprintf( stderr, _("%s: rename failed: %s\n"), prog, dn );
1209                         tool_perror( "ldap_rename", rc, NULL, NULL, NULL, NULL );
1210                         goto done;
1211                 } else {
1212                         printf( _("rename completed\n") );
1213                 }
1214
1215                 rc = process_response( ld, msgid, LDAP_RES_RENAME, dn );
1216
1217         } else {
1218                 rc = LDAP_SUCCESS;
1219         }
1220
1221 done:
1222         putchar( '\n' );
1223         return( rc );
1224 }
1225
1226 static const char *
1227 res2str( int res ) {
1228         switch ( res ) {
1229         case LDAP_RES_ADD:
1230                 return "ldap_add";
1231         case LDAP_RES_DELETE:
1232                 return "ldap_delete";
1233         case LDAP_RES_MODIFY:
1234                 return "ldap_modify";
1235         case LDAP_RES_MODRDN:
1236                 return "ldap_rename";
1237         default:
1238                 assert( 0 );
1239         }
1240
1241         return "ldap_unknown";
1242 }
1243
1244 static int process_response(
1245         LDAP *ld,
1246         int msgid,
1247         int op,
1248         const char *dn )
1249 {
1250         LDAPMessage     *res;
1251         int             rc = LDAP_OTHER, msgtype;
1252         struct timeval  tv = { 0, 0 };
1253         int             err;
1254         char            *text = NULL, *matched = NULL, **refs = NULL;
1255         LDAPControl     **ctrls = NULL;
1256
1257         for ( ; ; ) {
1258                 tv.tv_sec = 0;
1259                 tv.tv_usec = 100000;
1260
1261                 rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
1262                 if ( tool_check_abandon( ld, msgid ) ) {
1263                         return LDAP_CANCELLED;
1264                 }
1265
1266                 if ( rc == -1 ) {
1267                         ldap_get_option( ld, LDAP_OPT_RESULT_CODE, &rc );
1268                         tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1269                         return rc;
1270                 }
1271
1272                 if ( rc != 0 ) {
1273                         break;
1274                 }
1275         }
1276
1277         msgtype = ldap_msgtype( res );
1278
1279         rc = ldap_parse_result( ld, res, &err, &matched, &text, &refs, &ctrls, 1 );
1280         if ( rc == LDAP_SUCCESS ) rc = err;
1281
1282 #ifdef LDAP_X_TXN
1283         if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
1284                 rc = LDAP_SUCCESS;
1285         } else
1286 #endif
1287         if ( rc != LDAP_SUCCESS ) {
1288                 tool_perror( res2str( op ), rc, NULL, matched, text, refs );
1289         } else if ( msgtype != op ) {
1290                 fprintf( stderr, "%s: msgtype: expected %d got %d\n",
1291                         res2str( op ), op, msgtype );
1292                 rc = LDAP_OTHER;
1293         }
1294
1295         if ( text ) ldap_memfree( text );
1296         if ( matched ) ldap_memfree( matched );
1297         if ( text ) ber_memvfree( (void **)refs );
1298
1299         if ( ctrls ) {
1300                 tool_print_ctrls( ld, ctrls );
1301                 ldap_controls_free( ctrls );
1302         }
1303
1304         return rc;
1305 }