From 119cbcaba43185e483b74b359e56fcf5d921404f Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Mon, 21 Mar 2005 08:31:48 +0000
Subject: [PATCH] Schema unparsing support

---
 servers/slapd/at.c                |  76 +++++--
 servers/slapd/back-monitor/init.c |   4 +-
 servers/slapd/bconfig.c           | 342 ++++++++++++++++++++++--------
 servers/slapd/config.c            |   6 +-
 servers/slapd/config.h            |   3 +
 servers/slapd/cr.c                |  67 +++++-
 servers/slapd/oc.c                |  85 ++++++--
 servers/slapd/oidm.c              |  47 ++--
 servers/slapd/overlays/lastmod.c  |   4 +-
 servers/slapd/overlays/pcache.c   |   2 +-
 servers/slapd/overlays/ppolicy.c  |   2 +-
 servers/slapd/proto-slap.h        |  28 ++-
 servers/slapd/schema_prep.c       |   4 +-
 servers/slapd/schemaparse.c       |  15 +-
 servers/slapd/slap.h              |  11 +-
 15 files changed, 529 insertions(+), 167 deletions(-)

diff --git a/servers/slapd/at.c b/servers/slapd/at.c
index 190ecf86b9..4ff083f075 100644
--- a/servers/slapd/at.c
+++ b/servers/slapd/at.c
@@ -58,8 +58,8 @@ struct aindexrec {
 
 static Avlnode	*attr_index = NULL;
 static Avlnode	*attr_cache = NULL;
-static LDAP_SLIST_HEAD(ATList, slap_attribute_type) attr_list
-	= LDAP_SLIST_HEAD_INITIALIZER(&attr_list);
+static LDAP_STAILQ_HEAD(ATList, slap_attribute_type) attr_list
+	= LDAP_STAILQ_HEAD_INITIALIZER(attr_list);
 
 int at_oc_cache;
 
@@ -210,9 +210,9 @@ at_destroy( void )
 	AttributeType *a;
 	avl_free(attr_index, ldap_memfree);
 
-	while( !LDAP_SLIST_EMPTY(&attr_list) ) {
-		a = LDAP_SLIST_FIRST(&attr_list);
-		LDAP_SLIST_REMOVE_HEAD(&attr_list, sat_next);
+	while( !LDAP_STAILQ_EMPTY(&attr_list) ) {
+		a = LDAP_STAILQ_FIRST(&attr_list);
+		LDAP_STAILQ_REMOVE_HEAD(&attr_list, sat_next);
 
 		if (a->sat_subtypes) ldap_memfree(a->sat_subtypes);
 		ad_destroy(a->sat_ad);
@@ -230,7 +230,7 @@ at_start( AttributeType **at )
 {
 	assert( at );
 
-	*at = LDAP_SLIST_FIRST(&attr_list);
+	*at = LDAP_STAILQ_FIRST(&attr_list);
 
 	return (*at != NULL);
 }
@@ -244,7 +244,7 @@ at_next( AttributeType **at )
 	{
 		AttributeType *tmp = NULL;
 
-		LDAP_SLIST_FOREACH(tmp,&attr_list,sat_next) {
+		LDAP_STAILQ_FOREACH(tmp,&attr_list,sat_next) {
 			if ( tmp == *at ) {
 				break;
 			}
@@ -254,7 +254,7 @@ at_next( AttributeType **at )
 	}
 #endif
 
-	*at = LDAP_SLIST_NEXT(*at,sat_next);
+	*at = LDAP_STAILQ_NEXT(*at,sat_next);
 
 	return (*at != NULL);
 }
@@ -269,9 +269,6 @@ at_insert(
 	struct aindexrec	*air;
 	char			**names;
 
-	LDAP_SLIST_NEXT( sat, sat_next ) = NULL;
-	LDAP_SLIST_INSERT_HEAD( &attr_list, sat, sat_next );
-
 	if ( sat->sat_oid ) {
 		air = (struct aindexrec *)
 			ch_calloc( 1, sizeof(struct aindexrec) );
@@ -307,6 +304,8 @@ at_insert(
 		}
 	}
 
+	LDAP_STAILQ_INSERT_TAIL( &attr_list, sat, sat_next );
+
 	return 0;
 }
 
@@ -314,6 +313,7 @@ int
 at_add(
     LDAPAttributeType	*at,
 	int				user,
+	AttributeType	**rsat,
     const char		**err )
 {
 	AttributeType	*sat;
@@ -596,6 +596,8 @@ at_add(
 	}
 
 	code = at_insert(sat,err);
+	if ( code == 0 && rsat )
+		*rsat = sat;
 	return code;
 }
 
@@ -618,6 +620,53 @@ at_index_print( void )
 }
 #endif
 
+void
+at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
+{
+	AttributeType *at;
+	int i, num;
+	struct berval bv, *bva = NULL, idx;
+	char ibuf[32], *ptr;
+
+	if ( !start )
+		start = LDAP_STAILQ_FIRST( &attr_list );
+
+	/* count the result size */
+	i = 0;
+	for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
+		if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
+		i++;
+	}
+	if (!i) return;
+
+	num = i;
+	bva = ch_malloc( (num+1) * sizeof(struct berval) );
+	BER_BVZERO( bva );
+	idx.bv_val = ibuf;
+	if ( sys ) {
+		idx.bv_len = 0;
+		ibuf[0] = '\0';
+	}
+	i = 0;
+	for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
+		if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
+		if ( ldap_attributetype2bv( &at->sat_atype, &bv ) == NULL ) {
+			ber_bvarray_free( bva );
+		}
+		if ( !sys ) {
+			idx.bv_len = sprintf(idx.bv_val, "{%02d}", i);
+		}
+		bva[i].bv_len = idx.bv_len + bv.bv_len;
+		bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
+		strcpy( bva[i].bv_val, ibuf );
+		strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
+		i++;
+		bva[i].bv_val = NULL;
+		ldap_memfree( bv.bv_val );
+	}
+	*res = bva;
+}
+
 int
 at_schema_info( Entry *e )
 {
@@ -626,15 +675,14 @@ at_schema_info( Entry *e )
 	struct berval	val;
 	struct berval	nval;
 
-	LDAP_SLIST_FOREACH(at,&attr_list,sat_next) {
+	LDAP_STAILQ_FOREACH(at,&attr_list,sat_next) {
 		if( at->sat_flags & SLAP_AT_HIDE ) continue;
 
 		if ( ldap_attributetype2bv( &at->sat_atype, &val ) == NULL ) {
 			return -1;
 		}
 
-		nval.bv_val = at->sat_oid;
-		nval.bv_len = strlen(at->sat_oid);
+		ber_str2bv( at->sat_oid, 0, 0, &nval );
 
 		if( attr_merge_one( e, ad_attributeTypes, &val, &nval ) )
 		{
diff --git a/servers/slapd/back-monitor/init.c b/servers/slapd/back-monitor/init.c
index 20335f602e..8747e2a891 100644
--- a/servers/slapd/back-monitor/init.c
+++ b/servers/slapd/back-monitor/init.c
@@ -1035,7 +1035,7 @@ monitor_back_db_init(
 			return -1;
 		}
 
-		code = at_add(at, 0, &err);
+		code = at_add(at, 0, NULL, &err);
 		if ( code ) {
 			Debug( LDAP_DEBUG_ANY, "monitor_back_db_init: "
 				"%s in attributeType \"%s\"\n",
@@ -1078,7 +1078,7 @@ monitor_back_db_init(
 			return -1;
 		}
 
-		code = oc_add(oc, 0, &err);
+		code = oc_add(oc, 0, NULL, &err);
 		if ( code ) {
 			Debug( LDAP_DEBUG_ANY,
 				"objectclass \"%s\": %s \"%s\"\n" ,
diff --git a/servers/slapd/bconfig.c b/servers/slapd/bconfig.c
index a62876af53..58e6dd6b00 100644
--- a/servers/slapd/bconfig.c
+++ b/servers/slapd/bconfig.c
@@ -36,6 +36,7 @@
 #include "config.h"
 
 static struct berval config_rdn = BER_BVC("cn=config");
+static struct berval schema_rdn = BER_BVC("cn=schema");
 
 #ifdef SLAPD_MODULES
 typedef struct modpath_s {
@@ -43,16 +44,18 @@ typedef struct modpath_s {
 	struct berval mp_path;
 	BerVarray mp_loads;
 } ModPaths;
+
+static ModPaths modpaths, *modlast = &modpaths, *modcur = &modpaths;
 #endif
 
 typedef struct ConfigFile {
 	struct ConfigFile *c_sibs;
 	struct ConfigFile *c_kids;
 	struct berval c_file;
-#ifdef SLAPD_MODULES
-	ModPaths c_modpaths;
-	ModPaths *c_modlast;
-#endif
+	AttributeType *c_at_head, *c_at_tail;
+	ContentRule *c_cr_head, *c_cr_tail;
+	ObjectClass *c_oc_head, *c_oc_tail;
+	OidMacro *c_om_head, *c_om_tail;
 	BerVarray c_dseFiles;
 } ConfigFile;
 
@@ -63,6 +66,7 @@ typedef struct CfOcInfo {
 } CfOcInfo;
 
 typedef struct CfEntryInfo {
+	struct CfEntryInfo *ce_parent;
 	struct CfEntryInfo *ce_sibs;
 	struct CfEntryInfo *ce_kids;
 	Entry *ce_entry;
@@ -96,7 +100,7 @@ static AttributeDescription *cfAd_backend, *cfAd_database, *cfAd_overlay,
 	*cfAd_include;
 
 static ObjectClass *cfOc_schema, *cfOc_global, *cfOc_backend, *cfOc_database,
-	*cfOc_include, *cfOc_overlay;
+	*cfOc_include, *cfOc_overlay, *cfOc_module;
 
 static ConfigFile cf_prv, *cfn = &cf_prv;
 
@@ -227,9 +231,12 @@ ConfigTable config_back_cf_table[] = {
 			"DESC 'File for slapd command line options' "
 			"EQUALITY caseIgnoreMatch "
 			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
-	/* Use standard 'attributeTypes' attr */
 	{ "attribute",	"attribute", 2, 0, 9, ARG_PAREN|ARG_MAGIC|CFG_ATTR,
-		&config_generic, NULL, NULL, NULL },
+		&config_generic, "( OLcfgAt:4 NAME 'olcAttributeTypes' "
+			"DESC 'OpenLDAP attributeTypes' "
+			"EQUALITY objectIdentifierFirstComponentMatch "
+			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.3 X-ORDERED 'VALUES' )",
+				NULL, NULL },
 	{ "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
 		&config_generic, "( OLcfgAt:5 NAME 'olcAttributeOptions' "
 			"EQUALITY caseIgnoreMatch "
@@ -276,9 +283,12 @@ ConfigTable config_back_cf_table[] = {
 		&config_disallows, "( OLcfgAt:15 NAME 'olcDisallows' "
 			"EQUALITY caseIgnoreMatch "
 			"SYNTAX OMsDirectoryString )", NULL, NULL },
-	/* use standard schema */
 	{ "ditcontentrule",	NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT,
-		&config_generic, NULL, NULL, NULL },
+		&config_generic, "( OLcfgAt:16 NAME 'olcDitContentRules' "
+			"DESC 'OpenLDAP DIT content rules' "
+			"EQUALITY objectIdentifierFirstComponentMatch "
+			"SYNTAX 1.3.6.1.4.1.1466.115.121.1.16 X-ORDERED 'VALUES' )",
+			NULL, NULL },
 	{ "gentlehup", "on|off", 2, 2, 0,
 #ifdef SIGHUP
 		ARG_ON_OFF, &global_gentlehup,
@@ -340,9 +350,12 @@ ConfigTable config_back_cf_table[] = {
 #endif
 		"( OLcfgAt:31 NAME 'olcModulePath' "
 			"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
-	/* use standard schema */
 	{ "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
-		&config_generic, NULL, NULL, NULL },
+		&config_generic, "( OLcfgAt:32 NAME 'olcObjectClasses' "
+		"DESC 'OpenLDAP object classes' "
+		"EQUALITY objectIdentifierFirstComponentMatch "
+		"SYNTAX 1.3.6.1.4.1.1466.115.121.1.37 X-ORDERED 'VALUES' )",
+			NULL, NULL },
 	{ "objectidentifier", NULL,	0, 0, 0, ARG_MAGIC|CFG_OID,
 		&config_generic, "( OLcfgAt:33 NAME 'olcObjectIdentifier' "
 			"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
@@ -578,7 +591,7 @@ static ConfigOCs cf_ocs[] = {
 		 "olcDefaultSearchBase $ olcDisallows $ olcGentleHUP $ "
 		 "olcIdleTimeout $ olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
 		 "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
-		 "olcLogLevel $ olcModuleLoad $ olcModulePath $ olcObjectIdentifier $ "
+		 "olcLogLevel $ olcModulePath $ olcObjectIdentifier $ "
 		 "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
 		 "olcPlugin $ olcPluginLogFile $ olcReadOnly $ olcReferral $ "
 		 "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
@@ -594,8 +607,8 @@ static ConfigOCs cf_ocs[] = {
 		"NAME 'olcSchemaConfig' "
 		"DESC 'OpenLDAP schema object' "
 		"SUP olcConfig STRUCTURAL "
-		"MAY ( olcObjectIdentifier $ attributeTypes $ objectClasses $ "
-		 "ditContentRules ) )", Cft_Schema, &cfOc_schema },
+		"MAY ( olcObjectIdentifier $ olcAttributeTypes $ olcObjectClasses $ "
+		 "olcDitContentRules ) )", Cft_Schema, &cfOc_schema },
 	{ "( OLcfgOc:4 "
 		"NAME 'olcBackendConfig' "
 		"DESC 'OpenLDAP Backend-specific options' "
@@ -620,8 +633,16 @@ static ConfigOCs cf_ocs[] = {
 		"NAME 'olcIncludeFile' "
 		"DESC 'OpenLDAP configuration include file' "
 		"SUP olcConfig STRUCTURAL "
-		"MAY ( olcInclude $ olcModuleLoad $ olcModulePath $ olcRootDSE ) )",
+		"MAY ( olcInclude $ olcRootDSE ) )",
 		Cft_Include, &cfOc_include },
+#ifdef SLAPD_MODULES
+	{ "( OLcfgOc:8 "
+		"NAME 'olcModuleList' "
+		"DESC 'OpenLDAP dynamic module info' "
+		"SUP olcConfig STRUCTURAL "
+		"MUST olcModuleLoad  )",
+		Cft_Module, &cfOc_module },
+#endif
 	{ NULL, 0, NULL }
 };
 
@@ -687,11 +708,51 @@ config_generic(ConfigArgs *c) {
 		case CFG_DEPTH:
 			c->value_int = c->be->be_max_deref_depth;
 			break;
-		case CFG_OID:
-			oidm_unparse( &c->rvalue_vals );
+		case CFG_OID: {
+			ConfigFile *cf = c->private;
+			if ( !cf )
+				oidm_unparse( &c->rvalue_vals, NULL, NULL, 1 );
+			else if ( cf->c_om_head )
+				oidm_unparse( &c->rvalue_vals, cf->c_om_head,
+					cf->c_om_tail, 0 );
+			if ( !c->rvalue_vals )
+				rc = 1;
+			}
+			break;
+		case CFG_OC: {
+			ConfigFile *cf = c->private;
+			if ( !cf )
+				oc_unparse( &c->rvalue_vals, NULL, NULL, 1 );
+			else if ( cf->c_oc_head )
+				oc_unparse( &c->rvalue_vals, cf->c_oc_head,
+					cf->c_oc_tail, 0 );
 			if ( !c->rvalue_vals )
 				rc = 1;
+			}
 			break;
+		case CFG_ATTR: {
+			ConfigFile *cf = c->private;
+			if ( !cf )
+				at_unparse( &c->rvalue_vals, NULL, NULL, 1 );
+			else if ( cf->c_at_head )
+				at_unparse( &c->rvalue_vals, cf->c_at_head,
+					cf->c_at_tail, 0 );
+			if ( !c->rvalue_vals )
+				rc = 1;
+			}
+			break;
+		case CFG_DIT: {
+			ConfigFile *cf = c->private;
+			if ( !cf )
+				cr_unparse( &c->rvalue_vals, NULL, NULL, 1 );
+			else if ( cf->c_cr_head )
+				cr_unparse( &c->rvalue_vals, cf->c_cr_head,
+					cf->c_cr_tail, 0 );
+			if ( !c->rvalue_vals )
+				rc = 1;
+			}
+			break;
+			
 		case CFG_CHECK:
 			c->value_int = global_schemacheck;
 			break;
@@ -725,7 +786,7 @@ config_generic(ConfigArgs *c) {
 				c->value_string = ch_strdup( c->be->be_replogfile );
 			break;
 		case CFG_ROOTDSE: {
-			ConfigFile *cf = (ConfigFile *)c->line;
+			ConfigFile *cf = c->private;
 			if ( cf->c_dseFiles ) {
 				value_add( &c->rvalue_vals, cf->c_dseFiles );
 			} else {
@@ -750,26 +811,24 @@ config_generic(ConfigArgs *c) {
 			break;
 #ifdef SLAPD_MODULES
 		case CFG_MODLOAD: {
-			ConfigFile *cf = (ConfigFile *)c->line;
-			ModPaths *mp;
-			for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
-				int j;
-				if (!mp->mp_loads) continue;
-				for (j=0; !BER_BVISNULL(&mp->mp_loads[j]); j++) {
+			ModPaths *mp = c->private;
+			if (mp->mp_loads) {
+				int i;
+				for (i=0; !BER_BVISNULL(&mp->mp_loads[i]); i++) {
 					struct berval bv;
 					bv.bv_val = c->log;
-					bv.bv_len = sprintf( bv.bv_val, "{%d}{%d}%s", i, j,
-						mp->mp_loads[j].bv_val );
+					bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
+						mp->mp_loads[i].bv_val );
 					value_add_one( &c->rvalue_vals, &bv );
 				}
 			}
+
 			rc = c->rvalue_vals ? 0 : 1;
 			}
 			break;
 		case CFG_MODPATH: {
-			ConfigFile *cf = (ConfigFile *)c->line;
 			ModPaths *mp;
-			for (i=0, mp=&cf->c_modpaths; mp; mp=mp->mp_next, i++) {
+			for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
 				struct berval bv;
 				if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
 					continue;
@@ -898,20 +957,41 @@ config_generic(ConfigArgs *c) {
 			c->be->be_max_deref_depth = c->value_int;
 			break;
 
-		case CFG_OID:
-			if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1)) return(0);
+		case CFG_OID: {
+			OidMacro *om;
+
+			if(parse_oidm(c->fname, c->lineno, c->argc, c->argv, 1, &om))
+				return(1);
+			if (!cfn->c_om_head) cfn->c_om_head = om;
+			cfn->c_om_tail = om;
+			}
 			break;
 
-		case CFG_OC:
-			if(parse_oc(c->fname, c->lineno, p, c->argv)) return(1);
+		case CFG_OC: {
+			ObjectClass *oc;
+
+			if(parse_oc(c->fname, c->lineno, p, c->argv, &oc)) return(1);
+			if (!cfn->c_oc_head) cfn->c_oc_head = oc;
+			cfn->c_oc_tail = oc;
+			}
 			break;
 
-		case CFG_DIT:
-			if(parse_cr(c->fname, c->lineno, p, c->argv)) return(1);
+		case CFG_DIT: {
+			ContentRule *cr;
+
+			if(parse_cr(c->fname, c->lineno, p, c->argv, &cr)) return(1);
+			if (!cfn->c_cr_head) cfn->c_cr_head = cr;
+			cfn->c_cr_tail = cr;
+			}
 			break;
 
-		case CFG_ATTR:
-			if(parse_at(c->fname, c->lineno, p, c->argv)) return(1);
+		case CFG_ATTR: {
+			AttributeType *at;
+
+			if(parse_at(c->fname, c->lineno, p, c->argv, &at)) return(1);
+			if (!cfn->c_at_head) cfn->c_at_head = at;
+			cfn->c_at_tail = at;
+			}
 			break;
 
 		case CFG_ATOPT:
@@ -1007,7 +1087,7 @@ config_generic(ConfigArgs *c) {
 			{
 				struct berval bv;
 				ber_str2bv(c->line, 0, 1, &bv);
-				ber_bvarray_add( &cfn->c_modlast->mp_loads, &bv );
+				ber_bvarray_add( &modcur->mp_loads, &bv );
 			}
 			break;
 
@@ -1017,16 +1097,18 @@ config_generic(ConfigArgs *c) {
 			{
 				ModPaths *mp;
 
-				if (!cfn->c_modpaths.mp_loads) {
-					mp = &cfn->c_modpaths;
+				if (!modpaths.mp_loads) {
+					mp = &modpaths;
 				} else {
 					mp = ch_malloc( sizeof( ModPaths ));
-					cfn->c_modlast->mp_next = mp;
+					modlast->mp_next = mp;
 				}
 				ber_str2bv(c->argv[1], 0, 1, &mp->mp_path);
 				mp->mp_next = NULL;
 				mp->mp_loads = NULL;
-				cfn->c_modlast = mp;
+				modlast = mp;
+				if ( c->op == SLAP_CONFIG_ADD )
+					modcur = mp;
 			}
 			
 			break;
@@ -1064,8 +1146,8 @@ config_generic(ConfigArgs *c) {
 static int
 config_fname(ConfigArgs *c) {
 	if(c->op == SLAP_CONFIG_EMIT) {
-		if (c->line) {
-			ConfigFile *cf = (ConfigFile *)c->line;
+		if (c->private) {
+			ConfigFile *cf = c->private;
 			value_add_one( &c->rvalue_vals, &cf->c_file );
 			return 0;
 		}
@@ -1904,9 +1986,6 @@ config_include(ConfigArgs *c) {
 		return 1;
 	}
 	cf = ch_calloc( 1, sizeof(ConfigFile));
-#ifdef SLAPD_MODULES
-	cf->c_modlast = &cf->c_modpaths;
-#endif
 	if ( cfn->c_kids ) {
 		for (cf2=cfn->c_kids; cf2 && cf2->c_sibs; cf2=cf2->c_sibs) ;
 		cf2->c_sibs = cf;
@@ -2933,6 +3012,13 @@ config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
 			rc = LDAP_CONSTRAINT_VIOLATION;
 			goto leave;
 		}
+#ifdef SLAPD_MODULES
+	case Cft_Module:
+		if ( !last || last->ce_type != Cft_Global ) {
+			rc = LDAP_CONSTRAINT_VIOLATION;
+			goto leave;
+		}
+#endif
 		break;
 	}
 
@@ -2940,10 +3026,15 @@ config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
 	 * performing any set actions.
 	 */
 	switch (colst[0]->co_type) {
-	case Cft_Global:	/* */
 	case Cft_Schema:
+		/* The cn=schema entry is all hardcoded, so never reparse it */
+		if (last->ce_type == Cft_Global )
+			goto ok;
+		/* FALLTHRU */
+	case Cft_Global:
 		ca.be = cfb->cb_be;
 		break;
+
 	case Cft_Backend:
 		if ( last->ce_type == Cft_Backend )
 			ca.bi = last->ce_bi;
@@ -2968,6 +3059,27 @@ config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
 		if ( !rs )	/* ignored */
 			break;
 		type_ad = cfAd_include;
+#ifdef SLAPD_MODULES
+	case Cft_Module: {
+		ModPaths *mp;
+		char *ptr;
+		ptr = strchr( e->e_name.bv_val, '{' );
+		if ( !ptr ) {
+			rc = LDAP_NAMING_VIOLATION;
+			goto leave;
+		}
+		j = atoi(ptr+1);
+		for (i=0, mp=&modpaths; mp && i<j; mp=mp->mp_next);
+		/* There is no corresponding modpath for this load? */
+		if ( i != j ) {
+			rc = LDAP_NAMING_VIOLATION;
+			goto leave;
+		}
+		module_path( mp->mp_path.bv_val );
+		ca.private = mp;
+		}
+		break;
+#endif
 	}
 	init_config_argv( &ca );
 	if ( type_ad ) {
@@ -3019,7 +3131,9 @@ config_add_internal( CfBackInfo *cfb, Entry *e, SlapReply *rs )
 			if ( rc ) goto leave;
 		}
 	}
+ok:
 	ce = ch_calloc( 1, sizeof(CfEntryInfo) );
+	ce->ce_parent = last;
 	ce->ce_entry = entry_dup( e );
 	ce->ce_entry->e_private = ce;
 	ce->ce_type = colst[0]->co_type;
@@ -3146,13 +3260,22 @@ out:
 }
 
 static Entry *
-config_alloc_entry( struct berval *pdn, struct berval *rdn )
+config_alloc_entry( CfEntryInfo *parent, struct berval *rdn )
 {
 	Entry *e = ch_calloc( 1, sizeof(Entry) );
 	CfEntryInfo *ce = ch_calloc( 1, sizeof(CfEntryInfo) );
+	struct berval pdn;
+
 	e->e_private = ce;
 	ce->ce_entry = e;
-	build_new_dn( &e->e_name, pdn, rdn, NULL );
+	ce->ce_parent = parent;
+	if ( parent ) {
+		pdn = parent->ce_entry->e_nname;
+	} else {
+		BER_BVZERO( &pdn );
+	}
+
+	build_new_dn( &e->e_name, &pdn, rdn, NULL );
 	ber_dupbv( &e->e_nname, &e->e_name );
 	return e;
 }
@@ -3247,21 +3370,24 @@ config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
 }
 
 static CfEntryInfo *
-config_build_includes( ConfigArgs *c, Entry *parent,
+config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
 	Operation *op, SlapReply *rs )
 {
 	Entry *e;
 	int i;
-	ConfigFile *cf = (ConfigFile *)c->line;
-	CfEntryInfo *ce, *ceparent, *ceprev;
+	ConfigFile *cf = c->private;
+	CfEntryInfo *ce, *ceprev;
 
-	ceparent = parent->e_private;
+	if ( ceparent->ce_kids ) {
+		for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
+			ceprev = ceprev->ce_sibs );
+	}
 
 	for (i=0; cf; cf=cf->c_sibs, i++) {
 		c->value_dn.bv_val = c->log;
-		c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include{%d}", i);
-		e = config_alloc_entry( &parent->e_nname, &c->value_dn );
-		c->line = (char *)cf;
+		c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=include{%02d}", i);
+		e = config_alloc_entry( ceparent, &c->value_dn );
+		c->private = cf;
 		config_build_entry( c, e, cfOc_include, &c->value_dn,
 			c->bi->bi_cf_table, NO_TABLE );
 		op->ora_e = e;
@@ -3276,13 +3402,54 @@ config_build_includes( ConfigArgs *c, Entry *parent,
 		}
 		ceprev = ce;
 		if ( cf->c_kids ) {
-			c->line = (char *)cf->c_kids;
-			config_build_includes( c, e, op, rs );
+			c->private = cf->c_kids;
+			config_build_includes( c, ce, op, rs );
 		}
 	}
 	return ce;
 }
 
+#ifdef SLAPD_MODULES
+
+static CfEntryInfo *
+config_build_modules( ConfigArgs *c, CfEntryInfo *ceparent,
+	Operation *op, SlapReply *rs )
+{
+	Entry *e;
+	int i;
+	CfEntryInfo *ce, *ceprev;
+	ModPaths *mp;
+
+	if ( ceparent->ce_kids ) {
+		for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
+			ceprev = ceprev->ce_sibs );
+	}
+
+	for (i=0, mp=&modpaths; mp; mp=mp->mp_next, i++) {
+		if ( BER_BVISNULL( &mp->mp_path ) && !mp->mp_loads )
+			continue;
+		c->value_dn.bv_val = c->log;
+		c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn=module{%02d}", i);
+		e = config_alloc_entry( ceparent, &c->value_dn );
+		ce = e->e_private;
+		ce->ce_type = Cft_Include;
+		c->private = mp;
+		config_build_entry( c, e, cfOc_module, &c->value_dn,
+			c->bi->bi_cf_table, NO_TABLE );
+		op->ora_e = e;
+		op->o_bd->be_add( op, rs );
+		ce->ce_bi = c->bi;
+		if ( !ceparent->ce_kids ) {
+			ceparent->ce_kids = ce;
+		} else {
+			ceprev->ce_sibs = ce;
+		}
+		ceprev = ce;
+	}
+	return ce;
+}
+#endif
+
 static int
 config_back_db_open( BackendDB *be )
 {
@@ -3319,15 +3486,15 @@ config_back_db_open( BackendDB *be )
 	rdn = config_rdn;
 	e = config_alloc_entry( NULL, &rdn );
 	ce = e->e_private;
+	ce->ce_type = Cft_Global;
 	cfb->cb_root = ce;
 	c.be = be;
 	c.bi = be->bd_info;
-	c.line = (char *)cfb->cb_config;
+	c.private = cfb->cb_config;
 	ct = c.bi->bi_cf_table;
 	config_build_entry( &c, e, cfOc_global, &rdn, ct, NO_TABLE );
 	op->ora_e = e;
 	op->o_bd->be_add( op, &rs );
-	ce->ce_type = Cft_Global;
 	ce->ce_bi = c.bi;
 
 	parent = e;
@@ -3337,12 +3504,33 @@ config_back_db_open( BackendDB *be )
 	 * schema, read-only. Child objects will contain runtime loaded schema
 	 * files.  FIXME
 	 */
+	rdn = schema_rdn;
+	e = config_alloc_entry( ceparent, &rdn );
+	ce = e->e_private;
+	ce->ce_type = Cft_Schema;
+	c.private = NULL;
+	config_build_entry( &c, e, cfOc_schema, &rdn, ct, NO_TABLE );
+	op->ora_e = e;
+	op->o_bd->be_add( op, &rs );
+	if ( !ceparent->ce_kids ) {
+		ceparent->ce_kids = ce;
+	} else {
+		ceprev->ce_sibs = ce;
+	}
+	ceprev = ce;
 
 	/* Create includeFile nodes... */
 	if ( cfb->cb_config->c_kids ) {
-		c.line = (char *)cfb->cb_config->c_kids;
-		ceprev = config_build_includes( &c, parent, op, &rs );
+		c.private = cfb->cb_config->c_kids;
+		ceprev = config_build_includes( &c, ceparent, op, &rs );
+	}
+
+#ifdef SLAPD_MODULES
+	/* Create Module nodes... */
+	if ( modpaths.mp_loads ) {
+		ceprev = config_build_includes( &c, ceparent, op, &rs );
 	}
+#endif
 
 	/* Create backend nodes. Skip if they don't provide a cf_table.
 	 * There usually aren't any of these.
@@ -3356,7 +3544,7 @@ config_back_db_open( BackendDB *be )
 
 		rdn.bv_val = c.log;
 		rdn.bv_len = sprintf(rdn.bv_val, "%s=%s", cfAd_backend->ad_cname.bv_val, bi->bi_type);
-		e = config_alloc_entry( &parent->e_nname, &rdn );
+		e = config_alloc_entry( ceparent, &rdn );
 		ce = e->e_private;
 		ce->ce_type = Cft_Backend;
 		ce->ce_bi = bi;
@@ -3389,7 +3577,7 @@ config_back_db_open( BackendDB *be )
 		rdn.bv_val = c.log;
 		rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s", cfAd_database->ad_cname.bv_val,
 			i, bi->bi_type);
-		e = config_alloc_entry( &parent->e_nname, &rdn );
+		e = config_alloc_entry( ceparent, &rdn );
 		ce = e->e_private;
 		c.be = bptr;
 		c.bi = bi;
@@ -3416,7 +3604,7 @@ config_back_db_open( BackendDB *be )
 				rdn.bv_val = c.log;
 				rdn.bv_len = sprintf(rdn.bv_val, "%s={%0x}%s",
 					cfAd_overlay->ad_cname.bv_val, j, on->on_bi.bi_type );
-				oe = config_alloc_entry( &e->e_nname, &rdn );
+				oe = config_alloc_entry( opar, &rdn );
 				ce = oe->e_private;
 				c.be = bptr;
 				c.bi = &on->on_bi;
@@ -3473,15 +3661,11 @@ config_back_db_init( Backend *be )
 static struct {
 	char *name;
 	AttributeDescription **desc;
-	AttributeDescription *sub;
 } ads[] = {
-	{ "attribute", NULL, NULL },
-	{ "backend", &cfAd_backend, NULL },
-	{ "database", &cfAd_database, NULL },
-	{ "ditcontentrule", NULL, NULL },
-	{ "include", &cfAd_include, NULL },
-	{ "objectclass", NULL, NULL },
-	{ "overlay", &cfAd_overlay, NULL },
+	{ "backend", &cfAd_backend },
+	{ "database", &cfAd_database },
+	{ "include", &cfAd_include },
+	{ "overlay", &cfAd_overlay },
 	{ NULL, NULL, NULL }
 };
 
@@ -3547,7 +3731,7 @@ config_back_initialize( BackendInfo *bi )
 	for (i=0; OidMacros[i].name; i++ ) {
 		argv[1] = OidMacros[i].name;
 		argv[2] = OidMacros[i].oid;
-		parse_oidm( "slapd", i, 3, argv, 0 );
+		parse_oidm( "slapd", i, 3, argv, 0, NULL );
 	}
 
 	bi->bi_cf_table = ct;
@@ -3556,18 +3740,10 @@ config_back_initialize( BackendInfo *bi )
 	if ( i ) return i;
 
 	/* set up the notable AttributeDescriptions */
-	ads[0].sub = slap_schema.si_ad_attributeTypes;
-	ads[3].sub = slap_schema.si_ad_ditContentRules;
-	ads[5].sub = slap_schema.si_ad_objectClasses;
-
 	i = 0;
 	for (;ct->name;ct++) {
 		if (strcmp(ct->name, ads[i].name)) continue;
-		if (ads[i].sub) {
-			ct->ad = ads[i].sub;
-		} else {
-			*ads[i].desc = ct->ad;
-		}
+		*ads[i].desc = ct->ad;
 		i++;
 		if (!ads[i].name) break;
 	}
diff --git a/servers/slapd/config.c b/servers/slapd/config.c
index 4ca60728b6..e1808dff99 100644
--- a/servers/slapd/config.c
+++ b/servers/slapd/config.c
@@ -404,7 +404,7 @@ init_config_attrs(ConfigTable *ct) {
 				ct[i].attribute, ldap_scherr2str(code), err );
 			return code;
 		}
-		code = at_add( at, 0, &err );
+		code = at_add( at, 0, NULL, &err );
 		if ( code && code != SLAP_SCHERR_ATTR_DUP ) {
 			fprintf( stderr, "init_config_attrs: AttributeType \"%s\": %s, %s\n",
 				ct[i].attribute, scherr2str(code), err );
@@ -438,7 +438,7 @@ init_config_ocs( ConfigOCs *ocs ) {
 				ocs[i].def, ldap_scherr2str(code), err );
 			return code;
 		}
-		code = oc_add(oc,0,&err);
+		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].def, scherr2str(code), err );
@@ -551,7 +551,7 @@ read_config_file(const char *fname, int depth, ConfigArgs *cf)
 			continue;
 		}
 
-		c->op = LDAP_MOD_ADD;
+		c->op = SLAP_CONFIG_ADD;
 
 		ct = config_find_keyword( config_back_cf_table, c );
 		if ( ct ) {
diff --git a/servers/slapd/config.h b/servers/slapd/config.h
index 821dd313c5..15d41f2349 100644
--- a/servers/slapd/config.h
+++ b/servers/slapd/config.h
@@ -35,6 +35,7 @@ typedef enum {
 	Cft_Database,
 	Cft_Overlay,
 	Cft_Include,
+	Cft_Module
 } ConfigType;
 
 #define ARGS_USERLAND	0x00000fff
@@ -99,10 +100,12 @@ typedef struct config_args_s {
 	BerVarray rvalue_vals;
 	BerVarray rvalue_nvals;
 #define	SLAP_CONFIG_EMIT	0x2000	/* emit instead of set */
+#define SLAP_CONFIG_ADD		0x4000	/* config file add vs LDAP add */
 	int op;
 	int type;	/* ConfigTable.arg_type & ARGS_USERLAND */
 	BackendDB *be;
 	BackendInfo *bi;
+	void *private;	/* anything */
 } ConfigArgs;
 
 #define value_int values.v_int
diff --git a/servers/slapd/cr.c b/servers/slapd/cr.c
index 4459cb66b0..eb9178d7e7 100644
--- a/servers/slapd/cr.c
+++ b/servers/slapd/cr.c
@@ -30,8 +30,8 @@ struct cindexrec {
 };
 
 static Avlnode	*cr_index = NULL;
-static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
-	= LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
+static LDAP_STAILQ_HEAD(CRList, slap_content_rule) cr_list
+	= LDAP_STAILQ_HEAD_INITIALIZER(cr_list);
 
 static int
 cr_index_cmp(
@@ -103,9 +103,9 @@ cr_destroy( void )
 
 	avl_free(cr_index, ldap_memfree);
 
-	while( !LDAP_SLIST_EMPTY(&cr_list) ) {
-		c = LDAP_SLIST_FIRST(&cr_list);
-		LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
+	while( !LDAP_STAILQ_EMPTY(&cr_list) ) {
+		c = LDAP_STAILQ_FIRST(&cr_list);
+		LDAP_STAILQ_REMOVE_HEAD(&cr_list, scr_next);
 
 		cr_destroy_one( c );
 	}
@@ -120,9 +120,6 @@ cr_insert(
 	struct cindexrec	*cir;
 	char			**names;
 
-	LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
-	LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
-
 	if ( scr->scr_oid ) {
 		cir = (struct cindexrec *)
 			ch_calloc( 1, sizeof(struct cindexrec) );
@@ -171,6 +168,8 @@ cr_insert(
 		}
 	}
 
+	LDAP_STAILQ_INSERT_TAIL(&cr_list, scr, scr_next);
+
 	return 0;
 }
 
@@ -334,6 +333,7 @@ int
 cr_add(
     LDAPContentRule	*cr,
 	int user,
+	ContentRule **rscr,
     const char		**err
 )
 {
@@ -399,9 +399,58 @@ cr_add(
 	}
 
 	code = cr_insert(scr,err);
+	if ( code == 0 && rscr )
+		*rscr = scr;
 	return code;
 }
 
+void
+cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
+{
+	ContentRule *cr;
+	int i, num;
+	struct berval bv, *bva = NULL, idx;
+	char ibuf[32], *ptr;
+
+	if ( !start )
+		start = LDAP_STAILQ_FIRST( &cr_list );
+
+	/* count the result size */
+	i = 0;
+	for ( cr=start; cr && cr!=end; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+		if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
+		i++;
+	}
+	if (!i) return;
+
+	num = i;
+	bva = ch_malloc( (num+1) * sizeof(struct berval) );
+	BER_BVZERO( bva );
+	idx.bv_val = ibuf;
+	if ( sys ) {
+		idx.bv_len = 0;
+		ibuf[0] = '\0';
+	}
+	i = 0;
+	for ( cr=start; cr && cr!=end; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+		if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
+		if ( ldap_contentrule2bv( &cr->scr_crule, &bv ) == NULL ) {
+			ber_bvarray_free( bva );
+		}
+		if ( !sys ) {
+			idx.bv_len = sprintf(idx.bv_val, "{%02d}", i);
+		}
+		bva[i].bv_len = idx.bv_len + bv.bv_len;
+		bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
+		strcpy( bva[i].bv_val, ibuf );
+		strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
+		i++;
+		bva[i].bv_val = NULL;
+		ldap_memfree( bv.bv_val );
+	}
+	*res = bva;
+}
+
 int
 cr_schema_info( Entry *e )
 {
@@ -412,7 +461,7 @@ cr_schema_info( Entry *e )
 	struct berval	val;
 	struct berval	nval;
 
-	LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
+	LDAP_STAILQ_FOREACH(cr, &cr_list, scr_next) {
 		if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
 			return -1;
 		}
diff --git a/servers/slapd/oc.c b/servers/slapd/oc.c
index 4afb1e1751..b923725a9d 100644
--- a/servers/slapd/oc.c
+++ b/servers/slapd/oc.c
@@ -120,8 +120,8 @@ struct oindexrec {
 
 static Avlnode	*oc_index = NULL;
 static Avlnode	*oc_cache = NULL;
-static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
-	= LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
+static LDAP_STAILQ_HEAD(OCList, slap_object_class) oc_list
+	= LDAP_STAILQ_HEAD_INITIALIZER(oc_list);
 
 static int
 oc_index_cmp(
@@ -179,8 +179,8 @@ oc_bvfind( struct berval *ocname )
 	return( NULL );
 }
 
-static LDAP_SLIST_HEAD(OCUList, slap_object_class) oc_undef_list
-	= LDAP_SLIST_HEAD_INITIALIZER(&oc_undef_list);
+static LDAP_STAILQ_HEAD(OCUList, slap_object_class) oc_undef_list
+	= LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
 
 ObjectClass *
 oc_bvfind_undef( struct berval *ocname )
@@ -191,7 +191,7 @@ oc_bvfind_undef( struct berval *ocname )
 		return oc;
 	}
 
-	LDAP_SLIST_FOREACH( oc, &oc_undef_list, soc_next ) {
+	LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
 		int	d = oc->soc_cname.bv_len - ocname->bv_len;
 
 		if ( d ) {
@@ -214,8 +214,8 @@ oc_bvfind_undef( struct berval *ocname )
 	oc->soc_cname.bv_val = (char *)&oc[ 1 ];
 	AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
 
-	LDAP_SLIST_NEXT( oc, soc_next ) = NULL;
-	LDAP_SLIST_INSERT_HEAD( &oc_undef_list, oc, soc_next );
+	LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
+	LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
 
 	return oc;
 }
@@ -378,9 +378,9 @@ oc_destroy( void )
 	ObjectClass *o;
 
 	avl_free(oc_index, ldap_memfree);
-	while( !LDAP_SLIST_EMPTY(&oc_list) ) {
-		o = LDAP_SLIST_FIRST(&oc_list);
-		LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
+	while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
+		o = LDAP_STAILQ_FIRST(&oc_list);
+		LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
 
 		if (o->soc_sups) ldap_memfree(o->soc_sups);
 		if (o->soc_required) ldap_memfree(o->soc_required);
@@ -388,9 +388,9 @@ oc_destroy( void )
 		ldap_objectclass_free((LDAPObjectClass *)o);
 	}
 	
-	while( !LDAP_SLIST_EMPTY(&oc_undef_list) ) {
-		o = LDAP_SLIST_FIRST(&oc_undef_list);
-		LDAP_SLIST_REMOVE_HEAD(&oc_undef_list, soc_next);
+	while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
+		o = LDAP_STAILQ_FIRST(&oc_undef_list);
+		LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
 
 		ch_free( (ObjectClass *)o );
 	}
@@ -404,9 +404,6 @@ oc_insert(
 	struct oindexrec	*oir;
 	char			**names;
 
-	LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
-	LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
-
 	if ( soc->soc_oid ) {
 		oir = (struct oindexrec *)
 			ch_calloc( 1, sizeof(struct oindexrec) );
@@ -454,6 +451,8 @@ oc_insert(
 			names++;
 		}
 	}
+	LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
+
 
 	return 0;
 }
@@ -462,6 +461,7 @@ int
 oc_add(
     LDAPObjectClass	*oc,
 	int user,
+	ObjectClass		**rsoc,
     const char		**err )
 {
 	ObjectClass	*soc;
@@ -522,10 +522,61 @@ oc_add(
 
 	if( user && op ) return SLAP_SCHERR_CLASS_BAD_USAGE;
 
+	if( !user ) soc->soc_flags |= SLAP_OC_HARDCODE;
+
 	code = oc_insert(soc,err);
+	if ( code == 0 && rsoc )
+		*rsoc = soc;
 	return code;
 }
 
+void
+oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
+{
+	ObjectClass *oc;
+	int i, num;
+	struct berval bv, *bva = NULL, idx;
+	char ibuf[32], *ptr;
+
+	if ( !start )
+		start = LDAP_STAILQ_FIRST( &oc_list );
+
+	/* count the result size */
+	i = 0;
+	for ( oc=start; oc && oc!=end; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
+		if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
+		i++;
+	}
+	if (!i) return;
+
+	num = i;
+	bva = ch_malloc( (num+1) * sizeof(struct berval) );
+	BER_BVZERO( bva );
+	idx.bv_val = ibuf;
+	if ( sys ) {
+		idx.bv_len = 0;
+		ibuf[0] = '\0';
+	}
+	i = 0;
+	for ( oc=start; oc && oc!=end; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
+		if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
+		if ( ldap_objectclass2bv( &oc->soc_oclass, &bv ) == NULL ) {
+			ber_bvarray_free( bva );
+		}
+		if ( !sys ) {
+			idx.bv_len = sprintf(idx.bv_val, "{%02d}", i);
+		}
+		bva[i].bv_len = idx.bv_len + bv.bv_len;
+		bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
+		strcpy( bva[i].bv_val, ibuf );
+		strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
+		i++;
+		bva[i].bv_val = NULL;
+		ldap_memfree( bv.bv_val );
+	}
+	*res = bva;
+}
+
 int
 oc_schema_info( Entry *e )
 {
@@ -534,7 +585,7 @@ oc_schema_info( Entry *e )
 	struct berval	val;
 	struct berval	nval;
 
-	LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
+	LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
 		if( oc->soc_flags & SLAP_OC_HIDE ) continue;
 
 		if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
diff --git a/servers/slapd/oidm.c b/servers/slapd/oidm.c
index 3a20fcde43..99df108cda 100644
--- a/servers/slapd/oidm.c
+++ b/servers/slapd/oidm.c
@@ -25,8 +25,8 @@
 #include "slap.h"
 #include "lutil.h"
 
-static LDAP_SLIST_HEAD(OidMacroList, slap_oid_macro) om_list
-	= LDAP_SLIST_HEAD_INITIALIZER(om_list);
+static LDAP_STAILQ_HEAD(OidMacroList, slap_oid_macro) om_list
+	= LDAP_STAILQ_HEAD_INITIALIZER(om_list);
 
 /* Replace an OID Macro invocation with its full numeric OID.
  * If the macro is used with "macroname:suffix" append ".suffix"
@@ -42,7 +42,7 @@ oidm_find(char *oid)
 		return oid;
 	}
 
-	LDAP_SLIST_FOREACH( om, &om_list, som_next ) {
+	LDAP_STAILQ_FOREACH( om, &om_list, som_next ) {
 		BerVarray names = om->som_names;
 
 		if( names == NULL ) {
@@ -78,9 +78,9 @@ void
 oidm_destroy()
 {
 	OidMacro *om;
-	while( !LDAP_SLIST_EMPTY( &om_list )) {
-		om = LDAP_SLIST_FIRST( &om_list );
-		LDAP_SLIST_REMOVE_HEAD( &om_list, som_next );
+	while( !LDAP_STAILQ_EMPTY( &om_list )) {
+		om = LDAP_STAILQ_FIRST( &om_list );
+		LDAP_STAILQ_REMOVE_HEAD( &om_list, som_next );
 
 		ber_bvarray_free(om->som_names);
 		ber_bvarray_free(om->som_subs);
@@ -96,7 +96,8 @@ parse_oidm(
     int		lineno,
     int		argc,
     char 	**argv,
-	int		user )
+	int		user,
+	OidMacro **rom)
 {
 	char *oid;
 	OidMacro *om;
@@ -124,7 +125,6 @@ usage:	fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
 		return 1;
 	}
 
-	LDAP_SLIST_NEXT( om, som_next ) = NULL;
 	om->som_names = NULL;
 	om->som_subs = NULL;
 	ber_str2bv( argv[1], 0, 1, &bv );
@@ -147,40 +147,53 @@ usage:	fprintf( stderr, "\tObjectIdentifier <name> <oid>\n");
 	if ( !user )
 		om->som_flags |= SLAP_OM_HARDCODE;
 
-	LDAP_SLIST_INSERT_HEAD( &om_list, om, som_next );
+	LDAP_STAILQ_INSERT_TAIL( &om_list, om, som_next );
+	if ( rom ) *rom = om;
 	return 0;
 }
 
-void oidm_unparse( BerVarray *res )
+void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
 {
 	OidMacro *om;
 	int i, j, num;
 	struct berval bv, *bva = NULL, idx;
 	char ibuf[32], *ptr;
 
+	if ( !start )
+		start = LDAP_STAILQ_FIRST( &om_list );
+
 	/* count the result size */
 	i = 0;
-	LDAP_SLIST_FOREACH( om, &om_list, som_next ) {
+	for ( om=start; om && om!=end; om=LDAP_STAILQ_NEXT(om, som_next)) {
+		if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
 		for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
 		i += j;
 	}
 	num = i;
+	if (!i) return;
+
 	bva = ch_malloc( (num+1) * sizeof(struct berval) );
 	BER_BVZERO( bva+num );
 	idx.bv_val = ibuf;
-	LDAP_SLIST_FOREACH( om, &om_list, som_next ) {
-		for ( j=0; !BER_BVISNULL(&om->som_names[j]); j++ );
-		for ( i=num-j, j=0; i<num; i++,j++ ) {
-			idx.bv_len = sprintf(idx.bv_val, "{%d}", i );
+	if ( sys ) {
+		idx.bv_len = 0;
+		ibuf[0] = '\0';
+	}
+	for ( i=0,om=start; om && om!=end; om=LDAP_STAILQ_NEXT(om, som_next)) {
+		if ( sys && !(om->som_flags & SLAP_OM_HARDCODE)) continue;
+		for ( j=0; !BER_BVISNULL(&om->som_names[j]); i++,j++ ) {
+			if ( !sys ) {
+				idx.bv_len = sprintf(idx.bv_val, "{%02d}", i );
+			}
 			bva[i].bv_len = idx.bv_len + om->som_names[j].bv_len +
 				om->som_subs[j].bv_len + 1;
 			bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
 			ptr = lutil_strcopy( bva[i].bv_val, ibuf );
 			ptr = lutil_strcopy( ptr, om->som_names[j].bv_val );
 			*ptr++ = ' ';
-			ptr = lutil_strcopy( ptr, om->som_subs[j].bv_val );
+			strcpy( ptr, om->som_subs[j].bv_val );
 		}
-		num -= j;
+		if ( i>=num ) break;
 	}
 	*res = bva;
 }
diff --git a/servers/slapd/overlays/lastmod.c b/servers/slapd/overlays/lastmod.c
index 714a2d10fa..a645f7d8b4 100644
--- a/servers/slapd/overlays/lastmod.c
+++ b/servers/slapd/overlays/lastmod.c
@@ -781,7 +781,7 @@ lastmod_db_init(
 				return -1;
 			}
 	
-			code = at_add(at, 0, &err);
+			code = at_add(at, 0, NULL, &err);
 			if ( code ) {
 				Debug( LDAP_DEBUG_ANY, "lastmod_init: "
 					"%s in attributeType '%s'\n",
@@ -824,7 +824,7 @@ lastmod_db_init(
 				return -1;
 			}
 
-			code = oc_add(oc, 0, &err);
+			code = oc_add(oc, 0, NULL, &err);
 			if ( code ) {
 				Debug( LDAP_DEBUG_ANY,
 					"objectClass '%s': %s \"%s\"\n" ,
diff --git a/servers/slapd/overlays/pcache.c b/servers/slapd/overlays/pcache.c
index 7c9054a2df..f991d4987f 100644
--- a/servers/slapd/overlays/pcache.c
+++ b/servers/slapd/overlays/pcache.c
@@ -1925,7 +1925,7 @@ int pcache_init()
 			ldap_scherr2str(code), err );
 		return code;
 	}
-	code = at_add( at, 0, &err );
+	code = at_add( at, 0, NULL, &err );
 	if ( !code ) {
 		slap_str2ad( at->at_names[0], &ad_queryid, &err );
 	}
diff --git a/servers/slapd/overlays/ppolicy.c b/servers/slapd/overlays/ppolicy.c
index 44ff4f9bde..aa66dc8e6f 100644
--- a/servers/slapd/overlays/ppolicy.c
+++ b/servers/slapd/overlays/ppolicy.c
@@ -1797,7 +1797,7 @@ int ppolicy_init()
 				ldap_scherr2str(code), err );
 			return code;
 		}
-		code = at_add( at, 0, &err );
+		code = at_add( at, 0, NULL, &err );
 		if ( !code ) {
 			slap_str2ad( at->at_names[0], pwd_OpSchema[i].ad, &err );
 		}
diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h
index ed1f1be62b..cb7b7bf3df 100644
--- a/servers/slapd/proto-slap.h
+++ b/servers/slapd/proto-slap.h
@@ -161,7 +161,8 @@ LDAP_SLAPD_F (int) at_delete_from_list LDAP_P((
 	int pos, AttributeType ***listp ));
 LDAP_SLAPD_F (int) at_schema_info LDAP_P(( Entry *e ));
 LDAP_SLAPD_F (int) at_add LDAP_P((
-	LDAPAttributeType *at, int user, const char **err ));
+	LDAPAttributeType *at, int user,
+	AttributeType **sat, const char **err ));
 LDAP_SLAPD_F (void) at_destroy LDAP_P(( void ));
 
 LDAP_SLAPD_F (int) is_at_subtype LDAP_P((
@@ -175,6 +176,9 @@ LDAP_SLAPD_F (int) is_at_syntax LDAP_P((
 LDAP_SLAPD_F (int) at_start LDAP_P(( AttributeType **at ));
 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 ));
+
 /*
  * attr.c
  */
@@ -512,11 +516,15 @@ LDAP_SLAPD_F (void) connection_assign_nextid LDAP_P((Connection *));
  * cr.c
  */
 LDAP_SLAPD_F (int) cr_schema_info( Entry *e );
+LDAP_SLAPD_F (void) cr_unparse LDAP_P((
+	BerVarray *bva, ContentRule *start, ContentRule *end, int system ));
 
 LDAP_SLAPD_F (int) cr_add LDAP_P((
 	LDAPContentRule *oc,
 	int user,
+	ContentRule **scr,
 	const char **err));
+
 LDAP_SLAPD_F (void) cr_destroy LDAP_P(( void ));
 
 LDAP_SLAPD_F (ContentRule *) cr_find LDAP_P((
@@ -951,6 +959,7 @@ LDAP_SLAPD_F (void) mra_free LDAP_P((
 LDAP_SLAPD_F (int) oc_add LDAP_P((
 	LDAPObjectClass *oc,
 	int user,
+	ObjectClass **soc,
 	const char **err));
 LDAP_SLAPD_F (void) oc_destroy LDAP_P(( void ));
 
@@ -1000,15 +1009,19 @@ LDAP_SLAPD_F (int) is_entry_objectclass LDAP_P((
 	 : is_entry_objectclass((e), slap_schema.si_oc_syncConsumerSubentry, 1))
 
 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 ));
 
 /*
  * oidm.c
  */
 LDAP_SLAPD_F(char *) oidm_find(char *oid);
 LDAP_SLAPD_F (void) oidm_destroy LDAP_P(( void ));
-LDAP_SLAPD_F (void) oidm_unparse LDAP_P(( BerVarray *bva ));
+LDAP_SLAPD_F (void) oidm_unparse LDAP_P((
+	BerVarray *bva, OidMacro *start, OidMacro *end, int system ));
 LDAP_SLAPD_F (int) parse_oidm LDAP_P((
-	const char *fname, int lineno, int argc, char **argv, int user ));
+	const char *fname, int lineno, int argc, char **argv, int user,
+	OidMacro **om ));
 
 /*
  * operation.c
@@ -1261,11 +1274,14 @@ LDAP_SLAPD_F (int) slap_schema_check LDAP_P((void));
 LDAP_SLAPD_F( int ) slap_valid_descr( const char * );
 
 LDAP_SLAPD_F (int) parse_cr LDAP_P((
-	const char *fname, int lineno, char *line, char **argv ));
+	const char *fname, int lineno, char *line, char **argv,
+	ContentRule **scr ));
 LDAP_SLAPD_F (int) parse_oc LDAP_P((
-	const char *fname, int lineno, char *line, char **argv ));
+	const char *fname, int lineno, char *line, char **argv,
+	ObjectClass **soc ));
 LDAP_SLAPD_F (int) parse_at LDAP_P((
-	const char *fname, int lineno, char *line, char **argv ));
+	const char *fname, int lineno, char *line, char **argv,
+	AttributeType **sat ));
 LDAP_SLAPD_F (char *) scherr2str LDAP_P((int code)) LDAP_GCCATTR((const));
 LDAP_SLAPD_F (int) dscompare LDAP_P(( const char *s1, const char *s2del,
 	char delim ));
diff --git a/servers/slapd/schema_prep.c b/servers/slapd/schema_prep.c
index b64f76751c..e05f5d63c5 100644
--- a/servers/slapd/schema_prep.c
+++ b/servers/slapd/schema_prep.c
@@ -1124,7 +1124,7 @@ slap_schema_load( void )
 				return LDAP_OTHER;
 			}
 
-			code = at_add( at, 0, &err );
+			code = at_add( at, 0, NULL, &err );
 			if ( code ) {
 				fprintf( stderr, "slap_schema_load: AttributeType "
 					"\"%s\": %s: \"%s\"\n",
@@ -1237,7 +1237,7 @@ slap_schema_load( void )
 				return LDAP_OTHER;
 			}
 
-			code = oc_add(oc,0,&err);
+			code = oc_add(oc,0,NULL,&err);
 			if ( code ) {
 				fprintf( stderr, "slap_schema_load: ObjectClass "
 					"\"%s\": %s: \"%s\"\n",
diff --git a/servers/slapd/schemaparse.c b/servers/slapd/schemaparse.c
index c1e7195ba2..16addf4a16 100644
--- a/servers/slapd/schemaparse.c
+++ b/servers/slapd/schemaparse.c
@@ -126,7 +126,8 @@ parse_cr(
     const char	*fname,
     int		lineno,
     char	*line,
-    char	**argv )
+    char	**argv,
+	ContentRule **scr )
 {
 	LDAPContentRule *cr;
 	int		code;
@@ -148,7 +149,7 @@ parse_cr(
 		return 1;
 	}
 
-	code = cr_add(cr,1,&err);
+	code = cr_add(cr,1,scr,&err);
 	if ( code ) {
 		fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
 			 fname, lineno, scherr2str(code), err);
@@ -164,7 +165,8 @@ parse_oc(
     const char	*fname,
     int		lineno,
     char	*line,
-    char	**argv )
+    char	**argv,
+	ObjectClass **soc )
 {
 	LDAPObjectClass *oc;
 	int		code;
@@ -186,7 +188,7 @@ parse_oc(
 		return 1;
 	}
 
-	code = oc_add(oc,1,&err);
+	code = oc_add(oc,1,soc,&err);
 	if ( code ) {
 		fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
 			 fname, lineno, scherr2str(code), err);
@@ -245,7 +247,8 @@ parse_at(
     const char	*fname,
     int		lineno,
     char	*line,
-    char	**argv )
+    char	**argv,
+	AttributeType **sat )
 {
 	LDAPAttributeType *at;
 	int		code;
@@ -274,7 +277,7 @@ parse_at(
 		return 1;
 	}
 
-	code = at_add(at,1,&err);
+	code = at_add(at,1,sat,&err);
 	if ( code ) {
 		fprintf( stderr, "%s: line %d: %s: \"%s\"\n",
 			 fname, lineno, scherr2str(code), err);
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index 163971107a..784b658567 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -325,7 +325,7 @@ typedef struct slap_oid_macro {
 	BerVarray som_subs;
 #define	SLAP_OM_HARDCODE	0x10000U	/* This is hardcoded schema */
 	int som_flags;
-	LDAP_SLIST_ENTRY(slap_oid_macro) som_next;
+	LDAP_STAILQ_ENTRY(slap_oid_macro) som_next;
 } OidMacro;
 
 /* forward declarations */
@@ -655,7 +655,7 @@ typedef struct slap_attribute_type {
 
 	slap_mask_t					sat_flags;
 
-	LDAP_SLIST_ENTRY(slap_attribute_type) sat_next;
+	LDAP_STAILQ_ENTRY(slap_attribute_type) sat_next;
 
 #define sat_oid				sat_atype.at_oid
 #define sat_names			sat_atype.at_names
@@ -709,7 +709,7 @@ typedef struct slap_object_class {
 #define soc_at_oids_may		soc_oclass.oc_at_oids_may
 #define soc_extensions		soc_oclass.oc_extensions
 
-	LDAP_SLIST_ENTRY(slap_object_class) soc_next;
+	LDAP_STAILQ_ENTRY(slap_object_class) soc_next;
 } ObjectClass;
 
 #define	SLAP_OC_ALIAS		0x0001
@@ -749,7 +749,10 @@ typedef struct slap_content_rule {
 #define scr_at_oids_may		scr_crule.cr_at_oids_may
 #define scr_at_oids_not		scr_crule.cr_at_oids_not
 
-	LDAP_SLIST_ENTRY( slap_content_rule ) scr_next;
+#define 	SLAP_CR_HARDCODE	0x10000U
+	int	scr_flags;
+
+	LDAP_STAILQ_ENTRY( slap_content_rule ) scr_next;
 } ContentRule;
 
 /* Represents a recognized attribute description ( type + options ). */
-- 
GitLab