📄 init.c
字号:
/* Copyright 2004 IBM Corporation * All rights reserved. * Redisribution and use in source and binary forms, with or without * modification, are permitted only as authorizd by the OpenLADP * Public License. *//* ACKNOWLEDGEMENTS * This work originally developed by Sang Seok Lim * 2004/06/18 03:20:00 slim@OpenLDAP.org */#include "portable.h"#include <ac/string.h>#include <ac/socket.h>#include <ldap_pvt.h>#include "lutil.h"#include <ldap.h>#include "slap.h"#include "component.h"#include "componentlib.h"#include "asn.h"#include <asn-gser.h>#include <string.h>#ifndef SLAPD_COMP_MATCH#define SLAPD_COMP_MATCH SLAPD_MOD_DYNAMIC#endif/* * Attribute and MatchingRule aliasing table */AttributeAliasing aa_table [ MAX_ALIASING_ENTRY ];MatchingRuleAliasing mra_table [ MAX_ALIASING_ENTRY ];OD_entry* gOD_table = NULL;AsnTypetoMatchingRuleTable* gATMR_table = NULL;intload_derived_matching_rule ( char* cfg_path ){}AttributeAliasing*comp_is_aliased_attribute( void *in ){ AttributeAliasing* curr_aa; int i; AttributeDescription *ad = (AttributeDescription*)in; for ( i = 0; aa_table[i].aa_aliasing_ad && i < MAX_ALIASING_ENTRY; i++ ) { if ( strncmp(aa_table[i].aa_aliasing_ad->ad_cname.bv_val , ad->ad_cname.bv_val, ad->ad_cname.bv_len) == 0 ) return &aa_table[i]; } return NULL;}static intadd_aa_entry( int index, char* aliasing_at_name, char* aliased_at_name, char* mr_name, char* component_filter ){ char text[1][128]; int rc; struct berval type; /* get and store aliasing AttributeDescription */ type.bv_val = aliasing_at_name; type.bv_len = strlen ( aliasing_at_name ); rc = slap_bv2ad ( &type, &aa_table[index].aa_aliasing_ad,(const char**)text ); if ( rc != LDAP_SUCCESS ) return rc; /* get and store aliased AttributeDescription */ type.bv_val = aliased_at_name; type.bv_len = strlen ( aliased_at_name ); rc = slap_bv2ad ( &type, &aa_table[index].aa_aliased_ad,(const char**)text ); if ( rc != LDAP_SUCCESS ) return rc; /* get and store componentFilterMatch */ type.bv_val = mr_name; type.bv_len = strlen ( mr_name); aa_table[index].aa_mr = mr_bvfind ( &type ); /* get and store a component filter */ type.bv_val = component_filter; type.bv_len = strlen ( component_filter ); rc = get_comp_filter( NULL, &type, &aa_table[index].aa_cf,(const char**)text); aa_table[index].aa_cf_str = component_filter; return rc;}/* * Initialize attribute aliasing table when this module is loaded * add_aa_entry ( index for the global table, * name of the aliasing attribute, * component filter with filling value parts "xxx" * ) * "xxx" will be replaced with effective values later. * See RFC3687 to understand the content of a component filter. */char* pre_processed_comp_filter[] = {/*1*/"item:{ component \"toBeSigned.issuer.rdnSequence\", rule distinguishedNameMatch, value xxx }",/*2*/"item:{ component \"toBeSigned.serialNumber\", rule integerMatch, value xxx }",/*3*/"and:{ item:{ component \"toBeSigned.serialNumber\", rule integerMatch, value xxx }, item:{ component \"toBeSigned.issuer.rdnSequence\", rule distinguishedNameMatch, value xxx } }"};static intinit_attribute_aliasing_table (){ int rc; int index = 0 ; rc = add_aa_entry ( index, "x509CertificateIssuer", "userCertificate","componentFilterMatch", pre_processed_comp_filter[index] ); if ( rc != LDAP_SUCCESS ) return LDAP_PARAM_ERROR; index++; rc = add_aa_entry ( index, "x509CertificateSerial","userCertificate", "componentFilterMatch", pre_processed_comp_filter[index] ); if ( rc != LDAP_SUCCESS ) return LDAP_PARAM_ERROR; index++; rc = add_aa_entry ( index, "x509CertificateSerialAndIssuer", "userCertificate", "componentFilterMatch", pre_processed_comp_filter[index] ); if ( rc != LDAP_SUCCESS ) return LDAP_PARAM_ERROR; index++; return LDAP_SUCCESS;}voidinit_component_description_table () { AsnTypeId id; struct berval mr; AsnTypetoSyntax* asn_to_syn; Syntax* syn; for ( id = BASICTYPE_BOOLEAN; id != ASNTYPE_END ; id++ ) { asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_subtypes = NULL; asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_syntax = NULL; /* Equality Matching Rule */ if ( asntype_to_compMR_mapping_tbl[id].atc_equality ) { mr.bv_val = asntype_to_compMR_mapping_tbl[id].atc_equality; mr.bv_len = strlen(asntype_to_compMR_mapping_tbl[id].atc_equality); asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_equality = mr_bvfind( &mr ); } /* Approx Matching Rule */ if ( asntype_to_compMR_mapping_tbl[id].atc_approx ) { mr.bv_val = asntype_to_compMR_mapping_tbl[id].atc_approx; mr.bv_len = strlen(asntype_to_compMR_mapping_tbl[id].atc_approx); asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_approx = mr_bvfind( &mr ); } /* Ordering Matching Rule */ if ( asntype_to_compMR_mapping_tbl[id].atc_ordering ) { mr.bv_val = asntype_to_compMR_mapping_tbl[id].atc_ordering; mr.bv_len = strlen(asntype_to_compMR_mapping_tbl[id].atc_ordering); asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_ordering= mr_bvfind( &mr ); } /* Substr Matching Rule */ if ( asntype_to_compMR_mapping_tbl[id].atc_substr ) { mr.bv_val = asntype_to_compMR_mapping_tbl[id].atc_substr; mr.bv_len = strlen(asntype_to_compMR_mapping_tbl[id].atc_substr); asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_substr = mr_bvfind( &mr ); } /* Syntax */ asn_to_syn = &asn_to_syntax_mapping_tbl[ id ]; if ( asn_to_syn->ats_syn_oid ) syn = syn_find ( asn_to_syn->ats_syn_oid ); else syn = NULL; asntype_to_compType_mapping_tbl[id].ac_comp_type.ct_syntax = syn; /* Initialize Component Descriptions of primitive ASN.1 types */ asntype_to_compdesc_mapping_tbl[id].atcd_cd.cd_comp_type = (AttributeType*)&asntype_to_compType_mapping_tbl[id].ac_comp_type; }}MatchingRule*retrieve_matching_rule( char* mr_oid, AsnTypeId type ) { char* tmp; struct berval mr_name = BER_BVNULL; AsnTypetoMatchingRuleTable* atmr; for ( atmr = gATMR_table ; atmr ; atmr = atmr->atmr_table_next ) { if ( strcmp( atmr->atmr_oid, mr_oid ) == 0 ) { tmp = atmr->atmr_table[type].atmr_mr_name; if ( tmp ) { mr_name.bv_val = tmp; mr_name.bv_len = strlen( tmp ); return mr_bvfind ( &mr_name ); } } } return (MatchingRule*)NULL;}void* comp_convert_attr_to_comp LDAP_P (( Attribute* a, Syntax *syn, struct berval* bv )){ char* peek_head; int mode, bytesDecoded, size, rc; void* component; char* oid = a->a_desc->ad_type->sat_atype.at_oid ; GenBuf* b = NULL; ExpBuf* buf = NULL; OidDecoderMapping* odm; /* look for the decoder registered for the given attribute */ odm = RetrieveOidDecoderMappingbyOid( oid, strlen(oid) ); if ( !odm || (!odm->BER_Decode && !odm->GSER_Decode) ) return (void*)NULL; buf = ExpBufAllocBuf(); ExpBuftoGenBuf( buf, &b ); ExpBufInstallDataInBuf ( buf, bv->bv_val, bv->bv_len ); BufResetInReadMode( b ); mode = DEC_ALLOC_MODE_2; /* * How can we decide which decoder will be called, GSER or BER? * Currently BER decoder is called for a certificate. * The flag of Attribute will say something about it in the future */ if ( syn && slap_syntax_is_ber ( syn ) ) {#if 0 rc =BDecComponentTop(odm->BER_Decode, a->a_comp_data->cd_mem_op, b, 0,0, &component,&bytesDecoded,mode ) ;#endif rc = odm->BER_Decode ( a->a_comp_data->cd_mem_op, b, (ComponentSyntaxInfo*)&component, &bytesDecoded, mode ); } else { rc = odm->GSER_Decode( a->a_comp_data->cd_mem_op, b, (ComponentSyntaxInfo**)component, &bytesDecoded, mode); } ExpBufFreeBuf( buf ); GenBufFreeBuf( b ); if ( rc == -1 ) {#if 0 ShutdownNibbleMemLocal ( a->a_comp_data->cd_mem_op ); free ( a->a_comp_data ); a->a_comp_data = NULL;#endif return (void*)NULL; } else { return component; }}#include <nibble-alloc.h>voidcomp_free_component ( void* mem_op ) { ShutdownNibbleMemLocal( (NibbleMem*)mem_op ); return;}voidcomp_convert_assert_to_comp ( void* mem_op, ComponentSyntaxInfo *csi_attr, struct berval* bv, ComponentSyntaxInfo** csi, int* len, int mode ){ int rc; GenBuf* genBuf; ExpBuf* buf; gser_decoder_func *decoder = csi_attr->csi_comp_desc->cd_gser_decoder; buf = ExpBufAllocBuf(); ExpBuftoGenBuf( buf, &genBuf ); ExpBufInstallDataInBuf ( buf, bv->bv_val, bv->bv_len ); BufResetInReadMode( genBuf ); if ( csi_attr->csi_comp_desc->cd_type_id == BASICTYPE_ANY ) decoder = ((ComponentAny*)csi_attr)->cai->GSER_Decode; rc = (*decoder)( mem_op, genBuf, csi, len, mode ); ExpBufFreeBuf ( buf ); GenBufFreeBuf( genBuf );}int intToAscii( int value, char* buf ) { int minus=0,i,temp; int total_num_digits; if ( value == 0 ){ buf[0] = '0'; return 1; } if ( value < 0 ){ minus = 1; value = value*(-1); buf[0] = '-'; } /* How many digits */ for ( temp = value, total_num_digits=0 ; temp ; total_num_digits++ ) temp = temp/10; total_num_digits += minus; for ( i = minus ; value ; i++ ) { buf[ total_num_digits - i - 1 ]= (char)(value%10 + '0'); value = value/10; } return i;}intcomp_convert_asn_to_ldap ( MatchingRule* mr, ComponentSyntaxInfo* csi, struct berval* bv, int *allocated ){ int rc; struct berval prettied; Syntax* syn; AsnTypetoSyntax* asn_to_syn = &asn_to_syntax_mapping_tbl[csi->csi_comp_desc->cd_type_id]; if ( asn_to_syn->ats_syn_oid ) csi->csi_syntax = syn_find ( asn_to_syn->ats_syn_oid ); else csi->csi_syntax = NULL; switch ( csi->csi_comp_desc->cd_type_id ) { case BASICTYPE_BOOLEAN : bv->bv_val = (char*)malloc( 5 ); *allocated = 1; bv->bv_len = 5; if ( ((ComponentBool*)csi)->value > 0 ) { strcpy ( bv->bv_val , "TRUE" ); bv->bv_len = 4; } else { strcpy ( bv->bv_val , "FALSE" ); bv->bv_len = 5; } break ; case BASICTYPE_NULL : bv->bv_len = 0; break; case BASICTYPE_INTEGER : bv->bv_val = (char*)malloc( INITIAL_ATTR_SIZE ); *allocated = 1; bv->bv_len = INITIAL_ATTR_SIZE; bv->bv_len = intToAscii(((ComponentInt*)csi)->value, bv->bv_val ); if ( bv->bv_len <= 0 ) return LDAP_INVALID_SYNTAX; break; case BASICTYPE_REAL : return LDAP_INVALID_SYNTAX; case BASICTYPE_ENUMERATED : bv->bv_val = (char*)malloc( INITIAL_ATTR_SIZE ); *allocated = 1; bv->bv_len = INITIAL_ATTR_SIZE; bv->bv_len = intToAscii(((ComponentEnum*)csi)->value, bv->bv_val ); if ( bv->bv_len <= 0 ) return LDAP_INVALID_SYNTAX; break; case BASICTYPE_OID : case BASICTYPE_OCTETSTRING : case BASICTYPE_BITSTRING : case BASICTYPE_NUMERIC_STR : case BASICTYPE_PRINTABLE_STR : case BASICTYPE_UNIVERSAL_STR : case BASICTYPE_IA5_STR : case BASICTYPE_BMP_STR : case BASICTYPE_UTF8_STR : case BASICTYPE_UTCTIME : case BASICTYPE_GENERALIZEDTIME : case BASICTYPE_GRAPHIC_STR : case BASICTYPE_VISIBLE_STR : case BASICTYPE_GENERAL_STR : case BASICTYPE_OBJECTDESCRIPTOR : case BASICTYPE_VIDEOTEX_STR : case BASICTYPE_T61_STR : case BASICTYPE_OCTETCONTAINING : case BASICTYPE_BITCONTAINING : case BASICTYPE_RELATIVE_OID : bv->bv_val = ((ComponentOcts*)csi)->value.octs; bv->bv_len = ((ComponentOcts*)csi)->value.octetLen; break; case BASICTYPE_ANY : csi = ((ComponentAny*)csi)->value; if ( csi->csi_comp_desc->cd_type != ASN_BASIC || csi->csi_comp_desc->cd_type_id == BASICTYPE_ANY ) return LDAP_INVALID_SYNTAX; return comp_convert_asn_to_ldap( mr, csi, bv, allocated ); case COMPOSITE_ASN1_TYPE : break; case RDNSequence : /*dnMatch*/ if( strncmp( mr->smr_mrule.mr_oid, DN_MATCH_OID, strlen(DN_MATCH_OID) ) != 0 ) return LDAP_INVALID_SYNTAX; *allocated = 1; rc = ConvertRDNSequence2RFC2253( (irRDNSequence*)csi, bv ); if ( rc != LDAP_SUCCESS ) return rc; break; case RelativeDistinguishedName : /*rdnMatch*/ if( strncmp( mr->smr_mrule.mr_oid, RDN_MATCH_OID, strlen(RDN_MATCH_OID) ) != 0 ) return LDAP_INVALID_SYNTAX; *allocated = 1; rc = ConvertRDN2RFC2253((irRelativeDistinguishedName*)csi,bv); if ( rc != LDAP_SUCCESS ) return rc; break; case TelephoneNumber : case FacsimileTelephoneNumber__telephoneNumber : break; case DirectoryString : return LDAP_INVALID_SYNTAX; case ASN_COMP_CERTIFICATE : case ASNTYPE_END : break; default : /*Only ASN Basic Type can be converted into LDAP string*/ return LDAP_INVALID_SYNTAX; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -