Home | History | Annotate | Line # | Download | only in slapd
      1 /*	$NetBSD: ava.c,v 1.4 2025/09/05 21:16:24 christos Exp $	*/
      2 
      3 /* ava.c - routines for dealing with attribute value assertions */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 1998-2024 The OpenLDAP Foundation.
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted only as authorized by the OpenLDAP
     12  * Public License.
     13  *
     14  * A copy of this license is available in the file LICENSE in the
     15  * top-level directory of the distribution or, alternatively, at
     16  * <http://www.OpenLDAP.org/license.html>.
     17  */
     18 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
     19  * All rights reserved.
     20  *
     21  * Redistribution and use in source and binary forms are permitted
     22  * provided that this notice is preserved and that due credit is given
     23  * to the University of Michigan at Ann Arbor. The name of the University
     24  * may not be used to endorse or promote products derived from this
     25  * software without specific prior written permission. This software
     26  * is provided ``as is'' without express or implied warranty.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __RCSID("$NetBSD: ava.c,v 1.4 2025/09/05 21:16:24 christos Exp $");
     31 
     32 #include "portable.h"
     33 
     34 #include <stdio.h>
     35 
     36 #include <ac/string.h>
     37 #include <ac/socket.h>
     38 
     39 #include "slap.h"
     40 
     41 #ifdef LDAP_COMP_MATCH
     42 #include "component.h"
     43 #endif
     44 
     45 void
     46 ava_free(
     47 	Operation *op,
     48 	AttributeAssertion *ava,
     49 	int	freeit )
     50 {
     51 #ifdef LDAP_COMP_MATCH
     52 	if ( ava->aa_cf && ava->aa_cf->cf_ca->ca_comp_data.cd_mem_op )
     53 		nibble_mem_free ( ava->aa_cf->cf_ca->ca_comp_data.cd_mem_op );
     54 #endif
     55 	op->o_tmpfree( ava->aa_value.bv_val, op->o_tmpmemctx );
     56 	if ( ava->aa_desc->ad_flags & SLAP_DESC_TEMPORARY )
     57 		op->o_tmpfree( ava->aa_desc, op->o_tmpmemctx );
     58 	if ( freeit ) op->o_tmpfree( (char *) ava, op->o_tmpmemctx );
     59 }
     60 
     61 AttributeAssertion *
     62 ava_dup(
     63 	AttributeAssertion *ava,
     64 	void *memctx )
     65 {
     66 	BerMemoryFunctions *mf = &slap_sl_mfuncs;
     67 	AttributeAssertion *nava;
     68 
     69 	nava = mf->bmf_malloc( sizeof(AttributeAssertion), memctx );
     70 	*nava = *ava;
     71 	if ( ava->aa_desc->ad_flags & SLAP_DESC_TEMPORARY )
     72 		nava->aa_desc = slap_bv2tmp_ad( &ava->aa_desc->ad_cname, memctx );
     73 	ber_dupbv_x( &nava->aa_value, &ava->aa_value, memctx );
     74 	return nava;
     75 }
     76 
     77 int
     78 get_ava(
     79 	Operation *op,
     80 	BerElement	*ber,
     81 	Filter *f,
     82 	unsigned usage,
     83 	const char **text )
     84 {
     85 	int rc;
     86 	ber_tag_t rtag;
     87 	struct berval type, value;
     88 	AttributeAssertion *aa;
     89 #ifdef LDAP_COMP_MATCH
     90 	AttributeAliasing* a_alias = NULL;
     91 #endif
     92 
     93 	rtag = ber_scanf( ber, "{mm}", &type, &value );
     94 
     95 	if( rtag == LBER_ERROR ) {
     96 		Debug( LDAP_DEBUG_ANY, "  get_ava ber_scanf\n" );
     97 		*text = "Error decoding attribute value assertion";
     98 		return SLAPD_DISCONNECT;
     99 	}
    100 
    101 	aa = op->o_tmpalloc( sizeof( AttributeAssertion ), op->o_tmpmemctx );
    102 	aa->aa_desc = NULL;
    103 	aa->aa_value.bv_val = NULL;
    104 #ifdef LDAP_COMP_MATCH
    105 	aa->aa_cf = NULL;
    106 #endif
    107 
    108 	rc = slap_bv2ad( &type, &aa->aa_desc, text );
    109 
    110 	if( rc != LDAP_SUCCESS ) {
    111 		f->f_choice |= SLAPD_FILTER_UNDEFINED;
    112 		*text = NULL;
    113 		rc = slap_bv2undef_ad( &type, &aa->aa_desc, text,
    114 				SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
    115 
    116 		if( rc != LDAP_SUCCESS ) {
    117 			Debug( LDAP_DEBUG_FILTER,
    118 			"get_ava: unknown attributeType %s\n", type.bv_val );
    119 			aa->aa_desc = slap_bv2tmp_ad( &type, op->o_tmpmemctx );
    120 			ber_dupbv_x( &aa->aa_value, &value, op->o_tmpmemctx );
    121 			f->f_ava = aa;
    122 			return LDAP_SUCCESS;
    123 		}
    124 	}
    125 
    126 	rc = asserted_value_validate_normalize(
    127 		aa->aa_desc, ad_mr(aa->aa_desc, usage),
    128 		usage, &value, &aa->aa_value, text, op->o_tmpmemctx );
    129 
    130 	if( rc != LDAP_SUCCESS ) {
    131 		f->f_choice |= SLAPD_FILTER_UNDEFINED;
    132 		Debug( LDAP_DEBUG_FILTER,
    133 		"get_ava: illegal value for attributeType %s\n", type.bv_val );
    134 		ber_dupbv_x( &aa->aa_value, &value, op->o_tmpmemctx );
    135 		*text = NULL;
    136 		rc = LDAP_SUCCESS;
    137 	}
    138 
    139 #ifdef LDAP_COMP_MATCH
    140 	if( is_aliased_attribute ) {
    141 		a_alias = is_aliased_attribute ( aa->aa_desc );
    142 		if ( a_alias ) {
    143 			rc = get_aliased_filter_aa ( op, aa, a_alias, text );
    144 			if( rc != LDAP_SUCCESS ) {
    145 				Debug( LDAP_DEBUG_FILTER,
    146 						"get_ava: Invalid Attribute Aliasing\n" );
    147 				return rc;
    148 			}
    149 		}
    150 	}
    151 #endif
    152 	f->f_ava = aa;
    153 	return LDAP_SUCCESS;
    154 }
    155