]> git.sur5r.net Git - openldap/blob - libraries/libldif/line64.c
ITS#2218: fix empty values w/ spaces
[openldap] / libraries / libldif / line64.c
1 /* line64.c - routines for dealing with the slapd line format */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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   '\r'
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 = "";
120                 vlen = 0;
121                 goto done;
122
123         } else if ( *s == '<' ) {
124                 s++;
125                 url = 1;
126
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 ( b64 ) {
146                 char *byte = s;
147
148                 if ( *s == '\0' ) {
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 );
152                         value = NULL;
153                         vlen = 0;
154                         goto done;
155                 }
156
157                 byte = 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 ( *s == '\0' ) {
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 );
205                         value = NULL;
206                         vlen = 0;
207                         goto done;
208                 }
209
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",
213                                 type, s );
214                         ber_memfree( freeme );
215                         return( -1 );
216                 }
217
218         } else {
219                 value = s;
220                 vlen = (int) (d - s);
221         }
222
223 done:
224         type = ber_strdup( type );
225
226         if( type == NULL ) {
227                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
228                         "ldif_parse_line: type malloc failed\n");
229                 ber_memfree( freeme );
230                 return( -1 );
231         }
232
233         if( !url && value != NULL ) {
234                 p = ber_memalloc( vlen + 1 );
235                 if( p == NULL ) {
236                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
237                                 "ldif_parse_line: value malloc failed\n");
238                         ber_memfree( type );
239                         ber_memfree( freeme );
240                         return( -1 );
241                 }
242                 AC_MEMCPY( p, value, vlen );
243                 p[vlen] = '\0';
244                 value = p;
245         }
246
247         ber_memfree( freeme );
248
249         *typep = type;
250         *valuep = value;
251         *vlenp = vlen;
252
253         return( 0 );
254 }
255
256 /*
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.
265  *
266  * ldif_getline will skip over any line which starts '#'.
267  *
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.
270  */
271
272 char *
273 ldif_getline( char **next )
274 {
275         char *line;
276
277         do {
278                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
279                         return( NULL );
280                 }
281
282                 line = *next;
283
284                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
285 #if CONTINUED_LINE_MARKER != '\r'
286                         if ( (*next)[-1] == '\r' ) {
287                                 (*next)[-1] = CONTINUED_LINE_MARKER;
288                         }
289 #endif
290
291                         if ( (*next)[1] != ' ' ) {
292                                 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
293                                         *(*next)++ = '\0';
294                                 }
295                                 *(*next)++ = '\0';
296                                 break;
297                         }
298
299                         **next = CONTINUED_LINE_MARKER;
300                         (*next)[1] = CONTINUED_LINE_MARKER;
301                         (*next)++;
302                 }
303         } while( *line == '#' );
304
305         return( line );
306 }
307
308 /* compatibility with U-Mich off by one bug */
309 #define LDIF_KLUDGE 1
310
311 void
312 ldif_sput(
313         char **out,
314         int type,
315         LDAP_CONST char *name,
316         LDAP_CONST char *val,
317         ber_len_t vlen )
318 {
319         const unsigned char *byte, *stop;
320         unsigned char   buf[3];
321         unsigned long   bits;
322         char            *save;
323         int             pad;
324         int             namelen = 0;
325
326         ber_len_t savelen;
327         ber_len_t len=0;
328         ber_len_t i;
329
330         /* prefix */
331         switch( type ) {
332         case LDIF_PUT_COMMENT:
333                 *(*out)++ = '#';
334                 len++;
335
336                 if( vlen ) {
337                         *(*out)++ = ' ';
338                         len++;
339                 }
340
341                 break;
342
343         case LDIF_PUT_SEP:
344                 *(*out)++ = '\n';
345                 return;
346         }
347
348         /* name (attribute type) */
349         if( name != NULL ) {
350                 /* put the name + ":" */
351                 namelen = strlen(name);
352                 strcpy(*out, name);
353                 *out += namelen;
354                 len += namelen;
355
356                 if( type != LDIF_PUT_COMMENT ) {
357                         *(*out)++ = ':';
358                         len++;
359                 }
360
361         }
362 #ifdef LDAP_DEBUG
363         else {
364                 assert( type == LDIF_PUT_COMMENT );
365         }
366 #endif
367
368         if( vlen == 0 ) {
369                 *(*out)++ = '\n';
370                 return;
371         }
372
373         switch( type ) {
374         case LDIF_PUT_NOVALUE:
375                 *(*out)++ = '\n';
376                 return;
377
378         case LDIF_PUT_URL: /* url value */
379                 *(*out)++ = '<';
380                 len++;
381                 break;
382
383         case LDIF_PUT_B64: /* base64 value */
384                 *(*out)++ = ':';
385                 len++;
386                 break;
387         }
388
389         switch( type ) {
390         case LDIF_PUT_TEXT:
391         case LDIF_PUT_URL:
392         case LDIF_PUT_B64:
393                 *(*out)++ = ' ';
394                 len++;
395                 /* fall-thru */
396
397         case LDIF_PUT_COMMENT:
398                 /* pre-encoded names */
399                 for ( i=0; i < vlen; i++ ) {
400                         if ( len > LDIF_LINE_WIDTH ) {
401                                 *(*out)++ = '\n';
402                                 *(*out)++ = ' ';
403                                 len = 1;
404                         }
405
406                         *(*out)++ = val[i];
407                         len++;
408                 }
409                 *(*out)++ = '\n';
410                 return;
411         }
412
413         save = *out;
414         savelen = len;
415
416         *(*out)++ = ' ';
417         len++;
418
419         stop = (const unsigned char *) (val + vlen);
420
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
426 #endif
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 */
432 #endif
433         ) {
434                 int b64 = 0;
435
436                 for ( byte = (const unsigned char *) val; byte < stop;
437                     byte++, len++ )
438                 {
439                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
440                                 b64 = 1;
441                                 break;
442                         }
443                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
444                                 *(*out)++ = '\n';
445                                 *(*out)++ = ' ';
446                                 len = 1;
447                         }
448                         *(*out)++ = *byte;
449                 }
450
451                 if( !b64 ) {
452                         *(*out)++ = '\n';
453                         return;
454                 }
455         }
456
457         *out = save;
458         *(*out)++ = ':';
459         *(*out)++ = ' ';
460         len = savelen + 2;
461
462         /* convert to base 64 (3 bytes => 4 base 64 digits) */
463         for ( byte = (const unsigned char *) val;
464                 byte < stop - 2;
465             byte += 3 )
466         {
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                         /* get b64 digit from high order 6 bits */
479                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
480                 }
481         }
482
483         /* add padding if necessary */
484         if ( byte < stop ) {
485                 for ( i = 0; byte + i < stop; i++ ) {
486                         buf[i] = byte[i];
487                 }
488                 for ( pad = 0; i < 3; i++, pad++ ) {
489                         buf[i] = '\0';
490                 }
491                 byte = buf;
492                 bits = (byte[0] & 0xff) << 16;
493                 bits |= (byte[1] & 0xff) << 8;
494                 bits |= (byte[2] & 0xff);
495
496                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
497                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
498                                 *(*out)++ = '\n';
499                                 *(*out)++ = ' ';
500                                 len = 1;
501                         }
502
503                         if( i + pad < 4 ) {
504                                 /* get b64 digit from low order 6 bits */
505                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
506                         } else {
507                                 *(*out)++ = '=';
508                         }
509                 }
510         }
511         *(*out)++ = '\n';
512 }
513
514
515 /*
516  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
517  */
518 char *
519 ldif_put(
520         int type,
521         LDAP_CONST char *name,
522         LDAP_CONST char *val,
523         ber_len_t vlen )
524 {
525     char        *buf, *p;
526     ber_len_t nlen;
527
528     nlen = ( name != NULL ) ? strlen( name ) : 0;
529
530         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
531
532     if ( buf == NULL ) {
533                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
534                         "ldif_type_and_value: malloc failed!" );
535                 return NULL;
536     }
537
538     p = buf;
539     ldif_sput( &p, type, name, val, vlen );
540     *p = '\0';
541
542     return( buf );
543 }
544
545 int ldif_is_not_printable(
546         LDAP_CONST char *val,
547         ber_len_t vlen )
548 {
549         if( vlen == 0 || val == NULL  ) {
550                 return -1;
551         }
552
553         if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
554                 isgraph( (unsigned char) val[vlen-1] ) )
555         {
556                 ber_len_t i;
557
558                 for ( i = 0; val[i]; i++ ) {
559                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
560                                 return 1;
561                         }
562                 }
563
564                 return 0;
565         }
566
567         return 1;
568 }
569
570 /*
571  * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
572  */
573 int
574 ldif_read_record(
575         FILE        *fp,
576         int         *lno,               /* ptr to line number counter              */
577         char        **bufp,     /* ptr to malloced output buffer           */
578         int         *buflenp )  /* ptr to length of *bufp                  */
579 {
580         char        linebuf[BUFSIZ], *line, *nbufp;
581         ber_len_t   lcur = 0, len, linesize;
582         int         last_ch = '\n', found_entry = 0, stop;
583
584         line     = linebuf;
585         linesize = sizeof( linebuf );
586
587         for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) {
588                 if ( fgets( line, linesize, fp ) == NULL ) {
589                         stop = 1;
590                         /* Add \n in case the file does not end with newline */
591                         line = "\n";
592                 }
593                 len = strlen( line );
594
595                 if ( last_ch == '\n' ) {
596                         (*lno)++;
597
598                         if ( line[0] == '\n' ) {
599                                 if ( !found_entry )
600                                         continue;
601                                 break;
602                         }
603
604                         if ( !found_entry ) {
605                                 /* Found a new entry */
606                                 found_entry = 1;
607
608                                 if ( isdigit( (unsigned char) line[0] ) ) {
609                                         /* skip index */
610                                         continue;
611                                 }
612                         }                       
613                 }
614
615                 if ( *buflenp - lcur <= len ) {
616                         *buflenp += len + BUFSIZ;
617                         nbufp = ber_memrealloc( *bufp, *buflenp );
618                         if( nbufp == NULL ) {
619                                 return 0;
620                         }
621                         *bufp = nbufp;
622                 }
623                 strcpy( *bufp + lcur, line );
624                 lcur += len;
625         }
626
627         return( found_entry );
628 }