/* eis_select.c - */

#ifndef lint
static char *rcsid = "$Header: /var/lib/cvsd/repos/CSRG/43BSDReno/contrib/isode-beta/quipu/eis_select.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/eis_select.c,v 1.1.1.1 2018/04/24 16:12:56 root Exp $
 *
 *
 * $Log: eis_select.c,v $
 * Revision 1.1.1.1  2018/04/24 16:12:56  root
 * BSD 4.3reno
 *
 * Revision 7.1  90/07/09  14:46:08  mrose
 * sync
 * 
 * Revision 7.0  89/11/23  22:17:29  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/commonarg.h"
#include "quipu/entry.h"

extern LLog * log_dsap;

#define EIS_SELECT eis.eis_select

static Attr_Sequence  cpy_as_comp_type ();
static Attr_Sequence  cpy_as_comp ();

Attr_Sequence eis_select (eis,entryptr,dn, qctx)
EntryInfoSelection eis;
Entry entryptr;
DN dn;
char qctx;	/* If TRUE - it is a Quipu context association */
		/* So treat as if "-allattributes -typesandvalves" */
		/* Plus - make sure the ACL is sent		*/
{
Attr_Sequence result = NULLATTR;
register Attr_Sequence trail;
register Attr_Sequence temp;
Attr_Sequence temp2;
register Attr_Sequence eptr;
DN node;
extern AttributeType at_acl;

	DLOG (log_dsap,LLOG_TRACE,("eis_select"));

	node = get_copy_dn (entryptr);

	if (eis.eis_allattributes || qctx) {
	    for(temp=entryptr->e_attributes; temp != NULLATTR; temp=temp->attr_link) {

		if ( ! (qctx && (AttrT_cmp (at_acl,temp->attr_type) == 0)))
			if ( (temp->attr_acl != NULLACL_INFO) && 
				(check_acl(dn,ACL_READ,temp->attr_acl,node ) != OK ))
					continue;

		if ((eis.eis_infotypes == EIS_ATTRIBUTETYPESONLY) && !qctx)
			temp2 = cpy_as_comp_type (temp);
		else
			temp2 = cpy_as_comp (temp);

		if (result == NULLATTR) {
			result = temp2;
			trail = result;
		} else {
			trail->attr_link = temp2;
			trail = temp2;
		}
	    }
	} else {
	    for(eptr=EIS_SELECT; eptr != NULLATTR; eptr=eptr->attr_link) {

		if ((temp = as_find_type (entryptr->e_attributes,eptr->attr_type)) == NULLATTR)
			continue;

		if ( (temp->attr_acl != NULLACL_INFO)&& (check_acl(dn,ACL_READ,temp->attr_acl,node ) != OK ))
			continue;

		if (eis.eis_infotypes == EIS_ATTRIBUTETYPESONLY)
			temp2 = cpy_as_comp_type (temp);
		else
			temp2 = cpy_as_comp (temp);

		if (result == NULLATTR) {
			result = temp2;
			trail = result;
		} else {
			trail->attr_link = temp2;
			trail = temp2;
		}
	    }
	}
	dn_free (node);

	return (result);
}

static Attr_Sequence  cpy_as_comp (as)
Attr_Sequence  as;
{
Attr_Sequence ptr;

	if (as==NULLATTR) {
		LLOG (log_dsap,LLOG_EXCEPTIONS,("copy of null as"));
		return (NULLATTR);
	}
	ptr = as_comp_alloc();
	ptr->attr_type = AttrT_cpy (as->attr_type);
	ptr->attr_value = avs_cpy (as->attr_value);
	ptr->attr_link = NULLATTR;
	ptr->attr_acl = NULLACL_INFO;

	return (ptr);
}


static Attr_Sequence  cpy_as_comp_type (as)
Attr_Sequence  as;
{
Attr_Sequence ptr;

	if (as==NULLATTR) {
		LLOG (log_dsap,LLOG_EXCEPTIONS,("copy of null as"));
		return (NULLATTR);
	}
	ptr = as_comp_alloc();
	ptr->attr_type = AttrT_cpy (as->attr_type);
	ptr->attr_value = NULLAV;
	ptr->attr_link = NULLATTR;
	ptr->attr_acl = NULLACL_INFO;
	return (ptr);
}



eis_check (eis,entryptr,dn)
EntryInfoSelection eis;
Entry entryptr;
DN dn;
{
register Attr_Sequence temp;
register Attr_Sequence as;
DN node;

	DLOG (log_dsap,LLOG_TRACE,("eis_check"));

	node = get_copy_dn (entryptr);

	if (eis.eis_allattributes) {
	    for(temp=entryptr->e_attributes; temp != NULLATTR; temp=temp->attr_link)
		if (temp->attr_acl != NULLACL_INFO)
			if (check_acl(NULLDN,ACL_READ,temp->attr_acl,node ) != OK )
				if (check_acl(dn,ACL_READ,temp->attr_acl,node ) == OK ) {
					dn_free (node);
					return NOTOK;
				}
	} else {
	    for(temp=EIS_SELECT; temp != NULLATTR; temp=temp->attr_link) {
		if ((as = as_find_type (entryptr->e_attributes,temp->attr_type)) == NULLATTR)
			continue;
		if (as->attr_acl != NULLACL_INFO)
			if (check_acl(NULLDN,ACL_READ,as->attr_acl,node ) != OK )
				if (check_acl(dn,ACL_READ,as->attr_acl,node ) == OK ) {
					dn_free (node);
					return NOTOK;
				}
	    }
	}
	dn_free (node);
	return OK;
}

