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