Changeset 141
- Timestamp:
- 11/20/07 20:01:52 (11 months ago)
- 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 15 6 */ 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 permitted20 * provided that this notice is preserved and that due credit is given21 * to the University of Michigan at Ann Arbor. The name of the22 * University may not be used to endorse or promote products derived23 * from this software without specific prior written permission. This24 * software is provided ``as is'' without express or implied warranty.25 */26 /* This work was originally developed by the University of Michigan27 * and distributed as part of U-MICH LDAP.28 */29 7 30 8 //#include "portable.h" 31 9 32 10 #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> 40 19 41 20 int ldif_debug = 0; 42 21 43 <<<<<<< line64.c44 #include "ldap_log.h"45 #include "lber_pvt.h"46 =======47 22 //#include "ldap_log.h" 48 23 #define LDAP_DEBUG_PARSE 0x0800 … … 52 27 //#include "lber_pvt.h" 53 28 #include <lber_types.h> 54 >>>>>>> 1.1.4.155 29 #include "ldif.h" 56 30 … … 59 33 #define CONTINUED_LINE_MARKER '\r' 60 34 35 #define CSRIMALLOC 61 36 #ifdef CSRIMALLOC 62 37 #define ber_memalloc malloc 63 38 #define ber_memcalloc calloc 64 39 #define ber_memrealloc realloc 40 #define ber_memfree free 65 41 #define ber_strdup strdup 42 #define AC_MEMCPY memcpy 66 43 #endif 44 45 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ); 46 int ber_pvt_log_printf( int errlvl, int loglvl, const char *fmt, ... ) 47 { 48 return 1; 49 } 50 51 int ldif_fetch_url( LDAP_CONST char *line, char **value, ber_len_t *vlen ); 52 int ldif_fetch_url( LDAP_CONST char *line, char **value, ber_len_t *vlen ) 53 { 54 return 1; 55 } 67 56 68 57 static const char nib2b64[0x40] = … … 92 81 * into components "type" and "value". if a double colon separates type from 93 82 * 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. 102 84 */ 103 85 … … 110 92 ) 111 93 { 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 int122 ldif_parse_line2(123 char *line,124 struct berval *type,125 struct berval *value,126 int *freeval127 )128 {129 94 char *s, *p, *d; 130 95 char nib; 131 96 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; 135 103 136 104 /* skip any leading space */ … … 139 107 } 140 108 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, ':' ); 156 120 157 121 if ( s == NULL ) { 158 122 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 ); 162 126 return( -1 ); 163 127 } 164 128 165 129 /* 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-- ) { 167 131 *p = '\0'; 168 132 } 169 133 *s++ = '\0'; 170 type->bv_len = s - type->bv_val - 1;171 134 172 135 url = 0; … … 201 164 /* no value is present, error out */ 202 165 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 ); 206 168 return( -1 ); 207 169 } 208 170 209 byte = value ->bv_val= s;210 211 for ( p = s, v alue->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 ) { 212 174 int i; 213 175 for ( i = 0; i < 4; i++ ) { … … 215 177 b642nib[ p[i] & 0x7f ] > 0x3f) ) { 216 178 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 ); 221 183 return( -1 ); 222 184 } … … 232 194 /* third digit */ 233 195 if ( p[2] == '=' ) { 234 v alue->bv_len += 1;196 vlen += 1; 235 197 break; 236 198 } … … 240 202 /* fourth digit */ 241 203 if ( p[3] == '=' ) { 242 v alue->bv_len += 2;204 vlen += 2; 243 205 break; 244 206 } … … 248 210 byte += 3; 249 211 } 250 s[ v alue->bv_len ] = '\0';212 s[ vlen ] = '\0'; 251 213 252 214 } else if ( url ) { … … 254 216 /* no value is present, error out */ 255 217 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 ); 259 220 return( -1 ); 260 221 } 261 222 262 if( ldif_fetch_url( s, &value ->bv_val, &value->bv_len ) ) {223 if( ldif_fetch_url( s, &value, &vlen ) ) { 263 224 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 ); 267 228 return( -1 ); 268 229 } 269 if ( freeval ) *freeval = 1;270 230 271 231 } 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 ) { 282 249 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 ); 286 253 return( -1 ); 287 254 } 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; 303 265 304 266 return( 0 ); … … 321 283 */ 322 284 323 int324 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 338 285 char * 339 286 ldif_getline( char **next ) … … 370 317 371 318 return( line ); 372 }373 374 /*375 * name and OID of attributeTypes that must be base64 encoded in any case376 */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 base64391 * encoded392 *393 * NOTE: this routine mallocs memory in a static struct which must394 * be explicitly freed when no longer required395 */396 int397 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 void453 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 any475 * of the attributeTypes listed in must_b64_encode476 */477 static int478 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 ) == 0490 || ber_bvcmp( &must_b64_encode[i].oid, &bv ) == 0 )491 {492 return 1;493 }494 }495 496 return 0;497 319 } 498 320 … … 617 439 #endif 618 440 #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 */ 620 445 #endif 621 446 ) { … … 720 545 if ( buf == NULL ) { 721 546 ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug, 722 _("ldif_type_and_value: malloc failed!"));547 "ldif_type_and_value: malloc failed!" ); 723 548 return NULL; 724 549 } … … 745 570 746 571 for ( i = 0; val[i]; i++ ) { 747 if ( !isascii( val[i] ) || !isprint( (unsigned char)val[i] ) ) {572 if ( !isascii( val[i] ) || !isprint( val[i] ) ) { 748 573 return 1; 749 574 } … … 756 581 } 757 582 758 LDIFFP *759 ldif_open(760 LDAP_CONST char *file,761 LDAP_CONST char *mode762 )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 void776 ldif_close(777 LDIFFP *lfp778 )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 790 583 /* 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. 792 585 */ 793 586 int 794 587 ldif_read_record( 795 LDIFFP *lfp,588 FILE *fp, 796 589 int *lno, /* ptr to line number counter */ 797 590 char **bufp, /* ptr to malloced output buffer */ … … 800 593 char linebuf[BUFSIZ], *line, *nbufp; 801 594 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; 803 596 804 597 line = linebuf; 805 598 linesize = sizeof( linebuf ); 806 599 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 ) { 826 602 stop = 1; 827 603 /* Add \n in case the file does not end with newline */ … … 834 610 835 611 if ( line[0] == '\n' ) { 836 if ( !found_entry ) { 837 lcur = 0; 838 top_comment = 0; 612 if ( !found_entry ) 839 613 continue; 840 }841 614 break; 842 615 } 843 616 844 617 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