]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
ed89f35270e101457c43a3e21bf86f92c913190e
[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", rc );
580                 return EXIT_FAILURE;
581         }
582
583         /* referrals */
584         if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
585                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
586         {
587                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
588                         referrals ? "on" : "off" );
589                 return EXIT_FAILURE;
590         }
591
592
593         if (version == -1 ) {
594                 version = 3;
595         }
596
597         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
598                 != LDAP_OPT_SUCCESS )
599         {
600                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
601                         version );
602                 return EXIT_FAILURE;
603         }
604
605         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
606                 if ( use_tls > 1 ) {
607                         ldap_perror( ld, "ldap_start_tls" );
608                         return( EXIT_FAILURE );
609                 }
610                 fprintf( stderr, "WARNING: could not start TLS\n" );
611         }
612
613         if (want_bindpw) {
614                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
615                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
616         }
617
618         if ( authmethod == LDAP_AUTH_SASL ) {
619 #ifdef HAVE_CYRUS_SASL
620                 void *defaults;
621
622                 if( sasl_secprops != NULL ) {
623                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
624                                 (void *) sasl_secprops );
625                         
626                         if( rc != LDAP_OPT_SUCCESS ) {
627                                 fprintf( stderr,
628                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
629                                         sasl_secprops );
630                                 return( EXIT_FAILURE );
631                         }
632                 }
633                 
634                 defaults = lutil_sasl_defaults( ld,
635                         sasl_mech,
636                         sasl_realm,
637                         sasl_authc_id,
638                         passwd.bv_val,
639                         sasl_authz_id );
640
641                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
642                         sasl_mech, NULL, NULL,
643                         sasl_flags, lutil_sasl_interact, defaults );
644
645                 if( rc != LDAP_SUCCESS ) {
646                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
647                         return( EXIT_FAILURE );
648                 }
649 #else
650                 fprintf( stderr, "%s: not compiled with SASL support\n",
651                         argv[0] );
652                 return( EXIT_FAILURE );
653 #endif
654         }
655         else {
656                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
657                                 != LDAP_SUCCESS ) {
658                         ldap_perror( ld, "ldap_bind" );
659                         return( EXIT_FAILURE );
660                 }
661         }
662
663     }
664
665     rc = 0;
666
667         if ( manageDSAit ) {
668                 int err;
669                 LDAPControl c;
670                 LDAPControl *ctrls[2];
671                 ctrls[0] = &c;
672                 ctrls[1] = NULL;
673
674                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
675                 c.ldctl_value.bv_val = NULL;
676                 c.ldctl_value.bv_len = 0;
677                 c.ldctl_iscritical = manageDSAit > 1;
678
679                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
680
681                 if( err != LDAP_OPT_SUCCESS ) {
682                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
683                                 c.ldctl_iscritical ? "critical " : "" );
684                         if( c.ldctl_iscritical ) {
685                                 exit( EXIT_FAILURE );
686                         }
687                 }
688         }
689
690         count = 0;
691     while (( rc == 0 || contoper ) &&
692                 ( rbuf = read_one_record( fp )) != NULL ) {
693         count++;
694
695         start = rbuf;
696
697     rc = process_ldif_rec( start, count );
698
699         if( rc )
700                 fprintf( stderr, "ldif_record() = %d\n", rc );
701                 free( rbuf );
702     }
703
704     if ( !not ) {
705                 ldap_unbind( ld );
706     }
707
708         return( rc );
709 }
710
711
712 static int
713 process_ldif_rec( char *rbuf, int count )
714 {
715     char        *line, *dn, *type, *newrdn, *newsup, *p;
716     int         rc, linenum, modop, replicaport;
717     int         expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
718     int         expect_deleteoldrdn, deleteoldrdn;
719     int         saw_replica, use_record, new_entry, delete_entry, got_all;
720     LDAPMod     **pmods;
721         int version;
722         struct berval val;
723
724     new_entry = ldapadd;
725
726     rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
727     expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
728         expect_sep = expect_ct = 0;
729     linenum = 0;
730         version = 0;
731     deleteoldrdn = 1;
732     use_record = force;
733     pmods = NULL;
734     dn = newrdn = newsup = NULL;
735
736     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
737         ++linenum;
738
739         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
740             expect_sep = 0;
741             expect_ct = 1;
742             continue;
743         }
744         
745         if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
746             fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
747                     prog, linenum, dn == NULL ? "" : dn );
748             rc = LDAP_PARAM_ERROR;
749             break;
750         }
751
752         if ( dn == NULL ) {
753             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
754                 ++saw_replica;
755                 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
756                     replicaport = 0;
757                 } else {
758                     *p++ = '\0';
759                     replicaport = atoi( p );
760                 }
761                 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
762                         replicaport == ldapport ) {
763                     use_record = 1;
764                 }
765             } else if ( count == 1 && linenum == 1 && 
766                         strcasecmp( type, T_VERSION_STR ) == 0 )
767                 {
768                         if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
769                         fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
770                                 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
771                         }
772                         version++;
773
774             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
775                 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
776                     perror( "strdup" );
777                     exit( EXIT_FAILURE );
778                 }
779                 expect_ct = 1;
780             }
781             goto end_line;      /* skip all lines until we see "dn:" */
782         }
783
784         if ( expect_ct ) {
785             expect_ct = 0;
786             if ( !use_record && saw_replica ) {
787                 printf( "%s: skipping change record for entry: %s\n"
788                         "\t(LDAP host/port does not match replica: lines)\n",
789                         prog, dn );
790                 free( dn );
791                 ber_memfree( type );
792                 ber_memfree( val.bv_val );
793                 return( 0 );
794             }
795
796             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
797                 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
798                         new_entry = 0;
799                         expect_modop = 1;
800                 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
801                         new_entry = 1;
802                 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
803                         || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
804                         || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
805                 {
806                     expect_newrdn = 1;
807                 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
808                     got_all = delete_entry = 1;
809                 } else {
810                     fprintf( stderr,
811                             "%s:  unknown %s \"%s\" (line %d of entry \"%s\")\n",
812                             prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
813                     rc = LDAP_PARAM_ERROR;
814                 }
815                 goto end_line;
816             } else if ( ldapadd ) {             /*  missing changetype => add */
817                 new_entry = 1;
818                 modop = LDAP_MOD_ADD;
819             } else {
820                 expect_modop = 1;       /* missing changetype => modify */
821             }
822         }
823
824         if ( expect_modop ) {
825             expect_modop = 0;
826             expect_sep = 1;
827             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
828                 modop = LDAP_MOD_ADD;
829                 goto end_line;
830             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
831                 modop = LDAP_MOD_REPLACE;
832                 addmodifyop( &pmods, modop, val.bv_val, NULL );
833                 goto end_line;
834             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
835                 modop = LDAP_MOD_DELETE;
836                 addmodifyop( &pmods, modop, val.bv_val, NULL );
837                 goto end_line;
838             } else {    /* no modify op:  use default */
839                 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
840             }
841         }
842
843         if ( expect_newrdn ) {
844             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
845                         if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
846                     perror( "strdup" );
847                     exit( EXIT_FAILURE );
848                 }
849                 expect_deleteoldrdn = 1;
850                 expect_newrdn = 0;
851             } else {
852                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
853                         prog, T_NEWRDNSTR, type, linenum, dn );
854                 rc = LDAP_PARAM_ERROR;
855             }
856         } else if ( expect_deleteoldrdn ) {
857             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
858                 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
859                 expect_deleteoldrdn = 0;
860                 expect_newsup = 1;
861                 got_all = 1;
862             } else {
863                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
864                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
865                 rc = LDAP_PARAM_ERROR;
866             }
867         } else if ( expect_newsup ) {
868             if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
869                 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
870                     perror( "strdup" );
871                     exit( EXIT_FAILURE );
872                 }
873                 expect_newsup = 0;
874             } else {
875                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
876                         prog, T_NEWSUPSTR, type, linenum, dn );
877                 rc = LDAP_PARAM_ERROR;
878             }
879         } else if ( got_all ) {
880             fprintf( stderr,
881                     "%s: extra lines at end (line %d of entry \"%s\")\n",
882                     prog, linenum, dn );
883             rc = LDAP_PARAM_ERROR;
884         } else {
885                 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
886         }
887
888 end_line:
889         ber_memfree( type );
890         ber_memfree( val.bv_val );
891     }
892
893         if( linenum == 0 ) {
894                 return 0;
895         }
896
897         if( version && linenum == 1 ) {
898                 return 0;
899         }
900
901     if ( rc == 0 ) {
902         if ( delete_entry ) {
903             rc = dodelete( dn );
904         } else if ( newrdn != NULL ) {
905             rc = dorename( dn, newrdn, newsup, deleteoldrdn );
906         } else {
907             rc = domodify( dn, pmods, new_entry );
908         }
909
910         if ( rc == LDAP_SUCCESS ) {
911             rc = 0;
912         }
913     }
914
915     if ( dn != NULL ) {
916         free( dn );
917     }
918     if ( newrdn != NULL ) {
919         free( newrdn );
920     }
921     if ( pmods != NULL ) {
922         ldap_mods_free( pmods, 1 );
923     }
924
925     return( rc );
926 }
927
928
929 static void
930 addmodifyop(
931         LDAPMod ***pmodsp,
932         int modop,
933         const char *attr,
934         struct berval *val )
935 {
936         LDAPMod         **pmods;
937         int                     i, j;
938
939         pmods = *pmodsp;
940         modop |= LDAP_MOD_BVALUES;
941
942         i = 0;
943         if ( pmods != NULL ) {
944                 for ( ; pmods[ i ] != NULL; ++i ) {
945                         if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
946                                 pmods[ i ]->mod_op == modop )
947                         {
948                                 break;
949                         }
950                 }
951         }
952
953         if ( pmods == NULL || pmods[ i ] == NULL ) {
954                 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
955                         sizeof( LDAPMod * ))) == NULL )
956                 {
957                         perror( "realloc" );
958                         exit( EXIT_FAILURE );
959                 }
960
961                 *pmodsp = pmods;
962                 pmods[ i + 1 ] = NULL;
963
964                 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
965                 if ( pmods[ i ] == NULL ) {
966                         perror( "calloc" );
967                         exit( EXIT_FAILURE );
968                 }
969
970                 pmods[ i ]->mod_op = modop;
971                 pmods[ i ]->mod_type = ber_strdup( attr );
972                 if ( pmods[ i ]->mod_type == NULL ) {
973                         perror( "strdup" );
974                         exit( EXIT_FAILURE );
975                 }
976         }
977
978         if ( val != NULL ) {
979                 j = 0;
980                 if ( pmods[ i ]->mod_bvalues != NULL ) {
981                         for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
982                                 /* Empty */;
983                         }
984                 }
985
986                 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
987                         pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
988                 if ( pmods[ i ]->mod_bvalues == NULL ) {
989                         perror( "ber_realloc" );
990                         exit( EXIT_FAILURE );
991                 }
992
993                 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
994                 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
995                 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
996                         perror( "ber_bvdup" );
997                         exit( EXIT_FAILURE );
998                 }
999         }
1000 }
1001
1002
1003 static int
1004 domodify(
1005         const char *dn,
1006         LDAPMod **pmods,
1007         int newentry )
1008 {
1009     int                 i, j, k, notascii, op;
1010     struct berval       *bvp;
1011
1012     if ( pmods == NULL ) {
1013         fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1014                 prog, dn );
1015         return( LDAP_PARAM_ERROR );
1016     }
1017
1018     if ( verbose ) {
1019         for ( i = 0; pmods[ i ] != NULL; ++i ) {
1020             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1021             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1022                     "replace" : op == LDAP_MOD_ADD ?
1023                     "add" : "delete", pmods[ i ]->mod_type );
1024             if ( pmods[ i ]->mod_bvalues != NULL ) {
1025                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1026                     bvp = pmods[ i ]->mod_bvalues[ j ];
1027                     notascii = 0;
1028                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1029                         if ( !isascii( bvp->bv_val[ k ] )) {
1030                             notascii = 1;
1031                             break;
1032                         }
1033                     }
1034                     if ( notascii ) {
1035                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1036                     } else {
1037                         printf( "\t%s\n", bvp->bv_val );
1038                     }
1039                 }
1040             }
1041         }
1042     }
1043
1044     if ( newentry ) {
1045         printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1046     } else {
1047         printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1048     }
1049
1050     if ( !not ) {
1051         if ( newentry ) {
1052             i = ldap_add_s( ld, dn, pmods );
1053         } else {
1054             i = ldap_modify_s( ld, dn, pmods );
1055         }
1056         if ( i != LDAP_SUCCESS ) {
1057             ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1058         } else if ( verbose ) {
1059             printf( "modify complete\n" );
1060         }
1061     } else {
1062         i = LDAP_SUCCESS;
1063     }
1064
1065     putchar( '\n' );
1066
1067     return( i );
1068 }
1069
1070
1071 static int
1072 dodelete(
1073         const char *dn )
1074 {
1075     int rc;
1076
1077     printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1078     if ( !not ) {
1079         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1080             ldap_perror( ld, "ldap_delete" );
1081         } else if ( verbose ) {
1082             printf( "delete complete" );
1083         }
1084     } else {
1085         rc = LDAP_SUCCESS;
1086     }
1087
1088     putchar( '\n' );
1089
1090     return( rc );
1091 }
1092
1093
1094 static int
1095 dorename(
1096         const char *dn,
1097         const char *newrdn,
1098         const char* newsup,
1099         int deleteoldrdn )
1100 {
1101     int rc;
1102
1103
1104     printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1105     if ( verbose ) {
1106         printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1107                 newrdn, deleteoldrdn ? "do not " : "" );
1108     }
1109     if ( !not ) {
1110         if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1111                 != LDAP_SUCCESS ) {
1112             ldap_perror( ld, "ldap_modrdn" );
1113         } else {
1114             printf( "modrdn completed\n" );
1115         }
1116     } else {
1117         rc = LDAP_SUCCESS;
1118     }
1119
1120     putchar( '\n' );
1121
1122     return( rc );
1123 }
1124
1125
1126 static char *
1127 read_one_record( FILE *fp )
1128 {
1129     char        *buf, line[ LDAPMOD_MAXLINE ];
1130     int         lcur, lmax;
1131
1132     lcur = lmax = 0;
1133     buf = NULL;
1134
1135     while ( fgets( line, sizeof(line), fp ) != NULL ) {
1136         int len = strlen( line );
1137
1138                 if( len < 2 ) {
1139                         if( buf == NULL ) {
1140                                 continue;
1141                         } else {
1142                                 break;
1143                         }
1144                 }
1145
1146                 if ( lcur + len + 1 > lmax ) {
1147                         lmax = LDAPMOD_MAXLINE
1148                                 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1149
1150                         if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1151                                 perror( "realloc" );
1152                                 exit( EXIT_FAILURE );
1153                         }
1154                 }
1155
1156                 strcpy( buf + lcur, line );
1157                 lcur += len;
1158     }
1159
1160     return( buf );
1161 }