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