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