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