]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Sync with HEAD
[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_ldap.h"
33 #include "ldif.h"
34 #include "ldap_defaults.h"
35 #include "ldap_log.h"
36
37 static char     *prog;
38 static char     *binddn = NULL;
39 static struct berval passwd = { 0, NULL };
40 static char *ldapuri = NULL;
41 static char     *ldaphost = NULL;
42 static int      ldapport = 0;
43 #ifdef HAVE_CYRUS_SASL
44 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
45 static char *sasl_realm = NULL;
46 static char     *sasl_authc_id = NULL;
47 static char     *sasl_authz_id = NULL;
48 static char     *sasl_mech = NULL;
49 static char     *sasl_secprops = NULL;
50 #endif
51 static int      use_tls = 0;
52 static int      ldapadd, not, verbose, contoper, force;
53 static LDAP     *ld = NULL;
54
55 #define LDAPMOD_MAXLINE         4096
56
57 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
58 #define T_VERSION_STR           "version"
59 #define T_REPLICA_STR           "replica"
60 #define T_DN_STR                "dn"
61 #define T_CHANGETYPESTR         "changetype"
62 #define T_ADDCTSTR              "add"
63 #define T_MODIFYCTSTR           "modify"
64 #define T_DELETECTSTR           "delete"
65 #define T_MODRDNCTSTR           "modrdn"
66 #define T_MODDNCTSTR            "moddn"
67 #define T_RENAMECTSTR           "rename"
68 #define T_MODOPADDSTR           "add"
69 #define T_MODOPREPLACESTR       "replace"
70 #define T_MODOPDELETESTR        "delete"
71 #define T_MODSEPSTR             "-"
72 #define T_NEWRDNSTR             "newrdn"
73 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
74 #define T_NEWSUPSTR             "newsuperior"
75
76
77 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
78 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
79 static void addmodifyop LDAP_P((
80         LDAPMod ***pmodsp, int modop,
81         const char *attr,
82         struct berval *value ));
83 static int domodify LDAP_P((
84         const char *dn,
85         LDAPMod **pmods,
86         int newentry ));
87 static int dodelete LDAP_P((
88         const char *dn ));
89 static int dorename LDAP_P((
90         const char *dn,
91         const char *newrdn,
92         const char *newsup,
93         int deleteoldrdn ));
94 static char *read_one_record LDAP_P(( FILE *fp ));
95
96 static void
97 usage( const char *prog )
98 {
99     fprintf( stderr,
100 "Add or modify entries from an LDAP server\n\n"
101 "usage: %s [options]\n"
102 "       The list of desired operations are read from stdin or from the file\n"
103 "       specified by \"-f file\".\n"
104 "Add or modify options:\n"
105 "  -a         add values (default%s)\n"
106 "  -c         continuous operation mode (do not stop on errors)\n"
107 "  -f file    read operations from `file'\n"
108 "  -F         force all changes records to be used\n"
109 "  -S file    write skipped modifications to `file'\n"
110
111 "Common options:\n"
112 "  -d level   set LDAP debugging level to `level'\n"
113 "  -D binddn  bind DN\n"
114 "  -h host    LDAP server\n"
115 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
116 "  -I         use SASL Interactive mode\n"
117 "  -k         use Kerberos authentication\n"
118 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
119 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
120 "  -n         show what would be done but don't actually update\n"
121 "  -O props   SASL security properties\n"
122 "  -p port    port on LDAP server\n"
123 "  -P version procotol version (default: 3)\n"
124 "  -Q         use SASL Quiet mode\n"
125 "  -R realm   SASL realm\n"
126 "  -U authcid SASL authentication identity\n"
127 "  -v         run in verbose mode (diagnostics to standard output)\n"
128 "  -w passwd  bind passwd (for simple authentication)\n"
129 "  -W         prompt for bind passwd\n"
130 "  -x         Simple authentication\n"
131 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
132 "  -Y mech    SASL mechanism\n"
133 "  -Z         Start TLS request (-ZZ to require successful response)\n"
134              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
135
136     exit( EXIT_FAILURE );
137 }
138
139
140 int
141 main( int argc, char **argv )
142 {
143     char                *infile, *rejfile, *rbuf, *start, *rejbuf;
144     FILE                *fp, *rejfp;
145         char            *matched_msg = NULL, *error_msg = NULL;
146         int             rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
147         int count, len;
148
149     if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
150         prog = argv[ 0 ];
151     } else {
152         ++prog;
153     }
154
155     /* Print usage when no parameters */
156     if( argc < 2 ) usage( prog );
157
158         /* strncmp instead of strcmp since NT binaries carry .exe extension */
159     ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
160
161     infile = NULL;
162     rejfile = NULL;
163     not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
164     authmethod = -1;
165         version = -1;
166
167     while (( i = getopt( argc, argv, "acrf:F"
168                 "Cd:D:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:Y:Z" )) != EOF )
169         {
170         switch( i ) {
171         /* Modify Options */
172         case 'a':       /* add */
173             ldapadd = 1;
174             break;
175         case 'c':       /* continuous operation */
176             contoper = 1;
177             break;
178         case 'f':       /* read from file */
179                 if( infile != NULL ) {
180                         fprintf( stderr, "%s: -f previously specified\n", prog );
181                         return EXIT_FAILURE;
182                 }
183             infile = strdup( optarg );
184             break;
185         case 'F':       /* force all changes records to be used */
186             force = 1;
187             break;
188
189         /* Common Options */
190         case 'C':
191                 referrals++;
192                 break;
193         case 'd':
194             debug |= atoi( optarg );
195             break;
196         case 'D':       /* bind DN */
197                 if( binddn != NULL ) {
198                         fprintf( stderr, "%s: -D previously specified\n", prog );
199                         return EXIT_FAILURE;
200                 }
201             binddn = strdup( optarg );
202             break;
203         case 'h':       /* ldap host */
204                 if( ldapuri != NULL ) {
205                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
206                         return EXIT_FAILURE;
207                 }
208                 if( ldaphost != NULL ) {
209                         fprintf( stderr, "%s: -h previously specified\n", prog );
210                         return EXIT_FAILURE;
211                 }
212             ldaphost = strdup( optarg );
213             break;
214         case 'H':       /* ldap URI */
215                 if( ldaphost != NULL ) {
216                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
217                         return EXIT_FAILURE;
218                 }
219                 if( ldapport ) {
220                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
221                         return EXIT_FAILURE;
222                 }
223                 if( ldapuri != NULL ) {
224                         fprintf( stderr, "%s: -H previously specified\n", prog );
225                         return EXIT_FAILURE;
226                 }
227             ldapuri = strdup( optarg );
228             break;
229         case 'I':
230 #ifdef HAVE_CYRUS_SASL
231                 if( version == LDAP_VERSION2 ) {
232                         fprintf( stderr, "%s: -I incompatible with version %d\n",
233                                 prog, version );
234                         return EXIT_FAILURE;
235                 }
236                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
237                         fprintf( stderr, "%s: incompatible previous "
238                                 "authentication choice\n",
239                                 prog );
240                         return EXIT_FAILURE;
241                 }
242                 authmethod = LDAP_AUTH_SASL;
243                 version = LDAP_VERSION3;
244                 sasl_flags = LDAP_SASL_INTERACTIVE;
245                 break;
246 #else
247                 fprintf( stderr, "%s: was not compiled with SASL support\n",
248                         prog );
249                 return( EXIT_FAILURE );
250 #endif
251         case 'k':       /* kerberos bind */
252 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
253                 if( version > LDAP_VERSION2 ) {
254                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
255                                 prog, version );
256                         return EXIT_FAILURE;
257                 }
258
259                 if( authmethod != -1 ) {
260                         fprintf( stderr, "%s: -k incompatible with previous "
261                                 "authentication choice\n", prog );
262                         return EXIT_FAILURE;
263                 }
264                         
265                 authmethod = LDAP_AUTH_KRBV4;
266 #else
267                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
268                 return EXIT_FAILURE;
269 #endif
270             break;
271         case 'K':       /* kerberos bind, part one only */
272 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
273                 if( version > LDAP_VERSION2 ) {
274                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
275                                 prog, version );
276                         return EXIT_FAILURE;
277                 }
278                 if( authmethod != -1 ) {
279                         fprintf( stderr, "%s: incompatible with previous "
280                                 "authentication choice\n", prog );
281                         return EXIT_FAILURE;
282                 }
283
284                 authmethod = LDAP_AUTH_KRBV41;
285 #else
286                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
287                 return( EXIT_FAILURE );
288 #endif
289             break;
290         case 'M':
291                 /* enable Manage DSA IT */
292                 if( version == LDAP_VERSION2 ) {
293                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
294                                 prog, version );
295                         return EXIT_FAILURE;
296                 }
297                 manageDSAit++;
298                 version = LDAP_VERSION3;
299                 break;
300         case 'n':       /* print deletes, don't actually do them */
301             ++not;
302             break;
303         case 'O':
304 #ifdef HAVE_CYRUS_SASL
305                 if( sasl_secprops != NULL ) {
306                         fprintf( stderr, "%s: -O previously specified\n", prog );
307                         return EXIT_FAILURE;
308                 }
309                 if( version == LDAP_VERSION2 ) {
310                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
311                                 prog, version );
312                         return EXIT_FAILURE;
313                 }
314                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
315                         fprintf( stderr, "%s: incompatible previous "
316                                 "authentication choice\n", prog );
317                         return EXIT_FAILURE;
318                 }
319                 authmethod = LDAP_AUTH_SASL;
320                 version = LDAP_VERSION3;
321                 sasl_secprops = strdup( optarg );
322 #else
323                 fprintf( stderr, "%s: not compiled with SASL support\n",
324                         prog );
325                 return( EXIT_FAILURE );
326 #endif
327                 break;
328         case 'p':
329                 if( ldapport ) {
330                         fprintf( stderr, "%s: -p previously specified\n", prog );
331                         return EXIT_FAILURE;
332                 }
333             ldapport = atoi( optarg );
334             break;
335         case 'P':
336                 switch( atoi(optarg) ) {
337                 case 2:
338                         if( version == LDAP_VERSION3 ) {
339                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
340                                         prog, version );
341                                 return EXIT_FAILURE;
342                         }
343                         version = LDAP_VERSION2;
344                         break;
345                 case 3:
346                         if( version == LDAP_VERSION2 ) {
347                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
348                                         prog, version );
349                                 return EXIT_FAILURE;
350                         }
351                         version = LDAP_VERSION3;
352                         break;
353                 default:
354                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
355                                 prog );
356                         usage( prog );
357                         return( EXIT_FAILURE );
358                 } break;
359         case 'Q':
360 #ifdef HAVE_CYRUS_SASL
361                 if( version == LDAP_VERSION2 ) {
362                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
363                                 prog, version );
364                         return EXIT_FAILURE;
365                 }
366                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
367                         fprintf( stderr, "%s: incompatible previous "
368                                 "authentication choice\n",
369                                 prog );
370                         return EXIT_FAILURE;
371                 }
372                 authmethod = LDAP_AUTH_SASL;
373                 version = LDAP_VERSION3;
374                 sasl_flags = LDAP_SASL_QUIET;
375                 break;
376 #else
377                 fprintf( stderr, "%s: not compiled with SASL support\n",
378                         prog );
379                 return( EXIT_FAILURE );
380 #endif
381         case 'r':       /* replace (obsolete) */
382                 break;
383
384         case 'R':
385 #ifdef HAVE_CYRUS_SASL
386                 if( sasl_realm != NULL ) {
387                         fprintf( stderr, "%s: -R previously specified\n", prog );
388                         return EXIT_FAILURE;
389                 }
390                 if( version == LDAP_VERSION2 ) {
391                         fprintf( stderr, "%s: -R incompatible with version %d\n",
392                                 prog, version );
393                         return EXIT_FAILURE;
394                 }
395                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
396                         fprintf( stderr, "%s: incompatible previous "
397                                 "authentication choice\n",
398                                 prog );
399                         return EXIT_FAILURE;
400                 }
401                 authmethod = LDAP_AUTH_SASL;
402                 version = LDAP_VERSION3;
403                 sasl_realm = strdup( optarg );
404 #else
405                 fprintf( stderr, "%s: not compiled with SASL support\n",
406                         prog );
407                 return( EXIT_FAILURE );
408 #endif
409                 break;
410         case 'S':       /* skipped modifications to file */
411                 if( rejfile != NULL ) {
412                         fprintf( stderr, "%s: -S previously specified\n", prog );
413                         return EXIT_FAILURE;
414                 }
415                 rejfile = strdup( optarg );
416                 break;
417         case 'U':
418 #ifdef HAVE_CYRUS_SASL
419                 if( sasl_authc_id != NULL ) {
420                         fprintf( stderr, "%s: -U previously specified\n", prog );
421                         return EXIT_FAILURE;
422                 }
423                 if( version == LDAP_VERSION2 ) {
424                         fprintf( stderr, "%s: -U incompatible with version %d\n",
425                                 prog, version );
426                         return EXIT_FAILURE;
427                 }
428                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429                         fprintf( stderr, "%s: incompatible previous "
430                                 "authentication choice\n",
431                                 prog );
432                         return EXIT_FAILURE;
433                 }
434                 authmethod = LDAP_AUTH_SASL;
435                 version = LDAP_VERSION3;
436                 sasl_authc_id = strdup( optarg );
437 #else
438                 fprintf( stderr, "%s: not compiled with SASL support\n",
439                         prog );
440                 return( EXIT_FAILURE );
441 #endif
442                 break;
443         case 'v':       /* verbose mode */
444             verbose++;
445             break;
446         case 'w':       /* password */
447             passwd.bv_val = strdup( optarg );
448                 {
449                         char* p;
450
451                         for( p = optarg; *p != '\0'; p++ ) {
452                                 *p = '\0';
453                         }
454                 }
455                 passwd.bv_len = strlen( passwd.bv_val );
456             break;
457         case 'W':
458                 want_bindpw++;
459                 break;
460         case 'Y':
461 #ifdef HAVE_CYRUS_SASL
462                 if( sasl_mech != NULL ) {
463                         fprintf( stderr, "%s: -Y previously specified\n", prog );
464                         return EXIT_FAILURE;
465                 }
466                 if( version == LDAP_VERSION2 ) {
467                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
468                                 prog, version );
469                         return EXIT_FAILURE;
470                 }
471                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
472                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
473                         return EXIT_FAILURE;
474                 }
475                 authmethod = LDAP_AUTH_SASL;
476                 version = LDAP_VERSION3;
477                 sasl_mech = strdup( optarg );
478 #else
479                 fprintf( stderr, "%s: not compiled with SASL support\n",
480                         prog );
481                 return( EXIT_FAILURE );
482 #endif
483                 break;
484         case 'x':
485                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
486                         fprintf( stderr, "%s: incompatible with previous "
487                                 "authentication choice\n", prog );
488                         return EXIT_FAILURE;
489                 }
490                 authmethod = LDAP_AUTH_SIMPLE;
491                 break;
492         case 'X':
493 #ifdef HAVE_CYRUS_SASL
494                 if( sasl_authz_id != NULL ) {
495                         fprintf( stderr, "%s: -X previously specified\n", prog );
496                         return EXIT_FAILURE;
497                 }
498                 if( version == LDAP_VERSION2 ) {
499                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
500                                 prog, version );
501                         return EXIT_FAILURE;
502                 }
503                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
504                         fprintf( stderr, "%s: -X incompatible with "
505                                 "authentication choice\n", prog );
506                         return EXIT_FAILURE;
507                 }
508                 authmethod = LDAP_AUTH_SASL;
509                 version = LDAP_VERSION3;
510                 sasl_authz_id = strdup( optarg );
511 #else
512                 fprintf( stderr, "%s: not compiled with SASL support\n",
513                         prog );
514                 return( EXIT_FAILURE );
515 #endif
516                 break;
517         case 'Z':
518 #ifdef HAVE_TLS
519                 if( version == LDAP_VERSION2 ) {
520                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
521                                 prog, version );
522                         return EXIT_FAILURE;
523                 }
524                 version = LDAP_VERSION3;
525                 use_tls++;
526 #else
527                 fprintf( stderr, "%s: not compiled with TLS support\n",
528                         prog );
529                 return( EXIT_FAILURE );
530 #endif
531                 break;
532         default:
533                 fprintf( stderr, "%s: unrecognized option -%c\n",
534                         prog, optopt );
535             usage( prog );
536         }
537     }
538
539         if (version == -1) {
540                 version = LDAP_VERSION3;
541         }
542         if (authmethod == -1 && version > LDAP_VERSION2) {
543 #ifdef HAVE_CYRUS_SASL
544                 authmethod = LDAP_AUTH_SASL;
545 #else
546                 authmethod = LDAP_AUTH_SIMPLE;
547 #endif
548         }
549
550         if ( argc != optind )
551         usage( prog );
552
553     if ( rejfile != NULL ) {
554         if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
555             perror( rejfile );
556             return( EXIT_FAILURE );
557         }
558     } else {
559         rejfp = NULL;
560     }
561
562     if ( infile != NULL ) {
563         if (( fp = fopen( infile, "r" )) == NULL ) {
564             perror( infile );
565             return( EXIT_FAILURE );
566         }
567     } else {
568         fp = stdin;
569     }
570
571         if ( debug ) {
572                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
573                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
574                 }
575                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
576                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
577                 }
578                 ldif_debug = debug;
579         }
580
581 #ifdef SIGPIPE
582         (void) SIGNAL( SIGPIPE, SIG_IGN );
583 #endif
584
585 #ifdef NEW_LOGGING
586     lutil_log_initialize( argc, argv );
587 #endif
588
589     if ( !not ) {
590         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
591                 if ( verbose ) {
592                         fprintf( stderr, "ldap_init( %s, %d )\n",
593                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
594                                 ldapport );
595                 }
596
597                 ld = ldap_init( ldaphost, ldapport );
598                 if( ld == NULL ) {
599                         perror("ldapmodify: ldap_init");
600                         return EXIT_FAILURE;
601                 }
602
603         } else {
604                 if ( verbose ) {
605                         fprintf( stderr, "ldap_initialize( %s )\n",
606                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
607                 }
608
609                 rc = ldap_initialize( &ld, ldapuri );
610                 if( rc != LDAP_SUCCESS ) {
611                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
612                                 rc, ldap_err2string(rc) );
613                         return EXIT_FAILURE;
614                 }
615         }
616
617         /* referrals */
618         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
619                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
620         {
621                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
622                         referrals ? "on" : "off" );
623                 return EXIT_FAILURE;
624         }
625
626
627         if (version == -1 ) {
628                 version = LDAP_VERSION3;
629         }
630
631         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
632                 != LDAP_OPT_SUCCESS )
633         {
634                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
635                         version );
636                 return EXIT_FAILURE;
637         }
638
639         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
640                 ldap_perror( ld, "ldap_start_tls" );
641                 if ( use_tls > 1 ) {
642                         return( EXIT_FAILURE );
643                 }
644         }
645
646         if (want_bindpw) {
647                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
648                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
649         }
650
651         if ( authmethod == LDAP_AUTH_SASL ) {
652 #ifdef HAVE_CYRUS_SASL
653                 void *defaults;
654
655                 if( sasl_secprops != NULL ) {
656                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
657                                 (void *) sasl_secprops );
658                         
659                         if( rc != LDAP_OPT_SUCCESS ) {
660                                 fprintf( stderr,
661                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
662                                         sasl_secprops );
663                                 return( EXIT_FAILURE );
664                         }
665                 }
666                 
667                 defaults = lutil_sasl_defaults( ld,
668                         sasl_mech,
669                         sasl_realm,
670                         sasl_authc_id,
671                         passwd.bv_val,
672                         sasl_authz_id );
673
674                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
675                         sasl_mech, NULL, NULL,
676                         sasl_flags, lutil_sasl_interact, defaults );
677
678                 if( rc != LDAP_SUCCESS ) {
679                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
680                         return( EXIT_FAILURE );
681                 }
682 #else
683                 fprintf( stderr, "%s: not compiled with SASL support\n",
684                         prog );
685                 return( EXIT_FAILURE );
686 #endif
687         }
688         else {
689                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
690                                 != LDAP_SUCCESS ) {
691                         ldap_perror( ld, "ldap_bind" );
692                         return( EXIT_FAILURE );
693                 }
694
695         }
696
697     }
698
699     rc = 0;
700
701         if ( manageDSAit ) {
702                 int err;
703                 LDAPControl c;
704                 LDAPControl *ctrls[2];
705                 ctrls[0] = &c;
706                 ctrls[1] = NULL;
707
708                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
709                 c.ldctl_value.bv_val = NULL;
710                 c.ldctl_value.bv_len = 0;
711                 c.ldctl_iscritical = manageDSAit > 1;
712
713                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
714
715                 if( err != LDAP_OPT_SUCCESS ) {
716                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
717                                 c.ldctl_iscritical ? "critical " : "" );
718                         if( c.ldctl_iscritical ) {
719                                 exit( EXIT_FAILURE );
720                         }
721                 }
722         }
723
724         count = 0;
725     while (( rc == 0 || contoper ) &&
726                 ( rbuf = read_one_record( fp )) != NULL ) {
727         count++;
728
729         start = rbuf;
730
731         if ( rejfp ) {
732                 len = strlen( rbuf );
733                 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
734                         perror( "realloc" );
735                         exit( EXIT_FAILURE );
736                 }
737                 memcpy( rejbuf, rbuf, len+1 );
738         }
739
740     rc = process_ldif_rec( start, count );
741
742         if ( rc && rejfp ) {
743                 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
744
745                 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
746                 if ( matched_msg != NULL && *matched_msg != '\0' ) {
747                         fprintf( rejfp, ", matched DN: %s", matched_msg );
748                 }
749
750                 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
751                 if ( error_msg != NULL && *error_msg != '\0' ) {
752                         fprintf( rejfp, ", additional info: %s", error_msg );
753                 }
754                 fprintf( rejfp, "\n%s\n", rejbuf );
755         }
756                 if (rejfp) 
757                         free( rejbuf );
758                 free( rbuf );
759     }
760
761     if ( !not ) {
762                 ldap_unbind( ld );
763     }
764
765     if ( rejfp != NULL ) {
766             fclose( rejfp );
767     }
768
769         return( rc );
770 }
771
772
773 static int
774 process_ldif_rec( char *rbuf, int count )
775 {
776     char        *line, *dn, *type, *newrdn, *newsup, *p;
777     int         rc, linenum, modop, replicaport;
778     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
779     int         expect_deleteoldrdn, deleteoldrdn;
780     int         saw_replica, use_record, new_entry, delete_entry, got_all;
781     LDAPMod     **pmods;
782         int version;
783         struct berval val;
784
785     new_entry = ldapadd;
786
787     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
788     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
789         expect_sep = expect_ct = 0;
790     linenum = 0;
791         version = 0;
792     deleteoldrdn = 1;
793     use_record = force;
794     pmods = NULL;
795     dn = newrdn = newsup = NULL;
796
797     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
798         ++linenum;
799
800         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
801             expect_sep = 0;
802             expect_ct = 1;
803             continue;
804         }
805         
806         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
807             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
808                     prog, linenum, dn == NULL ? "" : dn );
809             rc = LDAP_PARAM_ERROR;
810             break;
811         }
812
813         if ( dn == NULL ) {
814             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
815                 ++saw_replica;
816                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
817                     replicaport = 0;
818                 } else {
819                     *p++ = '\0';
820                     replicaport = atoi( p );
821                 }
822                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
823                         replicaport == ldapport ) {
824                     use_record = 1;
825                 }
826             } else if ( count == 1 && linenum == 1 && 
827                         strcasecmp( type, T_VERSION_STR ) == 0 )
828                 {
829                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
830                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
831                                 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
832                         }
833                         version++;
834
835             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
836                 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
837                     perror( "strdup" );
838                     exit( EXIT_FAILURE );
839                 }
840                 expect_ct = 1;
841             }
842             goto end_line;      /* skip all lines until we see "dn:" */
843         }
844
845         if ( expect_ct ) {
846             expect_ct = 0;
847             if ( !use_record && saw_replica ) {
848                 printf( "%s: skipping change record for entry: %s\n"
849                         "\t(LDAP host/port does not match replica: lines)\n",
850                         prog, dn );
851                 free( dn );
852                 ber_memfree( type );
853                 ber_memfree( val.bv_val );
854                 return( 0 );
855             }
856
857             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
858 #ifdef LIBERAL_CHANGETYPE_MODOP
859                 /* trim trailing spaces (and log warning ...) */
860
861                 int icnt;
862                 for ( icnt = val.bv_len; --icnt > 0; ) {
863                     if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
864                         break;
865                     }
866                 }
867
868                 if ( ++icnt != val.bv_len ) {
869                     fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
870                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
871                     val.bv_val[icnt] = '\0';
872                 }
873 #endif /* LIBERAL_CHANGETYPE_MODOP */
874
875                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
876                         new_entry = 0;
877                         expect_modop = 1;
878                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
879                         new_entry = 1;
880                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
881                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
882                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
883                 {
884                     expect_newrdn = 1;
885                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
886                     got_all = delete_entry = 1;
887                 } else {
888                     fprintf( stderr,
889                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
890                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
891                     rc = LDAP_PARAM_ERROR;
892                 }
893                 goto end_line;
894             } else if ( ldapadd ) {             /*  missing changetype => add */
895                 new_entry = 1;
896                 modop = LDAP_MOD_ADD;
897             } else {
898                 expect_modop = 1;       /* missing changetype => modify */
899             }
900         }
901
902         if ( expect_modop ) {
903 #ifdef LIBERAL_CHANGETYPE_MODOP
904             /* trim trailing spaces (and log warning ...) */
905             
906             int icnt;
907             for ( icnt = val.bv_len; --icnt > 0; ) {
908                 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
909                     break;
910                 }
911             }
912             
913             if ( ++icnt != val.bv_len ) {
914                 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
915                         prog, type, val.bv_val, linenum, dn );
916                 val.bv_val[icnt] = '\0';
917             }
918 #endif /* LIBERAL_CHANGETYPE_MODOP */
919
920             expect_modop = 0;
921             expect_sep = 1;
922             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
923                 modop = LDAP_MOD_ADD;
924                 goto end_line;
925             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
926                 modop = LDAP_MOD_REPLACE;
927                 addmodifyop( &pmods, modop, val.bv_val, NULL );
928                 goto end_line;
929             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
930                 modop = LDAP_MOD_DELETE;
931                 addmodifyop( &pmods, modop, val.bv_val, NULL );
932                 goto end_line;
933             } else {    /* no modify op:  use default */
934                 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
935             }
936         }
937
938         if ( expect_newrdn ) {
939             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
940                         if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
941                     perror( "strdup" );
942                     exit( EXIT_FAILURE );
943                 }
944                 expect_deleteoldrdn = 1;
945                 expect_newrdn = 0;
946             } else {
947                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
948                         prog, T_NEWRDNSTR, type, linenum, dn );
949                 rc = LDAP_PARAM_ERROR;
950             }
951         } else if ( expect_deleteoldrdn ) {
952             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
953                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
954                 expect_deleteoldrdn = 0;
955                 expect_newsup = 1;
956                 got_all = 1;
957             } else {
958                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
959                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
960                 rc = LDAP_PARAM_ERROR;
961             }
962         } else if ( expect_newsup ) {
963             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
964                 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
965                     perror( "strdup" );
966                     exit( EXIT_FAILURE );
967                 }
968                 expect_newsup = 0;
969             } else {
970                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
971                         prog, T_NEWSUPSTR, type, linenum, dn );
972                 rc = LDAP_PARAM_ERROR;
973             }
974         } else if ( got_all ) {
975             fprintf( stderr,
976                     "%s: extra lines at end (line %d of entry \"%s\")\n",
977                     prog, linenum, dn );
978             rc = LDAP_PARAM_ERROR;
979         } else {
980                 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
981         }
982
983 end_line:
984         ber_memfree( type );
985         ber_memfree( val.bv_val );
986     }
987
988         if( linenum == 0 ) {
989                 return 0;
990         }
991
992         if( version && linenum == 1 ) {
993                 return 0;
994         }
995
996     if ( rc == 0 ) {
997         if ( delete_entry ) {
998             rc = dodelete( dn );
999         } else if ( newrdn != NULL ) {
1000             rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1001         } else {
1002             rc = domodify( dn, pmods, new_entry );
1003         }
1004
1005         if ( rc == LDAP_SUCCESS ) {
1006             rc = 0;
1007         }
1008     }
1009
1010     if ( dn != NULL ) {
1011         free( dn );
1012     }
1013     if ( newrdn != NULL ) {
1014         free( newrdn );
1015     }
1016     if ( pmods != NULL ) {
1017         ldap_mods_free( pmods, 1 );
1018     }
1019
1020     return( rc );
1021 }
1022
1023
1024 static void
1025 addmodifyop(
1026         LDAPMod ***pmodsp,
1027         int modop,
1028         const char *attr,
1029         struct berval *val )
1030 {
1031         LDAPMod         **pmods;
1032         int                     i, j;
1033
1034         pmods = *pmodsp;
1035         modop |= LDAP_MOD_BVALUES;
1036
1037         i = 0;
1038         if ( pmods != NULL ) {
1039                 for ( ; pmods[ i ] != NULL; ++i ) {
1040                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1041                                 pmods[ i ]->mod_op == modop )
1042                         {
1043                                 break;
1044                         }
1045                 }
1046         }
1047
1048         if ( pmods == NULL || pmods[ i ] == NULL ) {
1049                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1050                         sizeof( LDAPMod * ))) == NULL )
1051                 {
1052                         perror( "realloc" );
1053                         exit( EXIT_FAILURE );
1054                 }
1055
1056                 *pmodsp = pmods;
1057                 pmods[ i + 1 ] = NULL;
1058
1059                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1060                 if ( pmods[ i ] == NULL ) {
1061                         perror( "calloc" );
1062                         exit( EXIT_FAILURE );
1063                 }
1064
1065                 pmods[ i ]->mod_op = modop;
1066                 pmods[ i ]->mod_type = ber_strdup( attr );
1067                 if ( pmods[ i ]->mod_type == NULL ) {
1068                         perror( "strdup" );
1069                         exit( EXIT_FAILURE );
1070                 }
1071         }
1072
1073         if ( val != NULL ) {
1074                 j = 0;
1075                 if ( pmods[ i ]->mod_bvalues != NULL ) {
1076                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1077                                 /* Empty */;
1078                         }
1079                 }
1080
1081                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1082                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1083                 if ( pmods[ i ]->mod_bvalues == NULL ) {
1084                         perror( "ber_realloc" );
1085                         exit( EXIT_FAILURE );
1086                 }
1087
1088                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1089                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1090                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1091                         perror( "ber_bvdup" );
1092                         exit( EXIT_FAILURE );
1093                 }
1094         }
1095 }
1096
1097
1098 static int
1099 domodify(
1100         const char *dn,
1101         LDAPMod **pmods,
1102         int newentry )
1103 {
1104     int                 i, j, k, notascii, op;
1105     struct berval       *bvp;
1106
1107     if ( pmods == NULL ) {
1108         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1109                 prog, dn );
1110         return( LDAP_PARAM_ERROR );
1111     } 
1112
1113     for ( i = 0; pmods[ i ] != NULL; ++i ) {
1114         op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1115         if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1116                 fprintf( stderr,
1117                         "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1118                         prog, pmods[i]->mod_type, dn );
1119                 return LDAP_PARAM_ERROR;
1120         }
1121     }
1122
1123     if ( verbose ) {
1124         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1125             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1126             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1127                     "replace" : op == LDAP_MOD_ADD ?
1128                     "add" : "delete", pmods[ i ]->mod_type );
1129             if ( pmods[ i ]->mod_bvalues != NULL ) {
1130                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1131                     bvp = pmods[ i ]->mod_bvalues[ j ];
1132                     notascii = 0;
1133                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1134                         if ( !isascii( bvp->bv_val[ k ] )) {
1135                             notascii = 1;
1136                             break;
1137                         }
1138                     }
1139                     if ( notascii ) {
1140                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1141                     } else {
1142                         printf( "\t%s\n", bvp->bv_val );
1143                     }
1144                 }
1145             }
1146         }
1147     }
1148
1149     if ( newentry ) {
1150         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1151     } else {
1152         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1153     }
1154
1155     if ( !not ) {
1156         if ( newentry ) {
1157             i = ldap_add_s( ld, dn, pmods );
1158         } else {
1159             i = ldap_modify_s( ld, dn, pmods );
1160         }
1161         if ( i != LDAP_SUCCESS ) {
1162                 /* print error message about failed update including DN */
1163                 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1164                 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1165         } else if ( verbose ) {
1166             printf( "modify complete\n" );
1167         }
1168     } else {
1169         i = LDAP_SUCCESS;
1170     }
1171
1172     putchar( '\n' );
1173
1174     return( i );
1175 }
1176
1177
1178 static int
1179 dodelete(
1180         const char *dn )
1181 {
1182     int rc;
1183
1184     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1185     if ( !not ) {
1186         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1187                 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1188                 ldap_perror( ld, "ldap_delete" );
1189         } else if ( verbose ) {
1190             printf( "delete complete" );
1191         }
1192     } else {
1193         rc = LDAP_SUCCESS;
1194     }
1195
1196     putchar( '\n' );
1197
1198     return( rc );
1199 }
1200
1201
1202 static int
1203 dorename(
1204         const char *dn,
1205         const char *newrdn,
1206         const char* newsup,
1207         int deleteoldrdn )
1208 {
1209     int rc;
1210
1211
1212     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1213     if ( verbose ) {
1214         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1215                 newrdn, deleteoldrdn ? "do not " : "" );
1216     }
1217     if ( !not ) {
1218         if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1219                 != LDAP_SUCCESS )
1220         {
1221                 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1222                 ldap_perror( ld, "ldap_modrdn" );
1223         } else {
1224             printf( "modrdn completed\n" );
1225         }
1226     } else {
1227         rc = LDAP_SUCCESS;
1228     }
1229
1230     putchar( '\n' );
1231
1232     return( rc );
1233 }
1234
1235
1236 static char *
1237 read_one_record( FILE *fp )
1238 {
1239     char        *buf, line[ LDAPMOD_MAXLINE ];
1240     int         lcur, lmax;
1241
1242     lcur = lmax = 0;
1243     buf = NULL;
1244
1245     while ( fgets( line, sizeof(line), fp ) != NULL ) {
1246         int len = strlen( line );
1247
1248                 if( len < 2 || ( len == 3 && *line == '\r' )) {
1249                         if( buf == NULL ) {
1250                                 continue;
1251                         } else {
1252                                 break;
1253                         }
1254                 }
1255
1256                 if ( lcur + len + 1 > lmax ) {
1257                         lmax = LDAPMOD_MAXLINE
1258                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1259
1260                         if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1261                                 perror( "realloc" );
1262                                 exit( EXIT_FAILURE );
1263                         }
1264                 }
1265
1266                 strcpy( buf + lcur, line );
1267                 lcur += len;
1268     }
1269
1270     return( buf );
1271 }