From 3f357270d4cbc812211d69fda6cf685c5963036b Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Mon, 21 Mar 2005 10:23:01 +0000
Subject: [PATCH] Fix unparsers, schema loading works

---
 servers/slapd/at.c             |  6 ++-
 servers/slapd/back-ldif/ldif.c | 27 ++++------
 servers/slapd/bconfig.c        | 94 ++++++++++++++++++++++++++++------
 servers/slapd/cr.c             |  6 ++-
 servers/slapd/oc.c             |  6 ++-
 servers/slapd/oidm.c           |  6 ++-
 servers/slapd/schema_init.c    |  2 +-
 7 files changed, 106 insertions(+), 41 deletions(-)

diff --git a/servers/slapd/at.c b/servers/slapd/at.c
index 4ff083f075..e4030aec05 100644
--- a/servers/slapd/at.c
+++ b/servers/slapd/at.c
@@ -633,9 +633,10 @@ at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
 
 	/* count the result size */
 	i = 0;
-	for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
+	for ( at=start; at; at=LDAP_STAILQ_NEXT(at, sat_next)) {
 		if ( sys && !(at->sat_flags & SLAP_AT_HARDCODE)) continue;
 		i++;
+		if ( at == end ) break;
 	}
 	if (!i) return;
 
@@ -648,7 +649,7 @@ at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
 		ibuf[0] = '\0';
 	}
 	i = 0;
-	for ( at=start; at && at!=end; at=LDAP_STAILQ_NEXT(at, sat_next)) {
+	for ( at=start; at; 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 );
@@ -663,6 +664,7 @@ at_unparse( BerVarray *res, AttributeType *start, AttributeType *end, int sys )
 		i++;
 		bva[i].bv_val = NULL;
 		ldap_memfree( bv.bv_val );
+		if ( at == end ) break;
 	}
 	*res = bva;
 }
