From 5342ce61dd287cc486d8492e315003cb322090c9 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Wed, 13 Sep 2006 12:48:45 +0000 Subject: [PATCH] Add register_at / register_oc helpers. --- servers/slapd/aci.c | 28 +-------- servers/slapd/at.c | 41 +++++++++++++ servers/slapd/back-bdb/monitor.c | 99 +++++--------------------------- servers/slapd/config.c | 68 +++------------------- servers/slapd/oc.c | 31 ++++++++++ servers/slapd/proto-slap.h | 10 ++++ 6 files changed, 108 insertions(+), 169 deletions(-) diff --git a/servers/slapd/aci.c b/servers/slapd/aci.c index 28b99388ea..75642b6fd4 100644 --- a/servers/slapd/aci.c +++ b/servers/slapd/aci.c @@ -702,37 +702,15 @@ aci_init( void ) } /* ACI attribute */ - at = ldap_str2attributetype( aci_at.desc, - &rc, &text, LDAP_SCHEMA_ALLOW_ALL ); - if ( !at ) { - Debug( LDAP_DEBUG_ANY, - "aci_init: AttributeType \"%s\" parse failed: %s %s\n", - aci_at.name, ldap_scherr2str( rc ), text ); - return rc; - } - - rc = at_add( at, 0, &sat, &text ); + rc = register_at( aci_at.desc, aci_at.ad, 0 ); if ( rc != LDAP_SUCCESS ) { - ldap_attributetype_free( at ); Debug( LDAP_DEBUG_ANY, - "aci_init: AttributeType \"%s\" load failed: %s %s\n", - aci_at.name, scherr2str( rc ), text ); + "aci_init: at_register failed\n", 0, 0, 0 ); return rc; } - ldap_memfree( at ); - - rc = slap_str2ad( aci_at.name, - aci_at.ad, &text ); - if ( rc != LDAP_SUCCESS ) { - Debug( LDAP_DEBUG_ANY, - "aci_init: unable to find AttributeDescription " - "\"%s\": %d (%s)\n", - aci_at.name, rc, text ); - return 1; - } /* install flags */ - sat->sat_flags |= aci_at.flags; + (*aci_at.ad)->ad_type->sat_flags |= aci_at.flags; return rc; } diff --git a/servers/slapd/at.c b/servers/slapd/at.c index 9453cabf02..813f52226e 100644 --- a/servers/slapd/at.c +++ b/servers/slapd/at.c @@ -892,3 +892,44 @@ at_schema_info( Entry *e ) } return 0; } + +int +register_at( char *def, AttributeDescription **ad, int dupok ) +{ + LDAPAttributeType *at; + int code, freeit = 0; + const char *err; + + at = ldap_str2attributetype( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL ); + if ( !at ) { + Debug( LDAP_DEBUG_ANY, + "register_at: AttributeType \"%s\": %s, %s\n", + def, ldap_scherr2str(code), err ); + return code; + } + + code = at_add( at, 0, NULL, &err ); + if ( code ) { + if ( code == SLAP_SCHERR_ATTR_DUP && dupok ) { + freeit = 1; + + } else { + ldap_attributetype_free( at ); + Debug( LDAP_DEBUG_ANY, + "register_at: AttributeType \"%s\": %s, %s\n", + def, scherr2str(code), err ); + return code; + } + } + code = slap_str2ad( at->at_names[0], ad, &err ); + if ( freeit || code ) { + ldap_attributetype_free( at ); + } else { + ldap_memfree( at ); + } + if ( code ) { + Debug( LDAP_DEBUG_ANY, "register_at: AttributeType \"%s\": %s\n", + def, err, 0 ); + } + return code; +} diff --git a/servers/slapd/back-bdb/monitor.c b/servers/slapd/back-bdb/monitor.c index fac8ce307b..7607618f61 100644 --- a/servers/slapd/back-bdb/monitor.c +++ b/servers/slapd/back-bdb/monitor.c @@ -57,11 +57,10 @@ static struct { }; static struct { - char *name; char *desc; AttributeDescription **ad; } s_at[] = { - { "olmBDBEntryCache", "( olmBDBAttributes:1 " + { "( olmBDBAttributes:1 " "NAME ( 'olmBDBEntryCache' ) " "DESC 'Number of items in Entry Cache' " "SUP monitorCounter " @@ -69,7 +68,7 @@ static struct { "USAGE directoryOperation )", &ad_olmBDBEntryCache }, - { "olmBDBEntryInfo", "( olmBDBAttributes:2 " + { "( olmBDBAttributes:2 " "NAME ( 'olmBDBEntryInfo' ) " "DESC 'Number of items in EntryInfo Cache' " "SUP monitorCounter " @@ -77,7 +76,7 @@ static struct { "USAGE directoryOperation )", &ad_olmBDBEntryInfo }, - { "olmBDBIDLCache", "( olmBDBAttributes:3 " + { "( olmBDBAttributes:3 " "NAME ( 'olmBDBIDLCache' ) " "DESC 'Number of items in IDL Cache' " "SUP monitorCounter " @@ -85,7 +84,7 @@ static struct { "USAGE directoryOperation )", &ad_olmBDBIDLCache }, - { "olmDbDirectory", "( olmBDBAttributes:4 " + { "( olmBDBAttributes:4 " "NAME ( 'olmDbDirectory' ) " "DESC 'Path name of the directory " "where the database environment resides' " @@ -98,13 +97,12 @@ static struct { }; static struct { - char *name; char *desc; ObjectClass **oc; } s_oc[] = { /* augments an existing object, so it must be AUXILIARY * FIXME: derive from some ABSTRACT "monitoredEntity"? */ - { "olmBDBDatabase", "( olmBDBObjectClasses:1 " + { "( olmBDBObjectClasses:1 " "NAME ( 'olmBDBDatabase' ) " "SUP top AUXILIARY " "MAY ( " @@ -187,7 +185,7 @@ bdb_monitor_free( /* don't care too much about return code... */ /* remove attrs */ - for ( i = 0; s_at[ i ].name != NULL; i++ ) { + for ( i = 0; s_at[ i ].desc != NULL; i++ ) { mod.sm_desc = *s_at[ i ].ad; mod.sm_values = NULL; rc = modify_delete_values( e, &mod, 1, &text, @@ -205,7 +203,6 @@ int bdb_monitor_initialize( void ) { int i, code; - const char *err; BackendInfo *bi; static int bdb_monitor_initialized = 0; @@ -236,90 +233,22 @@ bdb_monitor_initialize( void ) } } - for ( i = 0; s_at[ i ].name != NULL; i++ ) { - LDAPAttributeType *at; - - at = ldap_str2attributetype( s_at[ i ].desc, - &code, &err, LDAP_SCHEMA_ALLOW_ALL ); - if ( !at ) { - Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "AttributeType load failed: %s %s\n", - ldap_scherr2str( code ), err, 0 ); - return LDAP_INVALID_SYNTAX; - } - - code = at_add( at, 0, NULL, &err ); + for ( i = 0; s_at[ i ].desc != NULL; i++ ) { + code = register_at( s_at[ i ].desc, s_at[ i ].ad, 1 ); if ( code != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "AttributeType load failed: %s %s\n", - scherr2str( code ), err, 0 ); - code = LDAP_INVALID_SYNTAX; - goto done_at; - } - - code = slap_str2ad( s_at[ i ].name, - s_at[ i ].ad, &err ); - if ( code != LDAP_SUCCESS ) { - Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "unable to find AttributeDescription " - "\"%s\": %d (%s)\n", - s_at[ i ].name, code, err ); - code = LDAP_UNDEFINED_TYPE; - goto done_at; - } - -done_at:; - if ( code ) { - ldap_attributetype_free( at ); - return code; + "bdb_monitor_initialize: register_at failed\n", + 0, 0, 0 ); } - - ldap_memfree( at ); } - for ( i = 0; s_oc[ i ].name != NULL; i++ ) { - LDAPObjectClass *oc; - - oc = ldap_str2objectclass( s_oc[ i ].desc, - &code, &err, LDAP_SCHEMA_ALLOW_ALL ); - if ( !oc ) { - Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "ObjectClass load failed: %s %s\n", - ldap_scherr2str( code ), err, 0 ); - return LDAP_INVALID_SYNTAX; - } - - code = oc_add( oc, 0, NULL, &err ); + for ( i = 0; s_oc[ i ].desc != NULL; i++ ) { + code = register_oc( s_oc[ i ].desc, s_oc[ i ].oc, 1 ); if ( code != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "ObjectClass load failed: %s %s\n", - scherr2str( code ), err, 0 ); - code = LDAP_INVALID_SYNTAX; - goto done_oc; - } - - *s_oc[ i ].oc = oc_find( s_oc[ i ].name ); - if ( *s_oc[ i ].oc == NULL ) { - code = LDAP_UNDEFINED_TYPE; - Debug( LDAP_DEBUG_ANY, - "bdb_monitor_initialize: " - "unable to find objectClass \"%s\"\n", - s_oc[ i ].name, 0, 0 ); - goto done_oc; - } - -done_oc:; - if ( code != LDAP_SUCCESS ) { - ldap_objectclass_free( oc ); - return code; + "bdb_monitor_initialize: register_oc failed\n", + 0, 0, 0 ); } - - ldap_memfree( oc ); } return 0; diff --git a/servers/slapd/config.c b/servers/slapd/config.c index 70a5a6375f..dbc62367e1 100644 --- a/servers/slapd/config.c +++ b/servers/slapd/config.c @@ -471,49 +471,18 @@ config_get_vals(ConfigTable *cf, ConfigArgs *c) int init_config_attrs(ConfigTable *ct) { - LDAPAttributeType *at; int i, code; - const char *err; for (i=0; ct[i].name; i++ ) { - int freeit = 0; - if ( !ct[i].attribute ) continue; - at = ldap_str2attributetype( ct[i].attribute, - &code, &err, LDAP_SCHEMA_ALLOW_ALL ); - if ( !at ) { - fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n", - ct[i].attribute, ldap_scherr2str(code), err ); - return code; - } - - code = at_add( at, 0, NULL, &err ); - if ( code ) { - if ( code == SLAP_SCHERR_ATTR_DUP ) { - freeit = 1; - - } else { - ldap_attributetype_free( at ); - fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n", - ct[i].attribute, scherr2str(code), err ); - return code; - } - } - code = slap_str2ad( at->at_names[0], &ct[i].ad, &err ); - if ( freeit || code ) { - ldap_attributetype_free( at ); - } else { - ldap_memfree( at ); - } + code = register_at( ct[i].attribute, &ct[i].ad, 1 ); if ( code ) { - fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s\n", - ct[i].attribute, err ); + fprintf( stderr, "init_config_attrs: register_at failed\n" ); return code; - } else { + } #ifndef LDAP_DEVEL - ct[i].ad->ad_type->sat_flags |= SLAP_AT_HIDE; + ct[i].ad->ad_type->sat_flags |= SLAP_AT_HIDE; #endif - } } return 0; @@ -521,36 +490,17 @@ init_config_attrs(ConfigTable *ct) { int init_config_ocs( ConfigOCs *ocs ) { - int i; + int i, code; for (i=0;ocs[i].co_def;i++) { - LDAPObjectClass *oc; - int code; - const char *err; - - oc = ldap_str2objectclass( ocs[i].co_def, &code, &err, - LDAP_SCHEMA_ALLOW_ALL ); - if ( !oc ) { - fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n", - ocs[i].co_def, ldap_scherr2str(code), err ); - return code; - } - code = oc_add(oc,0,NULL,&err); - if ( code && code != SLAP_SCHERR_CLASS_DUP ) { - fprintf( stderr, "init_config_ocs: objectclass \"%s\": %s, %s\n", - ocs[i].co_def, scherr2str(code), err ); - ldap_objectclass_free(oc); + code = register_oc( ocs[i].co_def, &ocs[i].co_oc, 1 ); + if ( code ) { + fprintf( stderr, "init_config_ocs: register_oc failed\n" ); return code; } - ocs[i].co_oc = oc_find(oc->oc_names[0]); - if ( code ) { - ldap_objectclass_free(oc); - } else { - ldap_memfree(oc); #ifndef LDAP_DEVEL - ocs[i].co_oc->soc_flags |= SLAP_OC_HIDE; + ocs[i].co_oc->soc_flags |= SLAP_OC_HIDE; #endif - } } return 0; } diff --git a/servers/slapd/oc.c b/servers/slapd/oc.c index 4847c9cc07..d4e970d394 100644 --- a/servers/slapd/oc.c +++ b/servers/slapd/oc.c @@ -755,3 +755,34 @@ oc_schema_info( Entry *e ) } return 0; } + +int +register_oc( char *def, ObjectClass **soc, int dupok ) +{ + LDAPObjectClass *oc; + int code; + const char *err; + + oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL ); + if ( !oc ) { + Debug( LDAP_DEBUG_ANY, + "register_oc: objectclass \"%s\": %s, %s\n", + def, ldap_scherr2str(code), err ); + return code; + } + code = oc_add(oc,0,NULL,&err); + if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) { + Debug( LDAP_DEBUG_ANY, + "register_oc: objectclass \"%s\": %s, %s\n", + def, scherr2str(code), err ); + ldap_objectclass_free(oc); + return code; + } + *soc = oc_find(oc->oc_names[0]); + if ( code ) { + ldap_objectclass_free(oc); + } else { + ldap_memfree(oc); + } + return 0; +} diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index 91b84b741d..0d48e5118b 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -234,6 +234,11 @@ LDAP_SLAPD_F (int) at_next LDAP_P(( AttributeType **at )); LDAP_SLAPD_F (void) at_unparse LDAP_P(( BerVarray *bva, AttributeType *start, AttributeType *end, int system )); +LDAP_SLAPD_F (int) register_at LDAP_P(( + char *at, + AttributeDescription **ad, + int dupok )); + /* * attr.c */ @@ -1236,6 +1241,11 @@ LDAP_SLAPD_F (int) oc_schema_info( Entry *e ); LDAP_SLAPD_F (void) oc_unparse LDAP_P(( BerVarray *bva, ObjectClass *start, ObjectClass *end, int system )); +LDAP_SLAPD_F (int) register_oc LDAP_P(( + char *desc, + ObjectClass **oc, + int dupok )); + /* * oidm.c */ -- 2.39.5