]> git.sur5r.net Git - openldap/blob - libraries/liblutil/ldif.c
b664e89b7434491eede3c06b1102fd3e45c2100f
[openldap] / libraries / liblutil / ldif.c
1 /* ldif.c - routines for dealing with LDIF files */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor.  The name of the
22  * University may not be used to endorse or promote products derived
23  * from this software without specific prior written permission.  This
24  * software is provided ``as is'' without express or implied warranty.
25  */
26 /* This work was originally developed by the University of Michigan
27  * and distributed as part of U-MICH LDAP.
28  */
29
30 #include "portable.h"
31
32 #include <stdio.h>
33
34 #include <ac/stdlib.h>
35 #include <ac/ctype.h>
36
37 #include <ac/string.h>
38 #include <ac/socket.h>
39 #include <ac/time.h>
40
41 int ldif_debug = 0;
42
43 #include "ldap_log.h"
44 #include "lber_pvt.h"
45 #include "ldif.h"
46
47 #define RIGHT2                  0x03
48 #define RIGHT4                  0x0f
49 #define CONTINUED_LINE_MARKER   '\r'
50
51 #ifdef CSRIMALLOC
52 #define ber_memalloc malloc
53 #define ber_memcalloc calloc
54 #define ber_memrealloc realloc
55 #define ber_strdup strdup
56 #endif
57
58 static const char nib2b64[0x40] =
59         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
60
61 static const unsigned char b642nib[0x80] = {
62         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67         0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
68         0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
69         0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70         0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
71         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
72         0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
73         0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
74         0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
75         0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
76         0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
77         0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
78 };
79
80 /*
81  * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
82  * into components "type" and "value".  if a double colon separates type from
83  * value, then value is encoded in base 64, and parse_line un-decodes it
84  * (in place) before returning. The type and value are stored in malloc'd
85  * memory which must be freed by the caller.
86  *
87  * ldif_parse_line2 - operates in-place on input buffer, returning type
88  * in-place. Will return value in-place if possible, (must malloc for
89  * fetched URLs). If freeval is NULL, all return data will be malloc'd
90  * and the input line will be unmodified. Otherwise freeval is set to
91  * True if the value was malloc'd.
92  */
93
94 int
95 ldif_parse_line(
96     LDAP_CONST char     *line,
97     char        **typep,
98     char        **valuep,
99     ber_len_t *vlenp
100 )
101 {
102         struct berval type, value;
103         int rc = ldif_parse_line2( (char *)line, &type, &value, NULL );
104
105         *typep = type.bv_val;
106         *valuep = value.bv_val;
107         *vlenp = value.bv_len;
108         return rc;
109 }
110
111 int
112 ldif_parse_line2(
113     char        *line,
114         struct berval *type,
115         struct berval *value,
116         int             *freeval
117 )
118 {
119         char    *s, *p, *d; 
120         char    nib;
121         int     b64, url;
122
123         BER_BVZERO( type );
124         BER_BVZERO( value );
125
126         /* skip any leading space */
127         while ( isspace( (unsigned char) *line ) ) {
128                 line++;
129         }
130
131         if ( freeval ) {
132                 *freeval = 0;
133         } else {
134                 line = ber_strdup( line );
135
136                 if( line == NULL ) {
137                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
138                                 _("ldif_parse_line: line malloc failed\n"));
139                         return( -1 );
140                 }
141         }
142
143         type->bv_val = line;
144
145         s = strchr( type->bv_val, ':' );
146
147         if ( s == NULL ) {
148                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
149                         _("ldif_parse_line: missing ':' after %s\n"),
150                         type );
151                 if ( !freeval ) ber_memfree( line );
152                 return( -1 );
153         }
154
155         /* trim any space between type and : */
156         for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) {
157                 *p = '\0';
158         }
159         *s++ = '\0';
160         type->bv_len = s - type->bv_val - 1;
161
162         url = 0;
163         b64 = 0;
164
165         if ( *s == '<' ) {
166                 s++;
167                 url = 1;
168
169         } else if ( *s == ':' ) {
170                 /* base 64 encoded value */
171                 s++;
172                 b64 = 1;
173         }
174
175         /* skip space between : and value */
176         while ( isspace( (unsigned char) *s ) ) {
177                 s++;
178         }
179
180         /* check for continued line markers that should be deleted */
181         for ( p = s, d = s; *p; p++ ) {
182                 if ( *p != CONTINUED_LINE_MARKER )
183                         *d++ = *p;
184         }
185         *d = '\0';
186
187         if ( b64 ) {
188                 char *byte = s;
189
190                 if ( *s == '\0' ) {
191                         /* no value is present, error out */
192                         ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
193                                 _("ldif_parse_line: %s missing base64 value\n"), type );
194                         if ( !freeval ) ber_memfree( line );
195                         return( -1 );
196                 }
197
198                 byte = value->bv_val = s;
199
200                 for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) {
201                         int i;
202                         for ( i = 0; i < 4; i++ ) {
203                                 if ( p[i] != '=' && (p[i] & 0x80 ||
204                                     b642nib[ p[i] & 0x7f ] > 0x3f) ) {
205                                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
206                                                 _("ldif_parse_line: %s: invalid base64 encoding"
207                                                 " char (%c) 0x%x\n"),
208                                             type, p[i], p[i] );
209                                         if ( !freeval ) ber_memfree( line );
210                                         return( -1 );
211                                 }
212                         }
213
214                         /* first digit */
215                         nib = b642nib[ p[0] & 0x7f ];
216                         byte[0] = nib << 2;
217                         /* second digit */
218                         nib = b642nib[ p[1] & 0x7f ];
219                         byte[0] |= nib >> 4;
220                         byte[1] = (nib & RIGHT4) << 4;
221                         /* third digit */
222                         if ( p[2] == '=' ) {
223                                 value->bv_len += 1;
224                                 break;
225                         }
226                         nib = b642nib[ p[2] & 0x7f ];
227                         byte[1] |= nib >> 2;
228                         byte[2] = (nib & RIGHT2) << 6;
229                         /* fourth digit */
230                         if ( p[3] == '=' ) {
231                                 value->bv_len += 2;
232                                 break;
233                         }
234                         nib = b642nib[ p[3] & 0x7f ];
235                         byte[2] |= nib;
236
237                         byte += 3;
238                 }
239                 s[ value->bv_len ] = '\0';
240
241         } else if ( url ) {
242                 if ( *s == '\0' ) {
243                         /* no value is present, error out */
244                         ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
245                                 _("ldif_parse_line: %s missing URL value\n"), type );
246                         if ( !freeval ) ber_memfree( line );
247                         return( -1 );
248                 }
249
250                 if( ldif_fetch_url( s, &value->bv_val, &value->bv_len ) ) {
251                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
252                                 _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
253                                 type, s );
254                         if ( !freeval ) ber_memfree( line );
255                         return( -1 );
256                 }
257                 if ( freeval ) *freeval = 1;
258
259         } else {
260                 value->bv_val = s;
261                 value->bv_len = (int) (d - s);
262         }
263
264         if ( !freeval ) {
265                 struct berval bv = *type;
266
267                 ber_dupbv( type, &bv );
268
269                 if( BER_BVISNULL( type )) {
270                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
271                                 _("ldif_parse_line: type malloc failed\n"));
272                         if( url ) ber_memfree( value->bv_val );
273                         ber_memfree( line );
274                         return( -1 );
275                 }
276
277                 if( !url ) {
278                         bv = *value;
279                         ber_dupbv( value, &bv );
280                         if( BER_BVISNULL( value )) {
281                                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
282                                         _("ldif_parse_line: value malloc failed\n"));
283                                 ber_memfree( type->bv_val );
284                                 ber_memfree( line );
285                                 return( -1 );
286                         }
287                 }
288
289                 ber_memfree( line );
290         }
291
292         return( 0 );
293 }
294
295 /*
296  * ldif_getline - return the next "line" (minus newline) of input from a
297  * string buffer of lines separated by newlines, terminated by \n\n
298  * or \0.  this routine handles continued lines, bundling them into
299  * a single big line before returning.  if a line begins with a white
300  * space character, it is a continuation of the previous line. the white
301  * space character (nb: only one char), and preceeding newline are changed
302  * into CONTINUED_LINE_MARKER chars, to be deleted later by the
303  * ldif_parse_line() routine above.
304  *
305  * ldif_getline will skip over any line which starts '#'.
306  *
307  * ldif_getline takes a pointer to a pointer to the buffer on the first call,
308  * which it updates and must be supplied on subsequent calls.
309  */
310
311 int
312 ldif_countlines( LDAP_CONST char *buf )
313 {
314         char *nl;
315         int ret = 0;
316
317         if ( !buf ) return ret;
318
319         for ( nl = strchr(buf, '\n'); nl; nl = strchr(nl, '\n') ) {
320                 nl++;
321                 if ( *nl != ' ' ) ret++;
322         }
323         return ret;
324 }
325
326 char *
327 ldif_getline( char **next )
328 {
329         char *line;
330
331         do {
332                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
333                         return( NULL );
334                 }
335
336                 line = *next;
337
338                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
339 #if CONTINUED_LINE_MARKER != '\r'
340                         if ( (*next)[-1] == '\r' ) {
341                                 (*next)[-1] = CONTINUED_LINE_MARKER;
342                         }
343 #endif
344
345                         if ( (*next)[1] != ' ' ) {
346                                 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
347                                         *(*next)++ = '\0';
348                                 }
349                                 *(*next)++ = '\0';
350                                 break;
351                         }
352
353                         **next = CONTINUED_LINE_MARKER;
354                         (*next)[1] = CONTINUED_LINE_MARKER;
355                         (*next)++;
356                 }
357         } while( *line == '#' );
358
359         return( line );
360 }
361
362 /*
363  * name and OID of attributeTypes that must be base64 encoded in any case
364  */
365 typedef struct must_b64_encode_s {
366         struct berval   name;
367         struct berval   oid;
368 } must_b64_encode_s;
369
370 static must_b64_encode_s        default_must_b64_encode[] = {
371         { BER_BVC( "userPassword" ), BER_BVC( "2.5.4.35" ) },
372         { BER_BVNULL, BER_BVNULL }
373 };
374
375 static must_b64_encode_s        *must_b64_encode = default_must_b64_encode;
376
377 /*
378  * register name and OID of attributeTypes that must always be base64 
379  * encoded
380  *
381  * NOTE: this routine mallocs memory in a static struct which must 
382  * be explicitly freed when no longer required
383  */
384 int
385 ldif_must_b64_encode_register( LDAP_CONST char *name, LDAP_CONST char *oid )
386 {
387         int             i;
388         ber_len_t       len;
389
390         assert( must_b64_encode );
391         assert( name );
392         assert( oid );
393
394         len = strlen( name );
395
396         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
397                 if ( len != must_b64_encode[i].name.bv_len ) {
398                         continue;
399                 }
400
401                 if ( strcasecmp( name, must_b64_encode[i].name.bv_val ) == 0 ) {
402                         break;
403                 }
404         }
405
406         if ( !BER_BVISNULL( &must_b64_encode[i].name ) ) {
407                 return 1;
408         }
409
410         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ )
411                 /* just count */ ;
412
413         if ( must_b64_encode == default_must_b64_encode ) {
414                 must_b64_encode = ber_memalloc( sizeof( must_b64_encode_s ) * ( i + 2 ) );
415
416                 for ( i = 0; !BER_BVISNULL( &default_must_b64_encode[i].name ); i++ ) {
417                         ber_dupbv( &must_b64_encode[i].name, &default_must_b64_encode[i].name );
418                         ber_dupbv( &must_b64_encode[i].oid, &default_must_b64_encode[i].oid );
419                 }
420
421         } else {
422                 must_b64_encode_s       *tmp;
423
424                 tmp = ber_memrealloc( must_b64_encode,
425                         sizeof( must_b64_encode_s ) * ( i + 2 ) );
426                 if ( tmp == NULL ) {
427                         return 1;
428                 }
429                 must_b64_encode = tmp;
430         }
431
432         ber_str2bv( name, len, 1, &must_b64_encode[i].name );
433         ber_str2bv( oid, 0, 1, &must_b64_encode[i].oid );
434
435         BER_BVZERO( &must_b64_encode[i + 1].name );
436
437         return 0;
438 }
439
440 void
441 ldif_must_b64_encode_release( void )
442 {
443         int     i;
444
445         assert( must_b64_encode );
446
447         if ( must_b64_encode == default_must_b64_encode ) {
448                 return;
449         }
450
451         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
452                 ber_memfree( must_b64_encode[i].name.bv_val );
453                 ber_memfree( must_b64_encode[i].oid.bv_val );
454         }
455
456         ber_memfree( must_b64_encode );
457
458         must_b64_encode = default_must_b64_encode;
459 }
460
461 /*
462  * returns 1 iff the string corresponds to the name or the OID of any 
463  * of the attributeTypes listed in must_b64_encode
464  */
465 static int
466 ldif_must_b64_encode( LDAP_CONST char *s )
467 {
468         int             i;
469         struct berval   bv;
470
471         assert( must_b64_encode );
472         assert( s );
473
474         ber_str2bv( s, 0, 0, &bv );
475
476         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
477                 if ( ber_bvstrcasecmp( &must_b64_encode[i].name, &bv ) == 0
478                         || ber_bvcmp( &must_b64_encode[i].oid, &bv ) == 0 )
479                 {
480                         return 1;
481                 }
482         }
483
484         return 0;
485 }
486
487 /* compatibility with U-Mich off by one bug */
488 #define LDIF_KLUDGE 1
489
490 void
491 ldif_sput(
492         char **out,
493         int type,
494         LDAP_CONST char *name,
495         LDAP_CONST char *val,
496         ber_len_t vlen )
497 {
498         const unsigned char *byte, *stop;
499         unsigned char   buf[3];
500         unsigned long   bits;
501         char            *save;
502         int             pad;
503         int             namelen = 0;
504
505         ber_len_t savelen;
506         ber_len_t len=0;
507         ber_len_t i;
508
509         /* prefix */
510         switch( type ) {
511         case LDIF_PUT_COMMENT:
512                 *(*out)++ = '#';
513                 len++;
514
515                 if( vlen ) {
516                         *(*out)++ = ' ';
517                         len++;
518                 }
519
520                 break;
521
522         case LDIF_PUT_SEP:
523                 *(*out)++ = '\n';
524                 return;
525         }
526
527         /* name (attribute type) */
528         if( name != NULL ) {
529                 /* put the name + ":" */
530                 namelen = strlen(name);
531                 strcpy(*out, name);
532                 *out += namelen;
533                 len += namelen;
534
535                 if( type != LDIF_PUT_COMMENT ) {
536                         *(*out)++ = ':';
537                         len++;
538                 }
539
540         }
541 #ifdef LDAP_DEBUG
542         else {
543                 assert( type == LDIF_PUT_COMMENT );
544         }
545 #endif
546
547         if( vlen == 0 ) {
548                 *(*out)++ = '\n';
549                 return;
550         }
551
552         switch( type ) {
553         case LDIF_PUT_NOVALUE:
554                 *(*out)++ = '\n';
555                 return;
556
557         case LDIF_PUT_URL: /* url value */
558                 *(*out)++ = '<';
559                 len++;
560                 break;
561
562         case LDIF_PUT_B64: /* base64 value */
563                 *(*out)++ = ':';
564                 len++;
565                 break;
566         }
567
568         switch( type ) {
569         case LDIF_PUT_TEXT:
570         case LDIF_PUT_URL:
571         case LDIF_PUT_B64:
572                 *(*out)++ = ' ';
573                 len++;
574                 /* fall-thru */
575
576         case LDIF_PUT_COMMENT:
577                 /* pre-encoded names */
578                 for ( i=0; i < vlen; i++ ) {
579                         if ( len > LDIF_LINE_WIDTH ) {
580                                 *(*out)++ = '\n';
581                                 *(*out)++ = ' ';
582                                 len = 1;
583                         }
584
585                         *(*out)++ = val[i];
586                         len++;
587                 }
588                 *(*out)++ = '\n';
589                 return;
590         }
591
592         save = *out;
593         savelen = len;
594
595         *(*out)++ = ' ';
596         len++;
597
598         stop = (const unsigned char *) (val + vlen);
599
600         if ( type == LDIF_PUT_VALUE
601                 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
602                 && isgraph( (unsigned char) val[vlen-1] )
603 #ifndef LDAP_BINARY_DEBUG
604                 && strstr( name, ";binary" ) == NULL
605 #endif
606 #ifndef LDAP_PASSWD_DEBUG
607                 && !ldif_must_b64_encode( name )
608 #endif
609         ) {
610                 int b64 = 0;
611
612                 for ( byte = (const unsigned char *) val; byte < stop;
613                     byte++, len++ )
614                 {
615                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
616                                 b64 = 1;
617                                 break;
618                         }
619                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
620                                 *(*out)++ = '\n';
621                                 *(*out)++ = ' ';
622                                 len = 1;
623                         }
624                         *(*out)++ = *byte;
625                 }
626
627                 if( !b64 ) {
628                         *(*out)++ = '\n';
629                         return;
630                 }
631         }
632
633         *out = save;
634         *(*out)++ = ':';
635         *(*out)++ = ' ';
636         len = savelen + 2;
637
638         /* convert to base 64 (3 bytes => 4 base 64 digits) */
639         for ( byte = (const unsigned char *) val;
640                 byte < stop - 2;
641             byte += 3 )
642         {
643                 bits = (byte[0] & 0xff) << 16;
644                 bits |= (byte[1] & 0xff) << 8;
645                 bits |= (byte[2] & 0xff);
646
647                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
648                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
649                                 *(*out)++ = '\n';
650                                 *(*out)++ = ' ';
651                                 len = 1;
652                         }
653
654                         /* get b64 digit from high order 6 bits */
655                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
656                 }
657         }
658
659         /* add padding if necessary */
660         if ( byte < stop ) {
661                 for ( i = 0; byte + i < stop; i++ ) {
662                         buf[i] = byte[i];
663                 }
664                 for ( pad = 0; i < 3; i++, pad++ ) {
665                         buf[i] = '\0';
666                 }
667                 byte = buf;
668                 bits = (byte[0] & 0xff) << 16;
669                 bits |= (byte[1] & 0xff) << 8;
670                 bits |= (byte[2] & 0xff);
671
672                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
673                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
674                                 *(*out)++ = '\n';
675                                 *(*out)++ = ' ';
676                                 len = 1;
677                         }
678
679                         if( i + pad < 4 ) {
680                                 /* get b64 digit from low order 6 bits */
681                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
682                         } else {
683                                 *(*out)++ = '=';
684                         }
685                 }
686         }
687         *(*out)++ = '\n';
688 }
689
690
691 /*
692  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
693  */
694 char *
695 ldif_put(
696         int type,
697         LDAP_CONST char *name,
698         LDAP_CONST char *val,
699         ber_len_t vlen )
700 {
701     char        *buf, *p;
702     ber_len_t nlen;
703
704     nlen = ( name != NULL ) ? strlen( name ) : 0;
705
706         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
707
708     if ( buf == NULL ) {
709                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
710                         _("ldif_type_and_value: malloc failed!"));
711                 return NULL;
712     }
713
714     p = buf;
715     ldif_sput( &p, type, name, val, vlen );
716     *p = '\0';
717
718     return( buf );
719 }
720
721 int ldif_is_not_printable(
722         LDAP_CONST char *val,
723         ber_len_t vlen )
724 {
725         if( vlen == 0 || val == NULL  ) {
726                 return -1;
727         }
728
729         if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
730                 isgraph( (unsigned char) val[vlen-1] ) )
731         {
732                 ber_len_t i;
733
734                 for ( i = 0; val[i]; i++ ) {
735                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
736                                 return 1;
737                         }
738                 }
739
740                 return 0;
741         }
742
743         return 1;
744 }
745
746 /*
747  * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
748  */
749 int
750 ldif_read_record(
751         FILE        *fp,
752         int         *lno,               /* ptr to line number counter              */
753         char        **bufp,     /* ptr to malloced output buffer           */
754         int         *buflenp )  /* ptr to length of *bufp                  */
755 {
756         char        linebuf[BUFSIZ], *line, *nbufp;
757         ber_len_t   lcur = 0, len, linesize;
758         int         last_ch = '\n', found_entry = 0, stop, top_comment = 0;
759
760         line     = linebuf;
761         linesize = sizeof( linebuf );
762
763         for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) {
764                 if ( fgets( line, linesize, fp ) == NULL ) {
765                         stop = 1;
766                         /* Add \n in case the file does not end with newline */
767                         line = "\n";
768                 }
769                 len = strlen( line );
770
771                 if ( last_ch == '\n' ) {
772                         (*lno)++;
773
774                         if ( line[0] == '\n' ) {
775                                 if ( !found_entry ) {
776                                         lcur = 0;
777                                         top_comment = 0;
778                                         continue;
779                                 }
780                                 break;
781                         }
782
783                         if ( !found_entry ) {
784                                 if ( line[0] == '#' ) {
785                                         top_comment = 1;
786                                 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
787                                         /* Found a new entry */
788                                         found_entry = 1;
789
790                                         if ( isdigit( (unsigned char) line[0] ) ) {
791                                                 /* skip index */
792                                                 continue;
793                                         }
794                                 }
795                         }                       
796                 }
797
798                 if ( *buflenp - lcur <= len ) {
799                         *buflenp += len + BUFSIZ;
800                         nbufp = ber_memrealloc( *bufp, *buflenp );
801                         if( nbufp == NULL ) {
802                                 return 0;
803                         }
804                         *bufp = nbufp;
805                 }
806                 strcpy( *bufp + lcur, line );
807                 lcur += len;
808         }
809
810         return( found_entry );
811 }