]> git.sur5r.net Git - openldap/blob - servers/slapd/mra.c
d6fc66b58ad5d4208af539d71d13d9bfbf46c652
[openldap] / servers / slapd / mra.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* mra.c - routines for dealing with extensible matching rule assertions */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16
17
18 void
19 mra_free(
20     MatchingRuleAssertion *mra,
21     int freeit
22 )
23 {
24         ad_free( mra->ma_desc, 1 );
25         ch_free( (char *) mra->ma_rule );
26         ber_bvfree( mra->ma_value );
27         if ( freeit ) {
28                 ch_free( (char *) mra );
29         }
30 }
31
32 int
33 get_mra(
34     BerElement  *ber,
35     MatchingRuleAssertion       **mra,
36         const char **text
37 )
38 {
39         int rc, tag;
40         struct berval type, value, *nvalue;
41         MatchingRuleAssertion *ma;
42
43         ma = ch_malloc( sizeof( MatchingRuleAssertion ) );
44         ma->ma_rule = NULL;
45         ma->ma_desc = NULL;
46         ma->ma_dnattrs = 0;
47         ma->ma_value = NULL;
48
49         rc = ber_scanf( ber, "{t", &tag );
50
51         if( rc == LBER_ERROR ) {
52                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
53                 *text = "Error parsing matching rule assertion";
54                 return SLAPD_DISCONNECT;
55         }
56
57         if ( tag == LDAP_FILTER_EXT_OID ) {
58                 rc = ber_scanf( ber, "a", &ma->ma_rule );
59                 if ( rc == LBER_ERROR ) {
60                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for mr\n", 0, 0, 0 );
61                         *text = "Error parsing matching rule in matching rule assertion";
62                         return SLAPD_DISCONNECT;
63                 }
64
65                 rc = ber_scanf( ber, "t", &tag );
66
67                 if( rc == LBER_ERROR ) {
68                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
69                         *text = "Error parsing matching rule assertion";
70                         return SLAPD_DISCONNECT;
71                 }
72         }
73
74         if ( tag == LDAP_FILTER_EXT_TYPE ) {
75                 rc = ber_scanf( ber, "o", &type );
76                 if ( rc == LBER_ERROR ) {
77                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for ad\n", 0, 0, 0 );
78                         *text = "Error parsing attribute description in matching rule assertion";
79                         return SLAPD_DISCONNECT;
80                 }
81
82                 rc = slap_bv2ad( &type, &ma->ma_desc, text );
83                 ch_free( type.bv_val );
84
85                 if( rc != LDAP_SUCCESS ) {
86                         ch_free( value.bv_val );
87                         mra_free( ma, 1 );
88                         return rc;
89                 }
90
91                 rc = ber_scanf( ber, "t", &tag );
92
93                 if( rc == LBER_ERROR ) {
94                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
95                         *text = "Error parsing matching rule assertion";
96                         return SLAPD_DISCONNECT;
97                 }
98         }
99
100         rc = ber_scanf( ber, "o", &value );
101
102         if( rc == LBER_ERROR ) {
103                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
104                 *text = "Error decoding value in matching rule assertion";
105                 return SLAPD_DISCONNECT;
106         }
107
108         /*
109          * OK, if no matching rule, normalize for equality, otherwise
110          * normalize for the matching rule.
111          */
112         rc = value_normalize( ma->ma_desc, SLAP_MR_EQUALITY, &value, &nvalue, text );
113         ch_free( value.bv_val );
114
115         if( rc != LDAP_SUCCESS ) {
116                 ad_free( ma->ma_desc, 1 );
117                 ch_free( ma );
118                 return rc;
119         }
120
121         ma->ma_value = nvalue;
122         *mra = ma;
123
124         return LDAP_SUCCESS;
125 }
126