diff --git a/servers/slapd/back-ldif/ldif.c b/servers/slapd/back-ldif/ldif.c
index 8fed15d1d2..43a5de2a1e 100644
--- a/servers/slapd/back-ldif/ldif.c
+++ b/servers/slapd/back-ldif/ldif.c
@@ -103,36 +103,31 @@ dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
 }
 
 static char * slurp_file(int fd) {
-	int entry_buf_size = 40 * ENTRY_BUFF_INCREMENT;
 	int read_chars_total = 0;
 	int read_chars = 0;
-	int entry_size = 40 * ENTRY_BUFF_INCREMENT;
-	char * entry = (char *) malloc(sizeof(char) * 40 * ENTRY_BUFF_INCREMENT);
-	char * entry_pos = entry;
+	int entry_size;
+	char * entry;
+	char * entry_pos;
+	struct stat st;
+
+	fstat(fd, &st);
+	entry_size = st.st_size;
+	entry = ch_malloc( entry_size+1 );
+	entry_pos = entry;
 	
 	while(1) {
-		if(entry_size - read_chars_total == 0) {
-			entry = (char *) realloc(entry, sizeof(char) * 2 * entry_size);
-			entry_size = 2 * entry_size;
-		}
 		read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
 		if(read_chars == -1) {
 			SLAP_FREE(entry);
 			return NULL;
 		}
-		entry_pos += read_chars;
 		if(read_chars == 0) {
-			if(entry_size - read_chars_total > 0)
-				entry[read_chars_total] = '\0';
-			else {
-				entry = (char *) realloc(entry, sizeof(char) * entry_size + 1);
-				entry_size = entry_size + 1;
-				entry[read_chars_total] = '\0';
-			}	
+			entry[read_chars_total] = '\0';
 			break;
 		}
 		else {
 			read_chars_total += read_chars;
+			entry_pos += read_chars;
 		}
 	}
 	return entry;
diff --git a/servers/slapd/bconfig.c b/servers/slapd/bconfig.c
index 58e6dd6b00..0faa3a5083 100644
--- a/servers/slapd/bconfig.c
+++ b/servers/slapd/bconfig.c
@@ -234,8 +234,8 @@ ConfigTable config_back_cf_table[] = {
 	{ "attribute",	"attribute", 2, 0, 9, ARG_PAREN|ARG_MAGIC|CFG_ATTR,
 		&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' )",
+			"EQUALITY caseIgnoreMatch "
+			"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
 				NULL, NULL },
 	{ "attributeoptions", NULL, 0, 0, 0, ARG_MAGIC|CFG_ATOPT,
 		&config_generic, "( OLcfgAt:5 NAME 'olcAttributeOptions' "
@@ -286,8 +286,8 @@ ConfigTable config_back_cf_table[] = {
 	{ "ditcontentrule",	NULL, 0, 0, 0, ARG_MAGIC|CFG_DIT,
 		&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' )",
+			"EQUALITY caseIgnoreMatch "
+			"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
 			NULL, NULL },
 	{ "gentlehup", "on|off", 2, 2, 0,
 #ifdef SIGHUP
@@ -353,8 +353,8 @@ ConfigTable config_back_cf_table[] = {
 	{ "objectclass", "objectclass", 2, 0, 0, ARG_PAREN|ARG_MAGIC|CFG_OC,
 		&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' )",
+		"EQUALITY caseIgnoreMatch "
+		"SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )",
 			NULL, NULL },
 	{ "objectidentifier", NULL,	0, 0, 0, ARG_MAGIC|CFG_OID,
 		&config_generic, "( OLcfgAt:33 NAME 'olcObjectIdentifier' "
@@ -580,23 +580,26 @@ static ConfigOCs cf_ocs[] = {
 		"NAME 'olcConfig' "
 		"DESC 'OpenLDAP configuration object' "
 		"ABSTRACT SUP top "
-		"MAY ( cn $ olcConfigFile ) )", Cft_Abstract, NULL },
+		"MAY cn )", Cft_Abstract, NULL },
 	{ "( OLcfgOc:2 "
 		"NAME 'olcGlobal' "
 		"DESC 'OpenLDAP Global configuration options' "
 		"SUP olcConfig STRUCTURAL "
-		"MAY ( olcConfigDir $ olcAllows $ olcArgsFile $ olcAttributeOptions $ "
-		 "olcAuthIDRewrite $ olcAuthzPolicy $ olcAuthzRegexp $ "
-		 "olcConcurrency $ olcConnMaxPending $ olcConnMaxPendingAuth $ "
-		 "olcDefaultSearchBase $ olcDisallows $ olcGentleHUP $ "
-		 "olcIdleTimeout $ olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
+		"MAY ( olcConfigFile $ olcConfigDir $ olcAllows $ olcArgsFile $ "
+		 "olcAttributeOptions $ olcAttributeTypes $ olcAuthIDRewrite $ "
+		 "olcAuthzPolicy $ olcAuthzRegexp $ olcConcurrency $ "
+		 "olcConnMaxPending $ olcConnMaxPendingAuth $ olcDefaultSearchBase $ "
+		 "olcDisallows $ olcDitContentRules $ olcGentleHUP $ olcIdleTimeout $ "
+		 "olcIndexSubstrIfMaxLen $ olcIndexSubstrIfMinLen $ "
 		 "olcIndexSubstrAnyLen $ olcIndexSubstrAnyStep $ olcLocalSSF $ "
-		 "olcLogLevel $ olcModulePath $ olcObjectIdentifier $ "
+		 "olcLogLevel $ olcModulePath $ olcObjectClasses $ "
+		 "olcObjectIdentifier $ "
 		 "olcPasswordCryptSaltFormat $ olcPasswordHash $ olcPidFile $ "
 		 "olcPlugin $ olcPluginLogFile $ olcReadOnly $ olcReferral $ "
 		 "olcReplicaPidFile $ olcReplicaArgsFile $ olcReplicationInterval $ "
 		 "olcReplogFile $ olcRequires $ olcRestrict $ olcReverseLookup $ "
-		 "olcRootDSE $ olcRootPW $ olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
+		 "olcRootDSE $ olcRootPW $ "
+		 "olcSaslHost $ olcSaslRealm $ olcSaslSecProps $ "
 		 "olcSchemaCheck $ olcSchemaDN $ olcSecurity $ olcSizeLimit $ "
 		 "olcSockbufMaxIncoming $ olcSockbufMaxIncomingAuth $ olcSrvtab $ "
 		 "olcThreads $ olcTimeLimit $ olcTLSCACertificateFile $ "
@@ -633,7 +636,7 @@ static ConfigOCs cf_ocs[] = {
 		"NAME 'olcIncludeFile' "
 		"DESC 'OpenLDAP configuration include file' "
 		"SUP olcConfig STRUCTURAL "
-		"MAY ( olcInclude $ olcRootDSE ) )",
+		"MAY ( olcInclude $ olcConfigFile $ olcRootDSE ) )",
 		Cft_Include, &cfOc_include },
 #ifdef SLAPD_MODULES
 	{ "( OLcfgOc:8 "
@@ -2873,6 +2876,7 @@ sort_vals( Attribute *a )
 		/* Strip index from normalized values */
 		if ( !a->a_nvals || a->a_vals == a->a_nvals ) {
 			a->a_nvals = ch_malloc( (vals+1)*sizeof(struct berval));
+			BER_BVZERO(a->a_nvals+vals);
 			for ( i=0; i<vals; i++ ) {
 				ptr = strchr(a->a_vals[i].bv_val, '}') + 1;
 				a->a_nvals[i].bv_len = a->a_vals[i].bv_len -
@@ -3369,6 +3373,61 @@ config_build_entry( ConfigArgs *c, Entry *e, ObjectClass *oc,
 	return 0;
 }
 
+static void
+config_build_schema_inc( ConfigArgs *c, CfEntryInfo *ceparent,
+	Operation *op, SlapReply *rs )
+{
+	Entry *e;
+	ConfigFile *cf = c->private;
+	CfEntryInfo *ce, *ceprev;
+	char *ptr;
+	struct berval bv;
+
+	if ( ceparent->ce_kids ) {
+		for ( ceprev = ceparent->ce_kids; ceprev->ce_sibs;
+			ceprev = ceprev->ce_sibs );
+	}
+
+	for (; cf; cf=cf->c_sibs, c->depth++) {
+		c->value_dn.bv_val = c->log;
+		bv.bv_val = strrchr(cf->c_file.bv_val, LDAP_DIRSEP[0]);
+		if ( !bv.bv_val ) {
+			bv = cf->c_file;
+		} else {
+			bv.bv_val++;
+			bv.bv_len = cf->c_file.bv_len - (bv.bv_val - cf->c_file.bv_val);
+		}
+		ptr = strchr( bv.bv_val, '.' );
+		if ( ptr )
+			bv.bv_len = ptr - bv.bv_val;
+		c->value_dn.bv_len = sprintf(c->value_dn.bv_val, "cn={%02d}", c->depth);
+		strncpy( c->value_dn.bv_val + c->value_dn.bv_len, bv.bv_val,
+			bv.bv_len );
+		c->value_dn.bv_len += bv.bv_len;
+		c->value_dn.bv_val[c->value_dn.bv_len] ='\0';
+
+		e = config_alloc_entry( ceparent, &c->value_dn );
+		c->private = cf;
+		config_build_entry( c, e, cfOc_schema, &c->value_dn,
+			c->bi->bi_cf_table, NO_TABLE );
+		ce = e->e_private;
+		ce->ce_type = Cft_Schema;
+		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;
+		if ( cf->c_kids ) {
+			c->private = cf->c_kids;
+			config_build_schema_inc( c, ceparent, op, rs );
+		}
+	}
+}
+
 static CfEntryInfo *
 config_build_includes( ConfigArgs *c, CfEntryInfo *ceparent,
 	Operation *op, SlapReply *rs )
@@ -3519,8 +3578,11 @@ config_back_db_open( BackendDB *be )
 	}
 	ceprev = ce;
 
-	/* Create includeFile nodes... */
+	/* Create includeFile nodes and schema nodes for included schema... */
 	if ( cfb->cb_config->c_kids ) {
+		c.depth = 0;
+		c.private = cfb->cb_config->c_kids;
+		config_build_schema_inc( &c, ce, op, &rs );
 		c.private = cfb->cb_config->c_kids;
 		ceprev = config_build_includes( &c, ceparent, op, &rs );
 	}
diff --git a/servers/slapd/cr.c b/servers/slapd/cr.c
index eb9178d7e7..948f4ec0cd 100644
--- a/servers/slapd/cr.c
+++ b/servers/slapd/cr.c
@@ -417,9 +417,10 @@ cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
 
 	/* count the result size */
 	i = 0;
-	for ( cr=start; cr && cr!=end; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+	for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
 		if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
 		i++;
+		if ( cr == end ) break;
 	}
 	if (!i) return;
 
@@ -432,7 +433,7 @@ cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
 		ibuf[0] = '\0';
 	}
 	i = 0;
-	for ( cr=start; cr && cr!=end; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+	for ( cr=start; cr; 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 );
@@ -447,6 +448,7 @@ cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
 		i++;
 		bva[i].bv_val = NULL;
 		ldap_memfree( bv.bv_val );
+		if ( cr == end ) break;
 	}
 	*res = bva;
 }
diff --git a/servers/slapd/oc.c b/servers/slapd/oc.c
index b923725a9d..2b5b5f06f7 100644
--- a/servers/slapd/oc.c
+++ b/servers/slapd/oc.c
@@ -543,9 +543,10 @@ oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
 
 	/* count the result size */
 	i = 0;
-	for ( oc=start; oc && oc!=end; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
+	for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
 		if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
 		i++;
+		if ( oc == end ) break;
 	}
 	if (!i) return;
 
@@ -558,7 +559,7 @@ oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
 		ibuf[0] = '\0';
 	}
 	i = 0;
-	for ( oc=start; oc && oc!=end; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
+	for ( oc=start; oc; 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 );
@@ -573,6 +574,7 @@ oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
 		i++;
 		bva[i].bv_val = NULL;
 		ldap_memfree( bv.bv_val );
+		if ( oc == end ) break;
 	}
 	*res = bva;
 }
diff --git a/servers/slapd/oidm.c b/servers/slapd/oidm.c
index 99df108cda..0e0f6210a3 100644
--- a/servers/slapd/oidm.c
+++ b/servers/slapd/oidm.c
@@ -164,10 +164,11 @@ void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
 
 	/* count the result size */
 	i = 0;
-	for ( om=start; om && om!=end; om=LDAP_STAILQ_NEXT(om, som_next)) {
+	for ( om=start; om; 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;
+		if ( om == end ) break;
 	}
 	num = i;
 	if (!i) return;
@@ -179,7 +180,7 @@ void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
 		idx.bv_len = 0;
 		ibuf[0] = '\0';
 	}
-	for ( i=0,om=start; om && om!=end; om=LDAP_STAILQ_NEXT(om, som_next)) {
+	for ( i=0,om=start; om; 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 ) {
@@ -194,6 +195,7 @@ void oidm_unparse( BerVarray *res, OidMacro *start, OidMacro *end, int sys )
 			strcpy( ptr, om->som_subs[j].bv_val );
 		}
 		if ( i>=num ) break;
+		if ( om == end ) break;
 	}
 	*res = bva;
 }
diff --git a/servers/slapd/schema_init.c b/servers/slapd/schema_init.c
index 5d37337439..8396006f59 100644
--- a/servers/slapd/schema_init.c
+++ b/servers/slapd/schema_init.c
@@ -1635,7 +1635,7 @@ numericoidValidate(
 			return LDAP_SUCCESS;
 		}
 
-		if ( val.bv_val[0] == '0' ) {
+		if ( val.bv_val[0] == '0' && !OID_SEPARATOR( val.bv_val[1] )) {
 			break;
 		}
 
-- 
GitLab