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