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"
39 static char* invert_string(char* string);
40 static struct berval* merge_init_final(struct berval*, struct berval*, struct berval*);
41 static int strings_containment(struct berval* stored, struct berval* incoming);
43 /* find and remove string2 from string1
44 * from start if position = 1,
45 * from end if position = 3,
46 * from anywhere if position = 2
50 find_and_remove(struct berval* ber1, struct berval* ber2, int position)
57 char* string1=ber1->bv_val;
58 char* string2=ber2->bv_val;
66 arg1 = invert_string(string1);
67 arg2 = invert_string(string2);
73 temp = strstr(arg1, arg2);
93 temp = strstr(arg1, arg2);
97 string1 = invert_string(arg1+len);
108 temp = (char*) malloc( strlen( string1 ) + 1 );
109 strcpy( temp, string1 );
110 free( ber1->bv_val );
116 invert_string( char* string )
118 int len = strlen(string);
121 char* inverted = (char*)(malloc(len+1));
123 for (i=0; i<len; i++)
124 inverted[i] = string[len-i-1];
132 merge_init_final(struct berval* init, struct berval* any, struct berval* final)
134 struct berval* merged, *temp;
135 int i, any_count, count;
137 for (any_count=0; any && any[any_count].bv_val; any_count++)
147 merged = (struct berval*)(malloc((count+1)*sizeof(struct berval)));
151 ber_dupbv(temp, init);
155 for (i=0; i<any_count; i++) {
156 ber_dupbv(temp, any);
162 ber_dupbv(temp, final);
171 strings_containment(struct berval* stored, struct berval* incoming)
173 struct berval* element;
177 for ( element=stored; element->bv_val != NULL; element++ ) {
178 for (j = k; incoming[j].bv_val != NULL; j++) {
179 if (find_and_remove(&(incoming[j]), element, 2)) {
196 substr_containment_substr(Filter* stored, Filter* incoming)
203 struct berval* init_incoming = (struct berval*)(malloc(sizeof(struct berval)));
204 struct berval* final_incoming = (struct berval*)(malloc(sizeof(struct berval)));
205 struct berval* any_incoming = NULL;
206 struct berval* remaining_incoming;
207 struct berval* any_element;
209 if ((!(incoming->f_sub_initial.bv_val) && (stored->f_sub_initial.bv_val))
210 || (!(incoming->f_sub_final.bv_val) && (stored->f_sub_final.bv_val)))
214 ber_dupbv(init_incoming, &(incoming->f_sub_initial));
215 ber_dupbv(final_incoming, &(incoming->f_sub_final));
217 if (incoming->f_sub_any) {
218 for ( any_count=0; incoming->f_sub_any[any_count].bv_val != NULL;
222 any_incoming = (struct berval*)malloc((any_count+1) *
223 sizeof(struct berval));
225 for (i=0; i<any_count; i++) {
226 ber_dupbv(&(any_incoming[i]), &(incoming->f_sub_any[i]));
228 any_incoming[any_count].bv_val = NULL;
229 any_incoming[any_count].bv_len = 0;
232 if (find_and_remove(init_incoming,
233 &(stored->f_sub_initial), 1) && find_and_remove(final_incoming,
234 &(stored->f_sub_final), 3))
236 if (stored->f_sub_any == NULL) {
240 remaining_incoming = merge_init_final(init_incoming,
241 any_incoming, final_incoming);
242 rc = strings_containment(stored->f_sub_any, remaining_incoming);
248 ber_bvfree(init_incoming);
249 ber_bvfree(final_incoming);
251 for (i=0; i < any_count; i++)
252 free(any_incoming[i].bv_val);
261 substr_containment_equality(Filter* stored, Filter* incoming)
264 struct berval* incoming_val = (struct berval*)(malloc(2*sizeof(struct berval)));
267 ber_dupbv(incoming_val, &(incoming->f_av_value));
268 incoming_val[1].bv_val = NULL;
269 incoming_val[1].bv_len = 0;
271 if (find_and_remove(incoming_val,
272 &(stored->f_sub_initial), 1) && find_and_remove(incoming_val,
273 &(stored->f_sub_final), 3)) {
274 if (stored->f_sub_any == NULL){
278 rc = strings_containment(stored->f_sub_any, incoming_val);
284 if(incoming_val[0].bv_val)
285 free(incoming_val[0].bv_val);