2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
5 * Portions Copyright 2003 IBM Corporation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Apurva Kumar for inclusion
18 * in OpenLDAP Software based, in part, on existing OpenLDAP Software.
25 #include <ac/socket.h>
26 #include <ac/string.h>
30 #include "../back-ldap/back-ldap.h"
31 #include "back-meta.h"
32 #undef ldap_debug /* silence a warning in ldap-int.h */
33 #include "../../../libraries/libldap/ldap-int.h"
39 AttributeName** filter_attrs,
41 struct exception* result )
50 * FIXME: should we use an assert here?
53 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
54 result->type = FILTER_ERR;
58 switch ( f->f_choice ) {
59 case LDAP_FILTER_EQUALITY:
60 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
61 ( sizeof("(=)") - 1 );
62 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
64 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
65 f->f_av_desc->ad_cname.bv_val );
67 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
68 (*filter_cnt + 2)*sizeof(AttributeName));
71 ber_dupbv(&(*filter_attrs)[*filter_cnt].an_name,
72 &f->f_av_desc->ad_cname);
75 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
76 (*filter_attrs)[*filter_cnt].an_desc = NULL;
77 slap_bv2ad(&f->f_av_desc->ad_cname,
78 &(*filter_attrs)[*filter_cnt].an_desc, &text);
79 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
80 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
84 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
85 + ( sizeof("(>=)") - 1 );
86 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
88 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=)",
89 f->f_av_desc->ad_cname.bv_val);
91 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
92 (*filter_cnt + 2)*sizeof(AttributeName));
94 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
95 &f->f_av_desc->ad_cname);
98 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
99 (*filter_attrs)[*filter_cnt].an_desc = NULL;
100 slap_bv2ad(&f->f_av_desc->ad_cname,
101 &(*filter_attrs)[*filter_cnt].an_desc, &text);
102 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
103 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
109 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
110 + ( sizeof("(<=)") - 1 );
111 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
113 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=)",
114 f->f_av_desc->ad_cname.bv_val);
116 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
117 (*filter_cnt + 2)*sizeof(AttributeName));
119 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
120 &f->f_av_desc->ad_cname);
123 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
124 (*filter_attrs)[*filter_cnt].an_desc = NULL;
125 slap_bv2ad(&f->f_av_desc->ad_cname,
126 &(*filter_attrs)[*filter_cnt].an_desc, &text);
127 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
128 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
133 case LDAP_FILTER_APPROX:
134 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
135 + ( sizeof("(~=)") - 1 );
136 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
138 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=)",
139 f->f_av_desc->ad_cname.bv_val);
141 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
142 (*filter_cnt + 2)*sizeof(AttributeName));
145 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
146 &f->f_av_desc->ad_cname);
149 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
150 (*filter_attrs)[*filter_cnt].an_desc = NULL;
151 slap_bv2ad(&f->f_av_desc->ad_cname,
152 &(*filter_attrs)[*filter_cnt].an_desc, &text);
153 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
154 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
159 case LDAP_FILTER_SUBSTRINGS:
160 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
161 ( sizeof("(=)") - 1 );
162 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
164 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
165 f->f_sub_desc->ad_cname.bv_val );
167 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
168 (*filter_cnt + 2)*sizeof(AttributeName));
171 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
172 &f->f_av_desc->ad_cname);
175 (*filter_attrs)[*filter_cnt].an_name = f->f_av_desc->ad_cname;
176 (*filter_attrs)[*filter_cnt].an_desc = NULL;
177 slap_bv2ad(&f->f_av_desc->ad_cname,
178 &(*filter_attrs)[*filter_cnt].an_desc, &text);
179 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
180 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
185 case LDAP_FILTER_PRESENT:
186 fstr->bv_len = f->f_desc->ad_cname.bv_len +
187 ( sizeof("(=*)") - 1 );
188 fstr->bv_val = ch_malloc( fstr->bv_len + 1 );
190 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
191 f->f_desc->ad_cname.bv_val );
193 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
194 (*filter_cnt+2)*sizeof(AttributeName));
197 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
198 &f->f_av_desc->ad_cname);
201 (*filter_attrs)[*filter_cnt].an_name = f->f_desc->ad_cname;
202 (*filter_attrs)[*filter_cnt].an_desc = NULL;
203 slap_bv2ad(&f->f_desc->ad_cname,
204 &(*filter_attrs)[*filter_cnt].an_desc, &text);
205 (*filter_attrs)[*filter_cnt+1].an_name.bv_val = NULL;
206 (*filter_attrs)[*filter_cnt+1].an_name.bv_len = 0;
211 case LDAP_FILTER_AND:
213 case LDAP_FILTER_NOT:
214 fstr->bv_len = sizeof("(%)") - 1;
215 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
217 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
218 f->f_choice == LDAP_FILTER_AND ? '&' :
219 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
221 for ( p = f->f_list; p != NULL; p = p->f_next ) {
224 filter2template( p, &tmp, filter_attrs, filter_cnt,
226 if (result->type != SUCCESS) {
230 fstr->bv_len += tmp.bv_len;
231 fstr->bv_val = ch_realloc( fstr->bv_val,
234 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
235 /*"("*/ "%s)", tmp.bv_val );
237 ch_free( tmp.bv_val );
243 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
244 result->type = FILTER_ERR;