1 /* ldif.c - routines for dealing with LDIF files */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2011 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"),
195 if ( !freeval ) ber_memfree( line );
199 byte = value->bv_val = s;
201 for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) {
203 for ( i = 0; i < 4; i++ ) {
204 if ( p[i] != '=' && (p[i] & 0x80 ||
205 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
206 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
207 _("ldif_parse_line: %s: invalid base64 encoding"
208 " char (%c) 0x%x\n"),
209 type->bv_val, p[i], p[i] );
210 if ( !freeval ) ber_memfree( line );
216 nib = b642nib[ p[0] & 0x7f ];
219 nib = b642nib[ p[1] & 0x7f ];
221 byte[1] = (nib & RIGHT4) << 4;
227 nib = b642nib[ p[2] & 0x7f ];
229 byte[2] = (nib & RIGHT2) << 6;
235 nib = b642nib[ p[3] & 0x7f ];
240 s[ value->bv_len ] = '\0';
244 /* no value is present, error out */
245 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
246 _("ldif_parse_line: %s missing URL value\n"),
248 if ( !freeval ) ber_memfree( line );
252 if( ldif_fetch_url( s, &value->bv_val, &value->bv_len ) ) {
253 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
254 _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
256 if ( !freeval ) ber_memfree( line );
259 if ( freeval ) *freeval = 1;
263 value->bv_len = (int) (d - s);
267 struct berval bv = *type;
269 ber_dupbv( type, &bv );
271 if( BER_BVISNULL( type )) {
272 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
273 _("ldif_parse_line: type malloc failed\n"));
274 if( url ) ber_memfree( value->bv_val );
281 ber_dupbv( value, &bv );
282 if( BER_BVISNULL( value )) {
283 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
284 _("ldif_parse_line: value malloc failed\n"));
285 ber_memfree( type->bv_val );
298 * ldif_getline - return the next "line" (minus newline) of input from a
299 * string buffer of lines separated by newlines, terminated by \n\n
300 * or \0. this routine handles continued lines, bundling them into
301 * a single big line before returning. if a line begins with a white
302 * space character, it is a continuation of the previous line. the white
303 * space character (nb: only one char), and preceeding newline are changed
304 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
305 * ldif_parse_line() routine above.
307 * ldif_getline will skip over any line which starts '#'.
309 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
310 * which it updates and must be supplied on subsequent calls.
314 ldif_countlines( LDAP_CONST char *buf )
319 if ( !buf ) return ret;
321 for ( nl = strchr(buf, '\n'); nl; nl = strchr(nl, '\n') ) {
323 if ( *nl != ' ' ) ret++;
329 ldif_getline( char **next )
334 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
340 while ( (*next = strchr( *next, '\n' )) != NULL ) {
341 #if CONTINUED_LINE_MARKER != '\r'
342 if ( (*next)[-1] == '\r' ) {
343 (*next)[-1] = CONTINUED_LINE_MARKER;
347 if ( (*next)[1] != ' ' ) {
348 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
355 **next = CONTINUED_LINE_MARKER;
356 (*next)[1] = CONTINUED_LINE_MARKER;
359 } while( *line == '#' );
365 * name and OID of attributeTypes that must be base64 encoded in any case
367 typedef struct must_b64_encode_s {
372 static must_b64_encode_s default_must_b64_encode[] = {
373 { BER_BVC( "userPassword" ), BER_BVC( "2.5.4.35" ) },
374 { BER_BVNULL, BER_BVNULL }
377 static must_b64_encode_s *must_b64_encode = default_must_b64_encode;
380 * register name and OID of attributeTypes that must always be base64
383 * NOTE: this routine mallocs memory in a static struct which must
384 * be explicitly freed when no longer required
387 ldif_must_b64_encode_register( LDAP_CONST char *name, LDAP_CONST char *oid )
392 assert( must_b64_encode != NULL );
393 assert( name != NULL );
394 assert( oid != NULL );
396 len = strlen( name );
398 for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
399 if ( len != must_b64_encode[i].name.bv_len ) {
403 if ( strcasecmp( name, must_b64_encode[i].name.bv_val ) == 0 ) {
408 if ( !BER_BVISNULL( &must_b64_encode[i].name ) ) {
412 for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ )
415 if ( must_b64_encode == default_must_b64_encode ) {
416 must_b64_encode = ber_memalloc( sizeof( must_b64_encode_s ) * ( i + 2 ) );
418 for ( i = 0; !BER_BVISNULL( &default_must_b64_encode[i].name ); i++ ) {
419 ber_dupbv( &must_b64_encode[i].name, &default_must_b64_encode[i].name );
420 ber_dupbv( &must_b64_encode[i].oid, &default_must_b64_encode[i].oid );
424 must_b64_encode_s *tmp;
426 tmp = ber_memrealloc( must_b64_encode,
427 sizeof( must_b64_encode_s ) * ( i + 2 ) );
431 must_b64_encode = tmp;
434 ber_str2bv( name, len, 1, &must_b64_encode[i].name );
435 ber_str2bv( oid, 0, 1, &must_b64_encode[i].oid );
437 BER_BVZERO( &must_b64_encode[i + 1].name );
443 ldif_must_b64_encode_release( void )
447 assert( must_b64_encode != NULL );
449 if ( must_b64_encode == default_must_b64_encode ) {
453 for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
454 ber_memfree( must_b64_encode[i].name.bv_val );
455 ber_memfree( must_b64_encode[i].oid.bv_val );
458 ber_memfree( must_b64_encode );
460 must_b64_encode = default_must_b64_encode;
464 * returns 1 iff the string corresponds to the name or the OID of any
465 * of the attributeTypes listed in must_b64_encode
468 ldif_must_b64_encode( LDAP_CONST char *s )
473 assert( must_b64_encode != NULL );
476 ber_str2bv( s, 0, 0, &bv );
478 for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) {
479 if ( ber_bvstrcasecmp( &must_b64_encode[i].name, &bv ) == 0
480 || ber_bvcmp( &must_b64_encode[i].oid, &bv ) == 0 )
489 /* compatibility with U-Mich off by one bug */
490 #define LDIF_KLUDGE 1
492 /* NOTE: only preserved for binary compatibility */
497 LDAP_CONST char *name,
498 LDAP_CONST char *val,
501 ldif_sput_wrap( out, type, name, val, vlen, LDIF_LINE_WIDTH );
508 LDAP_CONST char *name,
509 LDAP_CONST char *val,
513 const unsigned char *byte, *stop;
514 unsigned char buf[3];
524 wrap = LDIF_LINE_WIDTH_WRAP( wrap );
528 case LDIF_PUT_COMMENT:
544 /* name (attribute type) */
546 /* put the name + ":" */
547 namelen = strlen(name);
552 if( type != LDIF_PUT_COMMENT ) {
560 assert( type == LDIF_PUT_COMMENT );
570 case LDIF_PUT_NOVALUE:
574 case LDIF_PUT_URL: /* url value */
579 case LDIF_PUT_B64: /* base64 value */
593 case LDIF_PUT_COMMENT:
594 /* pre-encoded names */
595 for ( i=0; i < vlen; i++ ) {
615 stop = (const unsigned char *) (val + vlen);
617 if ( type == LDIF_PUT_VALUE
618 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
619 && isgraph( (unsigned char) val[vlen-1] )
620 #ifndef LDAP_BINARY_DEBUG
621 && strstr( name, ";binary" ) == NULL
623 #ifndef LDAP_PASSWD_DEBUG
624 && !ldif_must_b64_encode( name )
629 for ( byte = (const unsigned char *) val; byte < stop;
632 if ( !isascii( *byte ) || !isprint( *byte ) ) {
636 if ( len - LDIF_KLUDGE > wrap ) {
655 /* convert to base 64 (3 bytes => 4 base 64 digits) */
656 for ( byte = (const unsigned char *) val;
660 bits = (byte[0] & 0xff) << 16;
661 bits |= (byte[1] & 0xff) << 8;
662 bits |= (byte[2] & 0xff);
664 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
665 if ( len - LDIF_KLUDGE > wrap ) {
671 /* get b64 digit from high order 6 bits */
672 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
676 /* add padding if necessary */
678 for ( i = 0; byte + i < stop; i++ ) {
681 for ( pad = 0; i < 3; i++, pad++ ) {
685 bits = (byte[0] & 0xff) << 16;
686 bits |= (byte[1] & 0xff) << 8;
687 bits |= (byte[2] & 0xff);
689 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
690 if ( len - LDIF_KLUDGE > wrap ) {
697 /* get b64 digit from low order 6 bits */
698 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
709 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
712 /* NOTE: only preserved for binary compatibility */
716 LDAP_CONST char *name,
717 LDAP_CONST char *val,
720 return ldif_put_wrap( type, name, val, vlen, LDIF_LINE_WIDTH );
726 LDAP_CONST char *name,
727 LDAP_CONST char *val,
734 nlen = ( name != NULL ) ? strlen( name ) : 0;
736 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED_WRAP( nlen, vlen, wrap ) + 1 );
739 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
740 _("ldif_type_and_value: malloc failed!"));
745 ldif_sput_wrap( &p, type, name, val, vlen, wrap );
751 int ldif_is_not_printable(
752 LDAP_CONST char *val,
755 if( vlen == 0 || val == NULL ) {
759 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
760 isgraph( (unsigned char) val[vlen-1] ) )
764 for ( i = 0; val[i]; i++ ) {
765 if ( !isascii( val[i] ) || !isprint( (unsigned char) val[i] ) ) {
778 LDAP_CONST char *file,
779 LDAP_CONST char *mode
782 FILE *fp = fopen( file, mode );
786 lfp = ber_memalloc( sizeof( LDIFFP ));
808 #define LDIF_MAXLINE 4096
811 * ldif_read_record - read an ldif record. Return 1 for success, 0 for EOF,
817 int *lno, /* ptr to line number counter */
818 char **bufp, /* ptr to malloced output buffer */
819 int *buflenp ) /* ptr to length of *bufp */
821 char line[LDIF_MAXLINE], *nbufp;
822 ber_len_t lcur = 0, len;
823 int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
825 for ( stop = 0; !stop; last_ch = line[len-1] ) {
826 /* If we're at the end of this file, see if we should pop
827 * back to a previous file. (return from an include)
829 while ( feof( lfp->fp )) {
831 LDIFFP *tmp = lfp->prev;
843 if ( fgets( line, sizeof( line ), lfp->fp ) == NULL ) {
847 len = strlen( line );
850 if ( len == 0 || line[len-1] != '\n' ) {
851 /* Add \n in case the line/file does not end with newline */
856 if ( last_ch == '\n' ) {
859 if ( line[0] == '\n' ||
860 ( line[0] == '\r' && line[1] == '\n' )) {
861 if ( !found_entry ) {
869 if ( !found_entry ) {
870 if ( line[0] == '#' ) {
872 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
873 /* Found a new entry */
876 if ( isdigit( (unsigned char) line[0] ) ) {
880 if ( !strncasecmp( line, "include:",
881 STRLENOF("include:"))) {
886 if ( line[len-1] == '\n' ) {
890 if ( line[len-1] == '\r' ) {
895 ptr = line + STRLENOF("include:");
896 while (isspace((unsigned char) *ptr)) ptr++;
897 fp2 = ldif_open_url( ptr );
899 LDIFFP *lnew = ber_memalloc( sizeof( LDIFFP ));
900 if ( lnew == NULL ) {
904 lnew->prev = lfp->prev;
912 /* We failed to open the file, this should
913 * be reported as an error somehow.
915 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
916 _("ldif_read_record: include %s failed\n"), ptr );
924 if ( *buflenp - lcur <= len ) {
925 *buflenp += len + LDIF_MAXLINE;
926 nbufp = ber_memrealloc( *bufp, *buflenp );
927 if( nbufp == NULL ) {
932 strcpy( *bufp + lcur, line );
936 return( found_entry );