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