1/*	$NetBSD: ava.c,v 1.1.1.3 2010/12/12 15:22:18 adam Exp $	*/
2
3/* ava.c - routines for dealing with attribute value assertions */
4/* OpenLDAP: pkg/ldap/servers/slapd/ava.c,v 1.45.2.6 2010/04/13 20:23:11 kurt Exp */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 1998-2010 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 "portable.h"
30
31#include <stdio.h>
32
33#include <ac/string.h>
34#include <ac/socket.h>
35
36#include "slap.h"
37
38#ifdef LDAP_COMP_MATCH
39#include "component.h"
40#endif
41
42void
43ava_free(
44	Operation *op,
45	AttributeAssertion *ava,
46	int	freeit )
47{
48#ifdef LDAP_COMP_MATCH
49	if ( ava->aa_cf && ava->aa_cf->cf_ca->ca_comp_data.cd_mem_op )
50		nibble_mem_free ( ava->aa_cf->cf_ca->ca_comp_data.cd_mem_op );
51#endif
52	op->o_tmpfree( ava->aa_value.bv_val, op->o_tmpmemctx );
53	if ( ava->aa_desc->ad_flags & SLAP_DESC_TEMPORARY )
54		op->o_tmpfree( ava->aa_desc, op->o_tmpmemctx );
55	if ( freeit ) op->o_tmpfree( (char *) ava, op->o_tmpmemctx );
56}
57
58int
59get_ava(
60	Operation *op,
61	BerElement	*ber,
62	Filter *f,
63	unsigned usage,
64	const char **text )
65{
66	int rc;
67	ber_tag_t rtag;
68	struct berval type, value;
69	AttributeAssertion *aa;
70#ifdef LDAP_COMP_MATCH
71	AttributeAliasing* a_alias = NULL;
72#endif
73
74	rtag = ber_scanf( ber, "{mm}", &type, &value );
75
76	if( rtag == LBER_ERROR ) {
77		Debug( LDAP_DEBUG_ANY, "  get_ava ber_scanf\n", 0, 0, 0 );
78		*text = "Error decoding attribute value assertion";
79		return SLAPD_DISCONNECT;
80	}
81
82	aa = op->o_tmpalloc( sizeof( AttributeAssertion ), op->o_tmpmemctx );
83	aa->aa_desc = NULL;
84	aa->aa_value.bv_val = NULL;
85#ifdef LDAP_COMP_MATCH
86	aa->aa_cf = NULL;
87#endif
88
89	rc = slap_bv2ad( &type, &aa->aa_desc, text );
90
91	if( rc != LDAP_SUCCESS ) {
92		f->f_choice |= SLAPD_FILTER_UNDEFINED;
93		*text = NULL;
94		rc = slap_bv2undef_ad( &type, &aa->aa_desc, text,
95				SLAP_AD_PROXIED|SLAP_AD_NOINSERT );
96
97		if( rc != LDAP_SUCCESS ) {
98			Debug( LDAP_DEBUG_FILTER,
99			"get_ava: unknown attributeType %s\n", type.bv_val, 0, 0 );
100			aa->aa_desc = slap_bv2tmp_ad( &type, op->o_tmpmemctx );
101			ber_dupbv_x( &aa->aa_value, &value, op->o_tmpmemctx );
102			f->f_ava = aa;
103			return rc;
104		}
105	}
106
107	rc = asserted_value_validate_normalize(
108		aa->aa_desc, ad_mr(aa->aa_desc, usage),
109		usage, &value, &aa->aa_value, text, op->o_tmpmemctx );
110
111	if( rc != LDAP_SUCCESS ) {
112		f->f_choice |= SLAPD_FILTER_UNDEFINED;
113		Debug( LDAP_DEBUG_FILTER,
114		"get_ava: illegal value for attributeType %s\n", type.bv_val, 0, 0 );
115		ber_dupbv_x( &aa->aa_value, &value, op->o_tmpmemctx );
116		*text = NULL;
117		rc = LDAP_SUCCESS;
118	}
119
120#ifdef LDAP_COMP_MATCH
121	if( is_aliased_attribute ) {
122		a_alias = is_aliased_attribute ( aa->aa_desc );
123		if ( a_alias ) {
124			rc = get_aliased_filter_aa ( op, aa, a_alias, text );
125			if( rc != LDAP_SUCCESS ) {
126				Debug( LDAP_DEBUG_FILTER,
127						"get_ava:Invalid Attribute Aliasing\n", 0, 0, 0 );
128				return rc;
129			}
130		}
131	}
132#endif
133	f->f_ava = aa;
134	return LDAP_SUCCESS;
135}
136