Changeset 141

Show
Ignore:
Timestamp:
11/20/07 20:01:52 (11 months ago)
Author:
daboo
Message:

Use Mac code which is from an older openldap code base and seems to work.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • Mulberry/branches/win32-merge-121/Plug-ins/CommunicatorAdbkIO/sources/line64.c

    r18 r141  
    1 /* ldif.c - routines for dealing with LDIF files */ 
    2 /* $OpenLDAP: pkg/ldap/libraries/liblutil/ldif.c,v 1.2.2.8 2006/04/03 19:49:55 kurt Exp $ */ 
    3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
    4  * 
    5  * Copyright 1998-2006 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>. 
     1/* line64.c - routines for dealing with the slapd line format */ 
     2/* $OpenLDAP: pkg/ldap/libraries/libldif/line64.c,v 1.45.2.4 2003/03/03 17:10:06 kurt Exp $ */ 
     3/* 
     4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved. 
     5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file 
    156 */ 
    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  */ 
    297 
    308//#include "portable.h" 
    319 
    3210#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> 
     11#include <string.h> 
     12 
     13//#include <ac/stdlib.h> 
     14//#include <ac/ctype.h> 
     15 
     16//#include <ac/string.h> 
     17//#include <ac/socket.h> 
     18//#include <ac/time.h> 
    4019 
    4120int ldif_debug = 0; 
    4221 
    43 <<<<<<< line64.c 
    44 #include "ldap_log.h" 
    45 #include "lber_pvt.h" 
    46 ======= 
    4722//#include "ldap_log.h" 
    4823#define LDAP_DEBUG_PARSE        0x0800 
     
    5227//#include "lber_pvt.h" 
    5328#include <lber_types.h> 
    54 >>>>>>> 1.1.4.1 
    5529#include "ldif.h" 
    5630 
     
    5933#define CONTINUED_LINE_MARKER   '\r' 
    6034 
     35#define CSRIMALLOC 
    6136#ifdef CSRIMALLOC 
    6237#define ber_memalloc malloc 
    6338#define ber_memcalloc calloc 
    6439#define ber_memrealloc realloc 
     40#define ber_memfree free 
    6541#define ber_strdup strdup 
     42#define AC_MEMCPY memcpy 
    6643#endif 
     44 
     45int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ); 
     46int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ) 
     47{ 
     48        return 1; 
     49} 
     50 
     51int ldif_fetch_url( LDAP_CONST char *line, char **value, ber_len_t *vlen ); 
     52int ldif_fetch_url( LDAP_CONST char *line, char **value, ber_len_t *vlen ) 
     53{ 
     54        return 1; 
     55} 
    6756 
    6857static const char nib2b64[0x40] = 
     
    9281 * into components "type" and "value".  if a double colon separates type from 
    9382 * value, then value is encoded in base 64, and parse_line un-decodes it 
    94  * (in place) before returning. The type and value are stored in malloc'd 
    95  * memory which must be freed by the caller. 
    96  * 
    97  * ldif_parse_line2 - operates in-place on input buffer, returning type 
    98  * in-place. Will return value in-place if possible, (must malloc for 
    99  * fetched URLs). If freeval is NULL, all return data will be malloc'd 
    100  * and the input line will be unmodified. Otherwise freeval is set to 
    101  * True if the value was malloc'd. 
     83 * (in place) before returning. 
    10284 */ 
    10385 
     
    11092) 
    11193{ 
    112         struct berval type, value; 
    113         int rc = ldif_parse_line2( (char *)line, &type, &value, NULL ); 
    114  
    115         *typep = type.bv_val; 
    116         *valuep = value.bv_val; 
    117         *vlenp = value.bv_len; 
    118         return rc; 
    119 } 
    120  
    121 int 
    122 ldif_parse_line2( 
    123     char        *line, 
    124         struct berval *type, 
    125         struct berval *value, 
    126         int             *freeval 
    127 ) 
    128 { 
    12994        char    *s, *p, *d;  
    13095        char    nib; 
    13196        int     b64, url; 
    132  
    133         BER_BVZERO( type ); 
    134         BER_BVZERO( value ); 
     97        char    *freeme, *type, *value; 
     98        ber_len_t vlen; 
     99 
     100        *typep = NULL; 
     101        *valuep = NULL; 
     102        *vlenp = 0; 
    135103 
    136104        /* skip any leading space */ 
     
    139107        } 
    140108 
    141         if ( freeval ) { 
    142                 *freeval = 0; 
    143         } else { 
    144                 line = ber_strdup( line ); 
    145  
    146                 if( line == NULL ) { 
    147                         ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    148                                 _("ldif_parse_line: line malloc failed\n")); 
    149                         return( -1 ); 
    150                 } 
    151         } 
    152  
    153         type->bv_val = line; 
    154  
    155         s = strchr( type->bv_val, ':' ); 
     109        freeme = ber_strdup( line ); 
     110 
     111        if( freeme == NULL ) { 
     112                ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
     113                        "ldif_parse_line: line malloc failed\n"); 
     114                return( -1 ); 
     115        } 
     116 
     117        type = freeme; 
     118 
     119        s = strchr( type, ':' ); 
    156120 
    157121        if ( s == NULL ) { 
    158122                ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug, 
    159                         _("ldif_parse_line: missing ':' after %s\n"), 
    160                         type->bv_val ); 
    161                 if ( !freeval ) ber_memfree( line ); 
     123                        "ldif_parse_line: missing ':' after %s\n", 
     124                        type ); 
     125                ber_memfree( freeme ); 
    162126                return( -1 ); 
    163127        } 
    164128 
    165129        /* trim any space between type and : */ 
    166         for ( p = &s[-1]; p > type->bv_val && isspace( * (unsigned char *) p ); p-- ) { 
     130        for ( p = &s[-1]; p > type && isspace( * (unsigned char *) p ); p-- ) { 
    167131                *p = '\0'; 
    168132        } 
    169133        *s++ = '\0'; 
    170         type->bv_len = s - type->bv_val - 1; 
    171134 
    172135        url = 0; 
     
    201164                        /* no value is present, error out */ 
    202165                        ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug, 
    203                                 _("ldif_parse_line: %s missing base64 value\n"), 
    204                                 type->bv_val ); 
    205                         if ( !freeval ) ber_memfree( line ); 
     166                                "ldif_parse_line: %s missing base64 value\n", type ); 
     167                        ber_memfree( freeme ); 
    206168                        return( -1 ); 
    207169                } 
    208170 
    209                 byte = value->bv_val = s; 
    210  
    211                 for ( p = s, value->bv_len = 0; p < d; p += 4, value->bv_len += 3 ) { 
     171                byte = value = s; 
     172 
     173                for ( p = s, vlen = 0; p < d; p += 4, vlen += 3 ) { 
    212174                        int i; 
    213175                        for ( i = 0; i < 4; i++ ) { 
     
    215177                                    b642nib[ p[i] & 0x7f ] > 0x3f) ) { 
    216178                                        ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    217                                                 _("ldif_parse_line: %s: invalid base64 encoding" 
    218                                                 " char (%c) 0x%x\n"), 
    219                                             type->bv_val, p[i], p[i] ); 
    220                                         if ( !freeval ) ber_memfree( line ); 
     179                                                "ldif_parse_line: %s: invalid base64 encoding" 
     180                                                " char (%c) 0x%x\n", 
     181                                            type, p[i], p[i] ); 
     182                                        ber_memfree( freeme ); 
    221183                                        return( -1 ); 
    222184                                } 
     
    232194                        /* third digit */ 
    233195                        if ( p[2] == '=' ) { 
    234                                 value->bv_len += 1; 
     196                                vlen += 1; 
    235197                                break; 
    236198                        } 
     
    240202                        /* fourth digit */ 
    241203                        if ( p[3] == '=' ) { 
    242                                 value->bv_len += 2; 
     204                                vlen += 2; 
    243205                                break; 
    244206                        } 
     
    248210                        byte += 3; 
    249211                } 
    250                 s[ value->bv_len ] = '\0'; 
     212                s[ vlen ] = '\0'; 
    251213 
    252214        } else if ( url ) { 
     
    254216                        /* no value is present, error out */ 
    255217                        ber_pvt_log_printf( LDAP_DEBUG_PARSE, ldif_debug, 
    256                                 _("ldif_parse_line: %s missing URL value\n"), 
    257                                 type->bv_val ); 
    258                         if ( !freeval ) ber_memfree( line ); 
     218                                "ldif_parse_line: %s missing URL value\n", type ); 
     219                        ber_memfree( freeme ); 
    259220                        return( -1 ); 
    260221                } 
    261222 
    262                 if( ldif_fetch_url( s, &value->bv_val, &value->bv_len ) ) { 
     223                if( ldif_fetch_url( s, &value, &vlen ) ) { 
    263224                        ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    264                                 _("ldif_parse_line: %s: URL \"%s\" fetch failed\n"), 
    265                                 type->bv_val, s ); 
    266                         if ( !freeval ) ber_memfree( line ); 
     225                                "ldif_parse_line: %s: URL \"%s\" fetch failed\n", 
     226                                type, s ); 
     227                        ber_memfree( freeme ); 
    267228                        return( -1 ); 
    268229                } 
    269                 if ( freeval ) *freeval = 1; 
    270230 
    271231        } else { 
    272                 value->bv_val = s; 
    273                 value->bv_len = (int) (d - s); 
    274         } 
    275  
    276         if ( !freeval ) { 
    277                 struct berval bv = *type; 
    278  
    279                 ber_dupbv( type, &bv ); 
    280  
    281                 if( BER_BVISNULL( type )) { 
     232                value = s; 
     233                vlen = (int) (d - s); 
     234        } 
     235 
     236        type = ber_strdup( type ); 
     237 
     238        if( type == NULL ) { 
     239                ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
     240                        "ldif_parse_line: type malloc failed\n"); 
     241                if( url ) ber_memfree( value ); 
     242                ber_memfree( freeme ); 
     243                return( -1 ); 
     244        } 
     245 
     246        if( !url ) { 
     247                p = ber_memalloc( vlen + 1 ); 
     248                if( p == NULL ) { 
    282249                        ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    283                                 _("ldif_parse_line: type malloc failed\n")); 
    284                         if( url ) ber_memfree( value->bv_val ); 
    285                         ber_memfree( line ); 
     250                                "ldif_parse_line: value malloc failed\n"); 
     251                        ber_memfree( type ); 
     252                        ber_memfree( freeme ); 
    286253                        return( -1 ); 
    287254                } 
    288  
    289                 if( !url ) { 
    290                         bv = *value; 
    291                         ber_dupbv( value, &bv ); 
    292                         if( BER_BVISNULL( value )) { 
    293                                 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    294                                         _("ldif_parse_line: value malloc failed\n")); 
    295                                 ber_memfree( type->bv_val ); 
    296                                 ber_memfree( line ); 
    297                                 return( -1 ); 
    298                         } 
    299                 } 
    300  
    301                 ber_memfree( line ); 
    302         } 
     255                AC_MEMCPY( p, value, vlen ); 
     256                p[vlen] = '\0'; 
     257                value = p; 
     258        } 
     259 
     260        ber_memfree( freeme ); 
     261 
     262        *typep = type; 
     263        *valuep = value; 
     264        *vlenp = vlen; 
    303265 
    304266        return( 0 ); 
     
    321283 */ 
    322284 
    323 int 
    324 ldif_countlines( LDAP_CONST char *buf ) 
    325 { 
    326         char *nl; 
    327         int ret = 0; 
    328  
    329         if ( !buf ) return ret; 
    330  
    331         for ( nl = strchr(buf, '\n'); nl; nl = strchr(nl, '\n') ) { 
    332                 nl++; 
    333                 if ( *nl != ' ' ) ret++; 
    334         } 
    335         return ret; 
    336 } 
    337  
    338285char * 
    339286ldif_getline( char **next ) 
     
    370317 
    371318        return( line ); 
    372 } 
    373  
    374 /* 
    375  * name and OID of attributeTypes that must be base64 encoded in any case 
    376  */ 
    377 typedef struct must_b64_encode_s { 
    378         struct berval   name; 
    379         struct berval   oid; 
    380 } must_b64_encode_s; 
    381  
    382 static must_b64_encode_s        default_must_b64_encode[] = { 
    383         { BER_BVC( "userPassword" ), BER_BVC( "2.5.4.35" ) }, 
    384         { BER_BVNULL, BER_BVNULL } 
    385 }; 
    386  
    387 static must_b64_encode_s        *must_b64_encode = default_must_b64_encode; 
    388  
    389 /* 
    390  * register name and OID of attributeTypes that must always be base64  
    391  * encoded 
    392  * 
    393  * NOTE: this routine mallocs memory in a static struct which must  
    394  * be explicitly freed when no longer required 
    395  */ 
    396 int 
    397 ldif_must_b64_encode_register( LDAP_CONST char *name, LDAP_CONST char *oid ) 
    398 { 
    399         int             i; 
    400         ber_len_t       len; 
    401  
    402         assert( must_b64_encode != NULL ); 
    403         assert( name != NULL ); 
    404         assert( oid != NULL ); 
    405  
    406         len = strlen( name ); 
    407  
    408         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) { 
    409                 if ( len != must_b64_encode[i].name.bv_len ) { 
    410                         continue; 
    411                 } 
    412  
    413                 if ( strcasecmp( name, must_b64_encode[i].name.bv_val ) == 0 ) { 
    414                         break; 
    415                 } 
    416         } 
    417  
    418         if ( !BER_BVISNULL( &must_b64_encode[i].name ) ) { 
    419                 return 1; 
    420         } 
    421  
    422         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) 
    423                 /* just count */ ; 
    424  
    425         if ( must_b64_encode == default_must_b64_encode ) { 
    426                 must_b64_encode = ber_memalloc( sizeof( must_b64_encode_s ) * ( i + 2 ) ); 
    427  
    428                 for ( i = 0; !BER_BVISNULL( &default_must_b64_encode[i].name ); i++ ) { 
    429                         ber_dupbv( &must_b64_encode[i].name, &default_must_b64_encode[i].name ); 
    430                         ber_dupbv( &must_b64_encode[i].oid, &default_must_b64_encode[i].oid ); 
    431                 } 
    432  
    433         } else { 
    434                 must_b64_encode_s       *tmp; 
    435  
    436                 tmp = ber_memrealloc( must_b64_encode, 
    437                         sizeof( must_b64_encode_s ) * ( i + 2 ) ); 
    438                 if ( tmp == NULL ) { 
    439                         return 1; 
    440                 } 
    441                 must_b64_encode = tmp; 
    442         } 
    443  
    444         ber_str2bv( name, len, 1, &must_b64_encode[i].name ); 
    445         ber_str2bv( oid, 0, 1, &must_b64_encode[i].oid ); 
    446  
    447         BER_BVZERO( &must_b64_encode[i + 1].name ); 
    448  
    449         return 0; 
    450 } 
    451  
    452 void 
    453 ldif_must_b64_encode_release( void ) 
    454 { 
    455         int     i; 
    456  
    457         assert( must_b64_encode != NULL ); 
    458  
    459         if ( must_b64_encode == default_must_b64_encode ) { 
    460                 return; 
    461         } 
    462  
    463         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) { 
    464                 ber_memfree( must_b64_encode[i].name.bv_val ); 
    465                 ber_memfree( must_b64_encode[i].oid.bv_val ); 
    466         } 
    467  
    468         ber_memfree( must_b64_encode ); 
    469  
    470         must_b64_encode = default_must_b64_encode; 
    471 } 
    472  
    473 /* 
    474  * returns 1 iff the string corresponds to the name or the OID of any  
    475  * of the attributeTypes listed in must_b64_encode 
    476  */ 
    477 static int 
    478 ldif_must_b64_encode( LDAP_CONST char *s ) 
    479 { 
    480         int             i; 
    481         struct berval   bv; 
    482  
    483         assert( must_b64_encode != NULL ); 
    484         assert( s != NULL ); 
    485  
    486         ber_str2bv( s, 0, 0, &bv ); 
    487  
    488         for ( i = 0; !BER_BVISNULL( &must_b64_encode[i].name ); i++ ) { 
    489                 if ( ber_bvstrcasecmp( &must_b64_encode[i].name, &bv ) == 0 
    490                         || ber_bvcmp( &must_b64_encode[i].oid, &bv ) == 0 ) 
    491                 { 
    492                         return 1; 
    493                 } 
    494         } 
    495  
    496         return 0; 
    497319} 
    498320 
     
    617439#endif 
    618440#ifndef LDAP_PASSWD_DEBUG 
    619                 && !ldif_must_b64_encode( name ) 
     441                && (namelen != (sizeof("userPassword")-1) 
     442                || strcasecmp( name, "userPassword" ) != 0)     /* encode userPassword */ 
     443                && (namelen != (sizeof("2.5.4.35")-1)  
     444                || strcasecmp( name, "2.5.4.35" ) != 0)         /* encode userPassword */ 
    620445#endif 
    621446        ) { 
     
    720545    if ( buf == NULL ) { 
    721546                ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 
    722                         _("ldif_type_and_value: malloc failed!")); 
     547                        "ldif_type_and_value: malloc failed!" ); 
    723548                return NULL; 
    724549    } 
     
    745570 
    746571                for ( i = 0; val[i]; i++ ) { 
    747                         if ( !isascii( val[i] ) || !isprint( (unsigned char) val[i] ) ) { 
     572                        if ( !isascii( val[i] ) || !isprint( val[i] ) ) { 
    748573                                return 1; 
    749574                        } 
     
    756581} 
    757582 
    758 LDIFFP * 
    759 ldif_open( 
    760         LDAP_CONST char *file, 
    761         LDAP_CONST char *mode 
    762 ) 
    763 { 
    764         FILE *fp = fopen( file, mode ); 
    765         LDIFFP *lfp = NULL; 
    766  
    767         if ( fp ) { 
    768                 lfp = ber_memalloc( sizeof( LDIFFP )); 
    769                 lfp->fp = fp; 
    770                 lfp->prev = NULL; 
    771         } 
    772         return lfp; 
    773 } 
    774  
    775 void 
    776 ldif_close( 
    777         LDIFFP *lfp 
    778 ) 
    779 { 
    780         LDIFFP *prev; 
    781  
    782         while ( lfp ) { 
    783                 fclose( lfp->fp ); 
    784                 prev = lfp->prev; 
    785                 ber_memfree( lfp ); 
    786                 lfp = prev; 
    787         } 
    788 } 
    789  
    790583/* 
    791  * ldif_read_record - read an ldif record.  Return 1 for success, 0 for EOF. 
     584 * slap_read_ldif - read an ldif record.  Return 1 for success, 0 for EOF. 
    792585 */ 
    793586int 
    794587ldif_read_record( 
    795         LDIFFP      *lfp, 
     588        FILE        *fp, 
    796589        int         *lno,               /* ptr to line number counter              */ 
    797590        char        **bufp,     /* ptr to malloced output buffer           */ 
     
    800593        char        linebuf[BUFSIZ], *line, *nbufp; 
    801594        ber_len_t   lcur = 0, len, linesize; 
    802         int         last_ch = '\n', found_entry = 0, stop, top_comment = 0; 
     595        int         last_ch = '\n', found_entry = 0, stop; 
    803596 
    804597        line     = linebuf; 
    805598        linesize = sizeof( linebuf ); 
    806599 
    807         for ( stop = 0;  !stop;  last_ch = line[len-1] ) { 
    808                 /* If we're at the end of this file, see if we should pop 
    809                  * back to a previous file. (return from an include) 
    810                  */ 
    811                 while ( feof( lfp->fp )) { 
    812                         if ( lfp->prev ) { 
    813                                 LDIFFP *tmp = lfp->prev; 
    814                                 fclose( lfp->fp ); 
    815                                 *lfp = *tmp; 
    816                                 ber_memfree( tmp ); 
    817                         } else { 
    818                                 stop = 1; 
    819                                 break; 
    820                         } 
    821                 } 
    822                 if ( stop ) 
    823                         break; 
    824  
    825                 if ( fgets( line, linesize, lfp->fp ) == NULL ) { 
     600        for ( stop = feof( fp );  !stop;  last_ch = line[len-1] ) { 
     601                if ( fgets( line, linesize, fp ) == NULL ) { 
    826602                        stop = 1; 
    827603                        /* Add \n in case the file does not end with newline */ 
     
    834610 
    835611                        if ( line[0] == '\n' ) { 
    836                                 if ( !found_entry ) { 
    837                                         lcur = 0; 
    838                                         top_comment = 0; 
     612                                if ( !found_entry ) 
    839613                                        continue; 
    840                                 } 
    841614                                break; 
    842615                        } 
    843616 
    844617                        if ( !found_entry ) { 
    845                                 if ( line[0] == '#' ) { 
    846                                         top_comment = 1; 
    847                                 } else if ( ! ( top_comment && line[0] == ' ' ) ) { 
    848                                         /* Found a new entry */ 
    849                                         found_entry = 1; 
    850  
    851                                         if ( isdigit( (unsigned char) line[0] ) ) { 
    852                                                 /* skip index */ 
    853                                                 continue; 
    854                                         } 
    855                                         if ( !strncasecmp( line, "include:", 
    856                                                 STRLENOF("include:"))) { 
    857                                                 FILE *fp2; 
    858                                                 char *ptr; 
    859                                                 found_entry = 0; 
    860  
    861                                                 if ( line[len-1] == '\n' ) { 
    862                                                         len--; 
    863                                                         line[len] = '\0'; 
    864                                                 } 
    865                                                 if ( line[len-1] == '\r' ) { 
    866                                                         len--; 
    867                                                         line[len] = '\0'; 
    868                                                 } 
    869  
    870