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