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