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