]> git.sur5r.net Git - openldap/blob - libraries/liblutil/ldif.c
fix ITS#3499 (may need further testing)
[openldap] / libraries / liblutil / ldif.c
1 /* ldif.c - routines for dealing with LDIF files */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26 /* This work was originally developed by the University of Michigan
27  * and distributed as part of U-MICH LDAP.
28  */
29
30 #include "portable.h"
31
32 #include <stdio.h>
33
34 #include <ac/stdlib.h>
35 #include <ac/ctype.h>
36
37 #include <ac/string.h>
38 #include <ac/socket.h>
39 #include <ac/time.h>
40
41 int ldif_debug = 0;
42
43 #include "ldap_log.h"
44 #include "lber_pvt.h"
45 #include "ldif.h"
46
47 #define RIGHT2                  0x03
48 #define RIGHT4                  0x0f
49 #define CONTINUED_LINE_MARKER   '\r'
50
51 #ifdef CSRIMALLOC
52 #define ber_memalloc malloc
53 #define ber_memcalloc calloc
54 #define ber_memrealloc realloc
55 #define ber_strdup strdup
56 #endif
57
58 static const char nib2b64[0x40] =
59         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
60
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
78 };
79
80 /*
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.
86  *
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.
92  */
93
94 int
95 ldif_parse_line(
96     LDAP_CONST char     *line,
97     char        **typep,
98     char        **valuep,
99     ber_len_t *vlenp
100 )
101 {
102         struct berval type, value;
103         int rc = ldif_parse_line2( (char *)line, &type, &value, NULL );
104
105         *typep = type.bv_val;
106         *valuep = value.bv_val;
107         *vlenp = value.bv_len;
108         return rc;
109 }
110
111 int
112 ldif_parse_line2(
113     char        *line,
114         struct berval *type,
115         struct berval *value,
116         int             *freeval
117 )
118 {
119         char    *s, *p, *d; 
120         char    nib;
121         int     b64, url;
122
123         BER_BVZERO( type );
124         BER_BVZERO( value );
125
126         /* skip any leading space */
127         while ( isspace( (unsigned char) *line ) ) {
128                 line++;
129         }
130
131         if ( freeval ) {
132                 *freeval = 0;
133         } else {
134                 line = ber_strdup( line );
135
136                 if( line == NULL ) {
137                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
138                                 _("ldif_parse_line: line malloc failed\n"));
139                         return( -1 );
140                 }
141         }
142
143         type->bv_val = line;
144
145         s = strchr( type->bv_val, ':' );
146
147         if ( s == NULL ) {
148                 ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug,
149                         _("ldif_parse_line: missing ':' after %s\n"),
150                         type );
151                 if ( !freeval ) ber_memfree( line );
152                 return( -1 );
153         }
154
155         /* trim any space between type and : */
156         for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) {
157                 *p = '\0';
158         }
159         *s++ = '\0';
160         type->bv_len = s - type->bv_val - 1;
161
162         url = 0;
163         b64 = 0;
164
165         if ( *s == '<' ) {
166                 s++;
167                 url = 1;
168
169         } else if ( *s == ':' ) {
170                 /* base 64 encoded value */
171                 s++;
172                 b64 = 1;
173         }
174
175         /* skip space between : and value */
176         while ( isspace( (unsigned char) *s ) ) {
177                 s++;
178         }
179
180         /* check for continued line markers that should be deleted */
181         for ( p = s, d = s; *p; p++ ) {
182                 if ( *p != CONTINUED_LINE_MARKER )
183                         *d++ = *p;
184         }
185         *d = '\0';
186
187         if ( b64 ) {
188                 char *byte = s;
189
190                 if ( *s == '\0' ) {
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 );
195                         return( -1 );
196                 }
197
198                 byte = value->bv_val = s;
199
200                 for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) {
201                         int i;
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"),
208                                             type, p[i], p[i] );
209                                         if ( !freeval ) ber_memfree( line );
210                                         return( -1 );
211                                 }
212                         }
213
214                         /* first digit */
215                         nib = b642nib[ p[0] & 0x7f ];
216                         byte[0] = nib << 2;
217                         /* second digit */
218                         nib = b642nib[ p[1] & 0x7f ];
219                         byte[0] |= nib >> 4;
220                         byte[1] = (nib & RIGHT4) << 4;
221                         /* third digit */
222                         if ( p[2] == '=' ) {
223                                 value->bv_len += 1;
224                                 break;
225                         }
226                         nib = b642nib[ p[2] & 0x7f ];
227                         byte[1] |= nib >> 2;
228                         byte[2] = (nib & RIGHT2) << 6;
229                         /* fourth digit */
230                         if ( p[3] == '=' ) {
231                                 value->bv_len += 2;
232                                 break;
233                         }
234                         nib = b642nib[ p[3] & 0x7f ];
235                         byte[2] |= nib;
236
237                         byte += 3;
238                 }
239                 s[ value->bv_len ] = '\0';
240
241         } else if ( url ) {
242                 if ( *s == '\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 );
247                         return( -1 );
248                 }
249
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"),
253                                 type, s );
254                         if ( !freeval ) ber_memfree( line );
255                         return( -1 );
256                 }
257                 if ( freeval ) *freeval = 1;
258
259         } else {
260                 value->bv_val = s;
261                 value->bv_len = (int) (d - s);
262         }
263
264         if ( !freeval ) {
265                 struct berval bv = *type;
266
267                 ber_dupbv( type, &bv );
268
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 );
273                         ber_memfree( line );
274                         return( -1 );
275                 }
276
277                 if( !url ) {
278                         bv = *value;
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 );
284                                 ber_memfree( line );
285                                 return( -1 );
286                         }
287                 }
288
289                 ber_memfree( line );
290         }
291
292         return( 0 );
293 }
294
295 /*
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.
304  *
305  * ldif_getline will skip over any line which starts '#'.
306  *
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.
309  */
310
311 char *
312 ldif_getline( char **next )
313 {
314         char *line;
315
316         do {
317                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
318                         return( NULL );
319                 }
320
321                 line = *next;
322
323                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
324 #if CONTINUED_LINE_MARKER != '\r'
325                         if ( (*next)[-1] == '\r' ) {
326                                 (*next)[-1] = CONTINUED_LINE_MARKER;
327                         }
328 #endif
329
330                         if ( (*next)[1] != ' ' ) {
331                                 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
332                                         *(*next)++ = '\0';
333                                 }
334                                 *(*next)++ = '\0';
335                                 break;
336                         }
337
338                         **next = CONTINUED_LINE_MARKER;
339                         (*next)[1] = CONTINUED_LINE_MARKER;
340                         (*next)++;
341                 }
342         } while( *line == '#' );
343
344         return( line );
345 }
346
347 /* compatibility with U-Mich off by one bug */
348 #define LDIF_KLUDGE 1
349
350 void
351 ldif_sput(
352         char **out,
353         int type,
354         LDAP_CONST char *name,
355         LDAP_CONST char *val,
356         ber_len_t vlen )
357 {
358         const unsigned char *byte, *stop;
359         unsigned char   buf[3];
360         unsigned long   bits;
361         char            *save;
362         int             pad;
363         int             namelen = 0;
364
365         ber_len_t savelen;
366         ber_len_t len=0;
367         ber_len_t i;
368
369         /* prefix */
370         switch( type ) {
371         case LDIF_PUT_COMMENT:
372                 *(*out)++ = '#';
373                 len++;
374
375                 if( vlen ) {
376                         *(*out)++ = ' ';
377                         len++;
378                 }
379
380                 break;
381
382         case LDIF_PUT_SEP:
383                 *(*out)++ = '\n';
384                 return;
385         }
386
387         /* name (attribute type) */
388         if( name != NULL ) {
389                 /* put the name + ":" */
390                 namelen = strlen(name);
391                 strcpy(*out, name);
392                 *out += namelen;
393                 len += namelen;
394
395                 if( type != LDIF_PUT_COMMENT ) {
396                         *(*out)++ = ':';
397                         len++;
398                 }
399
400         }
401 #ifdef LDAP_DEBUG
402         else {
403                 assert( type == LDIF_PUT_COMMENT );
404         }
405 #endif
406
407         if( vlen == 0 ) {
408                 *(*out)++ = '\n';
409                 return;
410         }
411
412         switch( type ) {
413         case LDIF_PUT_NOVALUE:
414                 *(*out)++ = '\n';
415                 return;
416
417         case LDIF_PUT_URL: /* url value */
418                 *(*out)++ = '<';
419                 len++;
420                 break;
421
422         case LDIF_PUT_B64: /* base64 value */
423                 *(*out)++ = ':';
424                 len++;
425                 break;
426         }
427
428         switch( type ) {
429         case LDIF_PUT_TEXT:
430         case LDIF_PUT_URL:
431         case LDIF_PUT_B64:
432                 *(*out)++ = ' ';
433                 len++;
434                 /* fall-thru */
435
436         case LDIF_PUT_COMMENT:
437                 /* pre-encoded names */
438                 for ( i=0; i < vlen; i++ ) {
439                         if ( len > LDIF_LINE_WIDTH ) {
440                                 *(*out)++ = '\n';
441                                 *(*out)++ = ' ';
442                                 len = 1;
443                         }
444
445                         *(*out)++ = val[i];
446                         len++;
447                 }
448                 *(*out)++ = '\n';
449                 return;
450         }
451
452         save = *out;
453         savelen = len;
454
455         *(*out)++ = ' ';
456         len++;
457
458         stop = (const unsigned char *) (val + vlen);
459
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
465 #endif
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 */
471 #endif
472         ) {
473                 int b64 = 0;
474
475                 for ( byte = (const unsigned char *) val; byte < stop;
476                     byte++, len++ )
477                 {
478                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
479                                 b64 = 1;
480                                 break;
481                         }
482                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
483                                 *(*out)++ = '\n';
484                                 *(*out)++ = ' ';
485                                 len = 1;
486                         }
487                         *(*out)++ = *byte;
488                 }
489
490                 if( !b64 ) {
491                         *(*out)++ = '\n';
492                         return;
493                 }
494         }
495
496         *out = save;
497         *(*out)++ = ':';
498         *(*out)++ = ' ';
499         len = savelen + 2;
500
501         /* convert to base 64 (3 bytes => 4 base 64 digits) */
502         for ( byte = (const unsigned char *) val;
503                 byte < stop - 2;
504             byte += 3 )
505         {
506                 bits = (byte[0] & 0xff) << 16;
507                 bits |= (byte[1] & 0xff) << 8;
508                 bits |= (byte[2] & 0xff);
509
510                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
511                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
512                                 *(*out)++ = '\n';
513                                 *(*out)++ = ' ';
514                                 len = 1;
515                         }
516
517                         /* get b64 digit from high order 6 bits */
518                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
519                 }
520         }
521
522         /* add padding if necessary */
523         if ( byte < stop ) {
524                 for ( i = 0; byte + i < stop; i++ ) {
525                         buf[i] = byte[i];
526                 }
527                 for ( pad = 0; i < 3; i++, pad++ ) {
528                         buf[i] = '\0';
529                 }
530                 byte = buf;
531                 bits = (byte[0] & 0xff) << 16;
532                 bits |= (byte[1] & 0xff) << 8;
533                 bits |= (byte[2] & 0xff);
534
535                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
536                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
537                                 *(*out)++ = '\n';
538                                 *(*out)++ = ' ';
539                                 len = 1;
540                         }
541
542                         if( i + pad < 4 ) {
543                                 /* get b64 digit from low order 6 bits */
544                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
545                         } else {
546                                 *(*out)++ = '=';
547                         }
548                 }
549         }
550         *(*out)++ = '\n';
551 }
552
553
554 /*
555  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
556  */
557 char *
558 ldif_put(
559         int type,
560         LDAP_CONST char *name,
561         LDAP_CONST char *val,
562         ber_len_t vlen )
563 {
564     char        *buf, *p;
565     ber_len_t nlen;
566
567     nlen = ( name != NULL ) ? strlen( name ) : 0;
568
569         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
570
571     if ( buf == NULL ) {
572                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
573                         _("ldif_type_and_value: malloc failed!"));
574                 return NULL;
575     }
576
577     p = buf;
578     ldif_sput( &p, type, name, val, vlen );
579     *p = '\0';
580
581     return( buf );
582 }
583
584 int ldif_is_not_printable(
585         LDAP_CONST char *val,
586         ber_len_t vlen )
587 {
588         if( vlen == 0 || val == NULL  ) {
589                 return -1;
590         }
591
592         if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
593                 isgraph( (unsigned char) val[vlen-1] ) )
594         {
595                 ber_len_t i;
596
597                 for ( i = 0; val[i]; i++ ) {
598                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
599                                 return 1;
600                         }
601                 }
602
603                 return 0;
604         }
605
606         return 1;
607 }
608
609 /*
610  * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
611  */
612 int
613 ldif_read_record(
614         FILE        *fp,
615         int         *lno,               /* ptr to line number counter              */
616         char        **bufp,     /* ptr to malloced output buffer           */
617         int         *buflenp )  /* ptr to length of *bufp                  */
618 {
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;
622
623         line     = linebuf;
624         linesize = sizeof( linebuf );
625
626         for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) {
627                 if ( fgets( line, linesize, fp ) == NULL ) {
628                         stop = 1;
629                         /* Add \n in case the file does not end with newline */
630                         line = "\n";
631                 }
632                 len = strlen( line );
633
634                 if ( last_ch == '\n' ) {
635                         (*lno)++;
636
637                         if ( line[0] == '\n' ) {
638                                 if ( !found_entry ) {
639                                         lcur = 0;
640                                         top_comment = 0;
641                                         continue;
642                                 }
643                                 break;
644                         }
645
646                         if ( !found_entry ) {
647                                 if ( line[0] == '#' ) {
648                                         top_comment = 1;
649                                 } else if ( ! ( top_comment && line[0] == ' ' ) ) {
650                                         /* Found a new entry */
651                                         found_entry = 1;
652
653                                         if ( isdigit( (unsigned char) line[0] ) ) {
654                                                 /* skip index */
655                                                 continue;
656                                         }
657                                 }
658                         }                       
659                 }
660
661                 if ( *buflenp - lcur <= len ) {
662                         *buflenp += len + BUFSIZ;
663                         nbufp = ber_memrealloc( *bufp, *buflenp );
664                         if( nbufp == NULL ) {
665                                 return 0;
666                         }
667                         *bufp = nbufp;
668                 }
669                 strcpy( *bufp + lcur, line );
670                 lcur += len;
671         }
672
673         return( found_entry );
674 }