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