1 /* ldif.c - routines for dealing with LDIF files */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
17 * All rights reserved.
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.
26 /* This work was originally developed by the University of Michigan
27 * and distributed as part of U-MICH LDAP.
34 #include <ac/stdlib.h>
37 #include <ac/string.h>
38 #include <ac/socket.h>
49 #define CONTINUED_LINE_MARKER '\r'
52 #define ber_memalloc malloc
53 #define ber_memcalloc calloc
54 #define ber_memrealloc realloc
55 #define ber_strdup strdup
58 static const char nib2b64[0x40] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
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
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.
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.
96 LDAP_CONST char *line,
102 struct berval type, value;
103 int rc = ldif_parse_line2( (char *)line, &type, &value, NULL );
105 *typep = type.bv_val;
106 *valuep = value.bv_val;
107 *vlenp = value.bv_len;
115 struct berval *value,
126 /* skip any leading space */
127 while ( isspace( (unsigned char) *line ) ) {
134 line = ber_strdup( line );
137 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
138 _("ldif_parse_line: line malloc failed\n"));
145 s = strchr( type->bv_val, ':' );
148 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
149 _("ldif_parse_line: missing ':' after %s\n"),
151 if ( !freeval ) ber_memfree( line );
155 /* trim any space between type and : */
156 for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) {
160 type->bv_len = s - type->bv_val - 1;
169 } else if ( *s == ':' ) {
170 /* base 64 encoded value */
175 /* skip space between : and value */
176 while ( isspace( (unsigned char) *s ) ) {
180 /* check for continued line markers that should be deleted */
181 for ( p = s, d = s; *p; p++ ) {
182 if ( *p != CONTINUED_LINE_MARKER )
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 );
198 byte = value->bv_val = s;
200 for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) {
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"),
209 if ( !freeval ) ber_memfree( line );
215 nib = b642nib[ p[0] & 0x7f ];
218 nib = b642nib[ p[1] & 0x7f ];
220 byte[1] = (nib & RIGHT4) << 4;
226 nib = b642nib[ p[2] & 0x7f ];
228 byte[2] = (nib & RIGHT2) << 6;
234 nib = b642nib[ p[3] & 0x7f ];
239 s[ value->bv_len ] = '\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 );
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"),
254 if ( !freeval ) ber_memfree( line );
257 if ( freeval ) *freeval = 1;
261 value->bv_len = (int) (d - s);
265 struct berval bv = *type;
267 ber_dupbv( type, &bv );
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 );
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 );
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.
305 * ldif_getline will skip over any line which starts '#'.
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.
312 ldif_countlines( LDAP_CONST char *buf )
317 if ( !buf ) return ret;
319 for ( nl = strchr(buf, '\n'); nl; nl = strchr(nl, '\n') ) {
321 if ( *nl != ' ' ) ret++;
327 ldif_getline( char **next )
332 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
338 while ( (*next = strchr( *next, '\n' )) != NULL ) {
339 #if CONTINUED_LINE_MARKER != '\r'
340 if ( (*next)[-1] == '\r' ) {
341 (*next)[-1] = CONTINUED_LINE_MARKER;
345 if ( (*next)[1] != ' ' ) {
346 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
353 **next = CONTINUED_LINE_MARKER;
354 (*next)[1] = CONTINUED_LINE_MARKER;
357 } while( *line == '#' );
362 /* compatibility with U-Mich off by one bug */
363 #define LDIF_KLUDGE 1
369 LDAP_CONST char *name,
370 LDAP_CONST char *val,
373 const unsigned char *byte, *stop;
374 unsigned char buf[3];
386 case LDIF_PUT_COMMENT:
402 /* name (attribute type) */
404 /* put the name + ":" */
405 namelen = strlen(name);
410 if( type != LDIF_PUT_COMMENT ) {
418 assert( type == LDIF_PUT_COMMENT );
428 case LDIF_PUT_NOVALUE:
432 case LDIF_PUT_URL: /* url value */
437 case LDIF_PUT_B64: /* base64 value */
451 case LDIF_PUT_COMMENT:
452 /* pre-encoded names */
453 for ( i=0; i < vlen; i++ ) {
454 if ( len > LDIF_LINE_WIDTH ) {
473 stop = (const unsigned char *) (val + vlen);
475 if ( type == LDIF_PUT_VALUE
476 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
477 && isgraph( (unsigned char) val[vlen-1] )
478 #ifndef LDAP_BINARY_DEBUG
479 && strstr( name, ";binary" ) == NULL
481 #ifndef LDAP_PASSWD_DEBUG
482 && (namelen != (sizeof("userPassword")-1)
483 || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
484 && (namelen != (sizeof("2.5.4.35")-1)
485 || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
490 for ( byte = (const unsigned char *) val; byte < stop;
493 if ( !isascii( *byte ) || !isprint( *byte ) ) {
497 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
516 /* convert to base 64 (3 bytes => 4 base 64 digits) */
517 for ( byte = (const unsigned char *) val;
521 bits = (byte[0] & 0xff) << 16;
522 bits |= (byte[1] & 0xff) << 8;
523 bits |= (byte[2] & 0xff);
525 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
526 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
532 /* get b64 digit from high order 6 bits */
533 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
537 /* add padding if necessary */
539 for ( i = 0; byte + i < stop; i++ ) {
542 for ( pad = 0; i < 3; i++, pad++ ) {
546 bits = (byte[0] & 0xff) << 16;
547 bits |= (byte[1] & 0xff) << 8;
548 bits |= (byte[2] & 0xff);
550 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
551 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
558 /* get b64 digit from low order 6 bits */
559 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
570 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
575 LDAP_CONST char *name,
576 LDAP_CONST char *val,
582 nlen = ( name != NULL ) ? strlen( name ) : 0;
584 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
587 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
588 _("ldif_type_and_value: malloc failed!"));
593 ldif_sput( &p, type, name, val, vlen );
599 int ldif_is_not_printable(
600 LDAP_CONST char *val,
603 if( vlen == 0 || val == NULL ) {
607 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
608 isgraph( (unsigned char) val[vlen-1] ) )
612 for ( i = 0; val[i]; i++ ) {
613 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
625 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
630 int *lno, /* ptr to line number counter */
631 char **bufp, /* ptr to malloced output buffer */
632 int *buflenp ) /* ptr to length of *bufp */
634 char linebuf[BUFSIZ], *line, *nbufp;
635 ber_len_t lcur = 0, len, linesize;
636 int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
639 linesize = sizeof( linebuf );
641 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
642 if ( fgets( line, linesize, fp ) == NULL ) {
644 /* Add \n in case the file does not end with newline */
647 len = strlen( line );
649 if ( last_ch == '\n' ) {
652 if ( line[0] == '\n' ) {
653 if ( !found_entry ) {
661 if ( !found_entry ) {
662 if ( line[0] == '#' ) {
664 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
665 /* Found a new entry */
668 if ( isdigit( (unsigned char) line[0] ) ) {
676 if ( *buflenp - lcur <= len ) {
677 *buflenp += len + BUFSIZ;
678 nbufp = ber_memrealloc( *bufp, *buflenp );
679 if( nbufp == NULL ) {
684 strcpy( *bufp + lcur, line );
688 return( found_entry );