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