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"
30 #undef ldap_debug /* silence a warning in ldap-int.h */
31 #include "../../../libraries/libldap/ldap-int.h"
38 AttributeName** filter_attrs,
40 struct exception* result )
49 * FIXME: should we use an assert here?
52 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
53 result->type = FILTER_ERR;
57 switch ( f->f_choice ) {
58 case LDAP_FILTER_EQUALITY:
59 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
60 ( sizeof("(=)") - 1 );
61 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
63 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
64 f->f_av_desc->ad_cname.bv_val );
66 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
67 (*filter_cnt + 2)*sizeof(AttributeName));
70 ber_dupbv(&(*filter_attrs)[*filter_cnt].an_name,
71 &f->f_av_desc->ad_cname);
74 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
75 (*filter_attrs)[*filter_cnt].an_desc = NULL;
76 slap_bv2ad(&f->f_av_desc->ad_cname,
77 &(*filter_attrs)[*filter_cnt].an_desc, &text);
78 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
79 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
83 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
84 + ( sizeof("(>=)") - 1 );
85 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
87 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=)",
88 f->f_av_desc->ad_cname.bv_val);
90 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
91 (*filter_cnt + 2)*sizeof(AttributeName));
93 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
94 &f->f_av_desc->ad_cname);
97 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
98 (*filter_attrs)[*filter_cnt].an_desc = NULL;
99 slap_bv2ad(&f->f_av_desc->ad_cname,
100 &(*filter_attrs)[*filter_cnt].an_desc, &text);
101 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
102 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
108 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
109 + ( sizeof("(<=)") - 1 );
110 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
112 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=)",
113 f->f_av_desc->ad_cname.bv_val);
115 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
116 (*filter_cnt + 2)*sizeof(AttributeName));
118 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
119 &f->f_av_desc->ad_cname);
122 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
123 (*filter_attrs)[*filter_cnt].an_desc = NULL;
124 slap_bv2ad(&f->f_av_desc->ad_cname,
125 &(*filter_attrs)[*filter_cnt].an_desc, &text);
126 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
127 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
132 case LDAP_FILTER_APPROX:
133 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
134 + ( sizeof("(~=)") - 1 );
135 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
137 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=)",
138 f->f_av_desc->ad_cname.bv_val);
140 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
141 (*filter_cnt + 2)*sizeof(AttributeName));
144 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
145 &f->f_av_desc->ad_cname);
148 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
149 (*filter_attrs)[*filter_cnt].an_desc = NULL;
150 slap_bv2ad(&f->f_av_desc->ad_cname,
151 &(*filter_attrs)[*filter_cnt].an_desc, &text);
152 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
153 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
158 case LDAP_FILTER_SUBSTRINGS:
159 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
160 ( sizeof("(=)") - 1 );
161 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
163 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
164 f->f_sub_desc->ad_cname.bv_val );
166 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
167 (*filter_cnt + 2)*sizeof(AttributeName));
170 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
171 &f->f_av_desc->ad_cname);
174 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
175 (*filter_attrs)[*filter_cnt].an_desc = NULL;
176 slap_bv2ad(&f->f_av_desc->ad_cname,
177 &(*filter_attrs)[*filter_cnt].an_desc, &text);
178 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
179 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
184 case LDAP_FILTER_PRESENT:
185 fstr->bv_len = f->f_desc->ad_cname.bv_len +
186 ( sizeof("(=*)") - 1 );
187 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
189 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
190 f->f_desc->ad_cname.bv_val );
192 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
193 (*filter_cnt+2)*sizeof(AttributeName));
196 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
197 &f->f_av_desc->ad_cname);
200 (*filter_attrs)[*filter_cnt].an_name = f->f_desc->ad_cname;
201 (*filter_attrs)[*filter_cnt].an_desc = NULL;
202 slap_bv2ad(&f->f_desc->ad_cname,
203 &(*filter_attrs)[*filter_cnt].an_desc, &text);
204 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
205 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
210 case LDAP_FILTER_AND:
212 case LDAP_FILTER_NOT:
213 fstr->bv_len = sizeof("(%)") - 1;
214 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
216 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
217 f->f_choice == LDAP_FILTER_AND ? '&' :
218 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
220 for ( p = f->f_list; p != NULL; p = p->f_next ) {
223 filter2template( p, &tmp, filter_attrs, filter_cnt,
225 if (result->type != SUCCESS) {
229 fstr->bv_len += tmp.bv_len;
230 fstr->bv_val = ch_realloc( fstr->bv_val,
233 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
234 /*"("*/ "%s)", tmp.bv_val );
236 ch_free( tmp.bv_val );
242 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
243 result->type = FILTER_ERR;
247 #endif /* LDAP_CACHING */