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_getline( char **next )
317 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
323 while ( (*next = strchr( *next, '\n' )) != NULL ) {
324 #if CONTINUED_LINE_MARKER != '\r'
325 if ( (*next)[-1] == '\r' ) {
326 (*next)[-1] = CONTINUED_LINE_MARKER;
330 if ( (*next)[1] != ' ' ) {
331 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
338 **next = CONTINUED_LINE_MARKER;
339 (*next)[1] = CONTINUED_LINE_MARKER;
342 } while( *line == '#' );
347 /* compatibility with U-Mich off by one bug */
348 #define LDIF_KLUDGE 1
354 LDAP_CONST char *name,
355 LDAP_CONST char *val,
358 const unsigned char *byte, *stop;
359 unsigned char buf[3];
371 case LDIF_PUT_COMMENT:
387 /* name (attribute type) */
389 /* put the name + ":" */
390 namelen = strlen(name);
395 if( type != LDIF_PUT_COMMENT ) {
403 assert( type == LDIF_PUT_COMMENT );
413 case LDIF_PUT_NOVALUE:
417 case LDIF_PUT_URL: /* url value */
422 case LDIF_PUT_B64: /* base64 value */
436 case LDIF_PUT_COMMENT:
437 /* pre-encoded names */
438 for ( i=0; i < vlen; i++ ) {
439 if ( len > LDIF_LINE_WIDTH ) {
458 stop = (const unsigned char *) (val + vlen);
460 if ( type == LDIF_PUT_VALUE
461 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
462 && isgraph( (unsigned char) val[vlen-1] )
463 #ifndef LDAP_BINARY_DEBUG
464 && strstr( name, ";binary" ) == NULL
466 #ifndef LDAP_PASSWD_DEBUG
467 && (namelen != (sizeof("userPassword")-1)
468 || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
469 && (namelen != (sizeof("2.5.4.35")-1)
470 || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
475 for ( byte = (const unsigned char *) val; byte < stop;
478 if ( !isascii( *byte ) || !isprint( *byte ) ) {
482 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
501 /* convert to base 64 (3 bytes => 4 base 64 digits) */
502 for ( byte = (const unsigned char *) val;
506 bits = (byte[0] & 0xff) << 16;
507 bits |= (byte[1] & 0xff) << 8;
508 bits |= (byte[2] & 0xff);
510 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
511 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
517 /* get b64 digit from high order 6 bits */
518 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
522 /* add padding if necessary */
524 for ( i = 0; byte + i < stop; i++ ) {
527 for ( pad = 0; i < 3; i++, pad++ ) {
531 bits = (byte[0] & 0xff) << 16;
532 bits |= (byte[1] & 0xff) << 8;
533 bits |= (byte[2] & 0xff);
535 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
536 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
543 /* get b64 digit from low order 6 bits */
544 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
555 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
560 LDAP_CONST char *name,
561 LDAP_CONST char *val,
567 nlen = ( name != NULL ) ? strlen( name ) : 0;
569 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
572 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
573 _("ldif_type_and_value: malloc failed!"));
578 ldif_sput( &p, type, name, val, vlen );
584 int ldif_is_not_printable(
585 LDAP_CONST char *val,
588 if( vlen == 0 || val == NULL ) {
592 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
593 isgraph( (unsigned char) val[vlen-1] ) )
597 for ( i = 0; val[i]; i++ ) {
598 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
610 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
615 int *lno, /* ptr to line number counter */
616 char **bufp, /* ptr to malloced output buffer */
617 int *buflenp ) /* ptr to length of *bufp */
619 char linebuf[BUFSIZ], *line, *nbufp;
620 ber_len_t lcur = 0, len, linesize;
621 int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
624 linesize = sizeof( linebuf );
626 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
627 if ( fgets( line, linesize, fp ) == NULL ) {
629 /* Add \n in case the file does not end with newline */
632 len = strlen( line );
634 if ( last_ch == '\n' ) {
637 if ( line[0] == '\n' ) {
638 if ( !found_entry ) {
646 if ( !found_entry ) {
647 if ( line[0] == '#' ) {
649 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
650 /* Found a new entry */
653 if ( isdigit( (unsigned char) line[0] ) ) {
661 if ( *buflenp - lcur <= len ) {
662 *buflenp += len + BUFSIZ;
663 nbufp = ber_memrealloc( *bufp, *buflenp );
664 if( nbufp == NULL ) {
669 strcpy( *bufp + lcur, line );
673 return( found_entry );