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