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
94 LDAP_CONST char *line,
100 return ldif_parse_line2( line, typep, valuep, vlenp, NULL );
105 LDAP_CONST char *line,
122 /* skip any leading space */
123 while ( isspace( (unsigned char) *line ) ) {
130 line = ber_strdup( line );
133 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
134 _("ldif_parse_line: line malloc failed\n"));
141 s = strchr( type, ':' );
144 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
145 _("ldif_parse_line: missing ':' after %s\n"),
147 if ( !alloc ) ber_memfree( line );
151 /* trim any space between type and : */
152 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
164 } else if ( *s == ':' ) {
165 /* base 64 encoded value */
170 /* skip space between : and value */
171 while ( isspace( (unsigned char) *s ) ) {
175 /* check for continued line markers that should be deleted */
176 for ( p = s, d = s; *p; p++ ) {
177 if ( *p != CONTINUED_LINE_MARKER )
186 /* no value is present, error out */
187 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
188 _("ldif_parse_line: %s missing base64 value\n"), type );
189 if ( !alloc ) ber_memfree( line );
195 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
197 for ( i = 0; i < 4; i++ ) {
198 if ( p[i] != '=' && (p[i] & 0x80 ||
199 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
200 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
201 _("ldif_parse_line: %s: invalid base64 encoding"
202 " char (%c) 0x%x\n"),
204 if ( !alloc ) ber_memfree( line );
210 nib = b642nib[ p[0] & 0x7f ];
213 nib = b642nib[ p[1] & 0x7f ];
215 byte[1] = (nib & RIGHT4) << 4;
221 nib = b642nib[ p[2] & 0x7f ];
223 byte[2] = (nib & RIGHT2) << 6;
229 nib = b642nib[ p[3] & 0x7f ];
238 /* no value is present, error out */
239 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
240 _("ldif_parse_line: %s missing URL value\n"), type );
241 if ( !alloc ) ber_memfree( line );
245 if( ldif_fetch_url( s, &value, &vlen ) ) {
246 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
247 _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
249 if ( !alloc ) ber_memfree( line );
252 if ( alloc ) *alloc = 1;
256 vlen = (int) (d - s);
260 type = ber_strdup( type );
263 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
264 _("ldif_parse_line: type malloc failed\n"));
265 if( url ) ber_memfree( value );
271 p = ber_memalloc( vlen + 1 );
273 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
274 _("ldif_parse_line: value malloc failed\n"));
279 AC_MEMCPY( p, value, vlen );
295 * ldif_getline - return the next "line" (minus newline) of input from a
296 * string buffer of lines separated by newlines, terminated by \n\n
297 * or \0. this routine handles continued lines, bundling them into
298 * a single big line before returning. if a line begins with a white
299 * space character, it is a continuation of the previous line. the white
300 * space character (nb: only one char), and preceeding newline are changed
301 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
302 * ldif_parse_line() routine above.
304 * ldif_getline will skip over any line which starts '#'.
306 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
307 * which it updates and must be supplied on subsequent calls.
311 ldif_getline( char **next )
316 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
322 while ( (*next = strchr( *next, '\n' )) != NULL ) {
323 #if CONTINUED_LINE_MARKER != '\r'
324 if ( (*next)[-1] == '\r' ) {
325 (*next)[-1] = CONTINUED_LINE_MARKER;
329 if ( (*next)[1] != ' ' ) {
330 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
337 **next = CONTINUED_LINE_MARKER;
338 (*next)[1] = CONTINUED_LINE_MARKER;
341 } while( *line == '#' );
346 /* compatibility with U-Mich off by one bug */
347 #define LDIF_KLUDGE 1
353 LDAP_CONST char *name,
354 LDAP_CONST char *val,
357 const unsigned char *byte, *stop;
358 unsigned char buf[3];
370 case LDIF_PUT_COMMENT:
386 /* name (attribute type) */
388 /* put the name + ":" */
389 namelen = strlen(name);
394 if( type != LDIF_PUT_COMMENT ) {
402 assert( type == LDIF_PUT_COMMENT );
412 case LDIF_PUT_NOVALUE:
416 case LDIF_PUT_URL: /* url value */
421 case LDIF_PUT_B64: /* base64 value */
435 case LDIF_PUT_COMMENT:
436 /* pre-encoded names */
437 for ( i=0; i < vlen; i++ ) {
438 if ( len > LDIF_LINE_WIDTH ) {
457 stop = (const unsigned char *) (val + vlen);
459 if ( type == LDIF_PUT_VALUE
460 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
461 && isgraph( (unsigned char) val[vlen-1] )
462 #ifndef LDAP_BINARY_DEBUG
463 && strstr( name, ";binary" ) == NULL
465 #ifndef LDAP_PASSWD_DEBUG
466 && (namelen != (sizeof("userPassword")-1)
467 || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
468 && (namelen != (sizeof("2.5.4.35")-1)
469 || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
474 for ( byte = (const unsigned char *) val; byte < stop;
477 if ( !isascii( *byte ) || !isprint( *byte ) ) {
481 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
500 /* convert to base 64 (3 bytes => 4 base 64 digits) */
501 for ( byte = (const unsigned char *) val;
505 bits = (byte[0] & 0xff) << 16;
506 bits |= (byte[1] & 0xff) << 8;
507 bits |= (byte[2] & 0xff);
509 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
510 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
516 /* get b64 digit from high order 6 bits */
517 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
521 /* add padding if necessary */
523 for ( i = 0; byte + i < stop; i++ ) {
526 for ( pad = 0; i < 3; i++, pad++ ) {
530 bits = (byte[0] & 0xff) << 16;
531 bits |= (byte[1] & 0xff) << 8;
532 bits |= (byte[2] & 0xff);
534 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
535 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
542 /* get b64 digit from low order 6 bits */
543 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
554 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
559 LDAP_CONST char *name,
560 LDAP_CONST char *val,
566 nlen = ( name != NULL ) ? strlen( name ) : 0;
568 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
571 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
572 _("ldif_type_and_value: malloc failed!"));
577 ldif_sput( &p, type, name, val, vlen );
583 int ldif_is_not_printable(
584 LDAP_CONST char *val,
587 if( vlen == 0 || val == NULL ) {
591 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
592 isgraph( (unsigned char) val[vlen-1] ) )
596 for ( i = 0; val[i]; i++ ) {
597 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
609 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
614 int *lno, /* ptr to line number counter */
615 char **bufp, /* ptr to malloced output buffer */
616 int *buflenp ) /* ptr to length of *bufp */
618 char linebuf[BUFSIZ], *line, *nbufp;
619 ber_len_t lcur = 0, len, linesize;
620 int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
623 linesize = sizeof( linebuf );
625 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
626 if ( fgets( line, linesize, fp ) == NULL ) {
628 /* Add \n in case the file does not end with newline */
631 len = strlen( line );
633 if ( last_ch == '\n' ) {
636 if ( line[0] == '\n' ) {
637 if ( !found_entry ) {
645 if ( !found_entry ) {
646 if ( line[0] == '#' ) {
648 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
649 /* Found a new entry */
652 if ( isdigit( (unsigned char) line[0] ) ) {
660 if ( *buflenp - lcur <= len ) {
661 *buflenp += len + BUFSIZ;
662 nbufp = ber_memrealloc( *bufp, *buflenp );
663 if( nbufp == NULL ) {
668 strcpy( *bufp + lcur, line );
672 return( found_entry );