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