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