]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
00c1899905d26036c785120e3dda02e51e027356
[openldap] / clients / tools / ldapmodify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodify.c - generic program to modify or add entries using LDAP */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13
14 #include <ac/ctype.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
18
19 #ifdef HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22
23 #ifdef HAVE_SYS_FILE_H
24 #include <sys/file.h>
25 #endif
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29
30 #include <ldap.h>
31
32 #include "lutil.h"
33 #include "lutil_ldap.h"
34 #include "ldif.h"
35 #include "ldap_defaults.h"
36 #include "ldap_log.h"
37 #include "ldap_pvt.h"
38
39 static char     *prog;
40 static char     *binddn = NULL;
41 static struct berval passwd = { 0, NULL };
42 static char *ldapuri = NULL;
43 static char     *ldaphost = NULL;
44 static int      ldapport = 0;
45 #ifdef HAVE_CYRUS_SASL
46 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
47 static char *sasl_realm = NULL;
48 static char     *sasl_authc_id = NULL;
49 static char     *sasl_authz_id = NULL;
50 static char     *sasl_mech = NULL;
51 static char     *sasl_secprops = NULL;
52 #endif
53 static int      use_tls = 0;
54 static int      ldapadd, not, verbose, contoper, force;
55 static LDAP     *ld = NULL;
56
57 #define LDAPMOD_MAXLINE         4096
58
59 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
60 #define T_VERSION_STR           "version"
61 #define T_REPLICA_STR           "replica"
62 #define T_DN_STR                "dn"
63 #define T_CONTROL_STR           "control"
64 #define T_CHANGETYPESTR         "changetype"
65 #define T_ADDCTSTR              "add"
66 #define T_MODIFYCTSTR           "modify"
67 #define T_DELETECTSTR           "delete"
68 #define T_MODRDNCTSTR           "modrdn"
69 #define T_MODDNCTSTR            "moddn"
70 #define T_RENAMECTSTR           "rename"
71 #define T_MODOPADDSTR           "add"
72 #define T_MODOPREPLACESTR       "replace"
73 #define T_MODOPDELETESTR        "delete"
74 #define T_MODSEPSTR             "-"
75 #define T_NEWRDNSTR             "newrdn"
76 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
77 #define T_NEWSUPSTR             "newsuperior"
78
79
80 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
81 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
82 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
83 static void addmodifyop LDAP_P((
84         LDAPMod ***pmodsp, int modop,
85         const char *attr,
86         struct berval *value ));
87 static int domodify LDAP_P((
88         const char *dn,
89         LDAPMod **pmods,
90     LDAPControl **pctrls,
91         int newentry ));
92 static int dodelete LDAP_P((
93         const char *dn,
94     LDAPControl **pctrls ));
95 static int dorename LDAP_P((
96         const char *dn,
97         const char *newrdn,
98         const char *newsup,
99         int deleteoldrdn,
100     LDAPControl **pctrls ));
101 static char *read_one_record LDAP_P(( FILE *fp ));
102
103 static void
104 usage( const char *prog )
105 {
106     fprintf( stderr,
107 "Add or modify entries from an LDAP server\n\n"
108 "usage: %s [options]\n"
109 "       The list of desired operations are read from stdin or from the file\n"
110 "       specified by \"-f file\".\n"
111 "Add or modify options:\n"
112 "  -a         add values (default%s)\n"
113 "  -c         continuous operation mode (do not stop on errors)\n"
114 "  -F         force all changes records to be used\n"
115 "  -S file    write skipped modifications to `file'\n"
116
117 "Common options:\n"
118 "  -d level   set LDAP debugging level to `level'\n"
119 "  -D binddn  bind DN\n"
120 "  -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
121 "             [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
122 "             [!]manageDSAit   (alternate form, see -M)\n"
123 "             [!]noop\n"
124 "  -f file    read operations from `file'\n"
125 "  -h host    LDAP server\n"
126 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
127 "  -I         use SASL Interactive mode\n"
128 "  -k         use Kerberos authentication\n"
129 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
130 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
131 "  -n         show what would be done but don't actually update\n"
132 "  -O props   SASL security properties\n"
133 "  -p port    port on LDAP server\n"
134 "  -P version procotol version (default: 3)\n"
135 "  -Q         use SASL Quiet mode\n"
136 "  -R realm   SASL realm\n"
137 "  -U authcid SASL authentication identity\n"
138 "  -v         run in verbose mode (diagnostics to standard output)\n"
139 "  -w passwd  bind passwd (for simple authentication)\n"
140 "  -W         prompt for bind passwd\n"
141 "  -x         Simple authentication\n"
142 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
143 "  -y file    Read passwd from file\n"
144 "  -Y mech    SASL mechanism\n"
145 "  -Z         Start TLS request (-ZZ to require successful response)\n"
146              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
147
148     exit( EXIT_FAILURE );
149 }
150
151
152 int
153 main( int argc, char **argv )
154 {
155     char                *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
156     FILE                *fp, *rejfp;
157         char            *matched_msg = NULL, *error_msg = NULL;
158         int             rc, retval, i, authmethod, version, want_bindpw;
159         int             debug, manageDSAit, noop, referrals;
160         int count, len;
161         char    *pw_file = NULL;
162         char    *control, *cvalue;
163         char    *authzid = NULL;
164         int             crit;
165
166     prog = lutil_progname( "ldapmodify", argc, argv );
167
168     /* Print usage when no parameters */
169     if( argc < 2 ) usage( prog );
170
171         /* strncmp instead of strcmp since NT binaries carry .exe extension */
172     ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
173
174     infile = NULL;
175     rejfile = NULL;
176     not = verbose = want_bindpw = debug = manageDSAit = noop = referrals = 0;
177     authmethod = -1;
178         version = -1;
179
180     while (( i = getopt( argc, argv, "acrf:E:F"
181                 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
182         {
183         switch( i ) {
184         /* Modify Options */
185         case 'a':       /* add */
186             ldapadd = 1;
187             break;
188         case 'c':       /* continuous operation */
189             contoper = 1;
190             break;
191         case 'E': /* modify controls */
192                 if( version == LDAP_VERSION2 ) {
193                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
194                                 prog, version );
195                         return EXIT_FAILURE;
196                 }
197
198                 /* should be extended to support comma separated list of
199                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
200                  */
201
202                 crit = 0;
203                 cvalue = NULL;
204                 if( optarg[0] == '!' ) {
205                         crit = 1;
206                         optarg++;
207                 }
208
209                 control = ber_strdup( optarg );
210                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
211                         *cvalue++ = '\0';
212                 }
213                 fprintf( stderr, "Invalid modify control name: %s\n", control );
214                 usage(prog);
215                 return EXIT_FAILURE;
216         case 'f':       /* read from file */
217                 if( infile != NULL ) {
218                         fprintf( stderr, "%s: -f previously specified\n", prog );
219                         return EXIT_FAILURE;
220                 }
221             infile = ber_strdup( optarg );
222             break;
223         case 'F':       /* force all changes records to be used */
224             force = 1;
225             break;
226
227         /* Common Options */
228         case 'C':
229                 referrals++;
230                 break;
231         case 'd':
232             debug |= atoi( optarg );
233             break;
234         case 'D':       /* bind DN */
235                 if( binddn != NULL ) {
236                         fprintf( stderr, "%s: -D previously specified\n", prog );
237                         return EXIT_FAILURE;
238                 }
239             binddn = ber_strdup( optarg );
240             break;
241         case 'e': /* general controls */
242                 if( version == LDAP_VERSION2 ) {
243                         fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
244                                 prog, version );
245                         return EXIT_FAILURE;
246                 }
247
248                 /* should be extended to support comma separated list of
249                  *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
250                  */
251
252                 crit = 0;
253                 cvalue = NULL;
254                 if( optarg[0] == '!' ) {
255                         crit = 1;
256                         optarg++;
257                 }
258
259                 control = ber_strdup( optarg );
260                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
261                         *cvalue++ = '\0';
262                 }
263
264                 if ( strcasecmp( control, "authzid" ) == 0 ) {
265                         if( authzid != NULL ) {
266                                 fprintf( stderr, "authzid control previously specified\n");
267                                 return EXIT_FAILURE;
268                         }
269                         if( cvalue == NULL ) {
270                                 fprintf( stderr, "authzid: control value expected\n" );
271                                 usage(prog);
272                                 return EXIT_FAILURE;
273                         }
274                         if( !crit ) {
275                                 fprintf( stderr, "authzid: must be marked critical\n" );
276                                 usage(prog);
277                                 return EXIT_FAILURE;
278                         }
279
280                         assert( authzid == NULL );
281                         authzid = control;
282
283                 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
284                         if( manageDSAit ) {
285                                 fprintf( stderr, "manageDSAit control previously specified\n");
286                                 return EXIT_FAILURE;
287                         }
288                         if( cvalue != NULL ) {
289                                 fprintf( stderr, "manageDSAit: no control value expected\n" );
290                                 usage(prog);
291                                 return EXIT_FAILURE;
292                         }
293
294                         manageDSAit = 1 + crit;
295
296                 } else if ( strcasecmp( control, "noop" ) == 0 ) {
297                         if( noop ) {
298                                 fprintf( stderr, "noop control previously specified\n");
299                                 return EXIT_FAILURE;
300                         }
301                         if( cvalue != NULL ) {
302                                 fprintf( stderr, "noop: no control value expected\n" );
303                                 usage(prog);
304                                 return EXIT_FAILURE;
305                         }
306
307                         noop = 1 + crit;
308
309                 } else {
310                         fprintf( stderr, "Invalid general control name: %s\n", control );
311                         usage(prog);
312                         return EXIT_FAILURE;
313                 }
314                 break;
315         case 'h':       /* ldap host */
316                 if( ldapuri != NULL ) {
317                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
318                         return EXIT_FAILURE;
319                 }
320                 if( ldaphost != NULL ) {
321                         fprintf( stderr, "%s: -h previously specified\n", prog );
322                         return EXIT_FAILURE;
323                 }
324             ldaphost = ber_strdup( optarg );
325             break;
326         case 'H':       /* ldap URI */
327                 if( ldaphost != NULL ) {
328                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
329                         return EXIT_FAILURE;
330                 }
331                 if( ldapport ) {
332                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
333                         return EXIT_FAILURE;
334                 }
335                 if( ldapuri != NULL ) {
336                         fprintf( stderr, "%s: -H previously specified\n", prog );
337                         return EXIT_FAILURE;
338                 }
339             ldapuri = ber_strdup( optarg );
340             break;
341         case 'I':
342 #ifdef HAVE_CYRUS_SASL
343                 if( version == LDAP_VERSION2 ) {
344                         fprintf( stderr, "%s: -I incompatible with version %d\n",
345                                 prog, version );
346                         return EXIT_FAILURE;
347                 }
348                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
349                         fprintf( stderr, "%s: incompatible previous "
350                                 "authentication choice\n",
351                                 prog );
352                         return EXIT_FAILURE;
353                 }
354                 authmethod = LDAP_AUTH_SASL;
355                 version = LDAP_VERSION3;
356                 sasl_flags = LDAP_SASL_INTERACTIVE;
357                 break;
358 #else
359                 fprintf( stderr, "%s: was not compiled with SASL support\n",
360                         prog );
361                 return( EXIT_FAILURE );
362 #endif
363         case 'k':       /* kerberos bind */
364 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
365                 if( version > LDAP_VERSION2 ) {
366                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
367                                 prog, version );
368                         return EXIT_FAILURE;
369                 }
370
371                 if( authmethod != -1 ) {
372                         fprintf( stderr, "%s: -k incompatible with previous "
373                                 "authentication choice\n", prog );
374                         return EXIT_FAILURE;
375                 }
376                         
377                 authmethod = LDAP_AUTH_KRBV4;
378 #else
379                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
380                 return EXIT_FAILURE;
381 #endif
382             break;
383         case 'K':       /* kerberos bind, part one only */
384 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
385                 if( version > LDAP_VERSION2 ) {
386                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
387                                 prog, version );
388                         return EXIT_FAILURE;
389                 }
390                 if( authmethod != -1 ) {
391                         fprintf( stderr, "%s: incompatible with previous "
392                                 "authentication choice\n", prog );
393                         return EXIT_FAILURE;
394                 }
395
396                 authmethod = LDAP_AUTH_KRBV41;
397 #else
398                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
399                 return( EXIT_FAILURE );
400 #endif
401             break;
402         case 'M':
403                 /* enable Manage DSA IT */
404                 if( version == LDAP_VERSION2 ) {
405                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
406                                 prog, version );
407                         return EXIT_FAILURE;
408                 }
409                 manageDSAit++;
410                 version = LDAP_VERSION3;
411                 break;
412         case 'n':       /* print deletes, don't actually do them */
413             ++not;
414             break;
415         case 'O':
416 #ifdef HAVE_CYRUS_SASL
417                 if( sasl_secprops != NULL ) {
418                         fprintf( stderr, "%s: -O previously specified\n", prog );
419                         return EXIT_FAILURE;
420                 }
421                 if( version == LDAP_VERSION2 ) {
422                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
423                                 prog, version );
424                         return EXIT_FAILURE;
425                 }
426                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
427                         fprintf( stderr, "%s: incompatible previous "
428                                 "authentication choice\n", prog );
429                         return EXIT_FAILURE;
430                 }
431                 authmethod = LDAP_AUTH_SASL;
432                 version = LDAP_VERSION3;
433                 sasl_secprops = ber_strdup( optarg );
434 #else
435                 fprintf( stderr, "%s: not compiled with SASL support\n",
436                         prog );
437                 return( EXIT_FAILURE );
438 #endif
439                 break;
440         case 'p':
441                 if( ldapport ) {
442                         fprintf( stderr, "%s: -p previously specified\n", prog );
443                         return EXIT_FAILURE;
444                 }
445             ldapport = atoi( optarg );
446             break;
447         case 'P':
448                 switch( atoi(optarg) ) {
449                 case 2:
450                         if( version == LDAP_VERSION3 ) {
451                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
452                                         prog, version );
453                                 return EXIT_FAILURE;
454                         }
455                         version = LDAP_VERSION2;
456                         break;
457                 case 3:
458                         if( version == LDAP_VERSION2 ) {
459                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
460                                         prog, version );
461                                 return EXIT_FAILURE;
462                         }
463                         version = LDAP_VERSION3;
464                         break;
465                 default:
466                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
467                                 prog );
468                         usage( prog );
469                         return( EXIT_FAILURE );
470                 }
471                 break;
472         case 'Q':
473 #ifdef HAVE_CYRUS_SASL
474                 if( version == LDAP_VERSION2 ) {
475                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
476                                 prog, version );
477                         return EXIT_FAILURE;
478                 }
479                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
480                         fprintf( stderr, "%s: incompatible previous "
481                                 "authentication choice\n",
482                                 prog );
483                         return EXIT_FAILURE;
484                 }
485                 authmethod = LDAP_AUTH_SASL;
486                 version = LDAP_VERSION3;
487                 sasl_flags = LDAP_SASL_QUIET;
488                 break;
489 #else
490                 fprintf( stderr, "%s: not compiled with SASL support\n",
491                         prog );
492                 return( EXIT_FAILURE );
493 #endif
494         case 'r':       /* replace (obsolete) */
495                 break;
496
497         case 'R':
498 #ifdef HAVE_CYRUS_SASL
499                 if( sasl_realm != NULL ) {
500                         fprintf( stderr, "%s: -R previously specified\n", prog );
501                         return EXIT_FAILURE;
502                 }
503                 if( version == LDAP_VERSION2 ) {
504                         fprintf( stderr, "%s: -R incompatible with version %d\n",
505                                 prog, version );
506                         return EXIT_FAILURE;
507                 }
508                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
509                         fprintf( stderr, "%s: incompatible previous "
510                                 "authentication choice\n",
511                                 prog );
512                         return EXIT_FAILURE;
513                 }
514                 authmethod = LDAP_AUTH_SASL;
515                 version = LDAP_VERSION3;
516                 sasl_realm = ber_strdup( optarg );
517 #else
518                 fprintf( stderr, "%s: not compiled with SASL support\n",
519                         prog );
520                 return( EXIT_FAILURE );
521 #endif
522                 break;
523         case 'S':       /* skipped modifications to file */
524                 if( rejfile != NULL ) {
525                         fprintf( stderr, "%s: -S previously specified\n", prog );
526                         return EXIT_FAILURE;
527                 }
528                 rejfile = ber_strdup( optarg );
529                 break;
530         case 'U':
531 #ifdef HAVE_CYRUS_SASL
532                 if( sasl_authc_id != NULL ) {
533                         fprintf( stderr, "%s: -U previously specified\n", prog );
534                         return EXIT_FAILURE;
535                 }
536                 if( version == LDAP_VERSION2 ) {
537                         fprintf( stderr, "%s: -U incompatible with version %d\n",
538                                 prog, version );
539                         return EXIT_FAILURE;
540                 }
541                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
542                         fprintf( stderr, "%s: incompatible previous "
543                                 "authentication choice\n",
544                                 prog );
545                         return EXIT_FAILURE;
546                 }
547                 authmethod = LDAP_AUTH_SASL;
548                 version = LDAP_VERSION3;
549                 sasl_authc_id = ber_strdup( optarg );
550 #else
551                 fprintf( stderr, "%s: not compiled with SASL support\n",
552                         prog );
553                 return( EXIT_FAILURE );
554 #endif
555                 break;
556         case 'v':       /* verbose mode */
557             verbose++;
558             break;
559         case 'w':       /* password */
560             passwd.bv_val = ber_strdup( optarg );
561                 {
562                         char* p;
563
564                         for( p = optarg; *p != '\0'; p++ ) {
565                                 *p = '\0';
566                         }
567                 }
568                 passwd.bv_len = strlen( passwd.bv_val );
569             break;
570         case 'W':
571                 want_bindpw++;
572                 break;
573         case 'y':
574                 pw_file = optarg;
575                 break;
576         case 'Y':
577 #ifdef HAVE_CYRUS_SASL
578                 if( sasl_mech != NULL ) {
579                         fprintf( stderr, "%s: -Y previously specified\n", prog );
580                         return EXIT_FAILURE;
581                 }
582                 if( version == LDAP_VERSION2 ) {
583                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
584                                 prog, version );
585                         return EXIT_FAILURE;
586                 }
587                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
588                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
589                         return EXIT_FAILURE;
590                 }
591                 authmethod = LDAP_AUTH_SASL;
592                 version = LDAP_VERSION3;
593                 sasl_mech = ber_strdup( optarg );
594 #else
595                 fprintf( stderr, "%s: not compiled with SASL support\n",
596                         prog );
597                 return( EXIT_FAILURE );
598 #endif
599                 break;
600         case 'x':
601                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
602                         fprintf( stderr, "%s: incompatible with previous "
603                                 "authentication choice\n", prog );
604                         return EXIT_FAILURE;
605                 }
606                 authmethod = LDAP_AUTH_SIMPLE;
607                 break;
608         case 'X':
609 #ifdef HAVE_CYRUS_SASL
610                 if( sasl_authz_id != NULL ) {
611                         fprintf( stderr, "%s: -X previously specified\n", prog );
612                         return EXIT_FAILURE;
613                 }
614                 if( version == LDAP_VERSION2 ) {
615                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
616                                 prog, version );
617                         return EXIT_FAILURE;
618                 }
619                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
620                         fprintf( stderr, "%s: -X incompatible with "
621                                 "authentication choice\n", prog );
622                         return EXIT_FAILURE;
623                 }
624                 authmethod = LDAP_AUTH_SASL;
625                 version = LDAP_VERSION3;
626                 sasl_authz_id = ber_strdup( optarg );
627 #else
628                 fprintf( stderr, "%s: not compiled with SASL support\n",
629                         prog );
630                 return( EXIT_FAILURE );
631 #endif
632                 break;
633         case 'Z':
634 #ifdef HAVE_TLS
635                 if( version == LDAP_VERSION2 ) {
636                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
637                                 prog, version );
638                         return EXIT_FAILURE;
639                 }
640                 version = LDAP_VERSION3;
641                 use_tls++;
642 #else
643                 fprintf( stderr, "%s: not compiled with TLS support\n",
644                         prog );
645                 return( EXIT_FAILURE );
646 #endif
647                 break;
648         default:
649                 fprintf( stderr, "%s: unrecognized option -%c\n",
650                         prog, optopt );
651             usage( prog );
652         }
653     }
654
655         if (version == -1) {
656                 version = LDAP_VERSION3;
657         }
658         if (authmethod == -1 && version > LDAP_VERSION2) {
659 #ifdef HAVE_CYRUS_SASL
660                 authmethod = LDAP_AUTH_SASL;
661 #else
662                 authmethod = LDAP_AUTH_SIMPLE;
663 #endif
664         }
665
666         if ( argc != optind )
667         usage( prog );
668
669     if ( rejfile != NULL ) {
670         if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
671             perror( rejfile );
672             return( EXIT_FAILURE );
673         }
674     } else {
675         rejfp = NULL;
676     }
677
678     if ( infile != NULL ) {
679         if (( fp = fopen( infile, "r" )) == NULL ) {
680             perror( infile );
681             return( EXIT_FAILURE );
682         }
683     } else {
684         fp = stdin;
685     }
686
687         if ( debug ) {
688                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
689                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
690                 }
691                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
692                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
693                 }
694                 ldif_debug = debug;
695         }
696
697 #ifdef SIGPIPE
698         (void) SIGNAL( SIGPIPE, SIG_IGN );
699 #endif
700
701     if ( !not ) {
702         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
703                 if ( verbose ) {
704                         fprintf( stderr, "ldap_init( %s, %d )\n",
705                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
706                                 ldapport );
707                 }
708
709                 ld = ldap_init( ldaphost, ldapport );
710                 if( ld == NULL ) {
711                         perror("ldapmodify: ldap_init");
712                         return EXIT_FAILURE;
713                 }
714
715         } else {
716                 if ( verbose ) {
717                         fprintf( stderr, "ldap_initialize( %s )\n",
718                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
719                 }
720
721                 rc = ldap_initialize( &ld, ldapuri );
722                 if( rc != LDAP_SUCCESS ) {
723                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
724                                 rc, ldap_err2string(rc) );
725                         return EXIT_FAILURE;
726                 }
727         }
728
729         /* referrals */
730         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
731                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
732         {
733                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
734                         referrals ? "on" : "off" );
735                 return EXIT_FAILURE;
736         }
737
738
739         if (version == -1 ) {
740                 version = LDAP_VERSION3;
741         }
742
743         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
744                 != LDAP_OPT_SUCCESS )
745         {
746                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
747                         version );
748                 return EXIT_FAILURE;
749         }
750
751         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
752                 ldap_perror( ld, "ldap_start_tls" );
753                 if ( use_tls > 1 ) {
754                         return( EXIT_FAILURE );
755                 }
756         }
757
758         if ( pw_file || want_bindpw ) {
759                 if ( pw_file ) {
760                         rc = lutil_get_filed_password( pw_file, &passwd );
761                         if( rc ) return EXIT_FAILURE;
762                 } else {
763                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
764                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
765                 }
766         }
767
768         if ( authmethod == LDAP_AUTH_SASL ) {
769 #ifdef HAVE_CYRUS_SASL
770                 void *defaults;
771
772                 if( sasl_secprops != NULL ) {
773                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
774                                 (void *) sasl_secprops );
775                         
776                         if( rc != LDAP_OPT_SUCCESS ) {
777                                 fprintf( stderr,
778                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
779                                         sasl_secprops );
780                                 return( EXIT_FAILURE );
781                         }
782                 }
783                 
784                 defaults = lutil_sasl_defaults( ld,
785                         sasl_mech,
786                         sasl_realm,
787                         sasl_authc_id,
788                         passwd.bv_val,
789                         sasl_authz_id );
790
791                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
792                         sasl_mech, NULL, NULL,
793                         sasl_flags, lutil_sasl_interact, defaults );
794
795                 if( rc != LDAP_SUCCESS ) {
796                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
797                         return( EXIT_FAILURE );
798                 }
799 #else
800                 fprintf( stderr, "%s: not compiled with SASL support\n",
801                         prog );
802                 return( EXIT_FAILURE );
803 #endif
804         }
805         else {
806                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
807                                 != LDAP_SUCCESS ) {
808                         ldap_perror( ld, "ldap_bind" );
809                         return( EXIT_FAILURE );
810                 }
811
812         }
813
814     }
815
816     rc = 0;
817
818         if ( authzid || manageDSAit || noop ) {
819                 int err, crit=0, i=0;
820                 LDAPControl c[3];
821                 LDAPControl *ctrls[4];
822
823                 if ( authzid ) {
824                         c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
825                         c[i].ldctl_value.bv_val = authzid;
826                         c[i].ldctl_value.bv_len = strlen( authzid );
827                         c[i].ldctl_iscritical = 1;
828
829                         if( c[i].ldctl_iscritical ) crit++;
830                         ctrls[i] = &c[i];
831                         ctrls[++i] = NULL;
832                 }
833
834                 if ( manageDSAit ) {
835                         c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
836                         c[i].ldctl_value.bv_val = NULL;
837                         c[i].ldctl_value.bv_len = 0;
838                         c[i].ldctl_iscritical = manageDSAit > 1;
839
840                         if( c[i].ldctl_iscritical ) crit++;
841                         ctrls[i] = &c[i];
842                         ctrls[++i] = NULL;
843                 }
844
845                 if ( noop ) {
846                         c[i].ldctl_oid = LDAP_CONTROL_NOOP;
847                         c[i].ldctl_value.bv_val = NULL;
848                         c[i].ldctl_value.bv_len = 0;
849                         c[i].ldctl_iscritical = noop > 1;
850
851                         if( c[i].ldctl_iscritical ) crit++;
852                         ctrls[i] = &c[i];
853                         ctrls[++i] = NULL;
854                 }
855         
856                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
857
858                 if( err != LDAP_OPT_SUCCESS ) {
859                         fprintf( stderr, "Could not set %scontrols\n",
860                                 crit ? "critical " : "" );
861                         if ( crit ) {
862                                 return EXIT_FAILURE;
863                         }
864                 }
865         }
866
867         count = 0;
868         retval = 0;
869     while (( rc == 0 || contoper ) &&
870                 ( rbuf = read_one_record( fp )) != NULL ) {
871         count++;
872
873         start = rbuf;
874
875         if ( rejfp ) {
876                 len = strlen( rbuf );
877                 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
878                         perror( "malloc" );
879                         exit( EXIT_FAILURE );
880                 }
881                 memcpy( rejbuf, rbuf, len+1 );
882         }
883
884     rc = process_ldif_rec( start, count );
885
886         if ( rc )
887                 retval = rc;
888         if ( rc && rejfp ) {
889                 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
890
891                 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
892                 if ( matched_msg != NULL && *matched_msg != '\0' ) {
893                         fprintf( rejfp, ", matched DN: %s", matched_msg );
894                 }
895
896                 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
897                 if ( error_msg != NULL && *error_msg != '\0' ) {
898                         fprintf( rejfp, ", additional info: %s", error_msg );
899                 }
900                 fprintf( rejfp, "\n%s\n", rejbuf );
901         }
902                 if (rejfp) 
903                         free( rejbuf );
904                 free( rbuf );
905     }
906
907     if ( !not ) {
908                 ldap_unbind( ld );
909     }
910
911     if ( rejfp != NULL ) {
912             fclose( rejfp );
913     }
914
915     return( retval );
916 }
917
918
919 static int
920 process_ldif_rec( char *rbuf, int count )
921 {
922     char        *line, *dn, *type, *newrdn, *newsup, *p;
923     int         rc, linenum, modop, replicaport;
924     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
925     int         expect_deleteoldrdn, deleteoldrdn;
926     int         saw_replica, use_record, new_entry, delete_entry, got_all;
927     LDAPMod     **pmods;
928         int version;
929         struct berval val;
930     LDAPControl **pctrls;
931
932     new_entry = ldapadd;
933
934     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
935     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
936         expect_sep = expect_ct = 0;
937     linenum = 0;
938         version = 0;
939     deleteoldrdn = 1;
940     use_record = force;
941     pmods = NULL;
942     pctrls = NULL;
943     dn = newrdn = newsup = NULL;
944
945     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
946         ++linenum;
947
948         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
949             expect_sep = 0;
950             expect_ct = 1;
951             continue;
952         }
953         
954         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
955             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
956                     prog, linenum, dn == NULL ? "" : dn );
957             rc = LDAP_PARAM_ERROR;
958             break;
959         }
960
961         if ( dn == NULL ) {
962             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
963                 ++saw_replica;
964                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
965                     replicaport = 0;
966                 } else {
967                     *p++ = '\0';
968                     replicaport = atoi( p );
969                 }
970                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
971                         replicaport == ldapport ) {
972                     use_record = 1;
973                 }
974             } else if ( count == 1 && linenum == 1 && 
975                         strcasecmp( type, T_VERSION_STR ) == 0 )
976                 {
977                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
978                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
979                                 prog, val.bv_val, linenum );
980                         }
981                         version++;
982
983             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
984                 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
985                     perror( "strdup" );
986                     exit( EXIT_FAILURE );
987                 }
988                 expect_ct = 1;
989             }
990             goto end_line;      /* skip all lines until we see "dn:" */
991         }
992
993         if ( expect_ct ) {
994         
995         /* Check for "control" tag after dn and before changetype. */
996         if (strcasecmp(type, T_CONTROL_STR) == 0) {
997             /* Parse and add it to the list of controls */
998             rc = parse_ldif_control( line, &pctrls );
999             if (rc != 0) {
1000                         fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
1001                                 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
1002             }
1003             goto end_line;
1004         }
1005         
1006             expect_ct = 0;
1007             if ( !use_record && saw_replica ) {
1008                 printf( "%s: skipping change record for entry: %s\n"
1009                         "\t(LDAP host/port does not match replica: lines)\n",
1010                         prog, dn );
1011                 free( dn );
1012                 ber_memfree( type );
1013                 ber_memfree( val.bv_val );
1014                 return( 0 );
1015             }
1016
1017             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
1018 #ifdef LIBERAL_CHANGETYPE_MODOP
1019                 /* trim trailing spaces (and log warning ...) */
1020
1021                 int icnt;
1022                 for ( icnt = val.bv_len; --icnt > 0; ) {
1023                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1024                         break;
1025                     }
1026                 }
1027
1028                 if ( ++icnt != val.bv_len ) {
1029                     fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1030                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1031                     val.bv_val[icnt] = '\0';
1032                 }
1033 #endif /* LIBERAL_CHANGETYPE_MODOP */
1034
1035                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
1036                         new_entry = 0;
1037                         expect_modop = 1;
1038                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
1039                         new_entry = 1;
1040                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
1041                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
1042                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
1043                 {
1044                     expect_newrdn = 1;
1045                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
1046                     got_all = delete_entry = 1;
1047                 } else {
1048                     fprintf( stderr,
1049                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
1050                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1051                     rc = LDAP_PARAM_ERROR;
1052                 }
1053                 goto end_line;
1054             } else if ( ldapadd ) {             /*  missing changetype => add */
1055                 new_entry = 1;
1056                 modop = LDAP_MOD_ADD;
1057             } else {
1058                 expect_modop = 1;       /* missing changetype => modify */
1059             }
1060         }
1061
1062         if ( expect_modop ) {
1063 #ifdef LIBERAL_CHANGETYPE_MODOP
1064             /* trim trailing spaces (and log warning ...) */
1065             
1066             int icnt;
1067             for ( icnt = val.bv_len; --icnt > 0; ) {
1068                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1069                     break;
1070                 }
1071             }
1072             
1073             if ( ++icnt != val.bv_len ) {
1074                 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1075                         prog, type, val.bv_val, linenum, dn );
1076                 val.bv_val[icnt] = '\0';
1077             }
1078 #endif /* LIBERAL_CHANGETYPE_MODOP */
1079
1080             expect_modop = 0;
1081             expect_sep = 1;
1082             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1083                 modop = LDAP_MOD_ADD;
1084                 goto end_line;
1085             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1086                 modop = LDAP_MOD_REPLACE;
1087                 addmodifyop( &pmods, modop, val.bv_val, NULL );
1088                 goto end_line;
1089             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1090                 modop = LDAP_MOD_DELETE;
1091                 addmodifyop( &pmods, modop, val.bv_val, NULL );
1092                 goto end_line;
1093             } else {    /* no modify op:  use default */
1094                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1095             }
1096         }
1097
1098         if ( expect_newrdn ) {
1099             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1100                         if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
1101                     perror( "strdup" );
1102                     exit( EXIT_FAILURE );
1103                 }
1104                 expect_deleteoldrdn = 1;
1105                 expect_newrdn = 0;
1106             } else {
1107                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1108                         prog, T_NEWRDNSTR, type, linenum, dn );
1109                 rc = LDAP_PARAM_ERROR;
1110             }
1111         } else if ( expect_deleteoldrdn ) {
1112             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1113                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1114                 expect_deleteoldrdn = 0;
1115                 expect_newsup = 1;
1116                 got_all = 1;
1117             } else {
1118                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1119                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1120                 rc = LDAP_PARAM_ERROR;
1121             }
1122         } else if ( expect_newsup ) {
1123             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1124                 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
1125                     perror( "strdup" );
1126                     exit( EXIT_FAILURE );
1127                 }
1128                 expect_newsup = 0;
1129             } else {
1130                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1131                         prog, T_NEWSUPSTR, type, linenum, dn );
1132                 rc = LDAP_PARAM_ERROR;
1133             }
1134         } else if ( got_all ) {
1135             fprintf( stderr,
1136                     "%s: extra lines at end (line %d of entry \"%s\")\n",
1137                     prog, linenum, dn );
1138             rc = LDAP_PARAM_ERROR;
1139         } else {
1140                 addmodifyop( &pmods, modop, type, &val );
1141         }
1142
1143 end_line:
1144         ber_memfree( type );
1145         ber_memfree( val.bv_val );
1146     }
1147
1148         if( linenum == 0 ) {
1149                 return 0;
1150         }
1151
1152         if( version && linenum == 1 ) {
1153                 return 0;
1154         }
1155
1156     /* If default controls are set (as with -M option) and controls are
1157        specified in the LDIF file, we must add the default controls to
1158        the list of controls sent with the ldap operation.
1159     */
1160     if ( rc == 0 ) {
1161         if (pctrls) {
1162             LDAPControl **defctrls = NULL;   /* Default server controls */
1163             LDAPControl **newctrls = NULL;
1164             ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
1165             if (defctrls) {
1166                 int npc=0;                  /* Number of LDIF controls */
1167                 int ndefc=0;                /* Number of default controls */
1168                 while (pctrls[npc])         /* Count LDIF controls */
1169                     npc++; 
1170                 while (defctrls[ndefc])     /* Count default controls */
1171                     ndefc++;
1172                 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
1173                 if (newctrls == NULL)
1174                     rc = LDAP_NO_MEMORY;
1175                 else {
1176                     int i;
1177                     pctrls = newctrls;
1178                     for (i=npc; i<npc+ndefc; i++) {
1179                         pctrls[i] = ldap_control_dup(defctrls[i-npc]);
1180                         if (pctrls[i] == NULL) {
1181                             rc = LDAP_NO_MEMORY;
1182                             break;
1183                         }
1184                     }
1185                     pctrls[npc+ndefc] = NULL;
1186                     ldap_controls_free(defctrls);  /* Must be freed by library */
1187                 }
1188             }
1189         }
1190     }
1191
1192
1193     if ( rc == 0 ) {
1194         if ( delete_entry ) {
1195             rc = dodelete( dn, pctrls );
1196         } else if ( newrdn != NULL ) {
1197             rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
1198         } else {
1199             rc = domodify( dn, pmods, pctrls, new_entry );
1200         }
1201
1202         if ( rc == LDAP_SUCCESS ) {
1203             rc = 0;
1204         }
1205     }
1206
1207     if ( dn != NULL ) {
1208         free( dn );
1209     }
1210     if ( newrdn != NULL ) {
1211         free( newrdn );
1212     }
1213     if ( pmods != NULL ) {
1214         ldap_mods_free( pmods, 1 );
1215     }
1216
1217     if (pctrls != NULL) {
1218         ldap_controls_free( pctrls );
1219     }
1220
1221     return( rc );
1222 }
1223
1224 /* Parse an LDIF control line of the form
1225       control:  oid  [true/false]  [: value]              or
1226       control:  oid  [true/false]  [:: base64-value]      or
1227       control:  oid  [true/false]  [:< url]
1228    The control is added to the list of controls in *ppctrls.
1229 */      
1230 static int
1231 parse_ldif_control( char *line, 
1232                     LDAPControl ***ppctrls )
1233 {
1234     char *oid = NULL;
1235     int criticality = 0;   /* Default is false if not present */
1236     char *type=NULL;
1237     char *val = NULL;
1238     ber_len_t value_len = 0;
1239     int i, rc=0;
1240     char *s, *oidStart, *pcolon;
1241     LDAPControl *newctrl = NULL;
1242     LDAPControl **pctrls = NULL;
1243
1244     if (ppctrls) {
1245         pctrls = *ppctrls;
1246     }
1247     s = line + strlen(T_CONTROL_STR);  /* Skip over "control" */
1248     pcolon = s;                        /* Save this position for later */
1249     if (*s++ != ':')                   /* Make sure colon follows */
1250         return ( LDAP_PARAM_ERROR );
1251     while (*s && isspace(*s))  s++;    /* Skip white space before OID */
1252
1253     /* OID should come next. Validate and extract it. */
1254     if (*s == 0)
1255         return ( LDAP_PARAM_ERROR );
1256     oidStart = s;
1257     while (isdigit(*s) || *s == '.')  s++;    /* OID should be digits or . */
1258     if (s == oidStart) 
1259         return ( LDAP_PARAM_ERROR );   /* OID was not present */
1260     if (*s) {                          /* End of OID should be space or NULL */
1261         if (!isspace(*s))
1262             return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
1263         *s++ = 0;                    /* Replace space with null to terminate */
1264     }
1265
1266     
1267     oid = ber_strdup(oidStart);
1268     if (oid == NULL)
1269         return ( LDAP_NO_MEMORY );
1270
1271     /* Optional Criticality field is next. */
1272     while (*s && isspace(*s))  s++;   /* Skip white space before criticality */
1273     if (strncasecmp(s, "true", 4) == 0) {
1274         criticality = 1;
1275         s += 4;
1276     } 
1277     else if (strncasecmp(s, "false", 5) == 0) {
1278         criticality = 0;
1279         s += 5;
1280     }
1281
1282     /* Optional value field is next */
1283     while (*s && isspace(*s))  s++;    /* Skip white space before value */
1284     if (*s) {
1285         if (*s != ':') {           /* If value is present, must start with : */
1286             rc = LDAP_PARAM_ERROR;
1287             goto cleanup;
1288         }
1289
1290         /* Shift value down over OID and criticality so it's in the form
1291              control: value
1292              control:: base64-value
1293              control:< url
1294            Then we can use ldif_parse_line to extract and decode the value
1295         */
1296         while ( (*pcolon++ = *s++) != 0)     /* Shift value */
1297             ;
1298         rc = ldif_parse_line(line, &type, &val, &value_len);
1299         if (type)  ber_memfree(type);   /* Don't need this field*/
1300         if (rc < 0) {
1301             rc = LDAP_PARAM_ERROR;
1302             goto cleanup;
1303         }
1304     }
1305
1306     /* Create a new LDAPControl structure. */
1307     newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1308     if ( newctrl == NULL ) {
1309         rc = LDAP_NO_MEMORY;
1310         goto cleanup;
1311     }
1312     newctrl->ldctl_oid = oid;
1313     oid = NULL;
1314     newctrl->ldctl_iscritical = criticality;
1315     newctrl->ldctl_value.bv_len = value_len;
1316     newctrl->ldctl_value.bv_val = val;
1317     val = NULL;
1318
1319     /* Add the new control to the passed-in list of controls. */
1320     i = 0;
1321     if (pctrls) {
1322         while ( pctrls[i] )      /* Count the # of controls passed in */
1323             i++;
1324     }
1325     /* Allocate 1 more slot for the new control and 1 for the NULL. */
1326     pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
1327     if (pctrls == NULL) {
1328         rc = LDAP_NO_MEMORY;
1329         goto cleanup;
1330     }
1331     pctrls[i] = newctrl;
1332     newctrl = NULL;
1333     pctrls[i+1] = NULL;
1334     *ppctrls = pctrls;
1335
1336 cleanup:
1337     if (newctrl) {
1338         if (newctrl->ldctl_oid)
1339             ber_memfree(newctrl->ldctl_oid);
1340         if (newctrl->ldctl_value.bv_val)
1341             ber_memfree(newctrl->ldctl_value.bv_val);
1342         ber_memfree(newctrl);
1343     }
1344     if (val)
1345         ber_memfree(val);
1346     if (oid)
1347         ber_memfree(oid);
1348
1349     return( rc );
1350 }
1351
1352
1353 static void
1354 addmodifyop(
1355         LDAPMod ***pmodsp,
1356         int modop,
1357         const char *attr,
1358         struct berval *val )
1359 {
1360         LDAPMod         **pmods;
1361         int                     i, j;
1362
1363         pmods = *pmodsp;
1364         modop |= LDAP_MOD_BVALUES;
1365
1366         i = 0;
1367         if ( pmods != NULL ) {
1368                 for ( ; pmods[ i ] != NULL; ++i ) {
1369                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1370                                 pmods[ i ]->mod_op == modop )
1371                         {
1372                                 break;
1373                         }
1374                 }
1375         }
1376
1377         if ( pmods == NULL || pmods[ i ] == NULL ) {
1378                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1379                         sizeof( LDAPMod * ))) == NULL )
1380                 {
1381                         perror( "realloc" );
1382                         exit( EXIT_FAILURE );
1383                 }
1384
1385                 *pmodsp = pmods;
1386                 pmods[ i + 1 ] = NULL;
1387
1388                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1389                 if ( pmods[ i ] == NULL ) {
1390                         perror( "calloc" );
1391                         exit( EXIT_FAILURE );
1392                 }
1393
1394                 pmods[ i ]->mod_op = modop;
1395                 pmods[ i ]->mod_type = ber_strdup( attr );
1396                 if ( pmods[ i ]->mod_type == NULL ) {
1397                         perror( "strdup" );
1398                         exit( EXIT_FAILURE );
1399                 }
1400         }
1401
1402         if ( val != NULL ) {
1403                 j = 0;
1404                 if ( pmods[ i ]->mod_bvalues != NULL ) {
1405                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1406                                 /* Empty */;
1407                         }
1408                 }
1409
1410                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1411                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1412                 if ( pmods[ i ]->mod_bvalues == NULL ) {
1413                         perror( "ber_realloc" );
1414                         exit( EXIT_FAILURE );
1415                 }
1416
1417                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1418                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1419                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1420                         perror( "ber_bvdup" );
1421                         exit( EXIT_FAILURE );
1422                 }
1423         }
1424 }
1425
1426
1427 static int
1428 domodify(
1429         const char *dn,
1430         LDAPMod **pmods,
1431     LDAPControl **pctrls,
1432         int newentry )
1433 {
1434     int                 i, j, k, notascii, op;
1435     struct berval       *bvp;
1436
1437     if ( pmods == NULL ) {
1438         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1439                 prog, dn );
1440         return( LDAP_PARAM_ERROR );
1441     } 
1442
1443     for ( i = 0; pmods[ i ] != NULL; ++i ) {
1444         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1445         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1446                 fprintf( stderr,
1447                         "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1448                         prog, pmods[i]->mod_type, dn );
1449                 return LDAP_PARAM_ERROR;
1450         }
1451     }
1452
1453     if ( verbose ) {
1454         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1455             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1456             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1457                     "replace" : op == LDAP_MOD_ADD ?
1458                     "add" : "delete", pmods[ i ]->mod_type );
1459             if ( pmods[ i ]->mod_bvalues != NULL ) {
1460                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1461                     bvp = pmods[ i ]->mod_bvalues[ j ];
1462                     notascii = 0;
1463                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1464                         if ( !isascii( bvp->bv_val[ k ] )) {
1465                             notascii = 1;
1466                             break;
1467                         }
1468                     }
1469                     if ( notascii ) {
1470                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1471                     } else {
1472                         printf( "\t%s\n", bvp->bv_val );
1473                     }
1474                 }
1475             }
1476         }
1477     }
1478
1479     if ( newentry ) {
1480         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1481     } else {
1482         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1483     }
1484
1485     if ( !not ) {
1486         if ( newentry ) {
1487             i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
1488         } else {
1489             i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
1490         }
1491         if ( i != LDAP_SUCCESS ) {
1492                 /* print error message about failed update including DN */
1493                 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1494                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1495         } else if ( verbose ) {
1496             printf( "modify complete\n" );
1497         }
1498     } else {
1499         i = LDAP_SUCCESS;
1500     }
1501
1502     putchar( '\n' );
1503
1504     return( i );
1505 }
1506
1507
1508 static int
1509 dodelete(
1510         const char *dn,
1511     LDAPControl **pctrls )
1512 {
1513     int rc;
1514
1515     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1516     if ( !not ) {
1517         if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
1518                 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1519                 ldap_perror( ld, "ldap_delete" );
1520         } else if ( verbose ) {
1521             printf( "delete complete" );
1522         }
1523     } else {
1524         rc = LDAP_SUCCESS;
1525     }
1526
1527     putchar( '\n' );
1528
1529     return( rc );
1530 }
1531
1532
1533 static int
1534 dorename(
1535         const char *dn,
1536         const char *newrdn,
1537         const char* newsup,
1538         int deleteoldrdn,
1539     LDAPControl **pctrls )
1540 {
1541     int rc;
1542
1543
1544     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1545     if ( verbose ) {
1546         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1547                 newrdn, deleteoldrdn ? "do not " : "" );
1548     }
1549     if ( !not ) {
1550         if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
1551                 != LDAP_SUCCESS ) {
1552                 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1553                 ldap_perror( ld, "ldap_modrdn" );
1554         } else {
1555             printf( "modrdn completed\n" );
1556         }
1557     } else {
1558         rc = LDAP_SUCCESS;
1559     }
1560
1561     putchar( '\n' );
1562
1563     return( rc );
1564 }
1565
1566
1567 static char *
1568 read_one_record( FILE *fp )
1569 {
1570     char        *buf, line[ LDAPMOD_MAXLINE ];
1571     int         lcur, lmax;
1572
1573     lcur = lmax = 0;
1574     buf = NULL;
1575
1576     while ( fgets( line, sizeof(line), fp ) != NULL ) {
1577         int len = strlen( line );
1578
1579                 if( len < 2 || ( len == 2 && *line == '\r' )) {
1580                         if( buf == NULL ) {
1581                                 continue;
1582                         } else {
1583                                 break;
1584                         }
1585                 }
1586
1587                 if ( lcur + len + 1 > lmax ) {
1588                         lmax = LDAPMOD_MAXLINE
1589                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1590
1591                         if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1592                                 perror( "realloc" );
1593                                 exit( EXIT_FAILURE );
1594                         }
1595                 }
1596
1597                 strcpy( buf + lcur, line );
1598                 lcur += len;
1599     }
1600
1601     return( buf );
1602 }
1603
1604