]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodify.c
Check for not LDAP_OPT_SUCCESS instead of LDAP_OPT_ERROR.
[openldap] / clients / tools / ldapmodify.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /* ldapmodify.c - generic program to modify or add entries using LDAP */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17
18 #include <sys/stat.h>
19
20 #ifdef HAVE_SYS_FILE_H
21 #include <sys/file.h>
22 #endif
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26
27 #include <lber.h>
28 #include <ldap.h>
29 #include <ldif.h>
30
31 #include <ldap_defaults.h>
32
33 static char     *prog;
34 static char     *binddn = NULL;
35 static char     *passwd = NULL;
36 static char     *ldaphost = NULL;
37 static int      ldapport = 0;
38 static int      new, replace, not, verbose, contoper, force, valsfromfiles;
39 static LDAP     *ld;
40
41 #define LDAPMOD_MAXLINE         4096
42
43 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
44 #define T_REPLICA_STR           "replica"
45 #define T_DN_STR                "dn"
46 #define T_CHANGETYPESTR         "changetype"
47 #define T_ADDCTSTR              "add"
48 #define T_MODIFYCTSTR           "modify"
49 #define T_DELETECTSTR           "delete"
50 #define T_MODRDNCTSTR           "modrdn"
51 #define T_MODOPADDSTR           "add"
52 #define T_MODOPREPLACESTR       "replace"
53 #define T_MODOPDELETESTR        "delete"
54 #define T_MODSEPSTR             "-"
55 #define T_NEWRDNSTR             "newrdn"
56 #define T_DELETEOLDRDNSTR       "deleteoldrdn"
57
58
59 static void usage LDAP_P(( const char *prog ));
60 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
61 static int process_ldif_rec LDAP_P(( char *rbuf ));
62 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
63         char *value, int vlen ));
64 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
65 static int dodelete LDAP_P(( char *dn ));
66 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
67 static int fromfile LDAP_P(( char *path, struct berval *bv ));
68 static char *read_one_record LDAP_P(( FILE *fp ));
69
70 static void
71 usage( const char *prog )
72 {
73     fprintf( stderr,
74                  "Add or modify entries from an LDAP server\n\n"
75              "usage: %s [-abcknrvF] [-d debug-level] [-P version] [-h ldaphost]\n"
76              "            [-p ldapport] [-D binddn] [-w passwd] [ -f file | < entryfile ]\n"
77              "       a    - add values (default%s)\n"
78              "       b    - read values from files (for binary attributes)\n"
79              "       c    - continuous operation\n"
80              "       D    - bind DN\n"
81              "       d    - debug level\n"
82              "       f    - read from file\n"
83              "       F    - force all changes records to be used\n"
84              "       h    - ldap host\n"
85              "       n    - print adds, don't actually do them\n"
86              "       p    - LDAP port\n"
87              "       r    - replace values\n"
88              "       v    - verbose mode\n"
89              "       w    - password\n"
90              , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
91     exit( EXIT_FAILURE );
92 }
93
94
95 int
96 main( int argc, char **argv )
97 {
98     char                *infile, *rbuf, *start, *p, *q;
99     FILE                *fp;
100         int             rc, i, use_ldif, authmethod, version, want_bindpw, debug;
101
102     if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
103         prog = argv[ 0 ];
104     } else {
105         ++prog;
106     }
107
108     /* Print usage when no parameters */
109     if( argc < 2 )
110         usage( prog );
111
112     new = ( strcmp( prog, "ldapadd" ) == 0 );
113
114     infile = NULL;
115     not = verbose = valsfromfiles = want_bindpw = debug = 0;
116     authmethod = LDAP_AUTH_SIMPLE;
117         version = -1;
118
119     while (( i = getopt( argc, argv, "WFabckKnrtvh:p:D:w:d:f:" )) != EOF ) {
120         switch( i ) {
121         case 'a':       /* add */
122             new = 1;
123             break;
124         case 'b':       /* read values from files (for binary attributes) */
125             valsfromfiles = 1;
126             break;
127         case 'c':       /* continuous operation */
128             contoper = 1;
129             break;
130         case 'r':       /* default is to replace rather than add values */
131             replace = 1;
132             break;
133         case 'k':       /* kerberos bind */
134 #ifdef HAVE_KERBEROS
135                 authmethod = LDAP_AUTH_KRBV4;
136 #else
137                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
138                 usage( argv[0] );
139                 return( EXIT_FAILURE );
140 #endif
141             break;
142         case 'K':       /* kerberos bind, part 1 only */
143 #ifdef HAVE_KERBEROS
144                 authmethod = LDAP_AUTH_KRBV41;
145 #else
146                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
147                 usage( argv[0] );
148                 return( EXIT_FAILURE );
149 #endif
150             break;
151         case 'F':       /* force all changes records to be used */
152             force = 1;
153             break;
154         case 'h':       /* ldap host */
155             ldaphost = strdup( optarg );
156             break;
157         case 'D':       /* bind DN */
158             binddn = strdup( optarg );
159             break;
160         case 'w':       /* password */
161             passwd = strdup( optarg );
162             break;
163         case 'd':
164             debug |= atoi( optarg );
165             break;
166         case 'f':       /* read from file */
167             infile = strdup( optarg );
168             break;
169         case 'p':
170             ldapport = atoi( optarg );
171             break;
172         case 'n':       /* print adds, don't actually do them */
173             ++not;
174             break;
175         case 'v':       /* verbose mode */
176             verbose++;
177             break;
178         case 'W':
179                 want_bindpw++;
180                 break;
181         case 'P':
182                 switch( atoi(optarg) )
183                 {
184                 case 2:
185                         version = LDAP_VERSION2;
186                         break;
187                 case 3:
188                         version = LDAP_VERSION3;
189                         break;
190                 default:
191                         fprintf( stderr, "protocol version should be 2 or 3\n" );
192                         usage( argv[0] );
193                 }
194                 break;
195         default:
196             usage( prog );
197         }
198     }
199
200     if ( argc != optind )
201         usage( prog );
202
203     if ( infile != NULL ) {
204         if (( fp = fopen( infile, "r" )) == NULL ) {
205             perror( infile );
206             return( EXIT_FAILURE );
207         }
208     } else {
209         fp = stdin;
210     }
211
212         if ( debug ) {
213                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
214                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
215                 }
216                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
217                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
218                 }
219                 ldif_debug = debug;
220         }
221
222 #ifdef SIGPIPE
223         (void) SIGNAL( SIGPIPE, SIG_IGN );
224 #endif
225
226     if ( !not ) {
227         if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
228             perror( "ldap_init" );
229             return( EXIT_FAILURE );
230         }
231
232         /* this seems prudent */
233         {
234                 int deref = LDAP_DEREF_NEVER;
235                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
236         }
237
238         if (want_bindpw)
239                 passwd = getpass("Enter LDAP Password: ");
240
241         if (version != -1 &&
242                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
243         {
244                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
245         }
246
247         if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
248             ldap_perror( ld, "ldap_bind" );
249             return( EXIT_FAILURE );
250         }
251     }
252
253     rc = 0;
254
255     while (( rc == 0 || contoper ) &&
256                 ( rbuf = read_one_record( fp )) != NULL ) {
257         /*
258          * we assume record is ldif/slapd.replog if the first line
259          * has a colon that appears to the left of any equal signs, OR
260          * if the first line consists entirely of digits (an entry id)
261          */
262         use_ldif = ( p = strchr( rbuf, ':' )) != NULL &&
263                 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
264                 (( q = strchr( rbuf, '=' )) == NULL || p < q );
265
266         start = rbuf;
267
268         if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
269             for ( p = rbuf; p < q; ++p ) {
270                 if ( !isdigit( (unsigned char) *p )) {
271                     break;
272                 }
273             }
274             if ( p >= q ) {
275                 use_ldif = 1;
276                 start = q + 1;
277             }
278         }
279
280         if ( use_ldif ) {
281             rc = process_ldif_rec( start );
282         } else {
283             rc = process_ldapmod_rec( start );
284         }
285
286         if( rc )
287             fprintf( stderr, "%s() = %d\n",
288                      use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
289
290         free( rbuf );
291     }
292
293     if ( !not ) {
294         ldap_unbind( ld );
295     }
296
297         return( rc );
298 }
299
300
301 static int
302 process_ldif_rec( char *rbuf )
303 {
304     char        *line, *dn, *type, *value, *newrdn, *p;
305     int         rc, linenum, vlen, modop, replicaport;
306     int         expect_modop, expect_sep, expect_ct, expect_newrdn;
307     int         expect_deleteoldrdn, deleteoldrdn;
308     int         saw_replica, use_record, new_entry, delete_entry, got_all;
309     LDAPMod     **pmods;
310
311     new_entry = new;
312
313     rc = got_all = saw_replica = delete_entry = expect_modop = 0;
314     expect_deleteoldrdn = expect_newrdn = expect_sep = expect_ct = 0;
315     linenum = 0;
316     deleteoldrdn = 1;
317     use_record = force;
318     pmods = NULL;
319     dn = newrdn = NULL;
320
321     while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
322         ++linenum;
323         if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
324             expect_sep = 0;
325             expect_ct = 1;
326             continue;
327         }
328         
329         if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
330             fprintf( stderr, "%s: invalid format (line %d of entry: %s\n",
331                     prog, linenum, dn == NULL ? "" : dn );
332             rc = LDAP_PARAM_ERROR;
333             break;
334         }
335
336         if ( dn == NULL ) {
337             if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
338                 ++saw_replica;
339                 if (( p = strchr( value, ':' )) == NULL ) {
340                     replicaport = 0;
341                 } else {
342                     *p++ = '\0';
343                     replicaport = atoi( p );
344                 }
345                 if ( strcasecmp( value, ldaphost ) == 0 &&
346                         replicaport == ldapport ) {
347                     use_record = 1;
348                 }
349             } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
350                 if (( dn = strdup( value )) == NULL ) {
351                     perror( "strdup" );
352                     exit( 1 );
353                 }
354                 expect_ct = 1;
355             }
356             continue;   /* skip all lines until we see "dn:" */
357         }
358
359         if ( expect_ct ) {
360             expect_ct = 0;
361             if ( !use_record && saw_replica ) {
362                 printf( "%s: skipping change record for entry: %s\n\t(LDAP host/port does not match replica: lines)\n",
363                         prog, dn );
364                 free( dn );
365                 return( 0 );
366             }
367
368             if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
369                 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
370                         new_entry = 0;
371                         expect_modop = 1;
372                 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
373                         new_entry = 1;
374                 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0 ) {
375                     expect_newrdn = 1;
376                 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
377                     got_all = delete_entry = 1;
378                 } else {
379                     fprintf( stderr,
380                             "%s:  unknown %s \"%s\" (line %d of entry: %s)\n",
381                             prog, T_CHANGETYPESTR, value, linenum, dn );
382                     rc = LDAP_PARAM_ERROR;
383                 }
384                 continue;
385             } else if ( new ) {         /*  missing changetype => add */
386                 new_entry = 1;
387                 modop = LDAP_MOD_ADD;
388             } else {
389                 expect_modop = 1;       /* missing changetype => modify */
390             }
391         }
392
393         if ( expect_modop ) {
394             expect_modop = 0;
395             expect_sep = 1;
396             if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
397                 modop = LDAP_MOD_ADD;
398                 continue;
399             } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
400                 modop = LDAP_MOD_REPLACE;
401                 continue;
402             } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
403                 modop = LDAP_MOD_DELETE;
404                 addmodifyop( &pmods, modop, value, NULL, 0 );
405                 continue;
406             } else {    /* no modify op:  use default */
407                 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
408             }
409         }
410
411         if ( expect_newrdn ) {
412             if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
413                 if (( newrdn = strdup( value )) == NULL ) {
414                     perror( "strdup" );
415                     exit( 1 );
416                 }
417                 expect_deleteoldrdn = 1;
418                 expect_newrdn = 0;
419             } else {
420                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
421                         prog, T_NEWRDNSTR, type, linenum, dn );
422                 rc = LDAP_PARAM_ERROR;
423             }
424         } else if ( expect_deleteoldrdn ) {
425             if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
426                 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
427                 got_all = 1;
428             } else {
429                 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry %s)\n",
430                         prog, T_DELETEOLDRDNSTR, type, linenum, dn );
431                 rc = LDAP_PARAM_ERROR;
432             }
433         } else if ( got_all ) {
434             fprintf( stderr,
435                     "%s: extra lines at end (line %d of entry %s)\n",
436                     prog, linenum, dn );
437             rc = LDAP_PARAM_ERROR;
438         } else {
439             addmodifyop( &pmods, modop, type, value, vlen );
440         }
441     }
442
443     if ( rc == 0 ) {
444         if ( delete_entry ) {
445             rc = dodelete( dn );
446         } else if ( newrdn != NULL ) {
447             rc = domodrdn( dn, newrdn, deleteoldrdn );
448         } else {
449             rc = domodify( dn, pmods, new_entry );
450         }
451
452         if ( rc == LDAP_SUCCESS ) {
453             rc = 0;
454         }
455     }
456
457     if ( dn != NULL ) {
458         free( dn );
459     }
460     if ( newrdn != NULL ) {
461         free( newrdn );
462     }
463     if ( pmods != NULL ) {
464         ldap_mods_free( pmods, 1 );
465     }
466
467     return( rc );
468 }
469
470
471 static int
472 process_ldapmod_rec( char *rbuf )
473 {
474     char        *line, *dn, *p, *q, *attr, *value;
475     int         rc, linenum, modop;
476     LDAPMod     **pmods;
477
478     pmods = NULL;
479     dn = NULL;
480     linenum = 0;
481     line = rbuf;
482     rc = 0;
483
484     while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
485         ++linenum;
486         if (( p = strchr( rbuf, '\n' )) == NULL ) {
487             rbuf = NULL;
488         } else {
489             if ( *(p-1) == '\\' ) {     /* lines ending in '\' are continued */
490                 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
491                 rbuf = p;
492                 continue;
493             }
494             *p++ = '\0';
495             rbuf = p;
496         }
497
498         if ( dn == NULL ) {     /* first line contains DN */
499             if (( dn = strdup( line )) == NULL ) {
500                 perror( "strdup" );
501                 exit( 1 );
502             }
503         } else {
504             if (( p = strchr( line, '=' )) == NULL ) {
505                 value = NULL;
506                 p = line + strlen( line );
507             } else {
508                 *p++ = '\0';
509                 value = p;
510             }
511
512             for ( attr = line;
513                   *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
514                 ;       /* skip attribute leading white space */
515             }
516
517             for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
518                 *q = '\0';      /* remove attribute trailing white space */
519             }
520
521             if ( value != NULL ) {
522                 while ( isspace( (unsigned char) *value )) {
523                     ++value;            /* skip value leading white space */
524                 }
525                 for ( q = value + strlen( value ) - 1; q > value &&
526                         isspace( (unsigned char) *q ); --q ) {
527                     *q = '\0';  /* remove value trailing white space */
528                 }
529                 if ( *value == '\0' ) {
530                     value = NULL;
531                 }
532
533             }
534
535             if ( value == NULL && new ) {
536                 fprintf( stderr, "%s: missing value on line %d (attr is %s)\n",
537                         prog, linenum, attr );
538                 rc = LDAP_PARAM_ERROR;
539             } else {
540                  switch ( *attr ) {
541                 case '-':
542                     modop = LDAP_MOD_DELETE;
543                     ++attr;
544                     break;
545                 case '+':
546                     modop = LDAP_MOD_ADD;
547                     ++attr;
548                     break;
549                 default:
550                     modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
551                 }
552
553                 addmodifyop( &pmods, modop, attr, value,
554                         ( value == NULL ) ? 0 : strlen( value ));
555             }
556         }
557
558         line = rbuf;
559     }
560
561     if ( rc == 0 ) {
562         if ( dn == NULL ) {
563             rc = LDAP_PARAM_ERROR;
564         } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
565             rc = 0;
566         }
567     }
568
569     if ( pmods != NULL ) {
570         ldap_mods_free( pmods, 1 );
571     }
572     if ( dn != NULL ) {
573         free( dn );
574     }
575
576     return( rc );
577 }
578
579
580 static void
581 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
582 {
583     LDAPMod             **pmods;
584     int                 i, j;
585     struct berval       *bvp;
586
587     pmods = *pmodsp;
588     modop |= LDAP_MOD_BVALUES;
589
590     i = 0;
591     if ( pmods != NULL ) {
592         for ( ; pmods[ i ] != NULL; ++i ) {
593             if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
594                     pmods[ i ]->mod_op == modop ) {
595                 break;
596             }
597         }
598     }
599
600     if ( pmods == NULL || pmods[ i ] == NULL ) {
601         if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
602                 sizeof( LDAPMod * ))) == NULL ) {
603             perror( "realloc" );
604             exit( 1 );
605         }
606         *pmodsp = pmods;
607         pmods[ i + 1 ] = NULL;
608         if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
609                 == NULL ) {
610             perror( "calloc" );
611             exit( 1 );
612         }
613         pmods[ i ]->mod_op = modop;
614         if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
615             perror( "strdup" );
616             exit( 1 );
617         }
618     }
619
620     if ( value != NULL ) {
621         j = 0;
622         if ( pmods[ i ]->mod_bvalues != NULL ) {
623             for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
624                 ;
625             }
626         }
627         if (( pmods[ i ]->mod_bvalues =
628                 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
629                 (j + 2) * sizeof( struct berval * ))) == NULL ) {
630             perror( "ber_realloc" );
631             exit( 1 );
632         }
633         pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
634         if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
635                 == NULL ) {
636             perror( "ber_memalloc" );
637             exit( 1 );
638         }
639         pmods[ i ]->mod_bvalues[ j ] = bvp;
640
641         if ( valsfromfiles && *value == '/' ) { /* get value from file */
642             if ( fromfile( value, bvp ) < 0 ) {
643                 exit( 1 );
644             }
645         } else {
646             bvp->bv_len = vlen;
647             if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
648                 perror( "malloc" );
649                 exit( 1 );
650             }
651             SAFEMEMCPY( bvp->bv_val, value, vlen );
652             bvp->bv_val[ vlen ] = '\0';
653         }
654     }
655 }
656
657
658 static int
659 domodify( char *dn, LDAPMod **pmods, int newentry )
660 {
661     int                 i, j, k, notascii, op;
662     struct berval       *bvp;
663
664     if ( pmods == NULL ) {
665         fprintf( stderr, "%s: no attributes to change or add (entry %s)\n",
666                 prog, dn );
667         return( LDAP_PARAM_ERROR );
668     }
669
670     if ( verbose ) {
671         for ( i = 0; pmods[ i ] != NULL; ++i ) {
672             op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
673             printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
674                     "replace" : op == LDAP_MOD_ADD ?
675                     "add" : "delete", pmods[ i ]->mod_type );
676             if ( pmods[ i ]->mod_bvalues != NULL ) {
677                 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
678                     bvp = pmods[ i ]->mod_bvalues[ j ];
679                     notascii = 0;
680                     for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
681                         if ( !isascii( bvp->bv_val[ k ] )) {
682                             notascii = 1;
683                             break;
684                         }
685                     }
686                     if ( notascii ) {
687                         printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
688                     } else {
689                         printf( "\t%s\n", bvp->bv_val );
690                     }
691                 }
692             }
693         }
694     }
695
696     if ( newentry ) {
697         printf( "%sadding new entry %s\n", not ? "!" : "", dn );
698     } else {
699         printf( "%smodifying entry %s\n", not ? "!" : "", dn );
700     }
701
702     if ( !not ) {
703         if ( newentry ) {
704             i = ldap_add_s( ld, dn, pmods );
705         } else {
706             i = ldap_modify_s( ld, dn, pmods );
707         }
708         if ( i != LDAP_SUCCESS ) {
709             ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
710         } else if ( verbose ) {
711             printf( "modify complete\n" );
712         }
713     } else {
714         i = LDAP_SUCCESS;
715     }
716
717     putchar( '\n' );
718
719     return( i );
720 }
721
722
723 static int
724 dodelete( char *dn )
725 {
726     int rc;
727
728     printf( "%sdeleting entry %s\n", not ? "!" : "", dn );
729     if ( !not ) {
730         if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
731             ldap_perror( ld, "ldap_delete" );
732         } else if ( verbose ) {
733             printf( "delete complete" );
734         }
735     } else {
736         rc = LDAP_SUCCESS;
737     }
738
739     putchar( '\n' );
740
741     return( rc );
742 }
743
744
745 static int
746 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
747 {
748     int rc;
749
750     if ( verbose ) {
751         printf( "new RDN: %s (%skeep existing values)\n",
752                 newrdn, deleteoldrdn ? "do not " : "" );
753     }
754
755     printf( "%smodifying rdn of entry %s\n", not ? "!" : "", dn );
756     if ( !not ) {
757         if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
758                 != LDAP_SUCCESS ) {
759             ldap_perror( ld, "ldap_modrdn" );
760         } else {
761             printf( "modrdn completed\n" );
762         }
763     } else {
764         rc = LDAP_SUCCESS;
765     }
766
767     putchar( '\n' );
768
769     return( rc );
770 }
771
772
773 static int
774 fromfile( char *path, struct berval *bv )
775 {
776         FILE            *fp;
777         long            rlen;
778         int             eof;
779
780         if (( fp = fopen( path, "r" )) == NULL ) {
781                 perror( path );
782                 return( -1 );
783         }
784
785         if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
786                 perror( path );
787                 fclose( fp );
788                 return( -1 );
789         }
790
791         bv->bv_len = ftell( fp );
792
793         if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
794                 perror( "malloc" );
795                 fclose( fp );
796                 return( -1 );
797         }
798
799         if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
800                 perror( path );
801                 fclose( fp );
802                 ber_memfree( bv->bv_val );
803                 bv->bv_val = NULL;
804                 return( -1 );
805         }
806
807         rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
808         eof = feof( fp );
809         fclose( fp );
810
811         if ( (unsigned long) rlen != bv->bv_len ) {
812                 perror( path );
813                 ber_memfree( bv->bv_val );
814                 bv->bv_val = NULL;
815                 return( -1 );
816         }
817
818         return( bv->bv_len );
819 }
820
821
822 static char *
823 read_one_record( FILE *fp )
824 {
825     int         len;
826     char        *buf, line[ LDAPMOD_MAXLINE ];
827     int         lcur, lmax;
828
829     lcur = lmax = 0;
830     buf = NULL;
831
832     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
833             (( len = strlen( line )) > 1 )) {
834         if ( lcur + len + 1 > lmax ) {
835             lmax = LDAPMOD_MAXLINE
836                     * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
837             if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
838                 perror( "realloc" );
839                 exit( 1 );
840             }
841         }
842         strcpy( buf + lcur, line );
843         lcur += len;
844     }
845
846     return( buf );
847 }