1 /* Copyright (c) 2003 by International Business Machines, Inc.
3 * International Business Machines, Inc. (hereinafter called IBM) grants
4 * permission under its copyrights to use, copy, modify, and distribute this
5 * Software with or without fee, provided that the above copyright notice and
6 * all paragraphs of this notice appear in all copies, and that the name of IBM
7 * not be used in connection with the marketing of any product incorporating
8 * the Software or modifications thereof, without specific, written prior
11 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
12 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
13 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
14 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
15 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
16 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
23 #include <ac/socket.h>
24 #include <ac/string.h>
28 #include "../back-ldap/back-ldap.h"
29 #include "back-meta.h"
31 #undef ldap_debug /* silence a warning in ldap-int.h */
33 #include "../../../libraries/libldap/ldap-int.h"
36 #include "back-meta.h"
38 static char* invert_string(char* string);
39 static struct berval* merge_init_final(struct berval*, struct berval*, struct berval*);
40 static int strings_containment(struct berval* stored, struct berval* incoming);
42 /* find and remove string2 from string1
43 * from start if position = 1,
44 * from end if position = 3,
45 * from anywhere if position = 2
49 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
56 char* string1=ber1->bv_val;
57 char* string2=ber2->bv_val;
65 arg1 = invert_string(string1);
66 arg2 = invert_string(string2);
72 temp = strstr(arg1, arg2);
92 temp = strstr(arg1, arg2);
96 string1 = invert_string(arg1+len);
107 temp = (char*) malloc( strlen( string1 ) + 1 );
108 strcpy( temp, string1 );
109 free( ber1->bv_val );
115 invert_string( char* string )
117 int len = strlen(string);
120 char* inverted = (char*)(malloc(len+1));
122 for (i=0; i<len; i++)
123 inverted[i] = string[len-i-1];
131 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
133 struct berval* merged, *temp;
134 int i, any_count, count;
136 for (any_count=0; any && any[any_count].bv_val; any_count++)
146 merged = (struct berval*)(malloc((count+1)*sizeof(struct berval)));
150 ber_dupbv(temp, init);
154 for (i=0; i<any_count; i++) {
155 ber_dupbv(temp, any);
161 ber_dupbv(temp, final);
170 strings_containment(struct berval* stored, struct berval* incoming)
172 struct berval* element;
176 for ( element=stored; element->bv_val != NULL; element++ ) {
177 for (j = k; incoming[j].bv_val != NULL; j++) {
178 if (find_and_remove(&(incoming[j]), element, 2)) {
195 substr_containment_substr(Filter* stored, Filter* incoming)
202 struct berval* init_incoming = (struct berval*)(malloc(sizeof(struct berval)));
203 struct berval* final_incoming = (struct berval*)(malloc(sizeof(struct berval)));
204 struct berval* any_incoming = NULL;
205 struct berval* remaining_incoming;
206 struct berval* any_element;
208 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
209 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
213 ber_dupbv(init_incoming, &(incoming->f_sub_initial));
214 ber_dupbv(final_incoming, &(incoming->f_sub_final));
216 if (incoming->f_sub_any) {
217 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
221 any_incoming = (struct berval*)malloc((any_count+1) *
222 sizeof(struct berval));
224 for (i=0; i<any_count; i++) {
225 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
227 any_incoming[any_count].bv_val = NULL;
228 any_incoming[any_count].bv_len = 0;
231 if (find_and_remove(init_incoming,
232 &(stored->f_sub_initial), 1) && find_and_remove(final_incoming,
233 &(stored->f_sub_final), 3))
235 if (stored->f_sub_any == NULL) {
239 remaining_incoming = merge_init_final(init_incoming,
240 any_incoming, final_incoming);
241 rc = strings_containment(stored->f_sub_any, remaining_incoming);
247 ber_bvfree(init_incoming);
248 ber_bvfree(final_incoming);
250 for (i=0; i < any_count; i++)
251 free(any_incoming[i].bv_val);
260 substr_containment_equality(Filter* stored, Filter* incoming)
263 struct berval* incoming_val = (struct berval*)(malloc(2*sizeof(struct berval)));
266 ber_dupbv(incoming_val, &(incoming->f_av_value));
267 incoming_val[1].bv_val = NULL;
268 incoming_val[1].bv_len = 0;
270 if (find_and_remove(incoming_val,
271 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
272 &(stored->f_sub_final), 3)) {
273 if (stored->f_sub_any == NULL){
277 rc = strings_containment(stored->f_sub_any, incoming_val);
283 if(incoming_val[0].bv_val)
284 free(incoming_val[0].bv_val);