]> git.sur5r.net Git - openldap/blob - servers/slapd/mra.c
Modify ad_cmp() macro to support use as an ordering function.
[openldap] / servers / slapd / mra.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #if 0   /* no longer a malloc'd string */
25         ch_free( mra->ma_rule_text.bv_val );
26 #endif
27         ch_free( mra->ma_value.bv_val );
28         if ( freeit ) {
29                 ch_free( (char *) mra );
30         }
31 }
32
33 int
34 get_mra(
35     BerElement  *ber,
36     MatchingRuleAssertion       **mra,
37         const char **text
38 )
39 {
40         int rc, tag;
41         ber_len_t length;
42         struct berval type, value;
43         MatchingRuleAssertion *ma;
44
45         ma = ch_malloc( sizeof( MatchingRuleAssertion ) );
46         ma->ma_rule = NULL;
47         ma->ma_rule_text.bv_val = NULL;
48         ma->ma_rule_text.bv_len = 0;
49         ma->ma_desc = NULL;
50         ma->ma_dnattrs = 0;
51         ma->ma_value.bv_val = NULL;
52
53         rc = ber_scanf( ber, "{t", &tag );
54
55         if( rc == LBER_ERROR ) {
56 #ifdef NEW_LOGGING
57                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
58                            "get_mra: ber_scanf (\"{t\") failure\n" ));
59 #else
60                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
61 #endif
62
63                 *text = "Error parsing matching rule assertion";
64                 mra_free( ma, 1 );
65                 return SLAPD_DISCONNECT;
66         }
67
68         if ( tag == LDAP_FILTER_EXT_OID ) {
69                 rc = ber_scanf( ber, "m", &ma->ma_rule_text );
70                 if ( rc == LBER_ERROR ) {
71 #ifdef NEW_LOGGING
72                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
73                                    "get_mra: ber_scanf(\"o\") failure.\n" ));
74 #else
75                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for mr\n", 0, 0, 0 );
76 #endif
77
78                         *text = "Error parsing matching rule in matching rule assertion";
79                         mra_free( ma, 1 );
80                         return SLAPD_DISCONNECT;
81                 }
82                 ma->ma_rule = mr_bvfind( &ma->ma_rule_text );
83
84                 rc = ber_scanf( ber, "t", &tag );
85
86                 if( rc == LBER_ERROR ) {
87 #ifdef NEW_LOGGING
88                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
89                                    "get_mra: ber_scanf (\"t\") failure\n" ));
90 #else
91                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
92 #endif
93
94                         *text = "Error parsing matching rule assertion";
95                         mra_free( ma, 1 );
96                         return SLAPD_DISCONNECT;
97                 }
98         }
99
100         if ( tag == LDAP_FILTER_EXT_TYPE ) {
101                 rc = ber_scanf( ber, "m", &type );
102                 if ( rc == LBER_ERROR ) {
103 #ifdef NEW_LOGGING
104                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
105                                    "get_mra: ber_scanf (\"o\") failure.\n" ));
106 #else
107                         Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf for ad\n", 0, 0, 0 );
108 #endif
109
110                         *text = "Error parsing attribute description in matching rule assertion";
111                         return SLAPD_DISCONNECT;
112                 }
113
114                 rc = slap_bv2ad( &type, &ma->ma_desc, text );
115
116                 if( rc != LDAP_SUCCESS ) {
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, "m", &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, &ma->ma_value, text );
170
171         if( rc != LDAP_SUCCESS ) {
172                 mra_free( ma, 1 );
173                 return rc;
174         }
175
176         tag = ber_peek_tag( ber, &length );
177
178         if ( tag == LDAP_FILTER_EXT_DNATTRS ) {
179                 rc = ber_scanf( ber, "b}", &ma->ma_dnattrs );
180         } else {
181                 rc = ber_scanf( ber, "}" );
182                 ma->ma_dnattrs = 0;
183         }
184
185         if( rc == LBER_ERROR ) {
186 #ifdef NEW_LOGGING
187                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
188                            "get_mra: ber_scanf failure\n"));
189 #else
190                 Debug( LDAP_DEBUG_ANY, "  get_mra ber_scanf\n", 0, 0, 0 );
191 #endif
192
193                 *text = "Error decoding dnattrs matching rule assertion";
194                 mra_free( ma, 1 );
195                 return SLAPD_DISCONNECT;
196         }
197
198         *mra = ma;
199
200         return LDAP_SUCCESS;
201 }
202