1 /* line64.c - routines for dealing with the slapd line format */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
15 #include <ac/string.h>
16 #include <ac/socket.h>
27 #define CONTINUED_LINE_MARKER '\r'
30 #define ber_memalloc malloc
31 #define ber_memcalloc calloc
32 #define ber_memrealloc realloc
33 #define ber_strdup strdup
36 static const char nib2b64[0x40] =
37 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
39 static const unsigned char b642nib[0x80] = {
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
46 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
47 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
48 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
49 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
50 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
51 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
52 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
53 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
54 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
55 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
59 * ldif_parse_line - takes a line of the form "type:[:] value" and splits it
60 * into components "type" and "value". if a double colon separates type from
61 * value, then value is encoded in base 64, and parse_line un-decodes it
62 * (in place) before returning.
67 LDAP_CONST char *line,
76 char *freeme, *type, *value;
83 /* skip any leading space */
84 while ( isspace( (unsigned char) *line ) ) {
88 freeme = ber_strdup( line );
90 if( freeme == NULL ) {
91 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
92 "ldif_parse_line: line malloc failed\n");
98 s = strchr( type, ':' );
101 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
102 "ldif_parse_line: missing ':' after %s\n",
104 ber_memfree( freeme );
108 /* trim any space between type and : */
109 for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
123 } else if ( *s == '<' ) {
127 } else if ( *s == ':' ) {
128 /* base 64 encoded value */
133 /* skip space between : and value */
134 while ( isspace( (unsigned char) *s ) ) {
138 /* check for continued line markers that should be deleted */
139 for ( p = s, d = s; *p; p++ ) {
140 if ( *p != CONTINUED_LINE_MARKER )
149 /* no value is present, error out */
150 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
151 "ldif_parse_line: %s missing base64 value\n", type );
159 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
161 for ( i = 0; i < 4; i++ ) {
162 if ( p[i] != '=' && (p[i] & 0x80 ||
163 b642nib[ p[i] & 0x7f ] > 0x3f) ) {
164 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
165 "ldif_parse_line: %s: invalid base64 encoding"
168 ber_memfree( freeme );
174 nib = b642nib[ p[0] & 0x7f ];
177 nib = b642nib[ p[1] & 0x7f ];
179 byte[1] = (nib & RIGHT4) << 4;
185 nib = b642nib[ p[2] & 0x7f ];
187 byte[2] = (nib & RIGHT2) << 6;
193 nib = b642nib[ p[3] & 0x7f ];
202 /* no value is present, error out */
203 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
204 "ldif_parse_line: %s missing URL value\n", type );
210 if( ldif_fetch_url( s, &value, &vlen ) ) {
211 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
212 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
214 ber_memfree( freeme );
220 vlen = (int) (d - s);
224 type = ber_strdup( type );
227 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
228 "ldif_parse_line: type malloc failed\n");
229 ber_memfree( freeme );
233 if( !url && value != NULL ) {
234 p = ber_memalloc( vlen + 1 );
236 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
237 "ldif_parse_line: value malloc failed\n");
239 ber_memfree( freeme );
242 AC_MEMCPY( p, value, vlen );
247 ber_memfree( freeme );
257 * ldif_getline - return the next "line" (minus newline) of input from a
258 * string buffer of lines separated by newlines, terminated by \n\n
259 * or \0. this routine handles continued lines, bundling them into
260 * a single big line before returning. if a line begins with a white
261 * space character, it is a continuation of the previous line. the white
262 * space character (nb: only one char), and preceeding newline are changed
263 * into CONTINUED_LINE_MARKER chars, to be deleted later by the
264 * ldif_parse_line() routine above.
266 * ldif_getline will skip over any line which starts '#'.
268 * ldif_getline takes a pointer to a pointer to the buffer on the first call,
269 * which it updates and must be supplied on subsequent calls.
273 ldif_getline( char **next )
278 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
284 while ( (*next = strchr( *next, '\n' )) != NULL ) {
285 #if CONTINUED_LINE_MARKER != '\r'
286 if ( (*next)[-1] == '\r' ) {
287 (*next)[-1] = CONTINUED_LINE_MARKER;
291 if ( (*next)[1] != ' ' ) {
292 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
299 **next = CONTINUED_LINE_MARKER;
300 (*next)[1] = CONTINUED_LINE_MARKER;
303 } while( *line == '#' );
308 /* compatibility with U-Mich off by one bug */
309 #define LDIF_KLUDGE 1
315 LDAP_CONST char *name,
316 LDAP_CONST char *val,
319 const unsigned char *byte, *stop;
320 unsigned char buf[3];
332 case LDIF_PUT_COMMENT:
348 /* name (attribute type) */
350 /* put the name + ":" */
351 namelen = strlen(name);
356 if( type != LDIF_PUT_COMMENT ) {
364 assert( type == LDIF_PUT_COMMENT );
374 case LDIF_PUT_NOVALUE:
378 case LDIF_PUT_URL: /* url value */
383 case LDIF_PUT_B64: /* base64 value */
397 case LDIF_PUT_COMMENT:
398 /* pre-encoded names */
399 for ( i=0; i < vlen; i++ ) {
400 if ( len > LDIF_LINE_WIDTH ) {
419 stop = (const unsigned char *) (val + vlen);
421 if ( type == LDIF_PUT_VALUE
422 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
423 && isgraph( (unsigned char) val[vlen-1] )
424 #ifndef LDAP_BINARY_DEBUG
425 && strstr( name, ";binary" ) == NULL
427 #ifndef LDAP_PASSWD_DEBUG
428 && (namelen != (sizeof("userPassword")-1)
429 || strcasecmp( name, "userPassword" ) != 0) /* encode userPassword */
430 && (namelen != (sizeof("2.5.4.35")-1)
431 || strcasecmp( name, "2.5.4.35" ) != 0) /* encode userPassword */
436 for ( byte = (const unsigned char *) val; byte < stop;
439 if ( !isascii( *byte ) || !isprint( *byte ) ) {
443 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
462 /* convert to base 64 (3 bytes => 4 base 64 digits) */
463 for ( byte = (const unsigned char *) val;
467 bits = (byte[0] & 0xff) << 16;
468 bits |= (byte[1] & 0xff) << 8;
469 bits |= (byte[2] & 0xff);
471 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
472 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
478 /* get b64 digit from high order 6 bits */
479 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
483 /* add padding if necessary */
485 for ( i = 0; byte + i < stop; i++ ) {
488 for ( pad = 0; i < 3; i++, pad++ ) {
492 bits = (byte[0] & 0xff) << 16;
493 bits |= (byte[1] & 0xff) << 8;
494 bits |= (byte[2] & 0xff);
496 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
497 if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
504 /* get b64 digit from low order 6 bits */
505 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
516 * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
521 LDAP_CONST char *name,
522 LDAP_CONST char *val,
528 nlen = ( name != NULL ) ? strlen( name ) : 0;
530 buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
533 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
534 "ldif_type_and_value: malloc failed!" );
539 ldif_sput( &p, type, name, val, vlen );
545 int ldif_is_not_printable(
546 LDAP_CONST char *val,
549 if( vlen == 0 || val == NULL ) {
553 if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
554 isgraph( (unsigned char) val[vlen-1] ) )
558 for ( i = 0; val[i]; i++ ) {
559 if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
571 * slap_read_ldif - read an ldif record. Return 1 for success, 0 for EOF.
576 int *lno, /* ptr to line number counter */
577 char **bufp, /* ptr to malloced output buffer */
578 int *buflenp ) /* ptr to length of *bufp */
580 char linebuf[BUFSIZ], *line, *nbufp;
581 ber_len_t lcur = 0, len, linesize;
582 int last_ch = '\n', found_entry = 0, stop;
585 linesize = sizeof( linebuf );
587 for ( stop = feof( fp ); !stop; last_ch = line[len-1] ) {
588 if ( fgets( line, linesize, fp ) == NULL ) {
590 /* Add \n in case the file does not end with newline */
593 len = strlen( line );
595 if ( last_ch == '\n' ) {
598 if ( line[0] == '\n' ) {
604 if ( !found_entry ) {
605 /* Found a new entry */
608 if ( isdigit( (unsigned char) line[0] ) ) {
615 if ( *buflenp - lcur <= len ) {
616 *buflenp += len + BUFSIZ;
617 nbufp = ber_memrealloc( *bufp, *buflenp );
618 if( nbufp == NULL ) {
623 strcpy( *bufp + lcur, line );
627 return( found_entry );