/* ds_modifyrdn.c - */

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

extern LLog * log_dsap;
extern int encode_DAS_ModifyRDNArgumentData();

do_ds_modifyrdn (arg, error, binddn,target,di_p,dsp)
    register struct ds_modifyrdn_arg     *arg;
    struct DSError              *error;
    DN                          binddn;
    DN                          target;
    struct di_block		**di_p;
    char 			dsp;
{
Entry  entryptr;
register RDN rdn;
Attr_Sequence as;
AV_Sequence avs;
RDN modrdn;
char * new_version ();
int retval;
extern int read_only;

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

	if (!dsp)
		target = arg->mra_object;

	/* stop aliases being dereferenced */
	arg->mra_common.ca_servicecontrol.svc_options |= SVC_OPT_DONTDEREFERENCEALIAS;

	if (target == NULLDN) {
		error->dse_type = DSE_NAMEERROR;
		error->ERR_NAME.DSE_na_problem = DSE_NA_NOSUCHOBJECT;
		error->ERR_NAME.DSE_na_matched = NULLDN;
		return (DS_ERROR_REMOTE);
	}

	switch(find_entry(target,&(arg->mra_common),binddn,NULLDNSEQ,TRUE,&(entryptr), error, di_p))
	{
	case DS_OK:
	    /* Filled out entryptr - carry on */
	    break;
	case DS_CONTINUE:
	    /* Filled out di_p - what do we do with it ?? */
	    return(DS_CONTINUE);

	case DS_X500_ERROR:
	    /* Filled out error - what do we do with it ?? */
	    return(DS_X500_ERROR);
	default:
	    /* SCREAM */
	    LLOG(log_dsap, LLOG_EXCEPTIONS, ("do_ds_modifyrdn() - find_entry failed"));
	    return(DS_ERROR_LOCAL);
	}

	if (read_only || entryptr->e_parent->e_lock) {
		error->dse_type = DSE_SERVICEERROR;
		error->ERR_SERVICE.DSE_sv_problem = DSE_SV_UNAVAILABLE;
		return (DS_ERROR_REMOTE);
	}

	/* Strong authentication  */
	if ((retval = check_security_parms((caddr_t) arg,
			encode_DAS_ModifyRDNArgumentData,
			arg->mra_common.ca_security,
			arg->mra_common.ca_sig, &binddn)) != 0)
	{
		error->dse_type = DSE_SECURITYERROR;
		error->ERR_SECURITY.DSE_sc_problem = retval;
		return (DS_ERROR_REMOTE);
	}

	/* not prepared to accept operation over DSP */
	if (dsp) {
		error->dse_type = DSE_SECURITYERROR;
		error->ERR_SECURITY.DSE_sc_problem = DSE_SC_AUTHENTICATION;
		return (DS_ERROR_REMOTE);
	}

	if ((check_acl (binddn,ACL_WRITE,entryptr->e_acl->ac_entry, target) == NOTOK)
	    || ((entryptr->e_parent->e_data != E_TYPE_CONSTRUCTOR) && (check_acl (binddn,ACL_WRITE,entryptr->e_parent->e_acl->ac_entry, target) == NOTOK)) ) {
		error->dse_type = DSE_SECURITYERROR;
		error->ERR_SECURITY.DSE_sc_problem = DSE_SC_ACCESSRIGHTS;
		return (DS_ERROR_REMOTE);
	}
	if ( ! (entryptr->e_leaf)) {
		error->dse_type = DSE_UPDATEERROR;
		error->ERR_UPDATE.DSE_up_problem = DSE_UP_NOTONNONLEAF;
		return (DS_ERROR_REMOTE);
	}

	/* first check that it is an allowed type */
	for (rdn=arg->mra_newrdn; rdn!=NULLRDN; rdn=rdn->rdn_next)
		if (check_schema_type (entryptr, rdn->rdn_at, error) == NOTOK)
			return (DS_ERROR_REMOTE);

	if (arg->deleterdn)
		for (rdn=entryptr->e_name; rdn!=NULLRDN; rdn=rdn->rdn_next)
			if (remove_attribute (entryptr,rdn->rdn_at,error,binddn,target,entryptr) != OK)
				return (DS_ERROR_REMOTE);


	/* must now add rdn as attribute */
	for (rdn=arg->mra_newrdn; rdn!=NULLRDN; rdn=rdn->rdn_next) {
		avs = avs_comp_new (AttrV_cpy(&rdn->rdn_av));
		as = as_comp_new (AttrT_cpy(rdn->rdn_at),avs, NULLACL_INFO);
		if (addrdn_attribute (entryptr,as,error,binddn,target) != OK)
			return (DS_ERROR_REMOTE);

	}

#ifdef TURBO_DISK
	if (turbo_delete(entryptr) != OK)
		fatal (-34,"mod rdn delete failed - check database");
#endif

	modrdn = entryptr->e_name;
	DATABASE_HEAP;
	entryptr->e_name = rdn_cpy(arg->mra_newrdn);

	modify_attr (entryptr,binddn);
	if (unravel_attribute (entryptr,error) != OK) {
		GENERAL_HEAP;
		LLOG (log_dsap,LLOG_EXCEPTIONS,("modify rdn protocol error"));
		rdn_free (modrdn);
		return (DS_ERROR_REMOTE);
	} else {
		GENERAL_HEAP;
		if (entryptr->e_parent != NULLENTRY) {
			if (entryptr->e_parent->e_edbversion)
				free (entryptr->e_parent->e_edbversion);
			entryptr->e_parent->e_edbversion = new_version();
		}

#ifdef TURBO_DISK
		if (turbo_write(entryptr) != OK)
			fatal (-34,"mod rdn failed - check database");
#else
		if ((journal (entryptr)) != OK)
			fatal (-34,"mod rdn failed - check database");
#endif

		rdn_free (modrdn);
		return (DS_OK);
	}

}


addrdn_attribute (eptr,newas,error,requestor,dn)
Entry eptr;
Attr_Sequence newas;
struct DSError *error;
DN requestor,dn;
{
register Attr_Sequence as;
struct acl_info * acl;

	DLOG (log_dsap,LLOG_DEBUG,("add attribute"));

	if ( (as = as_find_type (eptr->e_attributes,newas->attr_type)) == NULLATTR)
		acl = eptr->e_acl->ac_default;
	else
		acl = as->attr_acl;

	if (check_acl(requestor,ACL_WRITE,acl,dn) == NOTOK) {
		error->dse_type = DSE_SECURITYERROR;
		error->ERR_SECURITY.DSE_sc_problem = DSE_SC_ACCESSRIGHTS;
		DLOG (log_dsap,LLOG_DEBUG,("add acl failed"));
		return (NOTOK);
	}

	eptr->e_attributes = as_merge (newas,eptr->e_attributes);
	return (OK);
}
