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