]> git.sur5r.net Git - openldap/blob - libraries/libldif/line64.c
3f16c95d600a5a24c213d1dae8d76f0b99d04582
[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                 
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 + 1 );
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                 AC_MEMCPY( p, value, vlen );
235                 p[vlen] = '\0';
236                 value = p;
237         }
238
239         ber_memfree( freeme );
240
241         *typep = type;
242         *valuep = value;
243         *vlenp = vlen;
244
245         return( 0 );
246 }
247
248 /*
249  * ldif_getline - return the next "line" (minus newline) of input from a
250  * string buffer of lines separated by newlines, terminated by \n\n
251  * or \0.  this routine handles continued lines, bundling them into
252  * a single big line before returning.  if a line begins with a white
253  * space character, it is a continuation of the previous line. the white
254  * space character (nb: only one char), and preceeding newline are changed
255  * into CONTINUED_LINE_MARKER chars, to be deleted later by the
256  * ldif_parse_line() routine above.
257  *
258  * ldif_getline will skip over any line which starts '#'.
259  *
260  * ldif_getline takes a pointer to a pointer to the buffer on the first call,
261  * which it updates and must be supplied on subsequent calls.
262  */
263
264 char *
265 ldif_getline( char **next )
266 {
267         char *line;
268
269         do {
270                 if ( *next == NULL || **next == '\n' || **next == '\0' ) {
271                         return( NULL );
272                 }
273
274                 line = *next;
275
276                 while ( (*next = strchr( *next, '\n' )) != NULL ) {
277 #if CONTINUED_LINE_MARKER != '\r'
278                         if ( (*next)[-1] == '\r' ) {
279                                 (*next)[-1] = CONTINUED_LINE_MARKER;
280                         }
281 #endif
282
283                         if ( (*next)[1] != ' ' ) {
284                                 if ( (*next)[1] == '\r' && (*next)[2] == '\n' ) {
285                                         *(*next)++ = '\0';
286                                 }
287                                 *(*next)++ = '\0';
288                                 break;
289                         }
290
291                         **next = CONTINUED_LINE_MARKER;
292                         (*next)[1] = CONTINUED_LINE_MARKER;
293                         (*next)++;
294                 }
295         } while( *line == '#' );
296
297         return( line );
298 }
299
300 /* compatibility with U-Mich off by one bug */
301 #define LDIF_KLUDGE 1
302
303 void
304 ldif_sput(
305         char **out,
306         int type,
307         LDAP_CONST char *name,
308         LDAP_CONST char *val,
309         ber_len_t vlen )
310 {
311         const unsigned char *byte, *stop;
312         unsigned char   buf[3];
313         unsigned long   bits;
314         char            *save;
315         int             pad;
316         int             namelen = 0;
317
318         ber_len_t savelen;
319         ber_len_t len=0;
320         ber_len_t i;
321
322         /* prefix */
323         switch( type ) {
324         case LDIF_PUT_COMMENT:
325                 *(*out)++ = '#';
326                 len++;
327
328                 if( vlen ) {
329                         *(*out)++ = ' ';
330                         len++;
331                 }
332
333                 break;
334
335         case LDIF_PUT_SEP:
336                 *(*out)++ = '\n';
337                 return;
338         }
339
340         /* name (attribute type) */
341         if( name != NULL ) {
342                 /* put the name + ":" */
343                 namelen = strlen(name);
344                 strcpy(*out, name);
345                 *out += namelen;
346                 len += namelen;
347
348                 if( type != LDIF_PUT_COMMENT ) {
349                         *(*out)++ = ':';
350                         len++;
351                 }
352
353         }
354 #ifdef LDAP_DEBUG
355         else {
356                 assert( type == LDIF_PUT_COMMENT );
357         }
358 #endif
359
360         if( vlen == 0 ) {
361                 *(*out)++ = '\n';
362                 return;
363         }
364
365         switch( type ) {
366         case LDIF_PUT_NOVALUE:
367                 *(*out)++ = '\n';
368                 return;
369
370         case LDIF_PUT_URL: /* url value */
371                 *(*out)++ = '<';
372                 len++;
373                 break;
374
375         case LDIF_PUT_B64: /* base64 value */
376                 *(*out)++ = ':';
377                 len++;
378                 break;
379         }
380
381         switch( type ) {
382         case LDIF_PUT_TEXT:
383         case LDIF_PUT_URL:
384         case LDIF_PUT_B64:
385                 *(*out)++ = ' ';
386                 len++;
387                 /* fall-thru */
388
389         case LDIF_PUT_COMMENT:
390                 /* pre-encoded names */
391                 for ( i=0; i < vlen; i++ ) {
392                         if ( len > LDIF_LINE_WIDTH ) {
393                                 *(*out)++ = '\n';
394                                 *(*out)++ = ' ';
395                                 len = 1;
396                         }
397
398                         *(*out)++ = val[i];
399                         len++;
400                 }
401                 *(*out)++ = '\n';
402                 return;
403         }
404
405         save = *out;
406         savelen = len;
407
408         *(*out)++ = ' ';
409         len++;
410
411         stop = (const unsigned char *) (val + vlen);
412
413         if ( type == LDIF_PUT_VALUE
414                 && isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<'
415                 && isgraph( (unsigned char) val[vlen-1] )
416 #ifndef LDAP_BINARY_DEBUG
417                 && strstr( name, ";binary" ) == NULL
418 #endif
419 #ifndef LDAP_PASSWD_DEBUG
420                 && (namelen != (sizeof("userPassword")-1)
421                 || strcasecmp( name, "userPassword" ) != 0)     /* encode userPassword */
422                 && (namelen != (sizeof("2.5.4.35")-1) 
423                 || strcasecmp( name, "2.5.4.35" ) != 0)         /* encode userPassword */
424 #endif
425         ) {
426                 int b64 = 0;
427
428                 for ( byte = (const unsigned char *) val; byte < stop;
429                     byte++, len++ )
430                 {
431                         if ( !isascii( *byte ) || !isprint( *byte ) ) {
432                                 b64 = 1;
433                                 break;
434                         }
435                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
436                                 *(*out)++ = '\n';
437                                 *(*out)++ = ' ';
438                                 len = 1;
439                         }
440                         *(*out)++ = *byte;
441                 }
442
443                 if( !b64 ) {
444                         *(*out)++ = '\n';
445                         return;
446                 }
447         }
448
449         *out = save;
450         *(*out)++ = ':';
451         *(*out)++ = ' ';
452         len = savelen + 2;
453
454         /* convert to base 64 (3 bytes => 4 base 64 digits) */
455         for ( byte = (const unsigned char *) val;
456                 byte < stop - 2;
457             byte += 3 )
458         {
459                 bits = (byte[0] & 0xff) << 16;
460                 bits |= (byte[1] & 0xff) << 8;
461                 bits |= (byte[2] & 0xff);
462
463                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
464                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
465                                 *(*out)++ = '\n';
466                                 *(*out)++ = ' ';
467                                 len = 1;
468                         }
469
470                         /* get b64 digit from high order 6 bits */
471                         *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
472                 }
473         }
474
475         /* add padding if necessary */
476         if ( byte < stop ) {
477                 for ( i = 0; byte + i < stop; i++ ) {
478                         buf[i] = byte[i];
479                 }
480                 for ( pad = 0; i < 3; i++, pad++ ) {
481                         buf[i] = '\0';
482                 }
483                 byte = buf;
484                 bits = (byte[0] & 0xff) << 16;
485                 bits |= (byte[1] & 0xff) << 8;
486                 bits |= (byte[2] & 0xff);
487
488                 for ( i = 0; i < 4; i++, len++, bits <<= 6 ) {
489                         if ( len > LDIF_LINE_WIDTH+LDIF_KLUDGE ) {
490                                 *(*out)++ = '\n';
491                                 *(*out)++ = ' ';
492                                 len = 1;
493                         }
494
495                         if( i + pad < 4 ) {
496                                 /* get b64 digit from low order 6 bits */
497                                 *(*out)++ = nib2b64[ (bits & 0xfc0000L) >> 18 ];
498                         } else {
499                                 *(*out)++ = '=';
500                         }
501                 }
502         }
503         *(*out)++ = '\n';
504 }
505
506
507 /*
508  * ldif_type_and_value return BER malloc'd, zero-terminated LDIF line
509  */
510 char *
511 ldif_put(
512         int type,
513         LDAP_CONST char *name,
514         LDAP_CONST char *val,
515         ber_len_t vlen )
516 {
517     char        *buf, *p;
518     ber_len_t nlen;
519
520     nlen = ( name != NULL ) ? strlen( name ) : 0;
521
522         buf = (char *) ber_memalloc( LDIF_SIZE_NEEDED( nlen, vlen ) + 1 );
523
524     if ( buf == NULL ) {
525                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
526                         "ldif_type_and_value: malloc failed!" );
527                 return NULL;
528     }
529
530     p = buf;
531     ldif_sput( &p, type, name, val, vlen );
532     *p = '\0';
533
534     return( buf );
535 }
536
537 int ldif_is_not_printable(
538         LDAP_CONST char *val,
539         ber_len_t vlen )
540 {
541         if( vlen == 0 || val == NULL  ) {
542                 return -1;
543         }
544
545         if( isgraph( (unsigned char) val[0] ) && val[0] != ':' && val[0] != '<' &&
546                 isgraph( (unsigned char) val[vlen-1] ) )
547         {
548                 ber_len_t i;
549
550                 for ( i = 0; val[i]; i++ ) {
551                         if ( !isascii( val[i] ) || !isprint( val[i] ) ) {
552                                 return 1;
553                         }
554                 }
555
556                 return 0;
557         }
558
559         return 1;
560 }
561
562 /*
563  * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF.
564  */
565 int
566 ldif_read_record(
567         FILE        *fp,
568         int         *lno,               /* ptr to line number counter              */
569         char        **bufp,     /* ptr to malloced output buffer           */
570         int         *buflenp )  /* ptr to length of *bufp                  */
571 {
572         char        linebuf[BUFSIZ], *line, *nbufp;
573         ber_len_t   lcur = 0, len, linesize;
574         int         last_ch = '\n', found_entry = 0, stop;
575
576         line     = linebuf;
577         linesize = sizeof( linebuf );
578
579         for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) {
580                 if ( fgets( line, linesize, fp ) == NULL ) {
581                         stop = 1;
582                         /* Add \n in case the file does not end with newline */
583                         line = "\n";
584                 }
585                 len = strlen( line );
586
587                 if ( last_ch == '\n' ) {
588                         (*lno)++;
589
590                         if ( line[0] == '\n' ) {
591                                 if ( !found_entry )
592                                         continue;
593                                 break;
594                         }
595
596                         if ( !found_entry ) {
597                                 /* Found a new entry */
598                                 found_entry = 1;
599
600                                 if ( isdigit( (unsigned char) line[0] ) ) {
601                                         /* skip index */
602                                         continue;
603                                 }
604                         }                       
605                 }
606
607                 if ( *buflenp - lcur <= len ) {
608                         *buflenp += len + BUFSIZ;
609                         nbufp = ber_memrealloc( *bufp, *buflenp );
610                         if( nbufp == NULL ) {
611                                 return 0;
612                         }
613                         *bufp = nbufp;
614                 }
615                 strcpy( *bufp + lcur, line );
616                 lcur += len;
617         }
618
619         return( found_entry );
620 }