]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/cache-template.c
silence warning
[openldap] / servers / slapd / back-meta / cache-template.c
1 /* Copyright (c) 2003 by International Business Machines, Inc.
2  *
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
9  * permission.
10  *
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.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "slap.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"
32
33 #ifdef LDAP_CACHING
34 void 
35 filter2template(
36         Filter                  *f,
37         struct                  berval *fstr,
38         AttributeName**         filter_attrs, 
39         int*                    filter_cnt,
40         struct exception*       result  )
41 {
42         int     i;
43         Filter  *p;
44         struct berval tmp;
45         ber_len_t len;
46         const char* text; 
47
48         /*
49          * FIXME: should we use an assert here?
50          */
51         if ( f == NULL ) {
52                 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
53                 result->type = FILTER_ERR; 
54                 return; 
55         }
56
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 );
62
63                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
64                                 f->f_av_desc->ad_cname.bv_val );
65
66                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
67                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
68                 
69 #if 0   /* ? */
70                 ber_dupbv(&(*filter_attrs)[*filter_cnt].an_name,
71                                 &f->f_av_desc->ad_cname); 
72 #endif
73                 
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; 
80                 (*filter_cnt)++; 
81                 break;
82         case LDAP_FILTER_GE:
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 );
86
87                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=)",
88                         f->f_av_desc->ad_cname.bv_val);
89
90                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs, 
91                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
92 #if 0   /* ? */
93                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
94                                 &f->f_av_desc->ad_cname);
95 #endif
96
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; 
103                 (*filter_cnt)++; 
104
105                 break;
106
107         case LDAP_FILTER_LE:
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 );
111
112                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=)",
113                         f->f_av_desc->ad_cname.bv_val);
114
115                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs, 
116                                 (*filter_cnt + 2)*sizeof(AttributeName)); 
117 #if 0   /* ? */
118                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
119                                 &f->f_av_desc->ad_cname);
120 #endif
121                 
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; 
128                 (*filter_cnt++); 
129
130                 break;
131
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 );
136
137                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=)",
138                         f->f_av_desc->ad_cname.bv_val);
139                 
140                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
141                                 (*filter_cnt + 2)*sizeof(AttributeName));
142
143 #if 0   /* ? */
144                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
145                                 &f->f_av_desc->ad_cname);
146 #endif
147                 
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; 
154                 (*filter_cnt)++; 
155
156                 break;
157
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 );
162
163                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=)",
164                         f->f_sub_desc->ad_cname.bv_val );
165
166                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
167                                 (*filter_cnt + 2)*sizeof(AttributeName));
168
169 #if 0   /* ? */
170                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
171                                 &f->f_av_desc->ad_cname);
172 #endif
173                 
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; 
180                 (*filter_cnt)++; 
181
182                 break;
183
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 );
188
189                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
190                         f->f_desc->ad_cname.bv_val );
191
192                 *filter_attrs = (AttributeName *)ch_realloc(*filter_attrs,
193                                 (*filter_cnt+2)*sizeof(AttributeName)); 
194
195 #if 0   /* ? */
196                 ber_dupbv(&(*filter_attrs)[filter_cnt].an_name,
197                                 &f->f_av_desc->ad_cname);
198 #endif
199                 
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; 
206                 (*filter_cnt)++; 
207
208                 break;
209
210         case LDAP_FILTER_AND:
211         case LDAP_FILTER_OR:
212         case LDAP_FILTER_NOT:
213                 fstr->bv_len = sizeof("(%)") - 1;
214                 fstr->bv_val = ch_malloc( fstr->bv_len + 128 );
215
216                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
217                         f->f_choice == LDAP_FILTER_AND ? '&' :
218                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
219
220                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
221                         len = fstr->bv_len;
222
223                         filter2template( p, &tmp, filter_attrs, filter_cnt,
224                                         result); 
225                         if (result->type != SUCCESS) {
226                                 return; 
227                         }
228                         
229                         fstr->bv_len += tmp.bv_len;
230                         fstr->bv_val = ch_realloc( fstr->bv_val,
231                                         fstr->bv_len + 1 );
232
233                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
234                                 /*"("*/ "%s)", tmp.bv_val );
235
236                         ch_free( tmp.bv_val );
237                 }
238
239                 break;
240
241         default:
242                 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
243                 result->type = FILTER_ERR; 
244                 return;
245         }
246 }
247 #endif /* LDAP_CACHING */