/* schema.c - */

#ifndef lint
static char *rcsid = "$Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/quipu/schema.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $";
#endif

/*
 * $Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/quipu/schema.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $
 *
 *
 * $Log: schema.c,v $
 * Revision 1.1.1.1  2018/04/24 16:12:56  root
 * BSD 4.3reno
 *
 * Revision 7.2  90/07/09  14:46:34  mrose
 * sync
 * 
 * Revision 7.1  90/03/15  11:19:09  mrose
 * quipu-sync
 * 
 * Revision 7.0  89/11/23  22:18:03  mrose
 * Release 6.0
 * 
 */

/*
 *                                NOTICE
 *
 *    Acquisition, use, and distribution of this module and related
 *    materials are subject to the restrictions of a license agreement.
 *    Consult the Preface in the User's Manual for the full terms of
 *    this agreement.
 *
 */


#include "quipu/util.h"
#include "quipu/entry.h"
#include "quipu/ds_error.h"

extern int oidformat;
extern LLog * log_dsap;

extern AttributeType at_objectclass;
extern AttributeType at_schema;
extern OID alias_oc;

AV_Sequence oc_avs (as)
Attr_Sequence as;
{
Attr_Sequence at;

	if ((at = as_find_type (as,at_objectclass)) == NULLATTR) 
		return (NULLAV);

	return( at->attr_value);
}

real_check_schema (eptr,as,error)
Entry eptr;
Attr_Sequence as;
struct DSError * error;
{
register Attr_Sequence at;
AV_Sequence avs;
AV_Sequence avs_oc;
table_seq optr;
AV_Sequence tavs = NULLAV;
objectclass * oc;
extern OID alias_oc;

	DLOG (log_dsap,LLOG_TRACE,("check schema")) ;

	if (eptr->e_parent == NULLENTRY)
		return (OK);    /* no schema for root */

	if (eptr->e_data != E_DATA_MASTER)
		return (OK);	/* only check schema of MASTERed entries */

	avs_oc = avs = oc_avs (eptr->e_attributes);

	if ((at = as_find_type (eptr->e_parent->e_attributes,at_schema)) != NULLATTR) {
		/* what should default be !!! */
		
		tavs = at->attr_value;
		/* make sure object class is allowed */
		if (test_schema (tavs,avs) != OK) {
			LLOG (log_dsap,LLOG_EXCEPTIONS,("Specified object class is not in the tree structure (schema) list"));
			error->dse_type = DSE_UPDATEERROR;
			error->ERR_UPDATE.DSE_up_problem = DSE_UP_NAMINGVIOLATION;
			return (NOTOK);
		}
	}

	/* now check 'must contain' attributes */
	for (; avs != NULLAV; avs = avs->avseq_next) {
	    oc = (objectclass *) avs->avseq_av.av_struct;
	    for (optr=oc->oc_must; optr!=NULLTABLE_SEQ;  optr=optr->ts_next) {
		at = (as == NULLATTR) ? eptr->e_attributes : as;
		for (; at!=NULLATTR; at=at->attr_link)
			if (at->attr_type == optr->ts_oa)
				break;

		if (at == NULLATTR) {
			LLOG (log_dsap,LLOG_EXCEPTIONS,("'Must' attribute missing '%s'",attr2name(optr->ts_oa,OIDPART)));
			error->dse_type = DSE_UPDATEERROR;
			error->ERR_UPDATE.DSE_up_problem = DSE_UP_OBJECTCLASSVIOLATION;
			return (NOTOK);
		}
	   }
	}


	/* Now try the 'may' contain bits */
	/* BUT not if "alias" */

	if ( check_in_oc (alias_oc, avs_oc) )
		return (OK);

	at = (as == NULLATTR) ? eptr->e_attributes : as;
	for (; at!=NULLATTR; at=at->attr_link) {
		optr = NULLTABLE_SEQ;
		for (avs = avs_oc; avs != NULLAV; avs = avs->avseq_next) {
		    oc = (objectclass *) avs->avseq_av.av_struct;
		    optr=oc->oc_must;
		    if ((optr == NULLTABLE_SEQ) && (oc->oc_may == NULLTABLE_SEQ) && (oc->oc_hierachy == NULLOIDSEQ)) 
			return OK;	/* unknown object class */
		    for (; optr!=NULLTABLE_SEQ;  optr=optr->ts_next) 
			if (at->attr_type == optr->ts_oa)
					break;
		    if (optr != NULLTABLE_SEQ)
		    	break;

		    for (optr=oc->oc_may; optr!=NULLTABLE_SEQ;  optr=optr->ts_next) 
			if (at->attr_type == optr->ts_oa)
				break;
		    if (optr != NULLTABLE_SEQ)
		    	break;
		}
		if (optr == NULLTABLE_SEQ) {
			LLOG (log_dsap,LLOG_EXCEPTIONS,("attribute '%s' not allowed in the specified objectclass",attr2name(at->attr_type,OIDPART)));
			error->dse_type = DSE_UPDATEERROR;
			error->ERR_UPDATE.DSE_up_problem = DSE_UP_OBJECTCLASSVIOLATION;
			return (NOTOK);
		}
	}
	return (OK);

}

