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.
89 LDAP_CONST char *line,
98 char *freeme, *type, *value;
105 /* skip any leading space */
106 while ( isspace( (unsigned char) *line ) ) {
110 freeme = ber_strdup( line );
112 if( freeme == NULL ) {
113 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
114 _("ldif_parse_line: line malloc failed\n"));
120 s = strchr( type, ':' );
123 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
124 _("ldif_parse_line: missing ':' after %s\n"),
126 ber_memfree( freeme );
130 /* trim any space between type and : */
131 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
143 } else if ( *s == ':' ) {
144 /* base 64 encoded value */
149 /* skip space between : and value */
150 while ( isspace( (unsigned char) *s ) ) {
154 /* check for continued line markers that should be deleted */
155 for ( p = s, d = s; *p; p++ ) {
156 if ( *p != CONTINUED_LINE_MARKER )
165 /* no value is present, error out */
166 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
167 _("ldif_parse_line: %s missing base64 value\n"), type );
168 ber_memfree( freeme );
174 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
176 for ( i = 0; i < 4; i++ ) {
177 if ( p[i] != '=' && (p[i] & 0x80 ||
178 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
179 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
180 _("ldif_parse_line: %s: invalid base64 encoding"
181 " char (%c) 0x%x\n"),
183 ber_memfree( freeme );
189 nib = b642nib[ p[0] & 0x7f ];
192 nib = b642nib[ p[1] & 0x7f ];
194 byte[1] = (nib & RIGHT4) << 4;
200 nib = b642nib[ p[2] & 0x7f ];
202 byte[2] = (nib & RIGHT2) << 6;
208 nib = b642nib[ p[3] & 0x7f ];
217 /* no value is present, error out */
218 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
219 _("ldif_parse_line: %s missing URL value\n"), type );
220 ber_memfree( freeme );
224 if( ldif_fetch_url( s, &value, &vlen ) ) {
225 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
226 _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"),
228 ber_memfree( freeme );
234 vlen = (int) (d - s);
237 type = ber_strdup( type );
240 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
241 _("ldif_parse_line: type malloc failed\n"));
242 if( url ) ber_memfree( value );
243 ber_memfree( freeme );
248 p = ber_memalloc( vlen + 1 );
250 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
251 _("ldif_parse_line: value malloc failed\n"));
253 ber_memfree( freeme );
256 AC_MEMCPY( p, value, vlen );
261 ber_memfree( freeme );
271 * ldif_getline - return the next "line" (minus newline) of input from a
272 * string buffer of lines separated by newlines, terminated by \n\n
273 * or \0. this routine handles continued lines, bundling them into
274 * a single big line before returning. if a line begins with a white
275 * space character, it is a continuation of the previous line. the white
276 * space character (nb: only one char), and preceeding newline are changed
277 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
278 * ldif_parse_line() routine above.
280 * ldif_getline will skip over any line which starts '#'.
282 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
283 * which it updates and must be supplied on subsequent calls.
287 ldif_getline( char **next )
292 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
298 while ( (*next = strchr( *next, '\n' )) != NULL ) {
299 #if CONTINUED_LINE_MARKER != '\r'
300 if ( (*next)[-1] == '\r' ) {
301 (*next)[-1] = CONTINUED_LINE_MARKER;
305 if ( (*next)[1] != ' ' ) {
306 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
313 **next = CONTINUED_LINE_MARKER;
314 (*next)[1] = CONTINUED_LINE_MARKER;
317 } while( *line == '#' );
322 /* compatibility with U-Mich off by one bug */
323 #define LDIF_KLUDGE 1
329 LDAP_CONST char *name,
330 LDAP_CONST char *val,
333 const unsigned char *byte, *stop;
334 unsigned char buf[3];
346 case LDIF_PUT_COMMENT:
362 /* name (attribute type) */
364 /* put the name + ":" */
365 namelen = strlen(name);
370 if( type != LDIF_PUT_COMMENT ) {
378 assert( type == LDIF_PUT_COMMENT );
388 case LDIF_PUT_NOVALUE:
392 case LDIF_PUT_URL: /* url value */
397 case LDIF_PUT_B64: /* base64 value */
411 case LDIF_PUT_COMMENT:
412 /* pre-encoded names */
413 for ( i=0; i < vlen; i++ ) {
414 if ( len > LDIF_LINE_WIDTH ) {
433 stop = (const unsigned char *) (val + vlen);
435 if ( type == LDIF_PUT_VALUE
436 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
437 && isgraph( (unsigned char) val[vlen-1] )
438 #ifndef LDAP_BINARY_DEBUG
439 && strstr( name, ";binary" ) == NULL
441 #ifndef LDAP_PASSWD_DEBUG
442 && (namelen != (sizeof("userPassword")-1)
443 || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
444 && (namelen != (sizeof("2.5.4.35")-1)
445 || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
450 for ( byte = (const unsigned char *) val; byte < stop;
453 if ( !isascii( *byte ) || !isprint( *byte ) ) {
457 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
476 /* convert to base 64 (3 bytes => 4 base 64 digits) */
477 for ( byte = (const unsigned char *) val;
481 bits = (byte[0] & 0xff) << 16;
482 bits |= (byte[1] & 0xff) << 8;
483 bits |= (byte[2] & 0xff);
485 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
486 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
492 /* get b64 digit from high order 6 bits */
493 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
497 /* add padding if necessary */
499 for ( i = 0; byte + i < stop; i++ ) {
502 for ( pad = 0; i < 3; i++, pad++ ) {
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 ) {
518 /* get b64 digit from low order 6 bits */
519 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
530 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
535 LDAP_CONST char *name,
536 LDAP_CONST char *val,
542 nlen = ( name != NULL ) ? strlen( name ) : 0;
544 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
547 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
548 _("ldif_type_and_value: malloc failed!"));
553 ldif_sput( &p, type, name, val, vlen );
559 int ldif_is_not_printable(
560 LDAP_CONST char *val,
563 if( vlen == 0 || val == NULL ) {
567 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
568 isgraph( (unsigned char) val[vlen-1] ) )
572 for ( i = 0; val[i]; i++ ) {
573 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
585 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
590 int *lno, /* ptr to line number counter */
591 char **bufp, /* ptr to malloced output buffer */
592 int *buflenp ) /* ptr to length of *bufp */
594 char linebuf[BUFSIZ], *line, *nbufp;
595 ber_len_t lcur = 0, len, linesize;
596 int last_ch = '\n', found_entry = 0, stop, top_comment = 0;
599 linesize = sizeof( linebuf );
601 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
602 if ( fgets( line, linesize, fp ) == NULL ) {
604 /* Add \n in case the file does not end with newline */
607 len = strlen( line );
609 if ( last_ch == '\n' ) {
612 if ( line[0] == '\n' ) {
613 if ( !found_entry ) {
621 if ( !found_entry ) {
622 if ( line[0] == '#' ) {
624 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
625 /* Found a new entry */
628 if ( isdigit( (unsigned char) line[0] ) ) {
636 if ( *buflenp - lcur <= len ) {
637 *buflenp += len + BUFSIZ;
638 nbufp = ber_memrealloc( *bufp, *buflenp );
639 if( nbufp == NULL ) {
644 strcpy( *bufp + lcur, line );
648 return( found_entry );