]> git.sur5r.net Git - openldap/blob - libraries/libldif/line64.c
Fix ldif_parse_line, binary attributes got truncated because the final
[openldap] / libraries / libldif / line64.c
1 /* line64.c - routines for dealing with the slapd line format */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/ctype.h>
14
15 #include <ac/string.h>
16 #include <ac/socket.h>
17 #include <ac/time.h>
18
19 int ldif_debug = 0;
20
21 #include "ldap_log.h"
22 #include "lber_pvt.h"
23 #include "ldif.h"
24
25 #define RIGHT2                  0x03
26 #define RIGHT4                  0x0f
27 #define CONTINUED_LINE_MARKER   '\001'
28
29 #ifdef CSRIMALLOC
30 #define ber_memalloc malloc
31 #define ber_memcalloc calloc
32 #define ber_memrealloc realloc
33 #define ber_strdup strdup
34 #endif
35
36 static const char nib2b64[0x40] =
37         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
38
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
56 };
57
58 /*
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.
63  */
64
65 int
66 ldif_parse_line(
67     LDAP_CONST char     *line,
68     char        **typep,
69     char        **valuep,
70     ber_len_t *vlenp
71 )
72 {
73         char    *s, *p, *d; 
74         char    nib;
75         int     b64, url;
76         char    *freeme, *type, *value;
77         ber_len_t vlen;
78
79         *typep = NULL;
80         *valuep = NULL;
81         *vlenp = 0;
82
83         /* skip any leading space */
84         while ( isspace( (unsigned char) *line ) ) {
85                 line++;
86         }
87
88         freeme = ber_strdup( line );
89
90         if( freeme == NULL ) {
91                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
92                         "ldif_parse_line: line malloc failed\n");
93                 return( -1 );
94         }
95
96         type = freeme;
97
98         s = strchr( type, ':' );
99
100         if ( s == NULL ) {
101                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
102                         "ldif_parse_line: missing ':' after %s\n",
103                         type );
104                 ber_memfree( freeme );
105                 return( -1 );
106         }
107
108         /* trim any space between type and : */
109         for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) {
110                 *p = '\0';
111         }
112         *s++ = '\0';
113
114         url = 0;
115         b64 = 0;
116
117         if ( *s == '\0' ) {
118                 /* no value */
119                 value = NULL;
120                 vlen = 0;
121                 goto done;
122         }
123                 
124         if ( *s == '<' ) {
125                 s++;
126                 url = 1;
127         } else if ( *s == ':' ) {
128                 /* base 64 encoded value */
129                 s++;
130                 b64 = 1;
131         }
132
133         /* skip space between : and value */
134         while ( isspace( (unsigned char) *s ) ) {
135                 s++;
136         }
137
138         /* check for continued line markers that should be deleted */
139         for ( p = s, d = s; *p; p++ ) {
140                 if ( *p != CONTINUED_LINE_MARKER )
141                         *d++ = *p;
142         }
143         *d = '\0';
144
145         /* if no value is present, error out */
146         if ( *s == '\0' ) {
147                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
148                         "ldif_parse_line: %s missing %svalue\n", type,
149                                 url ? "URL " : b64 ? "base64 " : "" );
150                 value = NULL;
151                 vlen = 0;
152                 goto done;
153         }
154
155         if ( b64 ) {
156                 char *byte = s;
157
158                 value = s;
159
160                 for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) {
161                         int i;
162                         for ( i = 0; i < 4; i++ ) {
163                                 if ( p[i] != '=' && (p[i] & 0x80 ||
164                                     b642nib[ p[i] & 0x7f ] > 0x3f) ) {
165                                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
166                                                 "ldif_parse_line: %s: invalid base64 encoding"
167                                                 " char (%c) 0x%x\n",
168                                             type, p[i], p[i] );
169                                         ber_memfree( freeme );
170                                         return( -1 );
171                                 }
172                         }
173
174                         /* first digit */
175                         nib = b642nib[ p[0] & 0x7f ];
176                         byte[0] = nib << 2;
177                         /* second digit */
178                         nib = b642nib[ p[1] & 0x7f ];
179                         byte[0] |= nib >> 4;
180                         byte[1] = (nib & RIGHT4) << 4;
181                         /* third digit */
182                         if ( p[2] == '=' ) {
183                                 vlen += 1;
184                                 break;
185                         }
186                         nib = b642nib[ p[2] & 0x7f ];
187                         byte[1] |= nib >> 2;
188                         byte[2] = (nib & RIGHT2) << 6;
189                         /* fourth digit */
190                         if ( p[3] == '=' ) {
191                                 vlen += 2;
192                                 break;
193                         }
194                         nib = b642nib[ p[3] & 0x7f ];
195                         byte[2] |= nib;
196
197                         byte += 3;
198                 }
199                 s[ vlen ] = '\0';
200
201         } else if ( url ) {
202                 if( ldif_fetch_url( s, &value, &vlen ) ) {
203                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
204                                 "ldif_parse_line: %s: URL \"%s\" fetch failed\n",
205                                 type, s );
206                         ber_memfree( freeme );
207                         return( -1 );
208                 }
209
210         } else {
211                 value = s;
212                 vlen = (int) (d - s);
213         }
214
215 done:
216         type = ber_strdup( type );
217
218         if( type == NULL ) {
219                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
220                         "ldif_parse_line: type malloc failed\n");
221                 ber_memfree( freeme );
222                 return( -1 );
223         }
224
225         if( !url && value != NULL ) {
226                 p = ber_memalloc( vlen );
227                 if( p == NULL ) {
228                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
229                                 "ldif_parse_line: value malloc failed\n");
230                         ber_memfree( type );
231                         ber_memfree( freeme );
232                         return( -1 );
233                 }
234                 memcpy( p, value, vlen );
235                 value = p;
236         }
237
238         ber_memfree( freeme );
239
240         *typep = type;
241         *valuep = value;
242         *vlenp = vlen;
243
244         return( 0 );
245 }
246
247 /*
248  * ldif_getline - return the next "line" (minus newline) of input from a
249  * string buffer of lines separated by newlines, terminated by \n\n
250  * or \0.  this routine handles continued lines, bundling them into
251  * a single big line before returning.  if a line begins with a white
252  * space character, it is a continuation of the previous line. the white
253  * space character (nb: only one char), and preceeding newline are changed
254  * into CONTINUED_LINE_MARKER chars, to be deleted later by the
255  * ldif_parse_line() routine above.
256  *
257  * ldif_getline will skip over any line which starts '#'.
258  *
259  * ldif_getline takes a pointer to a pointer to the buffer on the first call,
260  * which it updates and must be supplied on subsequent calls.
261  */
262
263 char *
264 ldif_getline( char **next )
265 {
266         char *line;
267
268         do {
269                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
270                         return( NULL );
271                 }
272
273                 line = *next;
274
275                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
276                         unsigned char c = *(*next + 1);
277
278                         if ( !isspace( c ) || c == '\n' ) {
279                                 *(*next)++ = '\0';
280                                 break;
281                         }
282
283                         **next = CONTINUED_LINE_MARKER;
284                         *(*next+1) = CONTINUED_LINE_MARKER;
285                         (*next)++;
286                 }
287         } while( *line == '#' );
288
289         return( line );
290 }
291
292 /* compatibility with U-Mich off by one bug */
293 #define LDIF_KLUDGE 1
294
295 void
296 ldif_sput(
297         char **out,
298         int type,
299         LDAP_CONST char *name,
300         LDAP_CONST char *val,
301         ber_len_t vlen )
302 {
303         const unsigned char *byte, *stop;
304         unsigned char   buf[3];
305         unsigned long   bits;
306         char            *save;
307         int             pad;
308
309         ber_len_t savelen;
310         ber_len_t len=0;
311         ber_len_t i;
312
313         /* prefix */
314         switch( type ) {
315         case LDIF_PUT_COMMENT:
316                 if( name != NULL ) break;
317
318                 *(*out)++ = '#';
319                 len++;
320
321                 if( vlen ) {
322                         *(*out)++ = ' ';
323                         len++;
324                         break;
325                 }
326
327                 /* no value, fall-thru */
328
329         case LDIF_PUT_SEP:
330                 *(*out)++ = '\n';
331                 return;
332         }
333
334         /* name (attribute type) */
335         if( name != NULL ) {
336                 /* put the name + ":" */
337                 for ( i=0 ; name[i]; i++ ) {
338                         *(*out)++ = name[i];
339                         len++;
340                 }
341
342                 if( type != LDIF_PUT_COMMENT ) {
343                         *(*out)++ = ':';
344                         len++;
345                 }
346
347         }
348 #ifdef LDAP_DEBUG
349         else {
350                 assert( type == LDIF_PUT_COMMENT );
351         }
352 #endif
353
354         if( vlen == 0 ) {
355                 *(*out)++ = '\n';
356                 return;
357         }
358
359         switch( type ) {
360         case LDIF_PUT_NOVALUE:
361                 *(*out)++ = '\n';
362                 return;
363
364         case LDIF_PUT_URL: /* url value */
365                 *(*out)++ = '<';
366                 len++;
367                 break;
368
369         case LDIF_PUT_B64: /* base64 value */
370                 *(*out)++ = ':';
371                 len++;
372                 break;
373         }
374
375         switch( type ) {
376         case LDIF_PUT_TEXT:
377         case LDIF_PUT_URL:
378         case LDIF_PUT_B64:
379                 *(*out)++ = ' ';
380                 len++;
381                 /* fall-thru */
382
383         case LDIF_PUT_COMMENT:
384                 /* pre-encoded names */
385                 for ( i=0; i < vlen; i++ ) {
386                         if ( len > LDIF_LINE_WIDTH ) {
387                                 *(*out)++ = '\n';
388                                 *(*out)++ = ' ';
389                                 len = 1;
390                         }
391
392                         *(*out)++ = val[i];
393                         len++;
394                 }
395                 *(*out)++ = '\n';
396                 return;
397         }
398
399         save = *out;
400         savelen = len;
401
402         *(*out)++ = ' ';
403         len++;
404
405         stop = (const unsigned char *) (val + vlen);
406
407         if ( type == LDIF_PUT_VALUE
408                 && isgraph( val[0] ) && val[0] != ':' && val[0] != '<'
409                 && isgraph( val[vlen-1] )
410 #ifndef LDAP_BINARY_DEBUG
411                 && strstr( name, ";binary" ) == NULL
412 #endif
413 #ifndef LDAP_PASSWD_DEBUG
414                 && strcasecmp( name, "userPassword" ) != 0      /* encode userPassword */
415                 && strcasecmp( name, "2.5.4.35" ) != 0          /* encode userPassword */
416 #endif
417         ) {
418                 int b64 = 0;
419
420                 for ( byte = (const unsigned char *) val; byte < stop;
421                     byte++, len++ )
422                 {
423                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
424                                 b64 = 1;
425                                 break;
426                         }
427                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
428                                 *(*out)++ = '\n';
429                                 *(*out)++ = ' ';
430                                 len = 1;
431                         }
432                         *(*out)++ = *byte;
433                 }
434
435                 if( !b64 ) {
436                         *(*out)++ = '\n';
437                         return;
438                 }
439         }
440
441         *out = save;
442         *(*out)++ = ':';
443         *(*out)++ = ' ';
444         len = savelen + 2;
445
446         /* convert to base 64 (3 bytes => 4 base 64 digits) */
447         for ( byte = (const unsigned char *) val;
448                 byte < stop - 2;
449             byte += 3 )
450         {
451                 bits = (byte[0] & 0xff) << 16;
452                 bits |= (byte[1] & 0xff) << 8;
453                 bits |= (byte[2] & 0xff);
454
455                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
456                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
457                                 *(*out)++ = '\n';
458                                 *(*out)++ = ' ';
459                                 len = 1;
460                         }
461
462                         /* get b64 digit from high order 6 bits */
463                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
464                 }
465         }
466
467         /* add padding if necessary */
468         if ( byte < stop ) {
469                 for ( i = 0; byte + i < stop; i++ ) {
470                         buf[i] = byte[i];
471                 }
472                 for ( pad = 0; i < 3; i++, pad++ ) {
473                         buf[i] = '\0';
474                 }
475                 byte = buf;
476                 bits = (byte[0] & 0xff) << 16;
477                 bits |= (byte[1] & 0xff) << 8;
478                 bits |= (byte[2] & 0xff);
479
480                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
481                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
482                                 *(*out)++ = '\n';
483                                 *(*out)++ = ' ';
484                                 len = 1;
485                         }
486
487                         if( i + pad < 4 ) {
488                                 /* get b64 digit from low order 6 bits */
489                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
490                         } else {
491                                 *(*out)++ = '=';
492                         }
493                 }
494         }
495         *(*out)++ = '\n';
496 }
497
498
499 /*
500  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
501  */
502 char *
503 ldif_put(
504         int type,
505         LDAP_CONST char *name,
506         LDAP_CONST char *val,
507         ber_len_t vlen )
508 {
509     char        *buf, *p;
510     ber_len_t nlen;
511
512     nlen = ( name != NULL ) ? strlen( name ) : 0;
513
514         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
515
516     if ( buf == NULL ) {
517                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
518                         "ldif_type_and_value: malloc failed!" );
519                 return NULL;
520     }
521
522     p = buf;
523     ldif_sput( &p, type, name, val, vlen );
524     *p = '\0';
525
526     return( buf );
527 }
528
529 int ldif_is_not_printable(
530         LDAP_CONST char *val,
531         ber_len_t vlen )
532 {
533         if( vlen == 0 || val == NULL  ) {
534                 return -1;
535         }
536
537         if( isgraph( val[0] ) && val[0] != ':' && val[0] != '<' &&
538                 isgraph( val[vlen-1] ) )
539         {
540                 ber_len_t i;
541
542                 for ( i = 0; val[i]; i++ ) {
543                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
544                                 return 1;
545                         }
546                 }
547
548                 return 0;
549         }
550
551         return 1;
552 }
553
554 /*
555  * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
556  */
557 int
558 ldif_read_record(
559         FILE        *fp,
560         int         *lno,               /* ptr to line number counter              */
561         char        **bufp,     /* ptr to malloced output buffer           */
562         int         *buflenp )  /* ptr to length of *bufp                  */
563 {
564         char        linebuf[BUFSIZ], *line, *nbufp;
565         ber_len_t   lcur = 0, len, linesize;
566         int         last_ch = '\n', found_entry = 0, stop;
567
568         line     = linebuf;
569         linesize = sizeof( linebuf );
570
571         for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) {
572                 if ( fgets( line, linesize, fp ) == NULL ) {
573                         stop = 1;
574                         /* Add \n in case the file does not end with newline */
575                         line = "\n";
576                 }
577                 len = strlen( line );
578
579                 if ( last_ch == '\n' ) {
580                         (*lno)++;
581
582                         if ( line[0] == '\n' ) {
583                                 if ( !found_entry )
584                                         continue;
585                                 break;
586                         }
587
588                         if ( !found_entry ) {
589                                 /* Found a new entry */
590                                 found_entry = 1;
591
592                                 if ( isdigit( (unsigned char) line[0] ) ) {
593                                         /* skip index */
594                                         continue;
595                                 }
596                         }                       
597                 }
598
599                 if ( *buflenp - lcur <= len ) {
600                         *buflenp += len + BUFSIZ;
601                         nbufp = ber_memrealloc( *bufp, *buflenp );
602                         if( nbufp == NULL ) {
603                                 return 0;
604                         }
605                         *bufp = nbufp;
606                 }
607                 strcpy( *bufp + lcur, line );
608                 lcur += len;
609         }
610
611         return( found_entry );
612 }