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