check_schema_type (eptr,attr,error)
Entry eptr;
AttributeType attr;
struct DSError * error;
{
Attr_Sequence at;
register table_seq optr;
AV_Sequence avs;
AV_Sequence tavs = NULLAV;
objectclass * oc;

	DLOG (log_dsap,LLOG_TRACE,("check schema type"));

	if (eptr->e_parent == NULLENTRY)
		return (OK);    /* no schema for root */

	avs = oc_avs (eptr->e_attributes);

	if ((at = as_find_type (eptr->e_parent->e_attributes,at_schema)) != NULLATTR) {
		tavs = at->attr_value;
		if (test_schema (tavs,avs) != OK) {
			LLOG (log_dsap,LLOG_EXCEPTIONS,("given objectclass not in schema (tree structure) list"));
			error->dse_type = DSE_UPDATEERROR;
			error->ERR_UPDATE.DSE_up_problem = DSE_UP_NAMINGVIOLATION;
			return (NOTOK);
		}
	}

	/* Now try the 'may' contain bits */
	/* BUT not if "alias" */

	if ( check_in_oc (alias_oc, avs) )
		return (OK);

	for (; avs != NULLAV; avs = avs->avseq_next) {
	    oc = (objectclass *) avs->avseq_av.av_struct;
	    optr=oc->oc_must;
	    if ((optr == NULLTABLE_SEQ) && (oc->oc_may == NULLTABLE_SEQ) && (oc->oc_hierachy == NULLOIDSEQ)) 
		return (OK);	/* unknown object class */
	    for (; optr!=NULLTABLE_SEQ;  optr=optr->ts_next) 
		if (attr == optr->ts_oa)
			break;
	    if (optr != NULLTABLE_SEQ)
	    	break;
		
	    for (optr=oc->oc_may; optr!=NULLTABLE_SEQ;  optr=optr->ts_next) 
		if (attr == optr->ts_oa)
			break;
	    if (optr != NULLTABLE_SEQ)
	    	break;
	}

	if (optr == NULLTABLE_SEQ) {
		LLOG (log_dsap,LLOG_EXCEPTIONS,("Attribute '%s' not allowed for specified objectclass",attr2name(attr,OIDPART)));
		error->dse_type = DSE_UPDATEERROR;
		error->ERR_UPDATE.DSE_up_problem = DSE_UP_OBJECTCLASSVIOLATION;
		return (NOTOK);
	}
	return (OK);

}


test_schema (tree,oc)
AV_Sequence tree;
AV_Sequence oc;
{
AV_Sequence aptr, tavs;
struct tree_struct *tptr;
char found;
objectclass * oc1;

	if (oc == NULLAV)
		return (NOTOK);

	for (aptr=oc; aptr!= NULLAV; aptr=aptr->avseq_next) {
		found = FALSE;
		for (tavs=tree; tavs!=NULLAV ;tavs=tavs->avseq_next) {
			tptr = (struct tree_struct *) tavs->avseq_av.av_struct;
			if (tptr->tree_object == NULLOBJECTCLASS) {
				/* is this correct behaviour ? */
				found = TRUE;
				break;
			}
			oc1 = (objectclass *) aptr->avseq_av.av_struct;
			if (test_hierarchy (tptr->tree_object->oc_ot.ot_oid, oc1->oc_ot.ot_oid) == 0) {
				found = TRUE;
				break;
			}
		}
		if (found == FALSE) {
			return (NOTOK);
		}
	}
	return (OK);
}

test_hierarchy (a,b)    /* see if b in oc a */
OID a,b;
{
struct oid_seq * oidseq;
objectclass *oc;

	if (oid_cmp (a,b) == 0)
		return (OK);

	if ((oc = oid2oc(a)) == NULLOBJECTCLASS) {
		LLOG (log_dsap,LLOG_EXCEPTIONS,("got an illeagl object class!!!"));
		return (NOTOK);
	}

	for (oidseq = oc->oc_hierachy; oidseq != NULLOIDSEQ; oidseq = oidseq->oid_next) 
		if (test_hierarchy (oidseq->oid_oid,b) == OK)
			return (OK);
		
	return (NOTOK);
}
