]> git.sur5r.net Git - openldap/blob - servers/slapd/mra.c
7373978c3946b745689b5335d36ca757782fe5af
[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( mra->ma_rule_text );
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         ber_len_t length;
41         struct berval type, value, *nvalue;
42         MatchingRuleAssertion *ma;
43
44         ma = ch_malloc( sizeof( MatchingRuleAssertion ) );
45         ma->ma_rule = NULL;
46         ma->ma_rule_text = NULL;
47         ma->ma_desc = NULL;
48         ma->ma_dnattrs = 0;
49         ma->ma_value = NULL;
50
51         rc = ber_scanf( ber, "{t", &tag );
52
53         if( rc == LBER_ERROR ) {
54 #ifdef NEW_LOGGING
55             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
56                        "get_mra: ber_scanf (\"{t\") failure\n" ));
57 #else
58                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
59 #endif
60
61                 *text = "Error parsing matching rule assertion";
62                 mra_free( ma, 1 );
63                 return SLAPD_DISCONNECT;
64         }
65
66         if ( tag == LDAP_FILTER_EXT_OID ) {
67                 rc = ber_scanf( ber, "a", &ma->ma_rule_text );
68                 if ( rc == LBER_ERROR ) {
69 #ifdef NEW_LOGGING
70                     LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
71                                "get_mra: ber_scanf(\"a\") failure.\n" ));
72 #else
73                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for mr\n", 0, 0, 0 );
74 #endif
75
76                         *text = "Error parsing matching rule in matching rule assertion";
77                         mra_free( ma, 1 );
78                         return SLAPD_DISCONNECT;
79                 }
80                 ma->ma_rule = mr_find( ma->ma_rule_text );
81
82                 rc = ber_scanf( ber, "t", &tag );
83
84                 if( rc == LBER_ERROR ) {
85 #ifdef NEW_LOGGING
86                     LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
87                                "get_mra: ber_scanf (\"t\") failure\n" ));
88 #else
89                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
90 #endif
91
92                         *text = "Error parsing matching rule assertion";
93                         mra_free( ma, 1 );
94                         return SLAPD_DISCONNECT;
95                 }
96         }
97
98         if ( tag == LDAP_FILTER_EXT_TYPE ) {
99                 rc = ber_scanf( ber, "o", &type );
100                 if ( rc == LBER_ERROR ) {
101 #ifdef NEW_LOGGING
102                     LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
103                                "get_mra: ber_scanf (\"o\") failure.\n" ));
104 #else
105                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for ad\n", 0, 0, 0 );
106 #endif
107
108                         *text = "Error parsing attribute description in matching rule assertion";
109                         return SLAPD_DISCONNECT;
110                 }
111
112                 rc = slap_bv2ad( &type, &ma->ma_desc, text );
113                 ch_free( type.bv_val );
114
115                 if( rc != LDAP_SUCCESS ) {
116                         ch_free( value.bv_val );
117                         mra_free( ma, 1 );
118                         return rc;
119                 }
120
121                 rc = ber_scanf( ber, "t", &tag );
122
123                 if( rc == LBER_ERROR ) {
124 #ifdef NEW_LOGGING
125                     LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
126                                "get_mra: ber_scanf (\"t\") failure.\n" ));
127 #else
128                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
129 #endif
130
131                         *text = "Error parsing matching rule assertion";
132                         mra_free( ma, 1 );
133                         return SLAPD_DISCONNECT;
134                 }
135         }
136
137         if ( tag != LDAP_FILTER_EXT_VALUE ) {
138 #ifdef NEW_LOGGING
139             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
140                        "get_mra: ber_scanf missing value\n" ));
141 #else
142                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf missing value\n", 0, 0, 0 );
143 #endif
144
145                 *text = "Missing value in matching rule assertion";
146                 mra_free( ma, 1 );
147                 return SLAPD_DISCONNECT;
148         }
149
150         rc = ber_scanf( ber, "o", &value );
151
152         if( rc == LBER_ERROR ) {
153 #ifdef NEW_LOGGING
154             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
155                        "get_mra: ber_scanf (\"o\") failure.\n" ));
156 #else
157                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
158 #endif
159
160                 *text = "Error decoding value in matching rule assertion";
161                 mra_free( ma, 1 );
162                 return SLAPD_DISCONNECT;
163         }
164
165         /*
166          * OK, if no matching rule, normalize for equality, otherwise
167          * normalize for the matching rule.
168          */
169         rc = value_normalize( ma->ma_desc, SLAP_MR_EQUALITY, &value, &nvalue, text );
170         ch_free( value.bv_val );
171
172         if( rc != LDAP_SUCCESS ) {
173                 mra_free( ma, 1 );
174                 return rc;
175         }
176
177         ma->ma_value = nvalue;
178
179         tag = ber_peek_tag( ber, &length );
180
181         if ( tag == LDAP_FILTER_EXT_DNATTRS ) {
182                 rc = ber_scanf( ber, "b}", &ma->ma_dnattrs );
183         } else {
184                 rc = ber_scanf( ber, "}" );
185                 ma->ma_dnattrs = 0;
186         }
187
188         if( rc == LBER_ERROR ) {
189 #ifdef NEW_LOGGING
190             LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
191                        "get_mra: ber_scanf failure\n"));
192 #else
193                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
194 #endif
195
196                 *text = "Error decoding dnattrs matching rule assertion";
197                 mra_free( ma, 1 );
198                 return SLAPD_DISCONNECT;
199         }
200
201         *mra = ma;
202
203         return LDAP_SUCCESS;
204 }
205